-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremotefile_test.go
181 lines (161 loc) · 3.74 KB
/
remotefile_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package remotefile
import (
"bytes"
"crypto/rand"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"
)
func TestRemoteFile_Read(t *testing.T) {
size := 1000000
halfSize := size / 2
data, u := buildServer(size)
rf := &RemoteFile{
FileName: "t1",
URL: *u,
Length: int64(size),
LastModified: time.Now(),
}
buf := make([]byte, halfSize)
n, err := io.ReadFull(rf, buf)
crashIf(err)
if n != halfSize {
t.Error("should have read 5 bytes")
}
if !bytes.Equal(buf, data[0:halfSize]) {
t.Error("first half is wrong")
}
n, err = io.ReadFull(rf, buf)
crashIf(err)
if n != halfSize {
t.Error("should have read 5 bytes")
}
if !bytes.Equal(buf, data[halfSize:]) {
t.Error("second half is wrong")
}
}
func TestRemoteFile_Read2(t *testing.T) {
size := 10000
data, u := buildServer(size)
rf := &RemoteFile{
FileName: "t1",
URL: *u,
Length: int64(size),
LastModified: time.Now(),
}
receivedData, err := io.ReadAll(rf)
crashIf(err)
if !bytes.Equal(data, receivedData) {
t.Error("read problems.")
}
}
func TestRemoteFile_ReadAt(t *testing.T) {
size := 1000
data, u := buildServer(size)
rf := &RemoteFile{
FileName: "t1",
URL: *u,
Length: int64(size),
LastModified: time.Now(),
}
buf := make([]byte, 100)
n, err := rf.ReadAt(buf, 0)
crashIf(err)
if n != 100 {
t.Error("should have read 100 bytes")
}
if !bytes.Equal(buf, data[0:100]) {
t.Error("bytes are wrong")
}
n, err = rf.ReadAt(buf, 950)
if err != io.EOF {
t.Error("expecting EOF")
}
if n != 50 {
t.Error("should have read 50 bytes")
}
if !bytes.Equal(buf[0:n], data[950:]) {
t.Error("bytes are wrong")
}
}
func TestRemoteFile_Seek(t *testing.T) {
size := 1000
data, u := buildServer(size)
rf := &RemoteFile{
FileName: "t1",
URL: *u,
Length: int64(size),
LastModified: time.Now(),
}
currentOffset, err := rf.Seek(10, io.SeekStart)
crashIf(err)
if currentOffset != 10 {
t.Error("should have set offset to 0")
}
assertBytes(t, readByte(rf), data[10:11])
currentOffset, err = rf.Seek(15, io.SeekCurrent)
crashIf(err)
if currentOffset != 26 {
t.Error("should have set offset to 26 - the readByte call would have read one byte.")
}
currentOffset, err = rf.Seek(5, io.SeekEnd)
crashIf(err)
if currentOffset != 995 {
t.Error("should set offset from the end")
}
currentOffset, err = rf.Seek(100, io.SeekCurrent)
if err != ErrOffset {
t.Error("should have been an error")
}
if currentOffset != 995 {
t.Error("should not have changed the offset on error.")
}
currentOffset, err = rf.Seek(1001, io.SeekStart)
if err != ErrOffset {
t.Error("should have been an error")
}
if currentOffset != 995 {
t.Error("should not have changed the offset on error.")
}
currentOffset, err = rf.Seek(1001, io.SeekEnd)
if err != ErrOffset {
t.Error("should have been an error")
}
if currentOffset != 995 {
t.Error("should not have changed the offset on error.")
}
currentOffset, err = rf.Seek(1000, io.SeekStart)
if err != ErrOffset {
t.Error("should not allow setting offset outside bounds")
}
}
func assertBytes(t *testing.T, buf []byte, data []byte) {
if !bytes.Equal(buf, data) {
t.Error("wrong bytes")
}
}
func readByte(rf *RemoteFile) []byte {
buf := make([]byte, 1)
_, err := rf.Read(buf)
crashIf(err)
return buf
}
func buildServer(size int) ([]byte, *url.URL) {
data := make([]byte, size)
_, err := io.ReadFull(rand.Reader, data)
crashIf(err)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeContent(w, r, "t1", time.Now(), bytes.NewReader(data))
}))
u, err := url.Parse(server.URL)
crashIf(err)
return data, u
}
func crashIf(err error) {
if err != nil {
panic(err)
}
}