-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_key.go
76 lines (69 loc) · 1.49 KB
/
test_key.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
// Copyright 2021 The June Authors. All rights reserved.
// Use of this source code is governed by Apache License
// that can be found in the LICENSE file.
package key
import (
"bytes"
"encoding/json"
"math/rand"
"testing"
"github.com/reusee/e5"
)
func TestKeyJSON(t *testing.T) {
defer he(nil, e5.TestingFatal(t))
j, err := json.Marshal(Key{
Namespace: Namespace{'a', 'b', 'c'},
Hash: Hash{1, 2, 3},
})
ce(err)
if !bytes.Equal(
j,
[]byte(`"abc:0102030000000000000000000000000000000000000000000000000000000000"`),
) {
t.Fatal()
}
}
func TestNamespaceFromString(t *testing.T) {
defer he(nil, e5.TestingFatal(t))
for i := 0; i < 128; i++ {
var ns Namespace
rand.Read(ns[:rand.Intn(len(ns))+1])
str := ns.String()
ns2, err := NamespaceFromString(str)
ce(err)
if ns != ns2 {
t.Fatal()
}
}
}
func TestHashFromString(t *testing.T) {
defer he(nil, e5.TestingFatal(t))
for i := 0; i < 128; i++ {
var hash Hash
rand.Read(hash[:rand.Intn(len(hash))+1])
str := hash.String()
hash2, err := HashFromString(str)
ce(err)
if hash2 != hash {
t.Fatal()
}
}
}
func TestKeyFromString(t *testing.T) {
defer he(nil, e5.TestingFatal(t))
for i := 0; i < 128; i++ {
var key Key
gen:
rand.Read(key.Namespace[:rand.Intn(len(key.Namespace))+1])
if bytes.Contains(key.Namespace[:], []byte(":")) {
goto gen
}
rand.Read(key.Hash[:rand.Intn(len(key.Hash))+1])
str := key.String()
key2, err := KeyFromString(str)
ce(err)
if key2 != key {
t.Fatal()
}
}
}