-
Notifications
You must be signed in to change notification settings - Fork 22
/
token_test.go
145 lines (134 loc) · 3.1 KB
/
token_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package spg
import (
"bytes"
"testing"
)
func TestTokenizer(t *testing.T) {
type tokenVec struct {
Pwd Password
expectedTI Indices
expectedPW string
}
vecs := []tokenVec{}
vecs = append(vecs, tokenVec{
Pwd: Password{
tokens: Tokens{
{"correct", AtomType},
{" ", SeparatorType},
{"horse", AtomType},
{" ", SeparatorType},
{"battery", AtomType},
{" ", SeparatorType},
{"staple", AtomType},
},
Entropy: 44.0,
},
expectedTI: Indices{
byte(AlternatingIndexKind),
7, 1, 5, 1, 7, 1, 6,
},
expectedPW: "correct horse battery staple",
})
vecs = append(vecs, tokenVec{
Pwd: Password{
tokens: Tokens{
{"correct", AtomType},
{" ", SeparatorType},
{"horse", AtomType},
{" ", SeparatorType},
{"battery", AtomType},
{" ", SeparatorType},
{"staple", AtomType},
{" ", SeparatorType},
},
Entropy: 44.0,
},
expectedTI: Indices{
byte(FullIndexKind),
7, byte(AtomType),
1, byte(SeparatorType),
5, byte(AtomType),
1, byte(SeparatorType),
7, byte(AtomType),
1, byte(SeparatorType),
6, byte(AtomType),
1, byte(SeparatorType),
},
expectedPW: "correct horse battery staple ",
})
vecs = append(vecs, tokenVec{
Pwd: Password{
tokens: Tokens{
{"P", AtomType},
{"@", AtomType},
{"s", AtomType},
{"s", AtomType},
{"w", AtomType},
{"0", AtomType},
{"r", AtomType},
{"d", AtomType},
{"1", AtomType},
},
Entropy: 14.0,
},
expectedTI: Indices{byte(CharacterIndexKind)},
expectedPW: "P@ssw0rd1",
})
vecs = append(vecs, tokenVec{
Pwd: Password{
tokens: Tokens{
{"correct", AtomType},
{"horse", AtomType},
{"battery", AtomType},
{"staple", AtomType},
},
Entropy: 44.0,
},
expectedTI: Indices{
byte(VarAtomsIndexKind),
7, 5, 7, 6,
},
expectedPW: "correcthorsebatterystaple",
})
for _, tVec := range vecs {
tP := tVec.Pwd
ts := tVec.Pwd.Tokens()
ti, err := ts.MakeIndices()
if err != nil {
t.Errorf("failed to create token indices: %v", err)
}
if bytes.Compare(ti, tVec.expectedTI) != 0 {
t.Errorf("ti != expected\n\tti: %v\n\tExpected: %v", ti, tVec.expectedTI)
}
pw := tP.String()
ent := tP.Entropy
if pw != tVec.expectedPW {
t.Errorf("pw is %q. Expected %q", pw, tVec.expectedPW)
}
newP, err := Tokenize(pw, ti, ent)
if err != nil {
t.Errorf("couldn't tokenize: %v", err)
}
if newP.String() != pw {
t.Errorf("%q should equal %q", newP.String(), pw)
}
if len(newP.tokens) != len(tP.tokens) {
t.Errorf("tokens lengths don't match:\n\tOriginal: %v\n\tReconstructed: %v",
tP.Tokens(), newP.Tokens())
} else { // only run this test if lengths are equal
nt := newP.Tokens()
for i, tok := range ts {
if tok.Value() != nt[i].Value() {
t.Errorf("%d-th tokens Values don't match: %q != %q", i, tok.Value(), nt[i].Value())
}
if tok.Type() != nt[i].Type() {
t.Errorf("%d-th tokens Types don't match: %d != %d", i, tok.Type(), nt[i].Type())
}
}
}
}
}
/**
** Copyright 2018 AgileBits, Inc.
** Licensed under the Apache License, Version 2.0 (the "License").
**/