-
Notifications
You must be signed in to change notification settings - Fork 27
/
server_test.go
164 lines (137 loc) · 3.66 KB
/
server_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package wishlist
import (
"context"
"io"
"net"
"strconv"
"sync"
"testing"
"github.com/charmbracelet/ssh"
"github.com/stretchr/testify/require"
)
func TestToAddress(t *testing.T) {
require.Equal(t, "test:1234", toAddress("test", 1234))
}
func TestCloseAll(t *testing.T) {
t.Run("no err", func(t *testing.T) {
require.NoError(t, closeAll([]func() error{
func() error { return nil },
func() error { return nil },
}))
})
t.Run("error", func(t *testing.T) {
require.ErrorAs(t, closeAll([]func() error{
func() error { return nil },
func() error { return io.ErrClosedPipe },
func() error { return nil },
}), &io.ErrClosedPipe)
})
t.Run("multierror", func(t *testing.T) {
err := closeAll([]func() error{
func() error { return io.ErrClosedPipe },
func() error { return io.EOF },
})
require.ErrorAs(t, err, &io.ErrClosedPipe)
require.ErrorAs(t, err, &io.EOF)
})
}
func TestGetFirstOpenPort(t *testing.T) {
l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
t.Cleanup(func() { _ = l.Close() })
open := portFromAddr(t, l.Addr().String())
require.NoError(t, l.Close())
l2, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, l2.Close()) })
closed := portFromAddr(t, l2.Addr().String())
t.Log("open:", open)
t.Log("closed:", closed)
t.Run("gets port", func(t *testing.T) {
port, err := getFirstOpenPort("127.0.0.1", closed, open)
require.NoError(t, err)
require.Equal(t, open, port)
})
t.Run("only used ports", func(t *testing.T) {
port, err := getFirstOpenPort("127.0.0.1", closed)
require.Error(t, err)
require.Equal(t, int64(0), port)
})
}
func TestPublicKeyHandler(t *testing.T) {
t.Run("no users", func(t *testing.T) {
require.Nil(t, publicKeyAccessOption([]User{}))
})
t.Run("with users", func(t *testing.T) {
pubkey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMYKQ6pT3+iZBROfFKKT/4GVc1Xws776bE67cF3zUQPS foo@bar"
pubkey2 := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDfBMpbghW82c1zk9LauP7G/LqXtTeQrU6Do9FUY1FJ5 foo@bar"
k, _, _, _, err := ssh.ParseAuthorizedKey([]byte(pubkey))
require.NoError(t, err)
t.Run("authorized", func(t *testing.T) {
require.True(t, publicKeyAccessOption([]User{
{
Name: "test",
PublicKeys: []string{pubkey},
},
})(fakeCtx{}, k))
})
t.Run("unauthorized wrong username", func(t *testing.T) {
require.False(t, publicKeyAccessOption([]User{
{
Name: "not-test",
PublicKeys: []string{pubkey},
},
})(fakeCtx{}, k))
})
t.Run("unauthorized wrong key", func(t *testing.T) {
require.False(t, publicKeyAccessOption([]User{
{
Name: "test",
PublicKeys: []string{pubkey2},
},
})(fakeCtx{}, k))
})
t.Run("invalid key", func(t *testing.T) {
require.False(t, publicKeyAccessOption([]User{
{
Name: "test",
PublicKeys: []string{"giberrish"},
},
})(fakeCtx{}, k))
})
})
}
type fakeCtx struct {
context.Context
sync.Locker
}
func (ctx fakeCtx) User() string {
return "test"
}
func (ctx fakeCtx) SessionID() string {
return "test"
}
func (ctx fakeCtx) ClientVersion() string {
return "1.0"
}
func (ctx fakeCtx) ServerVersion() string {
return "1.0"
}
func (ctx fakeCtx) RemoteAddr() net.Addr {
return &net.IPAddr{}
}
func (ctx fakeCtx) LocalAddr() net.Addr {
return &net.IPAddr{}
}
func (ctx fakeCtx) Permissions() *ssh.Permissions {
return nil
}
func (ctx fakeCtx) SetValue(key, value interface{}) {}
func portFromAddr(tb testing.TB, addr string) int64 {
tb.Helper()
_, port, err := net.SplitHostPort(addr)
require.NoError(tb, err)
result, err := strconv.ParseInt(port, 10, 64)
require.NoError(tb, err)
return result
}