-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhyrumtoken_test.go
89 lines (71 loc) · 1.87 KB
/
hyrumtoken_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
package hyrumtoken_test
import (
"crypto/rand"
"reflect"
"testing"
"github.com/ssoready/hyrumtoken"
)
// testkey is a randomized key for testing. Do not use it in production.
var testkey = [32]byte{24, 12, 15, 90, 143, 133, 171, 28, 34, 75, 185, 194, 102, 93, 165, 183, 235, 96, 135, 135, 165, 1, 129, 91, 32, 7, 139, 135, 130, 2, 241, 168}
func TestEncoder(t *testing.T) {
type data struct {
Foo string
Bar string
}
in := data{
Foo: "foo",
Bar: "bar",
}
encoded := hyrumtoken.Marshal(&testkey, in)
var out data
err := hyrumtoken.Unmarshal(&testkey, encoded, &out)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if !reflect.DeepEqual(in, out) {
t.Fatalf("round-trip failure")
}
}
func TestEncoder_Unmarshal_empty(t *testing.T) {
data := 123
if err := hyrumtoken.Unmarshal(&testkey, "", &data); err != nil {
t.Fatalf("unexpected error: %s", err)
}
if data != 123 {
t.Fatalf("data unexpectedly modified: %d", data)
}
}
func TestEncoder_Marshal(t *testing.T) {
// test known produced values using fixed, zero rand and secret
r := rand.Reader
rand.Reader = zeroReader{}
defer func() {
rand.Reader = r
}()
token := hyrumtoken.Marshal(&testkey, 123)
if token != "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAULRUMRVA4GIqe5Y8N_z8B4J7hw==" {
t.Fatalf("encoding regression, got: %q", token)
}
}
func TestEncoder_Unmarshal(t *testing.T) {
// inverse of TestEncoder_Marshal
r := rand.Reader
rand.Reader = zeroReader{}
defer func() {
rand.Reader = r
}()
var data int
if err := hyrumtoken.Unmarshal(&testkey, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAULRUMRVA4GIqe5Y8N_z8B4J7hw==", &data); err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if data != 123 {
t.Fatalf("unmarshal regression, got: %d", data)
}
}
type zeroReader struct{}
func (z zeroReader) Read(p []byte) (n int, err error) {
for i := 0; i < len(p); i++ {
p[i] = 0
}
return len(p), nil
}