-
Notifications
You must be signed in to change notification settings - Fork 21
/
connection_test.go
107 lines (97 loc) · 2.14 KB
/
connection_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package ts3
import (
"bufio"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/ssh"
)
var sshClientTestConfig = &ssh.ClientConfig{
HostKeyCallback: ssh.InsecureIgnoreHostKey(), //nolint: gosec
}
func TestConnection(t *testing.T) {
testCases := map[string]struct {
conn Connection
newServer func(*testing.T) *server
}{
"legacyConnection": {
conn: new(legacyConnection),
newServer: func(t *testing.T) *server {
t.Helper()
return newServer(t)
},
},
"sshConnection": {
conn: &sshConnection{config: sshClientTestConfig},
newServer: func(t *testing.T) *server {
t.Helper()
return newServer(t, useSSH())
},
},
}
for description, tc := range testCases {
t.Run(description, func(t *testing.T) {
s := tc.newServer(t)
require.NotNil(t, s)
assert.NoError(t, tc.conn.Connect(s.Addr, time.Second))
line, _, err := bufio.NewReader(tc.conn).ReadLine()
assert.NoError(t, err)
assert.Equal(t, "TS3", string(line))
assert.NoError(t, s.Close())
assert.NoError(t, tc.conn.Close())
})
}
}
func TestVerifyAddr(t *testing.T) {
testCases := map[string]struct {
addr string
expected string
}{
"hostname without port": {
addr: "localhost",
expected: "localhost:1234",
},
"hostname with port": {
addr: "localhost:1337",
expected: "localhost:1337",
},
"IPv4 without port": {
addr: "127.0.0.1",
expected: "127.0.0.1:1234",
},
"IPv4 with port": {
addr: "127.0.0.1:1337",
expected: "127.0.0.1:1337",
},
"IPv6 without port": {
addr: "[::1]",
expected: "[::1]:1234",
},
"IPv6 with port": {
addr: "[::1]:1337",
expected: "[::1]:1337",
},
"IPv6 without brackets": {
addr: "::1",
},
"empty addr": {
addr: "",
expected: ":1234",
},
"invalid addr": {
addr: "invalid:ddd:",
},
}
for description, tc := range testCases {
t.Run(description, func(t *testing.T) {
addr, err := verifyAddr(tc.addr, 1234)
if tc.expected == "" {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tc.expected, addr)
})
}
}