forked from rlmcpherson/s3gof3r
-
Notifications
You must be signed in to change notification settings - Fork 3
/
s3gof3r_test.go
490 lines (422 loc) · 10.2 KB
/
s3gof3r_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package s3gof3r
import (
"bytes"
"crypto/rand"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"sync"
"syscall"
"testing"
"time"
)
var b *tB
func init() {
SetLogger(os.Stderr, "test: ", (log.LstdFlags | log.Lshortfile), true)
var err error
b, err = testBucket()
if err != nil {
log.Fatal(err)
}
}
func TestGetReader(t *testing.T) {
t.Parallel()
var getTests = []struct {
path string
config *Config
rSize int64
err error
}{
{"t1.test", nil, 1 * kb, nil},
{"no-md5", &Config{Scheme: "https", Client: ClientWithTimeout(clientTimeout), Md5Check: false}, 1, nil},
{"NoKey", nil, -1, &RespError{StatusCode: 404, Message: "The specified key does not exist."}},
{"6_mb_test",
&Config{Concurrency: 3, PartSize: 1 * mb, NTry: 2, Md5Check: true, Scheme: "https", Client: ClientWithTimeout(3 * time.Second)},
6 * mb,
nil},
{"b1", nil, 1, nil},
{"0byte", &Config{Scheme: "https", Client: ClientWithTimeout(clientTimeout), Md5Check: false}, 0, nil},
}
for _, tt := range getTests {
if tt.rSize >= 0 {
err := b.putReader(tt.path, &randSrc{Size: int(tt.rSize)})
if err != nil {
t.Fatal(err)
}
}
r, h, err := b.GetReader(tt.path, tt.config)
if err != nil {
errComp(tt.err, err, t, tt)
continue
}
t.Logf("headers %v\n", h)
w := ioutil.Discard
n, err := io.Copy(w, r)
if err != nil {
t.Error(err)
}
if n != tt.rSize {
t.Errorf("Expected size: %d. Actual: %d", tt.rSize, n)
}
err = r.Close()
errComp(tt.err, err, t, tt)
}
}
func TestPutWriter(t *testing.T) {
t.Parallel()
var putTests = []struct {
path string
data []byte
header http.Header
config *Config
wSize int64
err error
}{
{"testfile", []byte("test_data"), nil, nil, 9, nil},
{"", []byte("test_data"), nil, nil,
9, &RespError{StatusCode: 400, Message: "A key must be specified"}},
{"test0byte", []byte(""), nil, nil, 0, nil},
{"testhg", []byte("foo"), goodHeader(), nil, 3, nil},
{"testhb", []byte("foo"), badHeader(), nil, 3,
&RespError{StatusCode: 400, Message: "The encryption method specified is not supported"}},
{"nomd5", []byte("foo"), goodHeader(),
&Config{Concurrency: 1, PartSize: 5 * mb, NTry: 1, Md5Check: false, Scheme: "http", Client: http.DefaultClient}, 3, nil},
{"noconc", []byte("foo"), nil,
&Config{Concurrency: 0, PartSize: 5 * mb, NTry: 1, Md5Check: true, Scheme: "https", Client: ClientWithTimeout(5 * time.Second)}, 3, nil},
{"enc test", []byte("test_data"), nil, nil, 9, nil},
}
for _, tt := range putTests {
w, err := b.PutWriter(tt.path, tt.header, tt.config)
if err != nil {
errComp(tt.err, err, t, tt)
continue
}
r := bytes.NewReader(tt.data)
n, err := io.Copy(w, r)
if err != nil {
t.Error(err)
}
if n != tt.wSize {
t.Errorf("Expected size: %d. Actual: %d", tt.wSize, n)
}
err = w.Close()
errComp(tt.err, err, t, tt)
}
}
type putMulti struct {
path string
data io.Reader
header http.Header
config *Config
wSize int64
err error
}
func TestPutMulti(t *testing.T) {
t.Parallel()
var putMultiTests = []putMulti{
{"5mb_test.test", &randSrc{Size: int(5 * mb)}, goodHeader(), nil, 5 * mb, nil},
{"10mb_test.test", &randSrc{Size: int(10 * mb)}, goodHeader(),
&Config{Concurrency: 2, PartSize: 5 * mb, NTry: 2, Md5Check: true, Scheme: "https",
Client: ClientWithTimeout(2 * time.Second)}, 10 * mb, nil},
{"11mb_test.test", &randSrc{Size: int(11 * mb)}, goodHeader(),
&Config{Concurrency: 3, PartSize: 5 * mb, NTry: 2, Md5Check: true, Scheme: "https",
Client: ClientWithTimeout(5 * time.Second)}, 11 * mb, nil},
{"timeout.test1", &randSrc{Size: int(5 * mb)}, goodHeader(),
&Config{Concurrency: 1, PartSize: 5 * mb, NTry: 1, Md5Check: false, Scheme: "https",
Client: ClientWithTimeout(1 * time.Millisecond)}, 5 * mb,
errors.New("timeout")},
{"timeout.test2", &randSrc{Size: int(10 * mb)}, goodHeader(),
&Config{Concurrency: 1, PartSize: 5 * mb, NTry: 1, Md5Check: true, Scheme: "https",
Client: ClientWithTimeout(1 * time.Millisecond)}, 10 * mb,
errors.New("timeout")},
{"toosmallpart", &randSrc{Size: int(6 * mb)}, goodHeader(),
&Config{Concurrency: 4, PartSize: 2 * mb, NTry: 3, Md5Check: false, Scheme: "https",
Client: ClientWithTimeout(5 * time.Second)}, 6 * mb, nil},
}
wg := sync.WaitGroup{}
for _, tt := range putMultiTests {
w, err := b.PutWriter(tt.path, tt.header, tt.config)
if err != nil {
errComp(tt.err, err, t, tt)
continue
}
wg.Add(1)
go func(w io.WriteCloser, tt putMulti) {
n, err := io.Copy(w, tt.data)
if err != nil {
t.Error(err)
}
if n != tt.wSize {
t.Errorf("Expected size: %d. Actual: %d", tt.wSize, n)
}
err = w.Close()
errComp(tt.err, err, t, tt)
wg.Done()
}(w, tt)
}
wg.Wait()
}
type tB struct {
*Bucket
}
func testBucket() (*tB, error) {
k, err := InstanceKeys()
if err != nil {
k, err = EnvKeys()
if err != nil {
return nil, err
}
}
bucket := os.Getenv("TEST_BUCKET")
if bucket == "" {
return nil, errors.New("TEST_BUCKET must be set in environment")
}
s3 := New("", k)
b := tB{s3.Bucket(bucket)}
return &b, err
}
func (b *tB) putReader(path string, r io.Reader) error {
if r == nil {
return nil // special handling for nil case
}
w, err := b.PutWriter(path, nil, nil)
if err != nil {
return err
}
_, err = io.Copy(w, r)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return nil
}
func errComp(expect, actual error, t *testing.T, tt interface{}) bool {
if expect == nil && actual == nil {
return true
}
if expect == nil || actual == nil {
t.Errorf("called with %v\n Expected: %v\n Actual: %v\n", tt, expect, actual)
return false
}
if !strings.Contains(actual.Error(), expect.Error()) {
t.Errorf("called with %v\n Expected: %v\n Actual: %v\n", tt, expect, actual)
return false
}
return true
}
func goodHeader() http.Header {
header := make(http.Header)
header.Add("x-amz-server-side-encryption", "AES256")
header.Add("x-amz-meta-foometadata", "testmeta")
return header
}
func badHeader() http.Header {
header := make(http.Header)
header.Add("x-amz-server-side-encryption", "AES512")
return header
}
type randSrc struct {
Size int
total int
}
func (r *randSrc) Read(p []byte) (int, error) {
n, err := rand.Read(p)
r.total = r.total + n
if r.total >= r.Size {
return n - (r.total - r.Size), io.EOF
}
return n, err
}
func ExampleBucket_PutWriter() error {
k, err := EnvKeys() // get S3 keys from environment
if err != nil {
return err
}
// Open bucket to put file into
s3 := New("", k)
b := s3.Bucket("bucketName")
// open file to upload
file, err := os.Open("fileName")
if err != nil {
return err
}
// Open a PutWriter for upload
w, err := b.PutWriter(file.Name(), nil, nil)
if err != nil {
return err
}
if _, err = io.Copy(w, file); err != nil { // Copy into S3
return err
}
if err = w.Close(); err != nil {
return err
}
return nil
}
func ExampleBucket_GetReader() error {
k, err := EnvKeys() // get S3 keys from environment
if err != nil {
return err
}
// Open bucket to put file into
s3 := New("", k)
b := s3.Bucket("bucketName")
r, h, err := b.GetReader("keyName", nil)
if err != nil {
return err
}
// stream to standard output
if _, err = io.Copy(os.Stdout, r); err != nil {
return err
}
err = r.Close()
if err != nil {
return err
}
fmt.Println(h) // print key header data
return nil
}
func TestDelete(t *testing.T) {
t.Parallel()
var deleteTests = []struct {
path string
exist bool
err error
}{
{"delete1", true, nil},
{"delete 2", false, nil},
{"/delete 2", false, nil},
}
for _, tt := range deleteTests {
if tt.exist {
err := b.putReader(tt.path, &randSrc{Size: int(1 * kb)})
if err != nil {
t.Fatal(err)
}
}
err := b.Delete(tt.path)
t.Log(err)
errComp(tt.err, err, t, tt)
}
}
func TestGetVersion(t *testing.T) {
t.Parallel()
var versionTests = []struct {
path string
err error
}{
{"key1", nil},
}
for _, tt := range versionTests {
err := b.putReader(tt.path, &randSrc{Size: int(1 * kb)})
if err != nil {
t.Fatal(err)
}
// get version id
r, h, err := b.GetReader(tt.path, nil)
if err != nil {
t.Fatal(err)
}
r.Close()
v := h.Get("x-amz-version-id")
if v == "" {
t.Logf("versioning not enabled on %s\n", b.Name)
t.SkipNow()
}
// upload again for > 1 version
err = b.putReader(tt.path, &randSrc{Size: int(1 * kb)})
if err != nil {
t.Fatal(err)
}
// request first uploaded version
t.Logf("version id: %s", v)
p := fmt.Sprintf("%s?versionId=%s", tt.path, v)
r, _, err = b.GetReader(p, nil)
if err != nil {
t.Fatal(err)
}
r.Close()
errComp(tt.err, err, t, tt)
}
}
func TestPutWriteAfterClose(t *testing.T) {
t.Parallel()
w, err := b.PutWriter("test", nil, nil)
if err != nil {
t.Fatal(err)
}
err = w.Close()
if err != nil {
t.Fatal(err)
}
p := make([]byte, 10)
_, err = w.Write(p)
if err != syscall.EINVAL {
t.Errorf("expected %v on write after close, got %v", syscall.EINVAL, err)
}
}
func TestGetReadAfterClose(t *testing.T) {
t.Parallel()
r, _, err := b.GetReader("test", nil)
if err != nil {
t.Fatal(err)
}
err = r.Close()
if err != nil {
t.Fatal(err)
}
_, err = r.Read([]byte("foo"))
if err != syscall.EINVAL {
t.Errorf("expected %v on read after close, got %v", syscall.EINVAL, err)
}
}
func TestPutterAfterError(t *testing.T) {
t.Parallel()
w, err := b.PutWriter("test", nil, nil)
if err != nil {
t.Fatal(err)
}
p, ok := w.(*putter)
if !ok {
t.Fatal("putter type cast failed")
}
terr := fmt.Errorf("test error")
p.err = terr
_, err = w.Write([]byte("foo"))
if err != terr {
t.Errorf("expected error %v on Write, got %v", terr, err)
}
err = w.Close()
if err != terr {
t.Errorf("expected error %v on Close, got %v", terr, err)
}
}
func TestGetterAfterError(t *testing.T) {
t.Parallel()
r, _, err := b.GetReader("test", nil)
if err != nil {
t.Fatal(err)
}
g, ok := r.(*getter)
if !ok {
t.Fatal("getter type cast failed")
}
terr := fmt.Errorf("test error")
g.err = terr
_, err = r.Read([]byte("foo"))
if err != terr {
t.Errorf("expected error %v on Read, got %v", terr, err)
}
err = r.Close()
if err != terr {
t.Errorf("expected error %v on Close, got %v", terr, err)
}
}