forked from h2oai/wave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keychain_test.go
104 lines (90 loc) · 2.12 KB
/
keychain_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
// Copyright 2020 H2O.ai, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package wave
import (
"bytes"
"testing"
)
func TestGenerateRandString(t *testing.T) {
eq, _, no := Assert(t)
s, err := generateRandString(idChars, 20)
no(err)
eq(len(s), 20)
}
func TestCreateAccessKey(t *testing.T) {
eq, ok, no := Assert(t)
id, secret, hash, err := CreateAccessKey()
no(err)
eq(len(id), 20)
eq(len(secret), 40)
ok(len(hash) > 0)
}
func TestKeychainSerialization(t *testing.T) {
eq, _, no := Assert(t)
kc1, err := LoadKeychain(".wave-keychain")
no(err)
for i := 0; i < 5; i++ {
id, _, hash, err := CreateAccessKey()
no(err)
kc1.Add(id, hash)
}
err = kc1.Save()
no(err)
kc2, err := LoadKeychain(".wave-keychain")
no(err)
eq(len(kc1.keys), len(kc2.keys))
for k, v1 := range kc1.keys {
v2 := kc2.keys[k]
eq(bytes.Compare(v1, v2), 0)
}
}
func TestKeychainVerify(t *testing.T) {
_, ok, no := Assert(t)
kc, err := LoadKeychain(".wave-keychain")
no(err)
id, secret, hash, err := CreateAccessKey()
no(err)
kc.Add(id, hash)
ok(kc.verify(id, secret))
}
func TestKeychainManagement(t *testing.T) {
eq, ok, no := Assert(t)
// drain
kc, err := LoadKeychain(".wave-keychain")
no(err)
for _, id := range kc.IDs() { // clear
kc.Remove(id)
}
err = kc.Save()
no(err)
// load empty
kc, err = LoadKeychain(".wave-keychain")
no(err)
eq(0, kc.Len())
// fill
for i := 0; i < 5; i++ {
id, _, hash, err := CreateAccessKey()
no(err)
kc.Add(id, hash)
}
eq(5, kc.Len())
// ids must match
ids := kc.IDs()
eq(5, len(ids))
for _, id := range ids {
ok(kc.Remove(id))
}
// should be empty now
eq(0, kc.Len())
}