-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseeker_test.go
166 lines (145 loc) · 3.15 KB
/
seeker_test.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package sfstreams
import (
"bytes"
"crypto/rand"
"errors"
"io"
"testing"
)
type bufCloser struct {
io.ReadSeeker
io.Closer
}
func (b *bufCloser) Close() error {
return nil // no-op
}
func createSource(length int64, t *testing.T) (io.ReadSeekCloser, []byte) {
buf := make([]byte, length)
i, err := rand.Read(buf)
if err != nil {
panic(err)
}
if int64(i) != length {
t.Fatal("did not read enough random bytes")
}
return &bufCloser{ReadSeeker: bytes.NewReader(buf)}, buf
}
func TestDuplicateReads(t *testing.T) {
rsc, b := createSource(1024, t)
ps := newParentSeeker(rsc, 2)
s1 := newSyncSeeker(ps)
s2 := newSyncSeeker(ps)
// Seek to different places
_, err := s1.Seek(512, io.SeekStart)
if err != nil {
t.Fatal(err)
}
_, err = s2.Seek(128, io.SeekStart)
if err != nil {
t.Fatal(err)
}
// Read a segment of bytes from each
b1 := make([]byte, 128)
b2 := make([]byte, 128)
_, err = s1.Read(b1)
if err != nil {
t.Fatal(err)
}
_, err = s2.Read(b2)
if err != nil {
t.Fatal(err)
}
// Compare each segment to ensure the correct thing was read
for i := 512; i < 640; i++ {
if b1[i-512] != b[i] {
t.Fatalf("byte %d in segment 1 is incorrect", i)
}
}
for i := 128; i < 256; i++ {
if b2[i-128] != b[i] {
t.Fatalf("byte %d in segment 2 is incorrect", i)
}
}
}
func TestOverRead(t *testing.T) {
rsc, _ := createSource(1024, t)
ps := newParentSeeker(rsc, 1)
s1 := newSyncSeeker(ps)
// Discard the whole stream
_, err := io.Copy(io.Discard, s1)
if err != nil {
t.Fatal(err)
}
// Read from it again
b := make([]byte, 128)
i, err := s1.Read(b)
if !errors.Is(err, io.EOF) {
t.Fatal(err)
}
if i > 0 {
t.Fatalf("expected to read zero bytes, got %d", i)
}
}
type badStream struct {
io.ReadSeekCloser
pos int64
source *bytes.Reader
}
func (b *badStream) Read(buf []byte) (int, error) {
if b.pos >= int64(b.source.Len()) {
return 0, errors.New("the requested range cannot be satisfied")
}
i, err := b.source.Read(buf)
if !errors.Is(err, io.EOF) && (int64(i)+b.pos) >= int64(b.source.Len()) {
return i, io.EOF
}
return i, err
}
func (b *badStream) Seek(offset int64, whence int) (int64, error) {
p, err := b.source.Seek(offset, whence)
b.pos = p
return p, err
}
func (b *badStream) Close() error {
return nil // no-op
}
func TestImproperSourceOverRead(t *testing.T) {
_, b := createSource(1024, t)
bs := &badStream{source: bytes.NewReader(b)}
ps := newParentSeeker(bs, 1)
s1 := newSyncSeeker(ps)
// Discard the whole stream
_, err := io.Copy(io.Discard, s1)
if err != nil {
t.Fatal(err)
}
// Read from it again
rb := make([]byte, 128)
i, err := s1.Read(rb)
if !errors.Is(err, io.EOF) {
t.Fatal(err)
}
if i > 0 {
t.Fatalf("expected to read zero bytes, got %d", i)
}
}
func TestUseAfterClose(t *testing.T) {
rsc, _ := createSource(1024, t)
ps := newParentSeeker(rsc, 1)
s1 := newSyncSeeker(ps)
// Close the whole thing
err := s1.Close()
if err != nil {
t.Fatal(err)
}
// Now try to read/seek from it
b := make([]byte, 128)
_, err = s1.Read(b)
if !errors.Is(err, io.ErrClosedPipe) {
t.Fatal(err)
}
_, err = s1.Seek(12, io.SeekStart)
if !errors.Is(err, io.ErrClosedPipe) {
t.Fatal(err)
}
}