@@ -6,6 +6,7 @@ package saferio
6
6
7
7
import (
8
8
"bytes"
9
+ "io"
9
10
"testing"
10
11
)
11
12
@@ -37,3 +38,46 @@ func TestReadData(t *testing.T) {
37
38
}
38
39
})
39
40
}
41
+
42
+ func TestReadDataAt (t * testing.T ) {
43
+ const count = 100
44
+ input := bytes .Repeat ([]byte {'a' }, count )
45
+
46
+ t .Run ("small" , func (t * testing.T ) {
47
+ got , err := ReadDataAt (bytes .NewReader (input ), count , 0 )
48
+ if err != nil {
49
+ t .Fatal (err )
50
+ }
51
+ if ! bytes .Equal (got , input ) {
52
+ t .Errorf ("got %v, want %v" , got , input )
53
+ }
54
+ })
55
+
56
+ t .Run ("large" , func (t * testing.T ) {
57
+ _ , err := ReadDataAt (bytes .NewReader (input ), 10 << 30 , 0 )
58
+ if err == nil {
59
+ t .Error ("large read succeeded unexpectedly" )
60
+ }
61
+ })
62
+
63
+ t .Run ("maxint" , func (t * testing.T ) {
64
+ _ , err := ReadDataAt (bytes .NewReader (input ), 1 << 62 , 0 )
65
+ if err == nil {
66
+ t .Error ("large read succeeded unexpectedly" )
67
+ }
68
+ })
69
+
70
+ t .Run ("SectionReader" , func (t * testing.T ) {
71
+ // Reading 0 bytes from an io.SectionReader at the end
72
+ // of the section will return EOF, but ReadDataAt
73
+ // should succeed and return 0 bytes.
74
+ sr := io .NewSectionReader (bytes .NewReader (input ), 0 , 0 )
75
+ got , err := ReadDataAt (sr , 0 , 0 )
76
+ if err != nil {
77
+ t .Fatal (err )
78
+ }
79
+ if len (got ) > 0 {
80
+ t .Errorf ("got %d bytes, expected 0" , len (got ))
81
+ }
82
+ })
83
+ }
0 commit comments