@@ -2,7 +2,7 @@ package tests
2
2
3
3
import (
4
4
"context"
5
- "io/ioutil "
5
+ "fmt "
6
6
"os"
7
7
"strings"
8
8
"testing"
@@ -13,16 +13,47 @@ import (
13
13
"golang.org/x/crypto/ssh"
14
14
)
15
15
16
+ // password | private key | private key with passphrase | ssh agent
17
+ func buildClientConfig () (ssh.ClientConfig , error ) {
18
+ method := os .Getenv ("METHOD" )
19
+ if method == "" {
20
+ method = "password"
21
+ }
22
+
23
+ var clientConfig ssh.ClientConfig
24
+ switch method {
25
+ case "password" :
26
+ // Use SSH key authentication from the auth package.
27
+ // During testing we ignore the host key, don't do that when you use this.
28
+ config , _ := auth .PasswordKey ("bram" , "test" , ssh .InsecureIgnoreHostKey ())
29
+ return config , nil
30
+ case "private_key" :
31
+ config , _ := auth .PrivateKey ("bram" , "./tmp/id_rsa" , ssh .InsecureIgnoreHostKey ())
32
+ return config , nil
33
+ case "private_key_with_passphrase" :
34
+ config , _ := auth .PrivateKeyWithPassphrase (
35
+ "bram" , []byte ("passphrase" ), "./tmp/id_rsa" , ssh .InsecureIgnoreHostKey (),
36
+ )
37
+ return config , nil
38
+ case "ssh_agent" :
39
+ config , _ := auth .SshAgent ("bram" , ssh .InsecureIgnoreHostKey ())
40
+ return config , nil
41
+ }
42
+ return clientConfig , fmt .Errorf ("Unknown method: %s" , method )
43
+ }
44
+
16
45
func establishConnection (t * testing.T ) scp.Client {
17
- // Use SSH key authentication from the auth package.
18
- // During testing we ignore the host key, don't to that when you use this.
19
- clientConfig , _ := auth .PasswordKey ("bram" , "test" , ssh .InsecureIgnoreHostKey ())
46
+ // Build the client configuration.
47
+ clientConfig , err := buildClientConfig ()
48
+ if err != nil {
49
+ t .Fatalf ("Couldn't build the client configuration: %s" , err )
50
+ }
20
51
21
52
// Create a new SCP client.
22
53
client := scp .NewClient ("127.0.0.1:2244" , & clientConfig )
23
54
24
55
// Connect to the remote server.
25
- err : = client .Connect ()
56
+ err = client .Connect ()
26
57
if err != nil {
27
58
t .Fatalf ("Couldn't establish a connection to the remote server: %s" , err )
28
59
}
@@ -56,7 +87,7 @@ func TestCopy(t *testing.T) {
56
87
}
57
88
58
89
// Read what the receiver have written to disk.
59
- content , err := ioutil .ReadFile ("./tmp/" + filename )
90
+ content , err := os .ReadFile ("./tmp/" + filename )
60
91
if err != nil {
61
92
t .Errorf ("Result file could not be read: %s" , err )
62
93
}
@@ -118,19 +149,19 @@ func TestMultipleUploadsAndDownloads(t *testing.T) {
118
149
}
119
150
120
151
// Read what the receiver have written to disk.
121
- content , err := ioutil .ReadFile ("./tmp/" + remoteFilename1 )
152
+ content , err := os .ReadFile ("./tmp/" + remoteFilename1 )
122
153
if err != nil {
123
154
t .Errorf ("Result file could not be read: %s" , err )
124
155
}
125
156
126
157
// Read what the receiver have written to disk.
127
- content2 , err := ioutil .ReadFile ("./tmp/" + remoteFilename2 )
158
+ content2 , err := os .ReadFile ("./tmp/" + remoteFilename2 )
128
159
if err != nil {
129
160
t .Errorf ("Result file could not be read: %s" , err )
130
161
}
131
162
132
- download_result_1 , _ := ioutil .ReadFile ("./tmp/download_result_1" )
133
- download_result_2 , _ := ioutil .ReadFile ("./tmp/download_result_2" )
163
+ download_result_1 , _ := os .ReadFile ("./tmp/download_result_1" )
164
+ download_result_2 , _ := os .ReadFile ("./tmp/download_result_2" )
134
165
135
166
text1 := string (content )
136
167
expected := "It Works\n "
@@ -194,7 +225,7 @@ func TestDownloadFile(t *testing.T) {
194
225
t .Errorf ("Copy failed from remote: %s" , err .Error ())
195
226
}
196
227
197
- content , err := ioutil .ReadFile ("./tmp/output.txt" )
228
+ content , err := os .ReadFile ("./tmp/output.txt" )
198
229
if err != nil {
199
230
t .Errorf ("Result file could not be read: %s" , err )
200
231
}
0 commit comments