-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_integration_test.go
898 lines (766 loc) · 17.9 KB
/
client_integration_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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
package sftp
// sftp integration tests
// enable with -integration
import (
"crypto/sha1"
"flag"
"io"
"io/ioutil"
"math/rand"
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"testing"
"testing/quick"
"github.com/kr/fs"
)
const (
READONLY = true
READWRITE = false
debuglevel = "ERROR" // set to "DEBUG" for debugging
)
var testIntegration = flag.Bool("integration", false, "perform integration tests against sftp server process")
var testSftp = flag.String("sftp", "/usr/lib/openssh/sftp-server", "location of the sftp server binary")
// testClient returns a *Client connected to a localy running sftp-server
// the *exec.Cmd returned must be defer Wait'd.
func testClient(t testing.TB, readonly bool) (*Client, *exec.Cmd) {
if !*testIntegration {
t.Skip("skipping intergration test")
}
cmd := exec.Command(*testSftp, "-e", "-R", "-l", debuglevel) // log to stderr, read only
if !readonly {
cmd = exec.Command(*testSftp, "-e", "-l", debuglevel) // log to stderr
}
cmd.Stderr = os.Stdout
pw, err := cmd.StdinPipe()
if err != nil {
t.Fatal(err)
}
pr, err := cmd.StdoutPipe()
if err != nil {
t.Fatal(err)
}
if err := cmd.Start(); err != nil {
t.Skipf("could not start sftp-server process: %v", err)
}
sftp, err := NewClientPipe(pr, pw)
if err != nil {
t.Fatal(err)
}
if err := sftp.sendInit(); err != nil {
defer cmd.Wait()
t.Fatal(err)
}
if err := sftp.recvVersion(); err != nil {
defer cmd.Wait()
t.Fatal(err)
}
return sftp, cmd
}
func TestNewClient(t *testing.T) {
sftp, cmd := testClient(t, READONLY)
defer cmd.Wait()
if err := sftp.Close(); err != nil {
t.Fatal(err)
}
}
func TestClientLstat(t *testing.T) {
sftp, cmd := testClient(t, READONLY)
defer cmd.Wait()
defer sftp.Close()
f, err := ioutil.TempFile("", "sftptest")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
want, err := os.Lstat(f.Name())
if err != nil {
t.Fatal(err)
}
got, err := sftp.Lstat(f.Name())
if err != nil {
t.Fatal(err)
}
if !sameFile(want, got) {
t.Fatalf("Lstat(%q): want %#v, got %#v", f.Name(), want, got)
}
}
func TestClientLstatMissing(t *testing.T) {
sftp, cmd := testClient(t, READONLY)
defer cmd.Wait()
defer sftp.Close()
f, err := ioutil.TempFile("", "sftptest")
if err != nil {
t.Fatal(err)
}
os.Remove(f.Name())
_, err = sftp.Lstat(f.Name())
if err1, ok := err.(*StatusError); !ok || err1.Code != ssh_FX_NO_SUCH_FILE {
t.Fatalf("Lstat: want: %v, got %#v", ssh_FX_NO_SUCH_FILE, err)
}
}
func TestClientMkdir(t *testing.T) {
sftp, cmd := testClient(t, READWRITE)
defer cmd.Wait()
defer sftp.Close()
dir, err := ioutil.TempDir("", "sftptest")
if err != nil {
t.Fatal(err)
}
sub := path.Join(dir, "mkdir1")
if err := sftp.Mkdir(sub); err != nil {
t.Fatal(err)
}
if _, err := os.Lstat(sub); err != nil {
t.Fatal(err)
}
}
func TestClientOpen(t *testing.T) {
sftp, cmd := testClient(t, READONLY)
defer cmd.Wait()
defer sftp.Close()
f, err := ioutil.TempFile("", "sftptest")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
got, err := sftp.Open(f.Name())
if err != nil {
t.Fatal(err)
}
if err := got.Close(); err != nil {
t.Fatal(err)
}
}
const seekBytes = 128 * 1024
type seek struct {
offset int64
}
func (s seek) Generate(r *rand.Rand, _ int) reflect.Value {
s.offset = int64(r.Int31n(seekBytes))
return reflect.ValueOf(s)
}
func (s seek) set(t *testing.T, r io.ReadSeeker) {
if _, err := r.Seek(s.offset, os.SEEK_SET); err != nil {
t.Fatalf("error while seeking with %+v: %v", s, err)
}
}
func (s seek) current(t *testing.T, r io.ReadSeeker) {
const mid = seekBytes / 2
skip := s.offset / 2
if s.offset > mid {
skip = -skip
}
if _, err := r.Seek(mid, os.SEEK_SET); err != nil {
t.Fatalf("error seeking to midpoint with %+v: %v", s, err)
}
if _, err := r.Seek(skip, os.SEEK_CUR); err != nil {
t.Fatalf("error seeking from %d with %+v: %v", mid, s, err)
}
}
func (s seek) end(t *testing.T, r io.ReadSeeker) {
if _, err := r.Seek(-s.offset, os.SEEK_END); err != nil {
t.Fatalf("error seeking from end with %+v: %v", s, err)
}
}
func TestClientSeek(t *testing.T) {
sftp, cmd := testClient(t, READONLY)
defer cmd.Wait()
defer sftp.Close()
fOS, err := ioutil.TempFile("", "seek-test")
if err != nil {
t.Fatal(err)
}
defer fOS.Close()
fSFTP, err := sftp.Open(fOS.Name())
if err != nil {
t.Fatal(err)
}
defer fSFTP.Close()
writeN(t, fOS, seekBytes)
if err := quick.CheckEqual(
func(s seek) (string, int64) { s.set(t, fOS); return readHash(t, fOS) },
func(s seek) (string, int64) { s.set(t, fSFTP); return readHash(t, fSFTP) },
nil,
); err != nil {
t.Errorf("Seek: expected equal absolute seeks: %v", err)
}
if err := quick.CheckEqual(
func(s seek) (string, int64) { s.current(t, fOS); return readHash(t, fOS) },
func(s seek) (string, int64) { s.current(t, fSFTP); return readHash(t, fSFTP) },
nil,
); err != nil {
t.Errorf("Seek: expected equal seeks from middle: %v", err)
}
if err := quick.CheckEqual(
func(s seek) (string, int64) { s.end(t, fOS); return readHash(t, fOS) },
func(s seek) (string, int64) { s.end(t, fSFTP); return readHash(t, fSFTP) },
nil,
); err != nil {
t.Errorf("Seek: expected equal seeks from end: %v", err)
}
}
func TestClientCreate(t *testing.T) {
sftp, cmd := testClient(t, READWRITE)
defer cmd.Wait()
defer sftp.Close()
f, err := ioutil.TempFile("", "sftptest")
if err != nil {
t.Fatal(err)
}
defer f.Close()
defer os.Remove(f.Name())
f2, err := sftp.Create(f.Name())
if err != nil {
t.Fatal(err)
}
defer f2.Close()
}
func TestClientAppend(t *testing.T) {
sftp, cmd := testClient(t, READWRITE)
defer cmd.Wait()
defer sftp.Close()
f, err := ioutil.TempFile("", "sftptest")
if err != nil {
t.Fatal(err)
}
defer f.Close()
defer os.Remove(f.Name())
f2, err := sftp.OpenFile(f.Name(), os.O_RDWR|os.O_APPEND)
if err != nil {
t.Fatal(err)
}
defer f2.Close()
}
func TestClientCreateFailed(t *testing.T) {
sftp, cmd := testClient(t, READONLY)
defer cmd.Wait()
defer sftp.Close()
f, err := ioutil.TempFile("", "sftptest")
if err != nil {
t.Fatal(err)
}
defer f.Close()
defer os.Remove(f.Name())
f2, err := sftp.Create(f.Name())
if err1, ok := err.(*StatusError); !ok || err1.Code != ssh_FX_PERMISSION_DENIED {
t.Fatalf("Create: want: %v, got %#v", ssh_FX_PERMISSION_DENIED, err)
}
if err == nil {
f2.Close()
}
}
func TestClientFileStat(t *testing.T) {
sftp, cmd := testClient(t, READONLY)
defer cmd.Wait()
defer sftp.Close()
f, err := ioutil.TempFile("", "sftptest")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
want, err := os.Lstat(f.Name())
if err != nil {
t.Fatal(err)
}
f2, err := sftp.Open(f.Name())
if err != nil {
t.Fatal(err)
}
got, err := f2.Stat()
if err != nil {
t.Fatal(err)
}
if !sameFile(want, got) {
t.Fatalf("Lstat(%q): want %#v, got %#v", f.Name(), want, got)
}
}
func TestClientRemove(t *testing.T) {
sftp, cmd := testClient(t, READWRITE)
defer cmd.Wait()
defer sftp.Close()
f, err := ioutil.TempFile("", "sftptest")
if err != nil {
t.Fatal(err)
}
if err := sftp.Remove(f.Name()); err != nil {
t.Fatal(err)
}
if _, err := os.Lstat(f.Name()); !os.IsNotExist(err) {
t.Fatal(err)
}
}
func TestClientRemoveDir(t *testing.T) {
sftp, cmd := testClient(t, READWRITE)
defer cmd.Wait()
defer sftp.Close()
dir, err := ioutil.TempDir("", "sftptest")
if err != nil {
t.Fatal(err)
}
if err := sftp.Remove(dir); err != nil {
t.Fatal(err)
}
if _, err := os.Lstat(dir); !os.IsNotExist(err) {
t.Fatal(err)
}
}
func TestClientRemoveFailed(t *testing.T) {
sftp, cmd := testClient(t, READONLY)
defer cmd.Wait()
defer sftp.Close()
f, err := ioutil.TempFile("", "sftptest")
if err != nil {
t.Fatal(err)
}
if err := sftp.Remove(f.Name()); err == nil {
t.Fatalf("Remove(%v): want: permission denied, got %v", f.Name(), err)
}
if _, err := os.Lstat(f.Name()); err != nil {
t.Fatal(err)
}
}
func TestClientRename(t *testing.T) {
sftp, cmd := testClient(t, READWRITE)
defer cmd.Wait()
defer sftp.Close()
f, err := ioutil.TempFile("", "sftptest")
if err != nil {
t.Fatal(err)
}
f2 := f.Name() + ".new"
if err := sftp.Rename(f.Name(), f2); err != nil {
t.Fatal(err)
}
if _, err := os.Lstat(f.Name()); !os.IsNotExist(err) {
t.Fatal(err)
}
if _, err := os.Lstat(f2); err != nil {
t.Fatal(err)
}
}
func TestClientReadLine(t *testing.T) {
sftp, cmd := testClient(t, READWRITE)
defer cmd.Wait()
defer sftp.Close()
f, err := ioutil.TempFile("", "sftptest")
if err != nil {
t.Fatal(err)
}
f2 := f.Name() + ".sym"
if err := os.Symlink(f.Name(), f2); err != nil {
t.Fatal(err)
}
if _, err := sftp.ReadLink(f2); err != nil {
t.Fatal(err)
}
}
func sameFile(want, got os.FileInfo) bool {
return want.Name() == got.Name() &&
want.Size() == got.Size()
}
var clientReadTests = []struct {
n int64
}{
{0},
{1},
{1000},
{1024},
{1025},
{2048},
{4096},
{1 << 12},
{1 << 13},
{1 << 14},
{1 << 15},
{1 << 16},
{1 << 17},
{1 << 18},
{1 << 19},
{1 << 20},
}
func TestClientRead(t *testing.T) {
sftp, cmd := testClient(t, READONLY)
defer cmd.Wait()
defer sftp.Close()
d, err := ioutil.TempDir("", "sftptest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(d)
for _, tt := range clientReadTests {
f, err := ioutil.TempFile(d, "read-test")
if err != nil {
t.Fatal(err)
}
defer f.Close()
hash := writeN(t, f, tt.n)
f2, err := sftp.Open(f.Name())
if err != nil {
t.Fatal(err)
}
defer f2.Close()
hash2, n := readHash(t, f2)
if hash != hash2 || tt.n != n {
t.Errorf("Read: hash: want: %q, got %q, read: want: %v, got %v", hash, hash2, tt.n, n)
}
}
}
// readHash reads r until EOF returning the number of bytes read
// and the hash of the contents.
func readHash(t *testing.T, r io.Reader) (string, int64) {
h := sha1.New()
tr := io.TeeReader(r, h)
read, err := io.Copy(ioutil.Discard, tr)
if err != nil {
t.Fatal(err)
}
return string(h.Sum(nil)), read
}
// writeN writes n bytes of random data to w and returns the
// hash of that data.
func writeN(t *testing.T, w io.Writer, n int64) string {
rand, err := os.Open("/dev/urandom")
if err != nil {
t.Fatal(err)
}
defer rand.Close()
h := sha1.New()
mw := io.MultiWriter(w, h)
written, err := io.CopyN(mw, rand, n)
if err != nil {
t.Fatal(err)
}
if written != n {
t.Fatalf("CopyN(%v): wrote: %v", n, written)
}
return string(h.Sum(nil))
}
var clientWriteTests = []struct {
n int
total int64 // cumulative file size
}{
{0, 0},
{1, 1},
{0, 1},
{999, 1000},
{24, 1024},
{1023, 2047},
{2048, 4095},
{1 << 12, 8191},
{1 << 13, 16383},
{1 << 14, 32767},
{1 << 15, 65535},
{1 << 16, 131071},
{1 << 17, 262143},
{1 << 18, 524287},
{1 << 19, 1048575},
{1 << 20, 2097151},
{1 << 21, 4194303},
}
func TestClientWrite(t *testing.T) {
sftp, cmd := testClient(t, READWRITE)
defer cmd.Wait()
defer sftp.Close()
d, err := ioutil.TempDir("", "sftptest")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(d)
f := path.Join(d, "writeTest")
w, err := sftp.Create(f)
if err != nil {
t.Fatal(err)
}
defer w.Close()
for _, tt := range clientWriteTests {
got, err := w.Write(make([]byte, tt.n))
if err != nil {
t.Fatal(err)
}
if got != tt.n {
t.Errorf("Write(%v): wrote: want: %v, got %v", tt.n, tt.n, got)
}
fi, err := os.Stat(f)
if err != nil {
t.Fatal(err)
}
if total := fi.Size(); total != tt.total {
t.Errorf("Write(%v): size: want: %v, got %v", tt.n, tt.total, total)
}
}
}
// taken from github.com/kr/fs/walk_test.go
type PathTest struct {
path, result string
}
type Node struct {
name string
entries []*Node // nil if the entry is a file
mark int
}
var tree = &Node{
"testdata",
[]*Node{
{"a", nil, 0},
{"b", []*Node{}, 0},
{"c", nil, 0},
{
"d",
[]*Node{
{"x", nil, 0},
{"y", []*Node{}, 0},
{
"z",
[]*Node{
{"u", nil, 0},
{"v", nil, 0},
},
0,
},
},
0,
},
},
0,
}
func walkTree(n *Node, path string, f func(path string, n *Node)) {
f(path, n)
for _, e := range n.entries {
walkTree(e, filepath.Join(path, e.name), f)
}
}
func makeTree(t *testing.T) {
walkTree(tree, tree.name, func(path string, n *Node) {
if n.entries == nil {
fd, err := os.Create(path)
if err != nil {
t.Errorf("makeTree: %v", err)
return
}
fd.Close()
} else {
os.Mkdir(path, 0770)
}
})
}
func markTree(n *Node) { walkTree(n, "", func(path string, n *Node) { n.mark++ }) }
func checkMarks(t *testing.T, report bool) {
walkTree(tree, tree.name, func(path string, n *Node) {
if n.mark != 1 && report {
t.Errorf("node %s mark = %d; expected 1", path, n.mark)
}
n.mark = 0
})
}
// Assumes that each node name is unique. Good enough for a test.
// If clear is true, any incoming error is cleared before return. The errors
// are always accumulated, though.
func mark(path string, info os.FileInfo, err error, errors *[]error, clear bool) error {
if err != nil {
*errors = append(*errors, err)
if clear {
return nil
}
return err
}
name := info.Name()
walkTree(tree, tree.name, func(path string, n *Node) {
if n.name == name {
n.mark++
}
})
return nil
}
func TestClientWalk(t *testing.T) {
sftp, cmd := testClient(t, READONLY)
defer cmd.Wait()
defer sftp.Close()
makeTree(t)
errors := make([]error, 0, 10)
clear := true
markFn := func(walker *fs.Walker) (err error) {
for walker.Step() {
err = mark(walker.Path(), walker.Stat(), walker.Err(), &errors, clear)
if err != nil {
break
}
}
return err
}
// Expect no errors.
err := markFn(sftp.Walk(tree.name))
if err != nil {
t.Fatalf("no error expected, found: %s", err)
}
if len(errors) != 0 {
t.Fatalf("unexpected errors: %s", errors)
}
checkMarks(t, true)
errors = errors[0:0]
// Test permission errors. Only possible if we're not root
// and only on some file systems (AFS, FAT). To avoid errors during
// all.bash on those file systems, skip during go test -short.
if os.Getuid() > 0 && !testing.Short() {
// introduce 2 errors: chmod top-level directories to 0
os.Chmod(filepath.Join(tree.name, tree.entries[1].name), 0)
os.Chmod(filepath.Join(tree.name, tree.entries[3].name), 0)
// 3) capture errors, expect two.
// mark respective subtrees manually
markTree(tree.entries[1])
markTree(tree.entries[3])
// correct double-marking of directory itself
tree.entries[1].mark--
tree.entries[3].mark--
err := markFn(sftp.Walk(tree.name))
if err != nil {
t.Fatalf("expected no error return from Walk, got %s", err)
}
if len(errors) != 2 {
t.Errorf("expected 2 errors, got %d: %s", len(errors), errors)
}
// the inaccessible subtrees were marked manually
checkMarks(t, true)
errors = errors[0:0]
// 4) capture errors, stop after first error.
// mark respective subtrees manually
markTree(tree.entries[1])
markTree(tree.entries[3])
// correct double-marking of directory itself
tree.entries[1].mark--
tree.entries[3].mark--
clear = false // error will stop processing
err = markFn(sftp.Walk(tree.name))
if err == nil {
t.Fatalf("expected error return from Walk")
}
if len(errors) != 1 {
t.Errorf("expected 1 error, got %d: %s", len(errors), errors)
}
// the inaccessible subtrees were marked manually
checkMarks(t, false)
errors = errors[0:0]
// restore permissions
os.Chmod(filepath.Join(tree.name, tree.entries[1].name), 0770)
os.Chmod(filepath.Join(tree.name, tree.entries[3].name), 0770)
}
// cleanup
if err := os.RemoveAll(tree.name); err != nil {
t.Errorf("removeTree: %v", err)
}
}
func benchmarkRead(b *testing.B, bufsize int) {
size := 10*1024*1024 + 123 // ~10MiB
// open sftp client
sftp, cmd := testClient(b, READONLY)
defer cmd.Wait()
defer sftp.Close()
buf := make([]byte, bufsize)
b.ResetTimer()
b.SetBytes(int64(size))
for i := 0; i < b.N; i++ {
offset := 0
f2, err := sftp.Open("/dev/zero")
if err != nil {
b.Fatal(err)
}
defer f2.Close()
for offset < size {
n, err := io.ReadFull(f2, buf)
offset += n
if err == io.ErrUnexpectedEOF && offset != size {
b.Fatalf("read too few bytes! want: %d, got: %d", size, n)
}
if err != nil {
b.Fatal(err)
}
offset += n
}
}
}
func BenchmarkRead1k(b *testing.B) {
benchmarkRead(b, 1*1024)
}
func BenchmarkRead16k(b *testing.B) {
benchmarkRead(b, 16*1024)
}
func BenchmarkRead32k(b *testing.B) {
benchmarkRead(b, 32*1024)
}
func BenchmarkRead128k(b *testing.B) {
benchmarkRead(b, 128*1024)
}
func BenchmarkRead512k(b *testing.B) {
benchmarkRead(b, 512*1024)
}
func BenchmarkRead1MiB(b *testing.B) {
benchmarkRead(b, 1024*1024)
}
func BenchmarkRead4MiB(b *testing.B) {
benchmarkRead(b, 4*1024*1024)
}
func benchmarkWrite(b *testing.B, bufsize int) {
size := 10*1024*1024 + 123 // ~10MiB
// open sftp client
sftp, cmd := testClient(b, false)
defer cmd.Wait()
defer sftp.Close()
data := make([]byte, size)
b.ResetTimer()
b.SetBytes(int64(size))
for i := 0; i < b.N; i++ {
offset := 0
f, err := ioutil.TempFile("", "sftptest")
if err != nil {
b.Fatal(err)
}
defer os.Remove(f.Name())
f2, err := sftp.Create(f.Name())
if err != nil {
b.Fatal(err)
}
defer f2.Close()
for offset < size {
n, err := f2.Write(data[offset:min(len(data), offset+bufsize)])
if err != nil {
b.Fatal(err)
}
if offset+n < size && n != bufsize {
b.Fatalf("wrote too few bytes! want: %d, got: %d", size, n)
}
offset += n
}
f2.Close()
fi, err := os.Stat(f.Name())
if err != nil {
b.Fatal(err)
}
if fi.Size() != int64(size) {
b.Fatalf("wrong file size: want %d, got %d", size, fi.Size())
}
os.Remove(f.Name())
}
}
func BenchmarkWrite1k(b *testing.B) {
benchmarkWrite(b, 1*1024)
}
func BenchmarkWrite16k(b *testing.B) {
benchmarkWrite(b, 16*1024)
}
func BenchmarkWrite32k(b *testing.B) {
benchmarkWrite(b, 32*1024)
}
func BenchmarkWrite128k(b *testing.B) {
benchmarkWrite(b, 128*1024)
}
func BenchmarkWrite512k(b *testing.B) {
benchmarkWrite(b, 512*1024)
}
func BenchmarkWrite1MiB(b *testing.B) {
benchmarkWrite(b, 1024*1024)
}
func BenchmarkWrite4MiB(b *testing.B) {
benchmarkWrite(b, 4*1024*1024)
}