-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtokenizer_test.go
123 lines (102 loc) · 3.04 KB
/
tokenizer_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"bytes"
"errors"
"testing"
)
var (
// We're using 4-byte input because CryptoPAn only allows IP addresses
// as input.
value1 = blob([]byte{1, 2, 3, 4})
value2 = blob([]byte{5, 6, 7, 8})
)
func TestTokenize(t *testing.T) {
// Run the same tests over all our tokenizers. This works as long as
// there's a data format that they all accept.
for name, newTokenizer := range ourTokenizers {
tkzr := newTokenizer()
if _, err := tkzr.tokenize(value1); !errors.Is(err, errNoKey) {
t.Fatalf("%s: Expected error '%v' but got '%v'.", name, errNoKey, err)
}
_ = tkzr.resetKey()
t1, err := tkzr.tokenize(value1)
if err != nil {
t.Fatalf("%s: Tokenize failed unexpectedly: %v", name, err)
}
t2, err := tkzr.tokenize(value1)
if err != nil {
t.Fatalf("%s: Tokenize failed unexpectedly: %v", name, err)
}
if !bytes.Equal(t1, t2) {
t.Fatalf("%s: Tokenized values are not identical but they should be.", name)
}
t3, err := tkzr.tokenize(value2)
if err != nil {
t.Fatalf("%s: Tokenize failed unexpectedly: %v", name, err)
}
if bytes.Equal(t1, t3) {
t.Fatalf("%s: Tokenized values are identical but they shouldn't be.", name)
}
}
}
func TestTokenizeAndKeyID(t *testing.T) {
for name, newTokenizer := range ourTokenizers {
tkzr := newTokenizer()
_, _, err := tkzr.tokenizeAndKeyID(value1)
if !errors.Is(err, errNoKey) {
t.Fatalf("%s: Expected error '%v' but got '%v'.", name, errNoKey, err)
}
_ = tkzr.resetKey()
token1, keyID1, err := tkzr.tokenizeAndKeyID(value1)
if err != nil {
t.Fatalf("%s: Unexpected error: %v", name, err)
}
token2, keyID2, err := tkzr.tokenizeAndKeyID(value1)
if err != nil {
t.Fatalf("%s: Unexpected error: %v", name, err)
}
if !bytes.Equal(token1, token2) {
t.Fatalf("%s: Expected tokens to be identical but they aren't.", name)
}
if *keyID1 != *keyID2 {
t.Fatalf("%s: Expected key IDs to be identical but they aren't.", name)
}
}
}
func TestKeyID(t *testing.T) {
for name, newTokenizer := range ourTokenizers {
tkzr := newTokenizer()
if err := tkzr.resetKey(); err != nil {
t.Fatalf("%s: Failed to reset keys: %v", name, err)
}
k1 := *tkzr.keyID()
k2 := *tkzr.keyID()
if k1 != k2 {
t.Fatalf("%s: Expected key IDs to be equal but they aren't.", name)
}
_ = tkzr.resetKey()
k3 := *tkzr.keyID()
if k1 == k3 {
t.Fatalf("%s: Expected different key IDs but they are identical.", name)
}
}
}
func TestResetKeys(t *testing.T) {
var err error
for name, newTokenizer := range ourTokenizers {
tkzr := newTokenizer()
_ = tkzr.resetKey()
if _, err = tkzr.tokenize(value1); err != nil {
t.Fatalf("%s: Failed to tokenize: %v", name, err)
}
if err = tkzr.resetKey(); err != nil {
t.Fatalf("%s: Failed to reset keys: %v", name, err)
}
if _, err = tkzr.tokenize(value1); err != nil {
t.Fatalf("%s: Failed to tokenize: %v", name, err)
}
// We're not testing if a key reset causes two identical blobs to map
// to two different tokens because our verbatim tokenizer is
// implemented as f(x) = x.
}
}