-
Notifications
You must be signed in to change notification settings - Fork 3
/
tokengen_test.go
130 lines (118 loc) · 2.77 KB
/
tokengen_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
package tokengen
import (
"errors"
"fmt"
"os"
"testing"
)
type dummyReader struct{}
func (dr dummyReader) Read(_ []byte) (int, error) {
return 0, errors.New(`could not read`)
}
func TestTokengen_GenerateTokenMultipleLengths(t *testing.T) {
testCases := []int{
32,
4096,
64,
12,
1,
}
for _, expected := range testCases {
t.Run(fmt.Sprintf(`Length: %v`, expected), func(t *testing.T) {
tg, err := New(DefaultCharset, expected)
if err != nil {
t.Fatal(err)
}
token, err := tg.GenerateToken()
if err != nil {
t.Fatal(err)
}
if actual := len(token); actual != expected {
t.Fatalf("Expected length of %v got %v for tokengen of %v", expected, actual, tg)
}
})
}
}
func TestTokengen_OccurenceWithinTwoPercentOfAverage(t *testing.T) {
if testing.Short() {
t.Skip(`this test should be ran if you've done a major refactor.`)
}
file, err := os.Open(`random.data`)
defer file.Close()
if err != nil {
t.Fatal(err)
}
tokengen := Tokengen{
distributor: newRuneDistributor([]rune(DefaultCharset), 1<<12, file),
}
scores := genEmptyMap(DefaultCharset)
for i := 0; i < (1 << 9); i++ {
token, err := tokengen.GenerateToken()
if err != nil {
t.Log(err)
t.FailNow()
}
scores = total(token, scores)
}
total := 0
for _, score := range scores {
total += score
}
avg := total / len(DefaultCharset)
twoPercent := avg / 50
distribution := map[string]int{}
for char, val := range scores {
dev := val - avg
if dev < 0 {
dev = -dev
}
distribution[char] = dev
if dev > twoPercent {
t.Fatalf(`there is a greater than two percent deviaton from average of the character '%s' with a deviation of %v compared to a two percent being %v`, char, dev, twoPercent)
}
}
}
func TestTokengen_InvalidLength(t *testing.T) {
_, err := New(DefaultCharset, -13)
if err == nil {
t.Fatal(`no error returned for error case`)
}
}
func TestTokengen_InvalidCharset(t *testing.T) {
_, err := New(``, 40)
if err == nil {
t.Fatal(`no error returned for error case`)
}
}
func TestTokengen_GenerateToken(t *testing.T) {
tokengen, err := New(`0123456789abcdef`, 64)
if err != nil {
t.Fatal(err)
}
if _, err := tokengen.GenerateToken(); err != nil {
t.Log(err)
t.FailNow()
}
}
func TestTokengen_GenerateTokenErroringRandSource(t *testing.T) {
tokengen := Tokengen{
distributor: newRuneDistributor([]rune(DefaultCharset), 1<<12, dummyReader{}),
}
_, err := tokengen.GenerateToken()
if err == nil {
t.Fatal(`no error for invalid randsource`)
}
}
func genEmptyMap(charset string) map[string]int {
output := map[string]int{}
for _, char := range charset {
output[string(char)] = 0
}
return output
}
func total(token string, scores map[string]int) map[string]int {
for _, char := range token {
scores[string(char)]++
}
return scores
}