summaryrefslogtreecommitdiffstats
path: root/src/internal/saferio/io_test.go
blob: 356c9ebdd1a60880b8b89966b9a701b5a7481244 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package saferio

import (
	"bytes"
	"io"
	"testing"
)

func TestReadData(t *testing.T) {
	const count = 100
	input := bytes.Repeat([]byte{'a'}, count)

	t.Run("small", func(t *testing.T) {
		got, err := ReadData(bytes.NewReader(input), count)
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Equal(got, input) {
			t.Errorf("got %v, want %v", got, input)
		}
	})

	t.Run("large", func(t *testing.T) {
		_, err := ReadData(bytes.NewReader(input), 10<<30)
		if err == nil {
			t.Error("large read succeeded unexpectedly")
		}
	})

	t.Run("maxint", func(t *testing.T) {
		_, err := ReadData(bytes.NewReader(input), 1<<62)
		if err == nil {
			t.Error("large read succeeded unexpectedly")
		}
	})

	t.Run("small-EOF", func(t *testing.T) {
		_, err := ReadData(bytes.NewReader(nil), chunk-1)
		if err != io.EOF {
			t.Errorf("ReadData = %v, want io.EOF", err)
		}
	})

	t.Run("large-EOF", func(t *testing.T) {
		_, err := ReadData(bytes.NewReader(nil), chunk+1)
		if err != io.EOF {
			t.Errorf("ReadData = %v, want io.EOF", err)
		}
	})

	t.Run("large-UnexpectedEOF", func(t *testing.T) {
		_, err := ReadData(bytes.NewReader(make([]byte, chunk)), chunk+1)
		if err != io.ErrUnexpectedEOF {
			t.Errorf("ReadData = %v, want io.ErrUnexpectedEOF", err)
		}
	})
}

func TestReadDataAt(t *testing.T) {
	const count = 100
	input := bytes.Repeat([]byte{'a'}, count)

	t.Run("small", func(t *testing.T) {
		got, err := ReadDataAt(bytes.NewReader(input), count, 0)
		if err != nil {
			t.Fatal(err)
		}
		if !bytes.Equal(got, input) {
			t.Errorf("got %v, want %v", got, input)
		}
	})

	t.Run("large", func(t *testing.T) {
		_, err := ReadDataAt(bytes.NewReader(input), 10<<30, 0)
		if err == nil {
			t.Error("large read succeeded unexpectedly")
		}
	})

	t.Run("maxint", func(t *testing.T) {
		_, err := ReadDataAt(bytes.NewReader(input), 1<<62, 0)
		if err == nil {
			t.Error("large read succeeded unexpectedly")
		}
	})

	t.Run("SectionReader", func(t *testing.T) {
		// Reading 0 bytes from an io.SectionReader at the end
		// of the section will return EOF, but ReadDataAt
		// should succeed and return 0 bytes.
		sr := io.NewSectionReader(bytes.NewReader(input), 0, 0)
		got, err := ReadDataAt(sr, 0, 0)
		if err != nil {
			t.Fatal(err)
		}
		if len(got) > 0 {
			t.Errorf("got %d bytes, expected 0", len(got))
		}
	})
}

func TestSliceCap(t *testing.T) {
	t.Run("small", func(t *testing.T) {
		c := SliceCap((*int)(nil), 10)
		if c != 10 {
			t.Errorf("got capacity %d, want %d", c, 10)
		}
	})

	t.Run("large", func(t *testing.T) {
		c := SliceCap((*byte)(nil), 1<<30)
		if c < 0 {
			t.Error("SliceCap failed unexpectedly")
		} else if c == 1<<30 {
			t.Errorf("got capacity %d which is too high", c)
		}
	})

	t.Run("maxint", func(t *testing.T) {
		c := SliceCap((*byte)(nil), 1<<63)
		if c >= 0 {
			t.Errorf("SliceCap returned %d, expected failure", c)
		}
	})

	t.Run("overflow", func(t *testing.T) {
		c := SliceCap((*int64)(nil), 1<<62)
		if c >= 0 {
			t.Errorf("SliceCap returned %d, expected failure", c)
		}
	})
}