跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

server_integration_test.go

939 行 · 25.3 KB历史下载
1package sftp
2
3// sftp server integration tests
4// enable with -integration
5// example invokation (darwin): gofmt -w `find . -name \*.go` && (cd server_standalone/ ; go build -tags debug) && go test -tags debug github.com/pkg/sftp -integration -v -sftp /usr/libexec/sftp-server -run ServerCompareSubsystems
6
7import (
8 "bytes"
9 "crypto/ecdsa"
10 "crypto/elliptic"
11 crand "crypto/rand"
12 "crypto/x509"
13 "encoding/hex"
14 "encoding/pem"
15 "flag"
16 "fmt"
17 "io/ioutil"
18 "math/rand"
19 "net"
20 "os"
21 "os/exec"
22 "path/filepath"
23 "regexp"
24 "runtime"
25 "strconv"
26 "strings"
27 "testing"
28 "time"
29
30 "github.com/kr/fs"
31 "github.com/stretchr/testify/assert"
32 "golang.org/x/crypto/ssh"
33)
34
35func TestMain(m *testing.M) {
36 sftpClientLocation, _ := exec.LookPath("sftp")
37 testSftpClientBin = flag.String("sftp_client", sftpClientLocation, "location of the sftp client binary")
38
39 lookSFTPServer := []string{
40 "/usr/libexec/sftp-server",
41 "/usr/lib/openssh/sftp-server",
42 "/usr/lib/ssh/sftp-server",
43 "C:\\Program Files\\Git\\usr\\lib\\ssh\\sftp-server.exe",
44 }
45 sftpServer, _ := exec.LookPath("sftp-server")
46 if len(sftpServer) == 0 {
47 for _, location := range lookSFTPServer {
48 if _, err := os.Stat(location); err == nil {
49 sftpServer = location
50 break
51 }
52 }
53 }
54 testSftp = flag.String("sftp", sftpServer, "location of the sftp server binary")
55 flag.Parse()
56
57 os.Exit(m.Run())
58}
59
60func skipIfWindows(t testing.TB) {
61 if runtime.GOOS == "windows" {
62 t.Skip("skipping test on windows")
63 }
64}
65
66func skipIfPlan9(t testing.TB) {
67 if runtime.GOOS == "plan9" {
68 t.Skip("skipping test on plan9")
69 }
70}
71
72var testServerImpl = flag.Bool("testserver", false, "perform integration tests against sftp package server instance")
73var testIntegration = flag.Bool("integration", false, "perform integration tests against sftp server process")
74var testAllocator = flag.Bool("allocator", false, "perform tests using the allocator")
75var testSftp *string
76
77var testSftpClientBin *string
78var sshServerDebugStream = ioutil.Discard
79var sftpServerDebugStream = ioutil.Discard
80var sftpClientDebugStream = ioutil.Discard
81
82const (
83 GolangSFTP = true
84 OpenSSHSFTP = false
85)
86
87var (
88 hostPrivateKeySigner ssh.Signer
89 privKey = []byte(`
90-----BEGIN RSA PRIVATE KEY-----
91MIIEowIBAAKCAQEArhp7SqFnXVZAgWREL9Ogs+miy4IU/m0vmdkoK6M97G9NX/Pj
92wf8I/3/ynxmcArbt8Rc4JgkjT2uxx/NqR0yN42N1PjO5Czu0dms1PSqcKIJdeUBV
937gdrKSm9Co4d2vwfQp5mg47eG4w63pz7Drk9+VIyi9YiYH4bve7WnGDswn4ycvYZ
94slV5kKnjlfCdPig+g5P7yQYud0cDWVwyA0+kxvL6H3Ip+Fu8rLDZn4/P1WlFAIuc
95PAf4uEKDGGmC2URowi5eesYR7f6GN/HnBs2776laNlAVXZUmYTUfOGagwLsEkx8x
96XdNqntfbs2MOOoK+myJrNtcB9pCrM0H6um19uQIDAQABAoIBABkWr9WdVKvalgkP
97TdQmhu3mKRNyd1wCl+1voZ5IM9Ayac/98UAvZDiNU4Uhx52MhtVLJ0gz4Oa8+i16
98IkKMAZZW6ro/8dZwkBzQbieWUFJ2Fso2PyvB3etcnGU8/Yhk9IxBDzy+BbuqhYE2
991ebVQtz+v1HvVZzaD11bYYm/Xd7Y28QREVfFen30Q/v3dv7dOteDE/RgDS8Czz7w
100jMW32Q8JL5grz7zPkMK39BLXsTcSYcaasT2ParROhGJZDmbgd3l33zKCVc1zcj9B
101SA47QljGd09Tys958WWHgtj2o7bp9v1Ufs4LnyKgzrB80WX1ovaSQKvd5THTLchO
102kLIhUAECgYEA2doGXy9wMBmTn/hjiVvggR1aKiBwUpnB87Hn5xCMgoECVhFZlT6l
103WmZe7R2klbtG1aYlw+y+uzHhoVDAJW9AUSV8qoDUwbRXvBVlp+In5wIqJ+VjfivK
104zgIfzomL5NvDz37cvPmzqIeySTowEfbQyq7CUQSoDtE9H97E2wWZhDkCgYEAzJdJ
105k+NSFoTkHhfD3L0xCDHpRV3gvaOeew8524fVtVUq53X8m91ng4AX1r74dCUYwwiF
106gqTtSSJfx2iH1xKnNq28M9uKg7wOrCKrRqNPnYUO3LehZEC7rwUr26z4iJDHjjoB
107uBcS7nw0LJ+0Zeg1IF+aIdZGV3MrAKnrzWPixYECgYBsffX6ZWebrMEmQ89eUtFF
108u9ZxcGI/4K8ErC7vlgBD5ffB4TYZ627xzFWuBLs4jmHCeNIJ9tct5rOVYN+wRO1k
109/CRPzYUnSqb+1jEgILL6istvvv+DkE+ZtNkeRMXUndWwel94BWsBnUKe0UmrSJ3G
110sq23J3iCmJW2T3z+DpXbkQKBgQCK+LUVDNPE0i42NsRnm+fDfkvLP7Kafpr3Umdl
111tMY474o+QYn+wg0/aPJIf9463rwMNyyhirBX/k57IIktUdFdtfPicd2MEGETElWv
112nN1GzYxD50Rs2f/jKisZhEwqT9YNyV9DkgDdGGdEbJNYqbv0qpwDIg8T9foe8E1p
113bdErgQKBgAt290I3L316cdxIQTkJh1DlScN/unFffITwu127WMr28Jt3mq3cZpuM
114Aecey/eEKCj+Rlas5NDYKsB18QIuAw+qqWyq0LAKLiAvP1965Rkc4PLScl3MgJtO
115QYa37FK0p8NcDeUuF86zXBVutwS5nJLchHhKfd590ks57OROtm29
116-----END RSA PRIVATE KEY-----
117`)
118)
119
120func init() {
121 var err error
122 hostPrivateKeySigner, err = ssh.ParsePrivateKey(privKey)
123 if err != nil {
124 panic(err)
125 }
126}
127
128func keyAuth(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
129 permissions := &ssh.Permissions{
130 CriticalOptions: map[string]string{},
131 Extensions: map[string]string{},
132 }
133 return permissions, nil
134}
135
136func pwAuth(conn ssh.ConnMetadata, pw []byte) (*ssh.Permissions, error) {
137 permissions := &ssh.Permissions{
138 CriticalOptions: map[string]string{},
139 Extensions: map[string]string{},
140 }
141 return permissions, nil
142}
143
144func basicServerConfig() *ssh.ServerConfig {
145 config := ssh.ServerConfig{
146 Config: ssh.Config{
147 MACs: []string{"hmac-sha1"},
148 },
149 PasswordCallback: pwAuth,
150 PublicKeyCallback: keyAuth,
151 }
152 config.AddHostKey(hostPrivateKeySigner)
153 return &config
154}
155
156type sshServer struct {
157 useSubsystem bool
158 conn net.Conn
159 config *ssh.ServerConfig
160 sshConn *ssh.ServerConn
161 newChans <-chan ssh.NewChannel
162 newReqs <-chan *ssh.Request
163}
164
165func sshServerFromConn(conn net.Conn, useSubsystem bool, config *ssh.ServerConfig) (*sshServer, error) {
166 // From a standard TCP connection to an encrypted SSH connection
167 sshConn, newChans, newReqs, err := ssh.NewServerConn(conn, config)
168 if err != nil {
169 return nil, err
170 }
171
172 svr := &sshServer{useSubsystem, conn, config, sshConn, newChans, newReqs}
173 svr.listenChannels()
174 return svr, nil
175}
176
177func (svr *sshServer) Wait() error {
178 return svr.sshConn.Wait()
179}
180
181func (svr *sshServer) Close() error {
182 return svr.sshConn.Close()
183}
184
185func (svr *sshServer) listenChannels() {
186 go func() {
187 for chanReq := range svr.newChans {
188 go svr.handleChanReq(chanReq)
189 }
190 }()
191 go func() {
192 for req := range svr.newReqs {
193 go svr.handleReq(req)
194 }
195 }()
196}
197
198func (svr *sshServer) handleReq(req *ssh.Request) {
199 switch req.Type {
200 default:
201 rejectRequest(req)
202 }
203}
204
205type sshChannelServer struct {
206 svr *sshServer
207 chanReq ssh.NewChannel
208 ch ssh.Channel
209 newReqs <-chan *ssh.Request
210}
211
212type sshSessionChannelServer struct {
213 *sshChannelServer
214 env []string
215}
216
217func (svr *sshServer) handleChanReq(chanReq ssh.NewChannel) {
218 fmt.Fprintf(sshServerDebugStream, "channel request: %v, extra: '%v'\n", chanReq.ChannelType(), hex.EncodeToString(chanReq.ExtraData()))
219 switch chanReq.ChannelType() {
220 case "session":
221 if ch, reqs, err := chanReq.Accept(); err != nil {
222 fmt.Fprintf(sshServerDebugStream, "fail to accept channel request: %v\n", err)
223 chanReq.Reject(ssh.ResourceShortage, "channel accept failure")
224 } else {
225 chsvr := &sshSessionChannelServer{
226 sshChannelServer: &sshChannelServer{svr, chanReq, ch, reqs},
227 env: append([]string{}, os.Environ()...),
228 }
229 chsvr.handle()
230 }
231 default:
232 chanReq.Reject(ssh.UnknownChannelType, "channel type is not a session")
233 }
234}
235
236func (chsvr *sshSessionChannelServer) handle() {
237 // should maybe do something here...
238 go chsvr.handleReqs()
239}
240
241func (chsvr *sshSessionChannelServer) handleReqs() {
242 for req := range chsvr.newReqs {
243 chsvr.handleReq(req)
244 }
245 fmt.Fprintf(sshServerDebugStream, "ssh server session channel complete\n")
246}
247
248func (chsvr *sshSessionChannelServer) handleReq(req *ssh.Request) {
249 switch req.Type {
250 case "env":
251 chsvr.handleEnv(req)
252 case "subsystem":
253 chsvr.handleSubsystem(req)
254 default:
255 rejectRequest(req)
256 }
257}
258
259func rejectRequest(req *ssh.Request) error {
260 fmt.Fprintf(sshServerDebugStream, "ssh rejecting request, type: %s\n", req.Type)
261 err := req.Reply(false, []byte{})
262 if err != nil {
263 fmt.Fprintf(sshServerDebugStream, "ssh request reply had error: %v\n", err)
264 }
265 return err
266}
267
268func rejectRequestUnmarshalError(req *ssh.Request, s any, err error) error {
269 fmt.Fprintf(sshServerDebugStream, "ssh request unmarshaling error, type '%T': %v\n", s, err)
270 rejectRequest(req)
271 return err
272}
273
274// env request form:
275type sshEnvRequest struct {
276 Envvar string
277 Value string
278}
279
280func (chsvr *sshSessionChannelServer) handleEnv(req *ssh.Request) error {
281 envReq := &sshEnvRequest{}
282 if err := ssh.Unmarshal(req.Payload, envReq); err != nil {
283 return rejectRequestUnmarshalError(req, envReq, err)
284 }
285 req.Reply(true, nil)
286
287 found := false
288 for i, envstr := range chsvr.env {
289 if strings.HasPrefix(envstr, envReq.Envvar+"=") {
290 found = true
291 chsvr.env[i] = envReq.Envvar + "=" + envReq.Value
292 }
293 }
294 if !found {
295 chsvr.env = append(chsvr.env, envReq.Envvar+"="+envReq.Value)
296 }
297
298 return nil
299}
300
301// Payload: int: command size, string: command
302type sshSubsystemRequest struct {
303 Name string
304}
305
306type sshSubsystemExitStatus struct {
307 Status uint32
308}
309
310func (chsvr *sshSessionChannelServer) handleSubsystem(req *ssh.Request) error {
311 defer func() {
312 err1 := chsvr.ch.CloseWrite()
313 err2 := chsvr.ch.Close()
314 fmt.Fprintf(sshServerDebugStream, "ssh server subsystem request complete, err: %v %v\n", err1, err2)
315 }()
316
317 subsystemReq := &sshSubsystemRequest{}
318 if err := ssh.Unmarshal(req.Payload, subsystemReq); err != nil {
319 return rejectRequestUnmarshalError(req, subsystemReq, err)
320 }
321
322 // reply to the ssh client
323
324 // no idea if this is actually correct spec-wise.
325 // just enough for an sftp server to start.
326 if subsystemReq.Name != "sftp" {
327 return req.Reply(false, nil)
328 }
329
330 req.Reply(true, nil)
331
332 if !chsvr.svr.useSubsystem {
333 // use the openssh sftp server backend; this is to test the ssh code, not the sftp code,
334 // or is used for comparison between our sftp subsystem and the openssh sftp subsystem
335 cmd := exec.Command(*testSftp, "-e", "-l", "DEBUG") // log to stderr
336 cmd.Stdin = chsvr.ch
337 cmd.Stdout = chsvr.ch
338 cmd.Stderr = sftpServerDebugStream
339 if err := cmd.Start(); err != nil {
340 return err
341 }
342 return cmd.Wait()
343 }
344
345 sftpServer, err := NewServer(
346 chsvr.ch,
347 WithDebug(sftpServerDebugStream),
348 )
349 if err != nil {
350 return err
351 }
352
353 // wait for the session to close
354 runErr := sftpServer.Serve()
355 exitStatus := uint32(1)
356 if runErr == nil {
357 exitStatus = uint32(0)
358 }
359
360 _, exitStatusErr := chsvr.ch.SendRequest("exit-status", false, ssh.Marshal(sshSubsystemExitStatus{exitStatus}))
361 return exitStatusErr
362}
363
364// starts an ssh server to test. returns: host string and port
365func testServer(t *testing.T, useSubsystem bool, readonly bool) (func(), string, int) {
366 t.Helper()
367
368 if !*testIntegration {
369 t.Skip("skipping integration test")
370 }
371
372 listener, err := net.Listen("tcp", "127.0.0.1:0")
373 if err != nil {
374 t.Fatal(err)
375 }
376
377 host, portStr, err := net.SplitHostPort(listener.Addr().String())
378 if err != nil {
379 t.Fatal(err)
380 }
381 port, err := strconv.Atoi(portStr)
382 if err != nil {
383 t.Fatal(err)
384 }
385
386 shutdown := make(chan struct{})
387
388 go func() {
389 for {
390 conn, err := listener.Accept()
391 if err != nil {
392 select {
393 case <-shutdown:
394 default:
395 t.Error("ssh server socket closed:", err)
396 }
397 return
398 }
399
400 go func() {
401 defer conn.Close()
402
403 sshSvr, err := sshServerFromConn(conn, useSubsystem, basicServerConfig())
404 if err != nil {
405 t.Error(err)
406 return
407 }
408
409 _ = sshSvr.Wait()
410 }()
411 }
412 }()
413
414 return func() { close(shutdown); listener.Close() }, host, port
415}
416
417func makeDummyKey() (string, error) {
418 priv, err := ecdsa.GenerateKey(elliptic.P256(), crand.Reader)
419 if err != nil {
420 return "", fmt.Errorf("cannot generate key: %w", err)
421 }
422 der, err := x509.MarshalECPrivateKey(priv)
423 if err != nil {
424 return "", fmt.Errorf("cannot marshal key: %w", err)
425 }
426 block := &pem.Block{Type: "EC PRIVATE KEY", Bytes: der}
427 f, err := ioutil.TempFile("", "sftp-test-key-")
428 if err != nil {
429 return "", fmt.Errorf("cannot create temp file: %w", err)
430 }
431 defer func() {
432 if f != nil {
433 _ = f.Close()
434 _ = os.Remove(f.Name())
435 }
436 }()
437 if err := pem.Encode(f, block); err != nil {
438 return "", fmt.Errorf("cannot write key: %w", err)
439 }
440 if err := f.Close(); err != nil {
441 return "", fmt.Errorf("error closing key file: %w", err)
442 }
443 path := f.Name()
444 f = nil
445 return path, nil
446}
447
448type execError struct {
449 path string
450 stderr string
451 err error
452}
453
454func (e *execError) Error() string {
455 return fmt.Sprintf("%s: %v: %s", e.path, e.err, e.stderr)
456}
457
458func (e *execError) Unwrap() error {
459 return e.err
460}
461
462func (e *execError) Cause() error {
463 return e.err
464}
465
466func runSftpClient(t *testing.T, script string, path string, host string, port int) (string, error) {
467 // if sftp client binary is unavailable, skip test
468 if _, err := os.Stat(*testSftpClientBin); err != nil {
469 t.Skip("sftp client binary unavailable")
470 }
471
472 // make a dummy key so we don't rely on ssh-agent
473 dummyKey, err := makeDummyKey()
474 if err != nil {
475 return "", err
476 }
477 defer os.Remove(dummyKey)
478
479 cmd := exec.Command(
480 *testSftpClientBin,
481 // "-vvvv",
482 "-b", "-",
483 "-o", "StrictHostKeyChecking=no",
484 "-o", "LogLevel=ERROR",
485 "-o", "UserKnownHostsFile /dev/null",
486 // do not trigger ssh-agent prompting
487 "-o", "IdentityFile="+dummyKey,
488 "-o", "IdentitiesOnly=yes",
489 "-P", fmt.Sprintf("%d", port), fmt.Sprintf("%s:%s", host, path),
490 )
491
492 cmd.Stdin = strings.NewReader(script)
493
494 stdout := new(bytes.Buffer)
495 cmd.Stdout = stdout
496
497 stderr := new(bytes.Buffer)
498 cmd.Stderr = stderr
499
500 if err := cmd.Start(); err != nil {
501 return "", err
502 }
503
504 if err := cmd.Wait(); err != nil {
505 return stdout.String(), &execError{
506 path: cmd.Path,
507 stderr: stderr.String(),
508 err: err,
509 }
510 }
511
512 return stdout.String(), nil
513}
514
515// assert.Eventually seems to have a data rate on macOS with go 1.14 so replace it with this simpler function
516func waitForCondition(t *testing.T, condition func() bool) {
517 start := time.Now()
518 tick := 10 * time.Millisecond
519 waitFor := 100 * time.Millisecond
520 for !condition() {
521 time.Sleep(tick)
522 if time.Since(start) > waitFor {
523 break
524 }
525 }
526 assert.True(t, condition())
527}
528
529func checkAllocatorBeforeServerClose(t *testing.T, alloc *allocator) {
530 if alloc != nil {
531 // before closing the server we are, generally, waiting for new packets in recvPacket and we have a page allocated.
532 // Sometime the sendPacket returns some milliseconds after the client receives the response, and so we have 2
533 // allocated pages here, so wait some milliseconds. To avoid crashes we must be sure to not release the pages
534 // too soon.
535 waitForCondition(t, func() bool { return alloc.countUsedPages() <= 1 })
536 }
537}
538
539func checkAllocatorAfterServerClose(t *testing.T, alloc *allocator) {
540 if alloc != nil {
541 // wait for the server cleanup
542 waitForCondition(t, func() bool { return alloc.countUsedPages() == 0 })
543 waitForCondition(t, func() bool { return alloc.countAvailablePages() == 0 })
544 }
545}
546
547func TestServerCompareSubsystems(t *testing.T) {
548 if runtime.GOOS == "windows" {
549 // TODO (puellanivis): not sure how to fix this, the OpenSSH SFTP implementation closes immediately.
550 t.Skip()
551 }
552
553 shutdownGo, hostGo, portGo := testServer(t, GolangSFTP, READONLY)
554 defer shutdownGo()
555
556 shutdownOp, hostOp, portOp := testServer(t, OpenSSHSFTP, READONLY)
557 defer shutdownOp()
558
559 script := `
560ls /
561ls -l /
562ls /dev/
563ls -l /dev/
564ls -l /etc/
565ls -l /bin/
566ls -l /usr/bin/
567`
568 outputGo, err := runSftpClient(t, script, "/", hostGo, portGo)
569 if err != nil {
570 t.Fatal(err)
571 }
572
573 outputOp, err := runSftpClient(t, script, "/", hostOp, portOp)
574 if err != nil {
575 t.Fatal(err)
576 }
577
578 newlineRegex := regexp.MustCompile(`\r*\n`)
579 spaceRegex := regexp.MustCompile(`\s+`)
580 outputGoLines := newlineRegex.Split(outputGo, -1)
581 outputOpLines := newlineRegex.Split(outputOp, -1)
582
583 if len(outputGoLines) != len(outputOpLines) {
584 t.Fatalf("output line count differs, go = %d, openssh = %d", len(outputGoLines), len(outputOpLines))
585 }
586
587 for i, goLine := range outputGoLines {
588 opLine := outputOpLines[i]
589 bad := false
590 if goLine != opLine {
591 goWords := spaceRegex.Split(goLine, -1)
592 opWords := spaceRegex.Split(opLine, -1)
593 // some fields are allowed to be different..
594 // during testing as processes are created/destroyed.
595 for j, goWord := range goWords {
596 if j >= len(opWords) {
597 bad = true
598 break
599 }
600 opWord := opWords[j]
601 if goWord != opWord {
602 switch j {
603 case 1, 2, 3, 7:
604 // words[1] as the link count for directories like proc is unstable
605 // words[2] and [3] as these are users & groups
606 // words[7] as timestamps on dirs can vary for things like /tmp
607 case 8:
608 // words[8] can either have full path or just the filename
609 bad = !strings.HasSuffix(opWord, "/"+goWord)
610 default:
611 bad = true
612 }
613 }
614 }
615 }
616
617 if bad {
618 t.Errorf("outputs differ\n go: %q\nopenssh: %q\n", goLine, opLine)
619 }
620 }
621}
622
623var rng = rand.New(rand.NewSource(time.Now().Unix()))
624
625func randData(length int) []byte {
626 data := make([]byte, length)
627 for i := 0; i < length; i++ {
628 data[i] = byte(rng.Uint32())
629 }
630 return data
631}
632
633func randName() string {
634 return "sftp." + hex.EncodeToString(randData(16))
635}
636
637func TestServerMkdirRmdir(t *testing.T) {
638 shutdown, hostGo, portGo := testServer(t, GolangSFTP, READONLY)
639 defer shutdown()
640
641 tmpDir := "/tmp/" + randName()
642 defer os.RemoveAll(tmpDir)
643
644 // mkdir remote
645 if _, err := runSftpClient(t, "mkdir "+tmpDir, "/", hostGo, portGo); err != nil {
646 t.Fatal(err)
647 }
648
649 // directory should now exist
650 if _, err := os.Stat(tmpDir); err != nil {
651 t.Fatal(err)
652 }
653
654 // now remove the directory
655 if _, err := runSftpClient(t, "rmdir "+tmpDir, "/", hostGo, portGo); err != nil {
656 t.Fatal(err)
657 }
658
659 if _, err := os.Stat(tmpDir); err == nil {
660 t.Fatal("should have error after deleting the directory")
661 }
662}
663
664func TestServerLink(t *testing.T) {
665 skipIfWindows(t) // No hard links on windows.
666 shutdown, hostGo, portGo := testServer(t, GolangSFTP, READONLY)
667 defer shutdown()
668
669 tmpFileLocalData := randData(999)
670
671 linkdest := "/tmp/" + randName()
672 defer os.RemoveAll(linkdest)
673 if err := ioutil.WriteFile(linkdest, tmpFileLocalData, 0644); err != nil {
674 t.Fatal(err)
675 }
676
677 link := "/tmp/" + randName()
678 defer os.RemoveAll(link)
679
680 // now create a hard link within the new directory
681 if output, err := runSftpClient(t, fmt.Sprintf("ln %s %s", linkdest, link), "/", hostGo, portGo); err != nil {
682 t.Fatalf("failed: %v %v", err, string(output))
683 }
684
685 // file should now exist and be the same size as linkdest
686 if stat, err := os.Lstat(link); err != nil {
687 t.Fatal(err)
688 } else if int(stat.Size()) != len(tmpFileLocalData) {
689 t.Fatalf("wrong size: %v", len(tmpFileLocalData))
690 }
691}
692
693func TestServerSymlink(t *testing.T) {
694 skipIfWindows(t) // No symlinks on windows.
695 shutdown, hostGo, portGo := testServer(t, GolangSFTP, READONLY)
696 defer shutdown()
697
698 link := "/tmp/" + randName()
699 defer os.RemoveAll(link)
700
701 // now create a symbolic link within the new directory
702 if output, err := runSftpClient(t, "symlink /bin/sh "+link, "/", hostGo, portGo); err != nil {
703 t.Fatalf("failed: %v %v", err, string(output))
704 }
705
706 // symlink should now exist
707 if stat, err := os.Lstat(link); err != nil {
708 t.Fatal(err)
709 } else if (stat.Mode() & os.ModeSymlink) != os.ModeSymlink {
710 t.Fatalf("is not a symlink: %v", stat.Mode())
711 }
712}
713
714func TestServerPut(t *testing.T) {
715 shutdown, hostGo, portGo := testServer(t, GolangSFTP, READONLY)
716 defer shutdown()
717
718 tmpFileLocal := "/tmp/" + randName()
719 tmpFileRemote := "/tmp/" + randName()
720 defer os.RemoveAll(tmpFileLocal)
721 defer os.RemoveAll(tmpFileRemote)
722
723 t.Logf("put: local %v remote %v", tmpFileLocal, tmpFileRemote)
724
725 // create a file with random contents. This will be the local file pushed to the server
726 tmpFileLocalData := randData(10 * 1024 * 1024)
727 if err := ioutil.WriteFile(tmpFileLocal, tmpFileLocalData, 0644); err != nil {
728 t.Fatal(err)
729 }
730
731 // sftp the file to the server
732 if output, err := runSftpClient(t, "put "+tmpFileLocal+" "+tmpFileRemote, "/", hostGo, portGo); err != nil {
733 t.Fatalf("runSftpClient failed: %v, output\n%v\n", err, output)
734 }
735
736 // tmpFile2 should now exist, with the same contents
737 if tmpFileRemoteData, err := ioutil.ReadFile(tmpFileRemote); err != nil {
738 t.Fatal(err)
739 } else if string(tmpFileLocalData) != string(tmpFileRemoteData) {
740 t.Fatal("contents of file incorrect after put")
741 }
742}
743
744func TestServerResume(t *testing.T) {
745 shutdown, hostGo, portGo := testServer(t, GolangSFTP, READONLY)
746 defer shutdown()
747
748 tmpFileLocal := "/tmp/" + randName()
749 tmpFileRemote := "/tmp/" + randName()
750 defer os.RemoveAll(tmpFileLocal)
751 defer os.RemoveAll(tmpFileRemote)
752
753 t.Logf("put: local %v remote %v", tmpFileLocal, tmpFileRemote)
754
755 // create a local file with random contents to be pushed to the server
756 tmpFileLocalData := randData(2 * 1024 * 1024)
757 // only write half the data to simulate a split upload
758 half := 1024 * 1024
759 err := ioutil.WriteFile(tmpFileLocal, tmpFileLocalData[:half], 0644)
760 if err != nil {
761 t.Fatal(err)
762 }
763
764 // sftp the first half of the file to the server
765 output, err := runSftpClient(t, "put "+tmpFileLocal+" "+tmpFileRemote,
766 "/", hostGo, portGo)
767 if err != nil {
768 t.Fatalf("runSftpClient failed: %v, output\n%v\n", err, output)
769 }
770
771 // write the full file out
772 err = ioutil.WriteFile(tmpFileLocal, tmpFileLocalData, 0644)
773 if err != nil {
774 t.Fatal(err)
775 }
776 // re-sftp the full file with the append flag set
777 output, err = runSftpClient(t, "put -a "+tmpFileLocal+" "+tmpFileRemote,
778 "/", hostGo, portGo)
779 if err != nil {
780 t.Fatalf("runSftpClient failed: %v, output\n%v\n", err, output)
781 }
782
783 // tmpFileRemote should now exist, with the same contents
784 if tmpFileRemoteData, err := ioutil.ReadFile(tmpFileRemote); err != nil {
785 t.Fatal(err)
786 } else if string(tmpFileLocalData) != string(tmpFileRemoteData) {
787 t.Fatal("contents of file incorrect after put")
788 }
789}
790
791func TestServerGet(t *testing.T) {
792 shutdown, hostGo, portGo := testServer(t, GolangSFTP, READONLY)
793 defer shutdown()
794
795 tmpFileLocal := "/tmp/" + randName()
796 tmpFileRemote := "/tmp/" + randName()
797 defer os.RemoveAll(tmpFileLocal)
798 defer os.RemoveAll(tmpFileRemote)
799
800 t.Logf("get: local %v remote %v", tmpFileLocal, tmpFileRemote)
801
802 // create a file with random contents. This will be the remote file pulled from the server
803 tmpFileRemoteData := randData(10 * 1024 * 1024)
804 if err := ioutil.WriteFile(tmpFileRemote, tmpFileRemoteData, 0644); err != nil {
805 t.Fatal(err)
806 }
807
808 // sftp the file to the server
809 if output, err := runSftpClient(t, "get "+tmpFileRemote+" "+tmpFileLocal, "/", hostGo, portGo); err != nil {
810 t.Fatalf("runSftpClient failed: %v, output\n%v\n", err, output)
811 }
812
813 // tmpFile2 should now exist, with the same contents
814 if tmpFileLocalData, err := ioutil.ReadFile(tmpFileLocal); err != nil {
815 t.Fatal(err)
816 } else if string(tmpFileLocalData) != string(tmpFileRemoteData) {
817 t.Fatal("contents of file incorrect after put")
818 }
819}
820
821func compareDirectoriesRecursive(t *testing.T, aroot, broot string) {
822 walker := fs.Walk(aroot)
823 for walker.Step() {
824 if err := walker.Err(); err != nil {
825 t.Fatal(err)
826 }
827 // find paths
828 aPath := walker.Path()
829 aRel, err := filepath.Rel(aroot, aPath)
830 if err != nil {
831 t.Fatalf("could not find relative path for %v: %v", aPath, err)
832 }
833 bPath := filepath.Join(broot, aRel)
834
835 if aRel == "." {
836 continue
837 }
838
839 //t.Logf("comparing: %v a: %v b %v", aRel, aPath, bPath)
840
841 // if a is a link, the sftp recursive copy won't have copied it. ignore
842 aLink, err := os.Lstat(aPath)
843 if err != nil {
844 t.Fatalf("could not lstat %v: %v", aPath, err)
845 }
846 if aLink.Mode()&os.ModeSymlink != 0 {
847 continue
848 }
849
850 // stat the files
851 aFile, err := os.Stat(aPath)
852 if err != nil {
853 t.Fatalf("could not stat %v: %v", aPath, err)
854 }
855 bFile, err := os.Stat(bPath)
856 if err != nil {
857 t.Fatalf("could not stat %v: %v", bPath, err)
858 }
859
860 // compare stats, with some leniency for the timestamp
861 if aFile.Mode() != bFile.Mode() {
862 t.Fatalf("modes different for %v: %v vs %v", aRel, aFile.Mode(), bFile.Mode())
863 }
864 if !aFile.IsDir() {
865 if aFile.Size() != bFile.Size() {
866 t.Fatalf("sizes different for %v: %v vs %v", aRel, aFile.Size(), bFile.Size())
867 }
868 }
869 timeDiff := aFile.ModTime().Sub(bFile.ModTime())
870 if timeDiff > time.Second || timeDiff < -time.Second {
871 t.Fatalf("mtimes different for %v: %v vs %v", aRel, aFile.ModTime(), bFile.ModTime())
872 }
873
874 // compare contents
875 if !aFile.IsDir() {
876 if aContents, err := ioutil.ReadFile(aPath); err != nil {
877 t.Fatal(err)
878 } else if bContents, err := ioutil.ReadFile(bPath); err != nil {
879 t.Fatal(err)
880 } else if string(aContents) != string(bContents) {
881 t.Fatalf("contents different for %v", aRel)
882 }
883 }
884 }
885}
886
887func TestServerPutRecursive(t *testing.T) {
888 shutdown, hostGo, portGo := testServer(t, GolangSFTP, READONLY)
889 defer shutdown()
890
891 dirLocal, err := os.Getwd()
892 if err != nil {
893 t.Fatal(err)
894 }
895 tmpDirRemote := "/tmp/" + randName()
896 defer os.RemoveAll(tmpDirRemote)
897
898 t.Logf("put recursive: local %v remote %v", dirLocal, tmpDirRemote)
899
900 // On windows, the client copies the contents of the directory, not the directory itself.
901 winFix := ""
902 if runtime.GOOS == "windows" {
903 winFix = "/" + filepath.Base(dirLocal)
904 } //*/
905
906 // push this directory (source code etc) recursively to the server
907 if output, err := runSftpClient(t, "mkdir "+tmpDirRemote+"\r\nput -R -p "+dirLocal+" "+tmpDirRemote+winFix, "/", hostGo, portGo); err != nil {
908 t.Fatalf("runSftpClient failed: %v, output\n%v\n", err, output)
909 }
910
911 compareDirectoriesRecursive(t, dirLocal, filepath.Join(tmpDirRemote, filepath.Base(dirLocal)))
912}
913
914func TestServerGetRecursive(t *testing.T) {
915 shutdown, hostGo, portGo := testServer(t, GolangSFTP, READONLY)
916 defer shutdown()
917
918 dirRemote, err := os.Getwd()
919 if err != nil {
920 t.Fatal(err)
921 }
922 tmpDirLocal := "/tmp/" + randName()
923 defer os.RemoveAll(tmpDirLocal)
924
925 t.Logf("get recursive: local %v remote %v", tmpDirLocal, dirRemote)
926
927 // On windows, the client copies the contents of the directory, not the directory itself.
928 winFix := ""
929 if runtime.GOOS == "windows" {
930 winFix = "/" + filepath.Base(dirRemote)
931 }
932
933 // pull this directory (source code etc) recursively from the server
934 if output, err := runSftpClient(t, "lmkdir "+tmpDirLocal+"\r\nget -R -p "+dirRemote+" "+tmpDirLocal+winFix, "/", hostGo, portGo); err != nil {
935 t.Fatalf("runSftpClient failed: %v, output\n%v\n", err, output)
936 }
937
938 compareDirectoriesRecursive(t, dirRemote, filepath.Join(tmpDirLocal, filepath.Base(dirRemote)))
939}