forked from shazow/ssh-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
61 lines (48 loc) · 1.09 KB
/
auth_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
package sshchat
import (
"crypto/rand"
"crypto/rsa"
"testing"
"golang.org/x/crypto/ssh"
)
func NewRandomPublicKey(bits int) (ssh.PublicKey, error) {
key, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, err
}
return ssh.NewPublicKey(key.Public())
}
func ClonePublicKey(key ssh.PublicKey) (ssh.PublicKey, error) {
return ssh.ParsePublicKey(key.Marshal())
}
func TestAuthWhitelist(t *testing.T) {
key, err := NewRandomPublicKey(512)
if err != nil {
t.Fatal(err)
}
auth := NewAuth()
err = auth.Check(nil, key, "")
if err != nil {
t.Error("Failed to permit in default state:", err)
}
auth.Whitelist(key, 0)
keyClone, err := ClonePublicKey(key)
if err != nil {
t.Fatal(err)
}
if string(keyClone.Marshal()) != string(key.Marshal()) {
t.Error("Clone key does not match.")
}
err = auth.Check(nil, keyClone, "")
if err != nil {
t.Error("Failed to permit whitelisted:", err)
}
key2, err := NewRandomPublicKey(512)
if err != nil {
t.Fatal(err)
}
err = auth.Check(nil, key2, "")
if err == nil {
t.Error("Failed to restrict not whitelisted:", err)
}
}