This repository has been archived by the owner on Jan 22, 2024. It is now read-only.
forked from iromli/go-itsdangerous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignature_test.go
115 lines (99 loc) · 2.71 KB
/
signature_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
package itsdangerous
import (
"bytes"
"encoding/json"
"os"
"testing"
)
func assert(t *testing.T, actual, expected []byte) {
if !bytes.Equal(actual, expected) {
t.Errorf("expecting %s, got %s instead", expected, actual)
}
}
func TestSignatureSign(t *testing.T) {
s := NewSignature("secret-key", "", "", "", nil, nil)
expected := []byte("my string.wh6tMHxLgJqB6oY1uT73iMlyrOA")
actual, _ := s.Sign([]byte("my string"))
assert(t, actual, expected)
}
func TestSignatureUnsign(t *testing.T) {
s := NewSignature("secret-key", "", "", "", nil, nil)
expected := []byte("my string")
actual, _ := s.Unsign([]byte("my string.wh6tMHxLgJqB6oY1uT73iMlyrOA"))
assert(t, actual, expected)
}
func TestTimestampSignatureUnsign(t *testing.T) {
t.Skip()
s := NewTimestampSignature("secret-key", "", "", "", nil, nil)
expected := []byte("my string")
actual, _ := s.Unsign([]byte("my string.BpSAPw.NnKk1nQ206g1c1aJAS1Nxkt4aug"), 0)
assert(t, actual, expected)
}
type testCase struct {
Before string
After string
IsTimed bool
}
func testSig(t *testing.T, c testCase) {
s := NewSignature("super secret 1", "cookie-session", ".", "hmac", nil, nil)
before, err := s.UnsignB64([]byte(c.After))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(before, []byte(c.Before)) {
t.Fatalf("before did not match: expected: \n%s \ngot: \n%s", c.Before, before)
}
// zlib output differs between go / python
// so skip cases where After is compressed
if c.After[0] != '.' {
after, err := s.SignB64([]byte(c.Before))
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(after, []byte(c.After)) {
t.Fatalf("after did not match: expected: \n%s \ngot: \n%s", c.After, after)
}
}
}
func testTimedSig(t *testing.T, c testCase) {
s := NewTimestampSignature("super secret 1", "cookie-session", ".", "hmac", nil, nil)
before, err := s.UnsignB64([]byte(c.After), 0)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(before, []byte(c.Before)) {
t.Fatalf("before did not match: expected: \n%s \ngot: \n%s", c.Before, before)
}
// zlib output differs between go / python
// so skip cases where After is compressed
if c.After[0] != '.' {
after, err := s.SignB64([]byte(c.Before))
if err != nil {
t.Fatal(err)
}
a1 := bytes.Split(after, []byte{'.'})[0]
exp1 := bytes.Split([]byte(c.After), []byte{'.'})[0]
if !bytes.Equal(a1, exp1) {
t.Fatalf("after did not match: expected: \n%s \ngot: \n%s", exp1, a1)
}
}
}
func TestPythonOutput(t *testing.T) {
cs := []testCase{}
f, err := os.Open("testdata/testdata.json")
if err != nil {
t.Fatal(err)
}
dec := json.NewDecoder(f)
err = dec.Decode(&cs)
if err != nil {
t.Fatal(err)
}
for _, c := range cs {
if c.IsTimed {
testTimedSig(t, c)
} else {
testSig(t, c)
}
}
}