-
Notifications
You must be signed in to change notification settings - Fork 0
/
smokering_test.go
90 lines (69 loc) · 1.85 KB
/
smokering_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
package smokering_test
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"testing"
. "github.com/IngCr3at1on/smokering"
)
func getRandomKey() ([]byte, error) {
byt := make([]byte, 32)
_, err := rand.Read(byt)
return byt, err
}
func newSmokering(tb testing.TB) (*Smokering, cipher.Block) {
ring := New()
masterkey, err := getRandomKey()
ok(tb, err)
block, err := aes.NewCipher(masterkey)
ok(tb, err)
return ring, block
}
func TestSmokerings(t *testing.T) {
ring, block := newSmokering(t)
const (
note = "a test key"
id = "test"
)
_, err := ring.Key(id, note, block, aes.BlockSize, getRandomKey)
ok(t, err)
key := ring.GetKey(id)
assert(t, key != nil, "key should not be nil")
equals(t, key.GetNote(), note)
}
func TestGobEncodeDecode(t *testing.T) {
ring, block := newSmokering(t)
const (
note = "a test key"
id = "test"
)
_, err := ring.Key(id, note, block, aes.BlockSize, getRandomKey)
ok(t, err)
byt, err := ring.GobEncode()
ok(t, err)
assert(t, byt != nil, "it should return gob encoded bytes")
ring, err = NewFromGob(byt)
ok(t, err)
assert(t, ring != nil, "it should decode the smokering from bytes")
key := ring.GetKey(id)
assert(t, key != nil, "it should return the test key")
assert(t, key.GetNote() == "a test key", "the key note should be in tact")
}
func TestAsFromJson(t *testing.T) {
ring, block := newSmokering(t)
const (
note = "a test key"
id = "test"
)
_, err := ring.Key(id, note, block, aes.BlockSize, getRandomKey)
ok(t, err)
byt, err := ring.AsJSON()
ok(t, err)
assert(t, byt != nil, "it should return json encoded bytes")
ring, err = NewFromJSON(byt)
ok(t, err)
assert(t, ring != nil, "it should decode the smokering from bytes")
key := ring.GetKey(id)
assert(t, key != nil, "it should return the test key")
assert(t, key.GetNote() == "a test key", "the key note should be in tact")
}