-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbadge_test.go
105 lines (89 loc) · 1.86 KB
/
badge_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
package badge
import (
"bytes"
"testing"
)
func TestNew(t *testing.T) {
type user struct {
username []byte
id uint32
good bool
}
newTests := []user{
user{
[]byte("joe"),
uint32(0),
true,
},
user{
[]byte("....................................................................................................................................................................................................................................................................................................."),
uint32(0),
false,
},
user{
[]byte(""),
uint32(0),
false,
},
user{
nil,
uint32(0),
false,
},
}
for _, v := range newTests {
if b := New(v.username, v.id, []byte("key")); b == nil && v.good {
t.Fatal("unexpected error")
}
}
}
func TestGet(t *testing.T) {
type user struct {
username []byte
id uint32
key []byte
auth bool
}
goodKey := []byte("goodKey")
badKey := []byte("badKey")
getTests := []user{
user{
username: []byte("joe"),
id: uint32(0),
key: goodKey,
auth: true,
},
user{
username: []byte("joe"),
id: uint32(0),
key: badKey,
auth: false,
},
}
scratchBadge := make([]byte, 0)
for _, v := range getTests {
b := New(v.username, v.id, v.key)
username, id, auth := Get(b, goodKey)
if v.auth != auth {
t.Fatal("unexpected auth")
}
if auth && !bytes.Equal(username, v.username) {
t.Fatal("different username when decoded")
}
if auth && id != v.id {
t.Fatal("different id when decoded")
}
if auth {
scratchBadge = b
}
}
_, _, auth := Get(scratchBadge[:20], goodKey)
if auth {
t.Fatal("badge too short, shouldn't auth")
}
scratchBadge = append(scratchBadge, byte(0x00))
_, _, auth = Get(scratchBadge, goodKey)
if auth {
t.Fatal("badge too long, shouldn't auth")
}
}