Skip to content

Commit fb98cf8

Browse files
committed
Add test for other authentication methods
1 parent 0350bf9 commit fb98cf8

File tree

2 files changed

+103
-31
lines changed

2 files changed

+103
-31
lines changed

tests/basic_test.go

+42-11
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package tests
22

33
import (
44
"context"
5-
"io/ioutil"
5+
"fmt"
66
"os"
77
"strings"
88
"testing"
@@ -13,16 +13,47 @@ import (
1313
"golang.org/x/crypto/ssh"
1414
)
1515

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+
1645
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+
}
2051

2152
// Create a new SCP client.
2253
client := scp.NewClient("127.0.0.1:2244", &clientConfig)
2354

2455
// Connect to the remote server.
25-
err := client.Connect()
56+
err = client.Connect()
2657
if err != nil {
2758
t.Fatalf("Couldn't establish a connection to the remote server: %s", err)
2859
}
@@ -56,7 +87,7 @@ func TestCopy(t *testing.T) {
5687
}
5788

5889
// Read what the receiver have written to disk.
59-
content, err := ioutil.ReadFile("./tmp/" + filename)
90+
content, err := os.ReadFile("./tmp/" + filename)
6091
if err != nil {
6192
t.Errorf("Result file could not be read: %s", err)
6293
}
@@ -118,19 +149,19 @@ func TestMultipleUploadsAndDownloads(t *testing.T) {
118149
}
119150

120151
// Read what the receiver have written to disk.
121-
content, err := ioutil.ReadFile("./tmp/" + remoteFilename1)
152+
content, err := os.ReadFile("./tmp/" + remoteFilename1)
122153
if err != nil {
123154
t.Errorf("Result file could not be read: %s", err)
124155
}
125156

126157
// Read what the receiver have written to disk.
127-
content2, err := ioutil.ReadFile("./tmp/" + remoteFilename2)
158+
content2, err := os.ReadFile("./tmp/" + remoteFilename2)
128159
if err != nil {
129160
t.Errorf("Result file could not be read: %s", err)
130161
}
131162

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")
134165

135166
text1 := string(content)
136167
expected := "It Works\n"
@@ -194,7 +225,7 @@ func TestDownloadFile(t *testing.T) {
194225
t.Errorf("Copy failed from remote: %s", err.Error())
195226
}
196227

197-
content, err := ioutil.ReadFile("./tmp/output.txt")
228+
content, err := os.ReadFile("./tmp/output.txt")
198229
if err != nil {
199230
t.Errorf("Result file could not be read: %s", err)
200231
}

tests/run_all.sh

+61-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,70 @@
11
#!/usr/bin/env bash
22

3-
rm tmp/*
3+
cleanup() {
4+
local auth_method=$1
45

5-
echo "Running from $(pwd)"
6+
echo "Tearing down docker containers"
7+
docker stop go-scp-test
8+
docker rm go-scp-test
69

7-
echo "Starting docker containers"
10+
echo "Cleaning up"
11+
if [[ "$auth_method" == "ssh_agent" ]]; then
12+
ssh-add -d ./tmp/id_rsa
13+
fi
14+
rm tmp/*
15+
}
816

9-
docker run -d \
10-
--name go-scp-test \
11-
-p 2244:22 \
12-
-e SSH_USERS=bram:1000:1000 \
13-
-e SSH_ENABLE_PASSWORD_AUTH=true \
14-
-v $(pwd)/tmp:/data/ \
15-
-v $(pwd)/data:/input \
16-
-v $(pwd)/entrypoint.d/:/etc/entrypoint.d/ \
17-
panubo/sshd
17+
run_test() {
18+
local auth_method=$1
1819

19-
sleep 5
20+
echo "Testing with auth method: $auth_method"
2021

21-
echo "Running tests"
22-
go test -v
22+
echo "Running tests"
23+
METHOD="$auth_method" go test -v
24+
}
2325

24-
echo "Tearing down docker containers"
25-
docker stop go-scp-test
26-
docker rm go-scp-test
26+
run_docker_container() {
27+
local enable_password_auth=$1
2728

28-
echo "Cleaning up"
29-
rm tmp/*
29+
docker run -d \
30+
--name go-scp-test \
31+
-p 2244:22 \
32+
-e SSH_USERS=bram:1000:1000 \
33+
-e SSH_ENABLE_PASSWORD_AUTH=$enable_password_auth \
34+
-v $(pwd)/tmp:/data/ \
35+
-v $(pwd)/data:/input \
36+
-v $(pwd)/entrypoint.d/:/etc/entrypoint.d/ \
37+
${extra_mount:-} \
38+
panubo/sshd
39+
}
40+
41+
for auth_method in "password" "private_key" "private_key_with_passphrase" "ssh_agent"; do
42+
case "$auth_method" in
43+
"password")
44+
echo "Testing with password auth"
45+
run_docker_container true
46+
sleep 5
47+
run_test "$auth_method"
48+
cleanup
49+
;;
50+
"private_key" | "private_key_with_passphrase" | "ssh_agent")
51+
echo "Testing with $auth_method auth"
52+
ssh-keygen -t rsa -f ./tmp/id_rsa -N ""
53+
if [[ "$auth_method" == "private_key_with_passphrase" ]]; then
54+
ssh-keygen -p -f ./tmp/id_rsa -P "" -N "passphrase"
55+
fi
56+
if [[ "$auth_method" == "ssh_agent" ]]; then
57+
ssh-add ./tmp/id_rsa
58+
fi
59+
extra_mount="-v $(pwd)/tmp/id_rsa.pub:/etc/authorized_keys/bram:ro"
60+
run_docker_container false
61+
sleep 5
62+
run_test "$auth_method"
63+
cleanup "$auth_method"
64+
;;
65+
*)
66+
echo "Unsupported auth method $auth_method"
67+
exit 1
68+
;;
69+
esac
70+
done

0 commit comments

Comments
 (0)