8
8
"crypto/sha1"
9
9
"encoding"
10
10
"errors"
11
- "flag"
12
11
"io"
13
12
"io/ioutil"
14
13
"math/rand"
@@ -20,13 +19,12 @@ import (
20
19
"path/filepath"
21
20
"reflect"
22
21
"regexp"
22
+ "sort"
23
23
"strconv"
24
24
"testing"
25
25
"testing/quick"
26
26
"time"
27
27
28
- "sort"
29
-
30
28
"github.com/kr/fs"
31
29
)
32
30
@@ -38,10 +36,6 @@ const (
38
36
debuglevel = "ERROR" // set to "DEBUG" for debugging
39
37
)
40
38
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
-
45
39
type delayedWrite struct {
46
40
t time.Time
47
41
b []byte
@@ -210,6 +204,7 @@ func TestClientLstat(t *testing.T) {
210
204
if err != nil {
211
205
t .Fatal (err )
212
206
}
207
+ f .Close ()
213
208
defer os .Remove (f .Name ())
214
209
215
210
want , err := os .Lstat (f .Name ())
@@ -236,6 +231,7 @@ func TestClientLstatIsNotExist(t *testing.T) {
236
231
if err != nil {
237
232
t .Fatal (err )
238
233
}
234
+ f .Close ()
239
235
os .Remove (f .Name ())
240
236
241
237
if _ , err := sftp .Lstat (f .Name ()); ! os .IsNotExist (err ) {
@@ -291,6 +287,7 @@ func TestClientOpen(t *testing.T) {
291
287
if err != nil {
292
288
t .Fatal (err )
293
289
}
290
+ f .Close ()
294
291
defer os .Remove (f .Name ())
295
292
296
293
got , err := sftp .Open (f .Name ())
@@ -517,6 +514,8 @@ func TestClientFileStat(t *testing.T) {
517
514
}
518
515
519
516
func TestClientStatLink (t * testing.T ) {
517
+ skipIfWindows (t ) // Windows does not support links.
518
+
520
519
sftp , cmd := testClient (t , READONLY , NO_DELAY )
521
520
defer cmd .Wait ()
522
521
defer sftp .Close ()
@@ -585,6 +584,8 @@ func TestClientRemove(t *testing.T) {
585
584
if err != nil {
586
585
t .Fatal (err )
587
586
}
587
+ f .Close ()
588
+
588
589
if err := sftp .Remove (f .Name ()); err != nil {
589
590
t .Fatal (err )
590
591
}
@@ -636,6 +637,8 @@ func TestClientRename(t *testing.T) {
636
637
if err != nil {
637
638
t .Fatal (err )
638
639
}
640
+ f .Close ()
641
+
639
642
f2 := f .Name () + ".new"
640
643
if err := sftp .Rename (f .Name (), f2 ); err != nil {
641
644
t .Fatal (err )
@@ -657,6 +660,8 @@ func TestClientPosixRename(t *testing.T) {
657
660
if err != nil {
658
661
t .Fatal (err )
659
662
}
663
+ f .Close ()
664
+
660
665
f2 := f .Name () + ".new"
661
666
if err := sftp .PosixRename (f .Name (), f2 ); err != nil {
662
667
t .Fatal (err )
@@ -682,10 +687,10 @@ func TestClientGetwd(t *testing.T) {
682
687
if err != nil {
683
688
t .Fatal (err )
684
689
}
685
- if ! path .IsAbs (rwd ) {
690
+ if ! filepath .IsAbs (rwd ) {
686
691
t .Fatalf ("Getwd: wanted absolute path, got %q" , rwd )
687
692
}
688
- if lwd != rwd {
693
+ if filepath . ToSlash ( lwd ) != filepath . ToSlash ( rwd ) {
689
694
t .Fatalf ("Getwd: want %q, got %q" , lwd , rwd )
690
695
}
691
696
}
@@ -731,6 +736,7 @@ func TestClientSymlink(t *testing.T) {
731
736
}
732
737
733
738
func TestClientChmod (t * testing.T ) {
739
+ skipIfWindows (t ) // No UNIX permissions.
734
740
sftp , cmd := testClient (t , READWRITE , NO_DELAY )
735
741
defer cmd .Wait ()
736
742
defer sftp .Close ()
@@ -739,6 +745,8 @@ func TestClientChmod(t *testing.T) {
739
745
if err != nil {
740
746
t .Fatal (err )
741
747
}
748
+ f .Close ()
749
+
742
750
if err := sftp .Chmod (f .Name (), 0531 ); err != nil {
743
751
t .Fatal (err )
744
752
}
@@ -750,6 +758,7 @@ func TestClientChmod(t *testing.T) {
750
758
}
751
759
752
760
func TestClientChmodReadonly (t * testing.T ) {
761
+ skipIfWindows (t ) // No UNIX permissions.
753
762
sftp , cmd := testClient (t , READONLY , NO_DELAY )
754
763
defer cmd .Wait ()
755
764
defer sftp .Close ()
@@ -758,12 +767,15 @@ func TestClientChmodReadonly(t *testing.T) {
758
767
if err != nil {
759
768
t .Fatal (err )
760
769
}
770
+ f .Close ()
771
+
761
772
if err := sftp .Chmod (f .Name (), 0531 ); err == nil {
762
773
t .Fatal ("expected error" )
763
774
}
764
775
}
765
776
766
777
func TestClientChown (t * testing.T ) {
778
+ skipIfWindows (t ) // No UNIX permissions.
767
779
sftp , cmd := testClient (t , READWRITE , NO_DELAY )
768
780
defer cmd .Wait ()
769
781
defer sftp .Close ()
@@ -821,6 +833,7 @@ func TestClientChown(t *testing.T) {
821
833
}
822
834
823
835
func TestClientChownReadonly (t * testing.T ) {
836
+ skipIfWindows (t ) // No UNIX permissions.
824
837
sftp , cmd := testClient (t , READONLY , NO_DELAY )
825
838
defer cmd .Wait ()
826
839
defer sftp .Close ()
@@ -948,7 +961,9 @@ func TestClientTruncateReadonly(t *testing.T) {
948
961
}
949
962
950
963
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 &&
952
967
want .Size () == got .Size ()
953
968
}
954
969
@@ -997,7 +1012,7 @@ func TestClientReadDir(t *testing.T) {
997
1012
defer sftp1 .Close ()
998
1013
defer sftp2 .Close ()
999
1014
1000
- dir := "/dev/"
1015
+ dir := os . TempDir ()
1001
1016
1002
1017
d , err := os .Open (dir )
1003
1018
if err != nil {
@@ -1127,17 +1142,13 @@ func readHash(t *testing.T, r io.Reader) (string, int64) {
1127
1142
// writeN writes n bytes of random data to w and returns the
1128
1143
// hash of that data.
1129
1144
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 ()))
1135
1146
1136
1147
h := sha1 .New ()
1137
1148
1138
1149
mw := io .MultiWriter (w , h )
1139
1150
1140
- written , err := io .CopyN (mw , rand , n )
1151
+ written , err := io .CopyN (mw , r , n )
1141
1152
if err != nil {
1142
1153
t .Fatal (err )
1143
1154
}
@@ -1733,6 +1744,7 @@ func TestGlobUNC(t *testing.T) {
1733
1744
1734
1745
// sftp/issue/42, abrupt server hangup would result in client hangs.
1735
1746
func TestServerRoughDisconnect (t * testing.T ) {
1747
+ skipIfWindows (t )
1736
1748
if * testServerImpl {
1737
1749
t .Skipf ("skipping with -testserver" )
1738
1750
}
@@ -1757,6 +1769,7 @@ func TestServerRoughDisconnect(t *testing.T) {
1757
1769
// due to broadcastErr filling up the request channel
1758
1770
// this reproduces it about 50% of the time
1759
1771
func TestServerRoughDisconnect2 (t * testing.T ) {
1772
+ skipIfWindows (t )
1760
1773
if * testServerImpl {
1761
1774
t .Skipf ("skipping with -testserver" )
1762
1775
}
@@ -1784,6 +1797,7 @@ func TestServerRoughDisconnect2(t *testing.T) {
1784
1797
1785
1798
// sftp/issue/234 - abrupt shutdown during ReadFrom hangs client
1786
1799
func TestServerRoughDisconnect3 (t * testing.T ) {
1800
+ skipIfWindows (t )
1787
1801
if * testServerImpl {
1788
1802
t .Skipf ("skipping with -testserver" )
1789
1803
}
@@ -1811,6 +1825,7 @@ func TestServerRoughDisconnect3(t *testing.T) {
1811
1825
1812
1826
// sftp/issue/234 - also affected Write
1813
1827
func TestServerRoughDisconnect4 (t * testing.T ) {
1828
+ skipIfWindows (t )
1814
1829
if * testServerImpl {
1815
1830
t .Skipf ("skipping with -testserver" )
1816
1831
}
@@ -1846,6 +1861,7 @@ func TestServerRoughDisconnect4(t *testing.T) {
1846
1861
1847
1862
// sftp/issue/26 writing to a read only file caused client to loop.
1848
1863
func TestClientWriteToROFile (t * testing.T ) {
1864
+ skipIfWindows (t )
1849
1865
sftp , cmd := testClient (t , READWRITE , NO_DELAY )
1850
1866
defer cmd .Wait ()
1851
1867
defer sftp .Close ()
@@ -1862,6 +1878,7 @@ func TestClientWriteToROFile(t *testing.T) {
1862
1878
}
1863
1879
1864
1880
func benchmarkRead (b * testing.B , bufsize int , delay time.Duration ) {
1881
+ skipIfWindows (b )
1865
1882
size := 10 * 1024 * 1024 + 123 // ~10MiB
1866
1883
1867
1884
// open sftp client
@@ -2118,6 +2135,7 @@ func BenchmarkReadFrom4MiBDelay150Msec(b *testing.B) {
2118
2135
}
2119
2136
2120
2137
func benchmarkCopyDown (b * testing.B , fileSize int64 , delay time.Duration ) {
2138
+ skipIfWindows (b )
2121
2139
// Create a temp file and fill it with zero's.
2122
2140
src , err := ioutil .TempFile ("" , "sftptest" )
2123
2141
if err != nil {
@@ -2191,6 +2209,7 @@ func BenchmarkCopyDown10MiBDelay150Msec(b *testing.B) {
2191
2209
}
2192
2210
2193
2211
func benchmarkCopyUp (b * testing.B , fileSize int64 , delay time.Duration ) {
2212
+ skipIfWindows (b )
2194
2213
// Create a temp file and fill it with zero's.
2195
2214
src , err := ioutil .TempFile ("" , "sftptest" )
2196
2215
if err != nil {
0 commit comments