Skip to content

Commit c35a030

Browse files
authored
Merge pull request pkg#282 from kardianos/kardianos-fixup-tests
sftp: update tests to finish and not panic on windows
2 parents e6f3261 + d8ec5de commit c35a030

9 files changed

+79
-28
lines changed

client_integration_darwin_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"testing"
66
)
77

8-
const sftpServer = "/usr/libexec/sftp-server"
9-
108
func TestClientStatVFS(t *testing.T) {
119
if *testServerImpl {
1210
t.Skipf("go server does not support FXP_EXTENDED")

client_integration_linux_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"testing"
66
)
77

8-
const sftpServer = "/usr/lib/openssh/sftp-server"
9-
108
func TestClientStatVFS(t *testing.T) {
119
if *testServerImpl {
1210
t.Skipf("go server does not support FXP_EXTENDED")

client_integration_test.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"crypto/sha1"
99
"encoding"
1010
"errors"
11-
"flag"
1211
"io"
1312
"io/ioutil"
1413
"math/rand"
@@ -20,13 +19,12 @@ import (
2019
"path/filepath"
2120
"reflect"
2221
"regexp"
22+
"sort"
2323
"strconv"
2424
"testing"
2525
"testing/quick"
2626
"time"
2727

28-
"sort"
29-
3028
"github.com/kr/fs"
3129
)
3230

@@ -38,10 +36,6 @@ const (
3836
debuglevel = "ERROR" // set to "DEBUG" for debugging
3937
)
4038

41-
var testServerImpl = flag.Bool("testserver", false, "perform integration tests against sftp package server instance")
42-
var testIntegration = flag.Bool("integration", false, "perform integration tests against sftp server process")
43-
var testSftp = flag.String("sftp", sftpServer, "location of the sftp server binary")
44-
4539
type delayedWrite struct {
4640
t time.Time
4741
b []byte
@@ -210,6 +204,7 @@ func TestClientLstat(t *testing.T) {
210204
if err != nil {
211205
t.Fatal(err)
212206
}
207+
f.Close()
213208
defer os.Remove(f.Name())
214209

215210
want, err := os.Lstat(f.Name())
@@ -236,6 +231,7 @@ func TestClientLstatIsNotExist(t *testing.T) {
236231
if err != nil {
237232
t.Fatal(err)
238233
}
234+
f.Close()
239235
os.Remove(f.Name())
240236

241237
if _, err := sftp.Lstat(f.Name()); !os.IsNotExist(err) {
@@ -291,6 +287,7 @@ func TestClientOpen(t *testing.T) {
291287
if err != nil {
292288
t.Fatal(err)
293289
}
290+
f.Close()
294291
defer os.Remove(f.Name())
295292

296293
got, err := sftp.Open(f.Name())
@@ -517,6 +514,8 @@ func TestClientFileStat(t *testing.T) {
517514
}
518515

519516
func TestClientStatLink(t *testing.T) {
517+
skipIfWindows(t) // Windows does not support links.
518+
520519
sftp, cmd := testClient(t, READONLY, NO_DELAY)
521520
defer cmd.Wait()
522521
defer sftp.Close()
@@ -585,6 +584,8 @@ func TestClientRemove(t *testing.T) {
585584
if err != nil {
586585
t.Fatal(err)
587586
}
587+
f.Close()
588+
588589
if err := sftp.Remove(f.Name()); err != nil {
589590
t.Fatal(err)
590591
}
@@ -636,6 +637,8 @@ func TestClientRename(t *testing.T) {
636637
if err != nil {
637638
t.Fatal(err)
638639
}
640+
f.Close()
641+
639642
f2 := f.Name() + ".new"
640643
if err := sftp.Rename(f.Name(), f2); err != nil {
641644
t.Fatal(err)
@@ -657,6 +660,8 @@ func TestClientPosixRename(t *testing.T) {
657660
if err != nil {
658661
t.Fatal(err)
659662
}
663+
f.Close()
664+
660665
f2 := f.Name() + ".new"
661666
if err := sftp.PosixRename(f.Name(), f2); err != nil {
662667
t.Fatal(err)
@@ -682,10 +687,10 @@ func TestClientGetwd(t *testing.T) {
682687
if err != nil {
683688
t.Fatal(err)
684689
}
685-
if !path.IsAbs(rwd) {
690+
if !filepath.IsAbs(rwd) {
686691
t.Fatalf("Getwd: wanted absolute path, got %q", rwd)
687692
}
688-
if lwd != rwd {
693+
if filepath.ToSlash(lwd) != filepath.ToSlash(rwd) {
689694
t.Fatalf("Getwd: want %q, got %q", lwd, rwd)
690695
}
691696
}
@@ -731,6 +736,7 @@ func TestClientSymlink(t *testing.T) {
731736
}
732737

733738
func TestClientChmod(t *testing.T) {
739+
skipIfWindows(t) // No UNIX permissions.
734740
sftp, cmd := testClient(t, READWRITE, NO_DELAY)
735741
defer cmd.Wait()
736742
defer sftp.Close()
@@ -739,6 +745,8 @@ func TestClientChmod(t *testing.T) {
739745
if err != nil {
740746
t.Fatal(err)
741747
}
748+
f.Close()
749+
742750
if err := sftp.Chmod(f.Name(), 0531); err != nil {
743751
t.Fatal(err)
744752
}
@@ -750,6 +758,7 @@ func TestClientChmod(t *testing.T) {
750758
}
751759

752760
func TestClientChmodReadonly(t *testing.T) {
761+
skipIfWindows(t) // No UNIX permissions.
753762
sftp, cmd := testClient(t, READONLY, NO_DELAY)
754763
defer cmd.Wait()
755764
defer sftp.Close()
@@ -758,12 +767,15 @@ func TestClientChmodReadonly(t *testing.T) {
758767
if err != nil {
759768
t.Fatal(err)
760769
}
770+
f.Close()
771+
761772
if err := sftp.Chmod(f.Name(), 0531); err == nil {
762773
t.Fatal("expected error")
763774
}
764775
}
765776

766777
func TestClientChown(t *testing.T) {
778+
skipIfWindows(t) // No UNIX permissions.
767779
sftp, cmd := testClient(t, READWRITE, NO_DELAY)
768780
defer cmd.Wait()
769781
defer sftp.Close()
@@ -821,6 +833,7 @@ func TestClientChown(t *testing.T) {
821833
}
822834

823835
func TestClientChownReadonly(t *testing.T) {
836+
skipIfWindows(t) // No UNIX permissions.
824837
sftp, cmd := testClient(t, READONLY, NO_DELAY)
825838
defer cmd.Wait()
826839
defer sftp.Close()
@@ -948,7 +961,9 @@ func TestClientTruncateReadonly(t *testing.T) {
948961
}
949962

950963
func sameFile(want, got os.FileInfo) bool {
951-
return want.Name() == got.Name() &&
964+
_, wantName := filepath.Split(want.Name())
965+
_, gotName := filepath.Split(got.Name())
966+
return wantName == gotName &&
952967
want.Size() == got.Size()
953968
}
954969

@@ -997,7 +1012,7 @@ func TestClientReadDir(t *testing.T) {
9971012
defer sftp1.Close()
9981013
defer sftp2.Close()
9991014

1000-
dir := "/dev/"
1015+
dir := os.TempDir()
10011016

10021017
d, err := os.Open(dir)
10031018
if err != nil {
@@ -1127,17 +1142,13 @@ func readHash(t *testing.T, r io.Reader) (string, int64) {
11271142
// writeN writes n bytes of random data to w and returns the
11281143
// hash of that data.
11291144
func writeN(t *testing.T, w io.Writer, n int64) string {
1130-
rand, err := os.Open("/dev/urandom")
1131-
if err != nil {
1132-
t.Fatal(err)
1133-
}
1134-
defer rand.Close()
1145+
r := rand.New(rand.NewSource(time.Now().UnixNano()))
11351146

11361147
h := sha1.New()
11371148

11381149
mw := io.MultiWriter(w, h)
11391150

1140-
written, err := io.CopyN(mw, rand, n)
1151+
written, err := io.CopyN(mw, r, n)
11411152
if err != nil {
11421153
t.Fatal(err)
11431154
}
@@ -1733,6 +1744,7 @@ func TestGlobUNC(t *testing.T) {
17331744

17341745
// sftp/issue/42, abrupt server hangup would result in client hangs.
17351746
func TestServerRoughDisconnect(t *testing.T) {
1747+
skipIfWindows(t)
17361748
if *testServerImpl {
17371749
t.Skipf("skipping with -testserver")
17381750
}
@@ -1757,6 +1769,7 @@ func TestServerRoughDisconnect(t *testing.T) {
17571769
// due to broadcastErr filling up the request channel
17581770
// this reproduces it about 50% of the time
17591771
func TestServerRoughDisconnect2(t *testing.T) {
1772+
skipIfWindows(t)
17601773
if *testServerImpl {
17611774
t.Skipf("skipping with -testserver")
17621775
}
@@ -1784,6 +1797,7 @@ func TestServerRoughDisconnect2(t *testing.T) {
17841797

17851798
// sftp/issue/234 - abrupt shutdown during ReadFrom hangs client
17861799
func TestServerRoughDisconnect3(t *testing.T) {
1800+
skipIfWindows(t)
17871801
if *testServerImpl {
17881802
t.Skipf("skipping with -testserver")
17891803
}
@@ -1811,6 +1825,7 @@ func TestServerRoughDisconnect3(t *testing.T) {
18111825

18121826
// sftp/issue/234 - also affected Write
18131827
func TestServerRoughDisconnect4(t *testing.T) {
1828+
skipIfWindows(t)
18141829
if *testServerImpl {
18151830
t.Skipf("skipping with -testserver")
18161831
}
@@ -1846,6 +1861,7 @@ func TestServerRoughDisconnect4(t *testing.T) {
18461861

18471862
// sftp/issue/26 writing to a read only file caused client to loop.
18481863
func TestClientWriteToROFile(t *testing.T) {
1864+
skipIfWindows(t)
18491865
sftp, cmd := testClient(t, READWRITE, NO_DELAY)
18501866
defer cmd.Wait()
18511867
defer sftp.Close()
@@ -1862,6 +1878,7 @@ func TestClientWriteToROFile(t *testing.T) {
18621878
}
18631879

18641880
func benchmarkRead(b *testing.B, bufsize int, delay time.Duration) {
1881+
skipIfWindows(b)
18651882
size := 10*1024*1024 + 123 // ~10MiB
18661883

18671884
// open sftp client
@@ -2118,6 +2135,7 @@ func BenchmarkReadFrom4MiBDelay150Msec(b *testing.B) {
21182135
}
21192136

21202137
func benchmarkCopyDown(b *testing.B, fileSize int64, delay time.Duration) {
2138+
skipIfWindows(b)
21212139
// Create a temp file and fill it with zero's.
21222140
src, err := ioutil.TempFile("", "sftptest")
21232141
if err != nil {
@@ -2191,6 +2209,7 @@ func BenchmarkCopyDown10MiBDelay150Msec(b *testing.B) {
21912209
}
21922210

21932211
func benchmarkCopyUp(b *testing.B, fileSize int64, delay time.Duration) {
2212+
skipIfWindows(b)
21942213
// Create a temp file and fill it with zero's.
21952214
src, err := ioutil.TempFile("", "sftptest")
21962215
if err != nil {
File renamed without changes.
File renamed without changes.

other_test.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

request-server_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func (cs csPair) testHandler() *root {
3232
const sock = "/tmp/rstest.sock"
3333

3434
func clientRequestServerPair(t *testing.T) *csPair {
35+
skipIfWindows(t)
3536
ready := make(chan bool)
3637
os.Remove(sock) // either this or signal handling
3738
var server *RequestServer

server_integration_test.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"path"
2323
"path/filepath"
2424
"regexp"
25+
"runtime"
2526
"strconv"
2627
"strings"
2728
"testing"
@@ -31,7 +32,40 @@ import (
3132
"golang.org/x/crypto/ssh"
3233
)
3334

34-
var testSftpClientBin = flag.String("sftp_client", "/usr/bin/sftp", "location of the sftp client binary")
35+
func TestMain(m *testing.M) {
36+
sftpClientLocation, _ := exec.LookPath("sftp")
37+
testSftpClientBin = flag.String("sftp_client", sftpClientLocation, "location of the sftp client binary")
38+
flag.Parse()
39+
40+
lookSFTPServer := []string{
41+
"/usr/libexec/sftp-server",
42+
"/usr/lib/openssh/sftp-server",
43+
}
44+
sftpServer, _ := exec.LookPath("sftp-server")
45+
if len(sftpServer) == 0 {
46+
for _, location := range lookSFTPServer {
47+
if _, err := os.Stat(location); err == nil {
48+
sftpServer = location
49+
break
50+
}
51+
}
52+
}
53+
testSftp = flag.String("sftp", sftpServer, "location of the sftp server binary")
54+
55+
os.Exit(m.Run())
56+
}
57+
58+
func skipIfWindows(t testing.TB) {
59+
if runtime.GOOS == "windows" {
60+
t.Skip("skipping test on windows")
61+
}
62+
}
63+
64+
var testServerImpl = flag.Bool("testserver", false, "perform integration tests against sftp package server instance")
65+
var testIntegration = flag.Bool("integration", false, "perform integration tests against sftp server process")
66+
var testSftp *string
67+
68+
var testSftpClientBin *string
3569
var sshServerDebugStream = ioutil.Discard
3670
var sftpServerDebugStream = ioutil.Discard
3771
var sftpClientDebugStream = ioutil.Discard
@@ -419,13 +453,17 @@ func runSftpClient(t *testing.T, script string, path string, host string, port i
419453
}
420454
cmd := exec.Command(*testSftpClientBin, args...)
421455
var stdout bytes.Buffer
456+
var stderr bytes.Buffer
422457
cmd.Stdin = bytes.NewBufferString(script)
423458
cmd.Stdout = &stdout
424-
cmd.Stderr = sftpClientDebugStream
459+
cmd.Stderr = &stderr
425460
if err := cmd.Start(); err != nil {
426461
return "", err
427462
}
428463
err = cmd.Wait()
464+
if err != nil {
465+
err = fmt.Errorf("%v: %s", err, stderr.String())
466+
}
429467
return stdout.String(), err
430468
}
431469

@@ -532,6 +570,7 @@ func TestServerMkdirRmdir(t *testing.T) {
532570
}
533571

534572
func TestServerSymlink(t *testing.T) {
573+
skipIfWindows(t) // No symlinks on windows.
535574
listenerGo, hostGo, portGo := testServer(t, GOLANG_SFTP, READONLY)
536575
defer listenerGo.Close()
537576

server_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ func TestInvalidExtendedPacket(t *testing.T) {
226226

227227
// test that server handles concurrent requests correctly
228228
func TestConcurrentRequests(t *testing.T) {
229+
skipIfWindows(t)
229230
client, server := clientServerPair(t)
230231
defer client.Close()
231232
defer server.Close()

0 commit comments

Comments
 (0)