-
Notifications
You must be signed in to change notification settings - Fork 4
/
request-server_test.go
780 lines (670 loc) · 20.5 KB
/
request-server_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
package sftp
import (
"context"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var _ = fmt.Print
type csPair struct {
cli *Client
svr *RequestServer
svrResult chan error
}
// these must be closed in order, else client.Close will hang
func (cs csPair) Close() {
cs.svr.Close()
cs.cli.Close()
os.Remove(sock)
}
func (cs csPair) testHandler() *root {
return cs.svr.Handlers.FileGet.(*root)
}
const sock = "/tmp/rstest.sock"
func clientRequestServerPair(t *testing.T) *csPair {
skipIfWindows(t)
skipIfPlan9(t)
ready := make(chan bool)
os.Remove(sock) // either this or signal handling
pair := &csPair{
svrResult: make(chan error, 1),
}
var server *RequestServer
go func() {
l, err := net.Listen("unix", sock)
if err != nil {
// neither assert nor t.Fatal reliably exit before Accept errors
panic(err)
}
ready <- true
fd, err := l.Accept()
require.NoError(t, err)
handlers := InMemHandler()
var options []RequestServerOption
if *testAllocator {
options = append(options, WithRSAllocator())
}
server = NewRequestServer(fd, handlers, options...)
err = server.Serve()
pair.svrResult <- err
}()
<-ready
defer os.Remove(sock)
c, err := net.Dial("unix", sock)
require.NoError(t, err)
client, err := NewClientPipe(c, c)
if err != nil {
t.Fatalf("%+v\n", err)
}
pair.svr = server
pair.cli = client
return pair
}
func checkRequestServerAllocator(t *testing.T, p *csPair) {
if p.svr.pktMgr.alloc == nil {
return
}
checkAllocatorBeforeServerClose(t, p.svr.pktMgr.alloc)
p.Close()
checkAllocatorAfterServerClose(t, p.svr.pktMgr.alloc)
}
// after adding logging, maybe check log to make sure packet handling
// was split over more than one worker
func TestRequestSplitWrite(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
w, err := p.cli.Create("/foo")
require.NoError(t, err)
p.cli.maxPacket = 3 // force it to send in small chunks
contents := "one two three four five six seven eight nine ten"
w.Write([]byte(contents))
w.Close()
r := p.testHandler()
f, err := r.fetch("/foo")
require.NoError(t, err)
assert.Equal(t, contents, string(f.content))
checkRequestServerAllocator(t, p)
}
func TestRequestCache(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
foo := NewRequest("", "foo")
foo.ctx, foo.cancelCtx = context.WithCancel(context.Background())
bar := NewRequest("", "bar")
fh := p.svr.nextRequest(foo)
bh := p.svr.nextRequest(bar)
assert.Len(t, p.svr.openRequests, 2)
_foo, ok := p.svr.getRequest(fh)
assert.Equal(t, foo.Method, _foo.Method)
assert.Equal(t, foo.Filepath, _foo.Filepath)
assert.Equal(t, foo.Target, _foo.Target)
assert.Equal(t, foo.Flags, _foo.Flags)
assert.Equal(t, foo.Attrs, _foo.Attrs)
assert.Equal(t, foo.state, _foo.state)
assert.NotNil(t, _foo.ctx)
assert.Equal(t, _foo.Context().Err(), nil, "context is still valid")
assert.True(t, ok)
_, ok = p.svr.getRequest("zed")
assert.False(t, ok)
p.svr.closeRequest(fh)
assert.Equal(t, _foo.Context().Err(), context.Canceled, "context is now canceled")
p.svr.closeRequest(bh)
assert.Len(t, p.svr.openRequests, 0)
checkRequestServerAllocator(t, p)
}
func TestRequestCacheState(t *testing.T) {
// test operation that uses open/close
p := clientRequestServerPair(t)
defer p.Close()
_, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
assert.Len(t, p.svr.openRequests, 0)
// test operation that doesn't open/close
err = p.cli.Remove("/foo")
assert.NoError(t, err)
assert.Len(t, p.svr.openRequests, 0)
checkRequestServerAllocator(t, p)
}
func putTestFile(cli *Client, path, content string) (int, error) {
w, err := cli.Create(path)
if err == nil {
defer w.Close()
return w.Write([]byte(content))
}
return 0, err
}
func getTestFile(cli *Client, path string) ([]byte, error) {
r, err := cli.Open(path)
if err != nil {
return nil, err
}
defer r.Close()
return ioutil.ReadAll(r)
}
func TestRequestWrite(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
n, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
assert.Equal(t, 5, n)
r := p.testHandler()
f, err := r.fetch("/foo")
require.NoError(t, err)
assert.False(t, f.isdir)
assert.Equal(t, f.content, []byte("hello"))
checkRequestServerAllocator(t, p)
}
func TestRequestWriteEmpty(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
n, err := putTestFile(p.cli, "/foo", "")
require.NoError(t, err)
assert.Equal(t, 0, n)
r := p.testHandler()
f, err := r.fetch("/foo")
require.NoError(t, err)
assert.False(t, f.isdir)
assert.Len(t, f.content, 0)
// lets test with an error
r.returnErr(os.ErrInvalid)
n, err = putTestFile(p.cli, "/bar", "")
require.Error(t, err)
r.returnErr(nil)
assert.Equal(t, 0, n)
checkRequestServerAllocator(t, p)
}
func TestRequestFilename(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
_, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
r := p.testHandler()
f, err := r.fetch("/foo")
require.NoError(t, err)
assert.Equal(t, f.Name(), "foo")
_, err = r.fetch("/bar")
assert.Error(t, err)
checkRequestServerAllocator(t, p)
}
func TestRequestJustRead(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
_, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
rf, err := p.cli.Open("/foo")
require.NoError(t, err)
defer rf.Close()
contents := make([]byte, 5)
n, err := rf.Read(contents)
if err != nil && err != io.EOF {
t.Fatalf("err: %v", err)
}
assert.Equal(t, 5, n)
assert.Equal(t, "hello", string(contents[0:5]))
checkRequestServerAllocator(t, p)
}
func TestRequestOpenFail(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
rf, err := p.cli.Open("/foo")
assert.Exactly(t, os.ErrNotExist, err)
assert.Nil(t, rf)
// if we return an error the sftp client will not close the handle
// ensure that we close it ourself
assert.Len(t, p.svr.openRequests, 0)
checkRequestServerAllocator(t, p)
}
func TestRequestCreate(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
fh, err := p.cli.Create("foo")
require.NoError(t, err)
err = fh.Close()
assert.NoError(t, err)
checkRequestServerAllocator(t, p)
}
func TestRequestReadAndWrite(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
file, err := p.cli.OpenFile("/foo", os.O_RDWR|os.O_CREATE)
require.NoError(t, err)
defer file.Close()
n, err := file.Write([]byte("hello"))
require.NoError(t, err)
assert.Equal(t, 5, n)
buf := make([]byte, 4)
n, err = file.ReadAt(buf, 1)
require.NoError(t, err)
assert.Equal(t, 4, n)
assert.Equal(t, []byte{'e', 'l', 'l', 'o'}, buf)
checkRequestServerAllocator(t, p)
}
func TestOpenFileExclusive(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
// first open should work
file, err := p.cli.OpenFile("/foo", os.O_RDWR|os.O_CREATE|os.O_EXCL)
require.NoError(t, err)
file.Close()
// second open should return error
_, err = p.cli.OpenFile("/foo", os.O_RDWR|os.O_CREATE|os.O_EXCL)
assert.Error(t, err)
checkRequestServerAllocator(t, p)
}
func TestOpenFileExclusiveNoSymlinkFollowing(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
// make a directory
err := p.cli.Mkdir("/foo")
require.NoError(t, err)
// make a symlink to that directory
err = p.cli.Symlink("/foo", "/foo2")
require.NoError(t, err)
// with O_EXCL, we can follow directory symlinks
file, err := p.cli.OpenFile("/foo2/bar", os.O_RDWR|os.O_CREATE|os.O_EXCL)
require.NoError(t, err)
err = file.Close()
require.NoError(t, err)
// we should have created the file above; and this create should fail.
_, err = p.cli.OpenFile("/foo/bar", os.O_RDWR|os.O_CREATE|os.O_EXCL)
require.Error(t, err)
// create a dangling symlink
err = p.cli.Symlink("/notexist", "/bar")
require.NoError(t, err)
// opening a dangling symlink with O_CREATE and O_EXCL should fail, regardless of target not existing.
_, err = p.cli.OpenFile("/bar", os.O_RDWR|os.O_CREATE|os.O_EXCL)
require.Error(t, err)
checkRequestServerAllocator(t, p)
}
func TestRequestMkdir(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
err := p.cli.Mkdir("/foo")
require.NoError(t, err)
r := p.testHandler()
f, err := r.fetch("/foo")
require.NoError(t, err)
assert.True(t, f.IsDir())
checkRequestServerAllocator(t, p)
}
func TestRequestRemove(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
_, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
r := p.testHandler()
_, err = r.fetch("/foo")
assert.NoError(t, err)
err = p.cli.Remove("/foo")
assert.NoError(t, err)
_, err = r.fetch("/foo")
assert.Equal(t, err, os.ErrNotExist)
checkRequestServerAllocator(t, p)
}
func TestRequestRename(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
_, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
content, err := getTestFile(p.cli, "/foo")
require.NoError(t, err)
require.Equal(t, []byte("hello"), content)
err = p.cli.Rename("/foo", "/bar")
require.NoError(t, err)
// file contents are now at /bar
content, err = getTestFile(p.cli, "/bar")
require.NoError(t, err)
require.Equal(t, []byte("hello"), content)
// /foo no longer exists
_, err = getTestFile(p.cli, "/foo")
require.Error(t, err)
_, err = putTestFile(p.cli, "/baz", "goodbye")
require.NoError(t, err)
content, err = getTestFile(p.cli, "/baz")
require.NoError(t, err)
require.Equal(t, []byte("goodbye"), content)
// SFTP-v2: SSH_FXP_RENAME may not overwrite existing files.
err = p.cli.Rename("/bar", "/baz")
require.Error(t, err)
// /bar and /baz are unchanged
content, err = getTestFile(p.cli, "/bar")
require.NoError(t, err)
require.Equal(t, []byte("hello"), content)
content, err = getTestFile(p.cli, "/baz")
require.NoError(t, err)
require.Equal(t, []byte("goodbye"), content)
// posix-rename@openssh.com extension allows overwriting existing files.
err = p.cli.PosixRename("/bar", "/baz")
require.NoError(t, err)
// /baz now has the contents of /bar
content, err = getTestFile(p.cli, "/baz")
require.NoError(t, err)
require.Equal(t, []byte("hello"), content)
// /bar no longer exists
_, err = getTestFile(p.cli, "/bar")
require.Error(t, err)
checkRequestServerAllocator(t, p)
}
func TestRequestRenameFail(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
_, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
_, err = putTestFile(p.cli, "/bar", "goodbye")
require.NoError(t, err)
err = p.cli.Rename("/foo", "/bar")
assert.IsType(t, &StatusError{}, err)
checkRequestServerAllocator(t, p)
}
func TestRequestStat(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
_, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
fi, err := p.cli.Stat("/foo")
require.NoError(t, err)
assert.Equal(t, fi.Name(), "foo")
assert.Equal(t, fi.Size(), int64(5))
assert.Equal(t, fi.Mode(), os.FileMode(0644))
assert.NoError(t, testOsSys(fi.Sys()))
checkRequestServerAllocator(t, p)
}
// NOTE: Setstat is a noop in the request server tests, but we want to test
// that is does nothing without crapping out.
func TestRequestSetstat(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
_, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
mode := os.FileMode(0644)
err = p.cli.Chmod("/foo", mode)
require.NoError(t, err)
fi, err := p.cli.Stat("/foo")
require.NoError(t, err)
assert.Equal(t, fi.Name(), "foo")
assert.Equal(t, fi.Size(), int64(5))
assert.Equal(t, fi.Mode(), os.FileMode(0644))
assert.NoError(t, testOsSys(fi.Sys()))
checkRequestServerAllocator(t, p)
}
func TestRequestFstat(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
_, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
fp, err := p.cli.Open("/foo")
require.NoError(t, err)
fi, err := fp.Stat()
require.NoError(t, err)
assert.Equal(t, fi.Name(), "foo")
assert.Equal(t, fi.Size(), int64(5))
assert.Equal(t, fi.Mode(), os.FileMode(0644))
assert.NoError(t, testOsSys(fi.Sys()))
checkRequestServerAllocator(t, p)
}
func TestRequestFsetstat(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
_, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
fp, err := p.cli.OpenFile("/foo", os.O_WRONLY)
require.NoError(t, err)
err = fp.Truncate(2)
fi, err := fp.Stat()
require.NoError(t, err)
assert.Equal(t, fi.Name(), "foo")
assert.Equal(t, fi.Size(), int64(2))
err = fp.Truncate(5)
require.NoError(t, err)
fi, err = fp.Stat()
require.NoError(t, err)
assert.Equal(t, fi.Name(), "foo")
assert.Equal(t, fi.Size(), int64(5))
err = fp.Close()
assert.NoError(t, err)
rf, err := p.cli.Open("/foo")
assert.NoError(t, err)
defer rf.Close()
contents := make([]byte, 20)
n, err := rf.Read(contents)
assert.EqualError(t, err, io.EOF.Error())
assert.Equal(t, 5, n)
assert.Equal(t, []byte{'h', 'e', 0, 0, 0}, contents[0:n])
checkRequestServerAllocator(t, p)
}
func TestRequestStatFail(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
fi, err := p.cli.Stat("/foo")
assert.Nil(t, fi)
assert.True(t, os.IsNotExist(err))
checkRequestServerAllocator(t, p)
}
func TestRequestLstat(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
_, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
err = p.cli.Symlink("/foo", "/bar")
require.NoError(t, err)
fi, err := p.cli.Lstat("/bar")
require.NoError(t, err)
assert.True(t, fi.Mode()&os.ModeSymlink == os.ModeSymlink)
checkRequestServerAllocator(t, p)
}
func TestRequestLink(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
_, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
err = p.cli.Link("/foo", "/bar")
require.NoError(t, err)
content, err := getTestFile(p.cli, "/bar")
assert.NoError(t, err)
assert.Equal(t, []byte("hello"), content)
checkRequestServerAllocator(t, p)
}
func TestRequestLinkFail(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
err := p.cli.Link("/foo", "/bar")
t.Log(err)
assert.True(t, os.IsNotExist(err))
checkRequestServerAllocator(t, p)
}
func TestRequestSymlink(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
_, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
err = p.cli.Symlink("/foo", "/bar")
require.NoError(t, err)
err = p.cli.Symlink("/bar", "/baz")
require.NoError(t, err)
r := p.testHandler()
fi, err := r.lfetch("/bar")
require.NoError(t, err)
assert.True(t, fi.Mode()&os.ModeSymlink == os.ModeSymlink)
fi, err = r.lfetch("/baz")
require.NoError(t, err)
assert.True(t, fi.Mode()&os.ModeSymlink == os.ModeSymlink)
content, err := getTestFile(p.cli, "/baz")
require.NoError(t, err)
assert.Equal(t, []byte("hello"), content)
checkRequestServerAllocator(t, p)
}
func TestRequestSymlinkLoop(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
err := p.cli.Symlink("/foo", "/bar")
require.NoError(t, err)
err = p.cli.Symlink("/bar", "/baz")
require.NoError(t, err)
err = p.cli.Symlink("/baz", "/foo")
require.NoError(t, err)
// test should fail if we reach this point
timer := time.NewTimer(1 * time.Second)
defer timer.Stop()
var content []byte
done := make(chan struct{})
go func() {
defer close(done)
content, err = getTestFile(p.cli, "/bar")
}()
select {
case <-timer.C:
t.Fatal("symlink loop following timed out")
return // just to let the compiler be absolutely sure
case <-done:
}
assert.Error(t, err)
assert.Len(t, content, 0)
checkRequestServerAllocator(t, p)
}
func TestRequestSymlinkDanglingFiles(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
// dangling links are ok. We will use "/foo" later.
err := p.cli.Symlink("/foo", "/bar")
require.NoError(t, err)
// creating a symlink in a non-existant directory should fail.
err = p.cli.Symlink("/dangle", "/foo/bar")
require.Error(t, err)
// creating a symlink under a dangling symlink should fail.
err = p.cli.Symlink("/dangle", "/bar/bar")
require.Error(t, err)
// opening a dangling link without O_CREATE should fail with os.IsNotExist == true
_, err = p.cli.OpenFile("/bar", os.O_RDONLY)
require.True(t, os.IsNotExist(err))
// overwriting a symlink is not allowed.
err = p.cli.Symlink("/dangle", "/bar")
require.Error(t, err)
// double symlink
err = p.cli.Symlink("/bar", "/baz")
require.NoError(t, err)
// opening a dangling link with O_CREATE should work.
_, err = putTestFile(p.cli, "/baz", "hello")
require.NoError(t, err)
// dangling link creation should create the target file itself.
content, err := getTestFile(p.cli, "/foo")
require.NoError(t, err)
assert.Equal(t, []byte("hello"), content)
// creating a symlink under a non-directory file should fail.
err = p.cli.Symlink("/dangle", "/foo/bar")
assert.Error(t, err)
checkRequestServerAllocator(t, p)
}
func TestRequestSymlinkDanglingDirectories(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
// dangling links are ok. We will use "/foo" later.
err := p.cli.Symlink("/foo", "/bar")
require.NoError(t, err)
// reading from a dangling symlink should fail.
_, err = p.cli.ReadDir("/bar")
require.True(t, os.IsNotExist(err))
// making a directory on a dangling symlink SHOULD NOT work.
err = p.cli.Mkdir("/bar")
require.Error(t, err)
// ok, now make directory, so we can test make files through the symlink.
err = p.cli.Mkdir("/foo")
require.NoError(t, err)
// should be able to make a file in that symlinked directory.
_, err = putTestFile(p.cli, "/bar/baz", "hello")
require.NoError(t, err)
// dangling directory creation should create the target directory itself.
content, err := getTestFile(p.cli, "/foo/baz")
assert.NoError(t, err)
assert.Equal(t, []byte("hello"), content)
checkRequestServerAllocator(t, p)
}
func TestRequestReadlink(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
_, err := putTestFile(p.cli, "/foo", "hello")
require.NoError(t, err)
err = p.cli.Symlink("/foo", "/bar")
require.NoError(t, err)
rl, err := p.cli.ReadLink("/bar")
assert.NoError(t, err)
assert.Equal(t, "foo", rl)
checkRequestServerAllocator(t, p)
}
func TestRequestReaddir(t *testing.T) {
p := clientRequestServerPair(t)
MaxFilelist = 22 // make not divisible by our test amount (100)
defer p.Close()
for i := 0; i < 100; i++ {
fname := fmt.Sprintf("/foo_%02d", i)
_, err := putTestFile(p.cli, fname, fname)
if err != nil {
t.Fatal("expected no error, got:", err)
}
}
_, err := p.cli.ReadDir("/foo_01")
assert.Equal(t, &StatusError{Code: sshFxFailure,
msg: " /foo_01: not a directory"}, err)
_, err = p.cli.ReadDir("/does_not_exist")
assert.Equal(t, os.ErrNotExist, err)
di, err := p.cli.ReadDir("/")
require.NoError(t, err)
require.Len(t, di, 100)
names := []string{di[18].Name(), di[81].Name()}
assert.Equal(t, []string{"foo_18", "foo_81"}, names)
assert.Len(t, p.svr.openRequests, 0)
checkRequestServerAllocator(t, p)
}
func TestCleanDisconnect(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
err := p.cli.conn.Close()
require.NoError(t, err)
// server must return io.EOF after a clean client close
// with no pending open requests
err = <-p.svrResult
require.EqualError(t, err, io.EOF.Error())
checkRequestServerAllocator(t, p)
}
func TestUncleanDisconnect(t *testing.T) {
p := clientRequestServerPair(t)
defer p.Close()
foo := NewRequest("", "foo")
p.svr.nextRequest(foo)
err := p.cli.conn.Close()
require.NoError(t, err)
// the foo request above is still open after the client disconnects
// so the server will convert io.EOF to io.ErrUnexpectedEOF
err = <-p.svrResult
require.EqualError(t, err, io.ErrUnexpectedEOF.Error())
checkRequestServerAllocator(t, p)
}
func TestCleanPath(t *testing.T) {
assert.Equal(t, "/", cleanPath("/"))
assert.Equal(t, "/", cleanPath("."))
assert.Equal(t, "/", cleanPath("/."))
assert.Equal(t, "/", cleanPath("/a/.."))
assert.Equal(t, "/a/c", cleanPath("/a/b/../c"))
assert.Equal(t, "/a/c", cleanPath("/a/b/../c/"))
assert.Equal(t, "/a", cleanPath("/a/b/.."))
assert.Equal(t, "/a/b/c", cleanPath("/a/b/c"))
assert.Equal(t, "/", cleanPath("//"))
assert.Equal(t, "/a", cleanPath("/a/"))
assert.Equal(t, "/a", cleanPath("a/"))
assert.Equal(t, "/a/b/c", cleanPath("/a//b//c/"))
// filepath.ToSlash does not touch \ as char on unix systems
// so os.PathSeparator is used for windows compatible tests
bslash := string(os.PathSeparator)
assert.Equal(t, "/", cleanPath(bslash))
assert.Equal(t, "/", cleanPath(bslash+bslash))
assert.Equal(t, "/a", cleanPath(bslash+"a"+bslash))
assert.Equal(t, "/a", cleanPath("a"+bslash))
assert.Equal(t, "/a/b/c",
cleanPath(bslash+"a"+bslash+bslash+"b"+bslash+bslash+"c"+bslash))
assert.Equal(t, "/C:/a", cleanPath("C:"+bslash+"a"))
}