forked from charmbracelet/wish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_test.go
83 lines (71 loc) · 2.28 KB
/
option_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
package wish
import (
"strings"
"testing"
"time"
"github.com/gliderlabs/ssh"
)
func TestWithIdleTimeout(t *testing.T) {
s := ssh.Server{}
requireNoError(t, WithIdleTimeout(time.Second)(&s))
requireEqual(t, time.Second, s.IdleTimeout)
}
func TestWithMaxTimeout(t *testing.T) {
s := ssh.Server{}
requireNoError(t, WithMaxTimeout(time.Second)(&s))
requireEqual(t, time.Second, s.MaxTimeout)
}
func TestParseAuthorizedKeys(t *testing.T) {
t.Run("valid", func(t *testing.T) {
keys, err := parseAuthorizedKeys("testdata/authorized_keys")
requireNoError(t, err)
requireEqual(t, 6, len(keys))
})
t.Run("invalid", func(t *testing.T) {
keys, err := parseAuthorizedKeys("testdata/invalid_authorized_keys")
requireEqual(t, `failed to parse "testdata/invalid_authorized_keys": ssh: no key found`, err.Error())
requireEqual(t, 0, len(keys))
})
t.Run("file not found", func(t *testing.T) {
keys, err := parseAuthorizedKeys("testdata/nope_authorized_keys")
requireEqual(t, `failed to parse "testdata/nope_authorized_keys": open testdata/nope_authorized_keys: no such file or directory`, err.Error())
requireEqual(t, 0, len(keys))
})
}
func TestWithAuthorizedKeys(t *testing.T) {
t.Run("valid", func(t *testing.T) {
s := ssh.Server{}
requireNoError(t, WithAuthorizedKeys("testdata/authorized_keys")(&s))
for key, authorize := range map[string]bool{
`ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMJlb/qf2B2kMNdBxfpCQqI2ctPcsOkdZGVh5zTRhKtH k3@test`: true,
`ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOhsthN+zSFSJF7V2HFSO4+2OJYRghuAA43CIbVyvzF8 k7@test`: false,
} {
parts := strings.Fields(key)
t.Run(parts[len(parts)-1], func(t *testing.T) {
key, _, _, _, err := ssh.ParseAuthorizedKey([]byte(key))
requireNoError(t, err)
requireEqual(t, authorize, s.PublicKeyHandler(nil, key))
})
}
})
t.Run("invalid", func(t *testing.T) {
s := ssh.Server{}
requireEqual(
t,
`failed to parse "testdata/invalid_authorized_keys": ssh: no key found`,
WithAuthorizedKeys("testdata/invalid_authorized_keys")(&s).Error(),
)
})
}
func requireEqual(tb testing.TB, a, b interface{}) {
tb.Helper()
if a != b {
tb.Errorf("expected %v, got %v", a, b)
}
}
func requireNoError(tb testing.TB, err error) {
tb.Helper()
if err != nil {
tb.Errorf("expected no error, got %v", err)
}
}