-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathparser_test.go
171 lines (158 loc) · 5.47 KB
/
parser_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package paseto
import (
"crypto"
"encoding/hex"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/ed25519"
)
func TestParse(t *testing.T) {
symmetricKey, _ := hex.DecodeString("707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f")
cases := map[string]struct {
token string
version Version
payload interface{}
footer interface{}
}{
"v1.local": {
token: "v1.local.rElw-WywOuwAqKC9Yao3YokSp7vx0YiUB9hLTnsVOYYTojmVaYumJSQt8aggtCaFKWyaodw5k-CUWhYKATopiabAl4OAmTxHCfm2E4NSPvrmMcmi8n-JcZ93HpcxC6rx_ps22vutv7iP7wf8QcSD1Mwx.Q3VvbiBBbHBpbnVz",
version: V1,
payload: []byte("Love is stronger than hate or fear"),
footer: []byte("Cuon Alpinus"),
},
"v1.public": {
token: "v1.public.TG9yZW0gSXBzdW1684wbBiSvpwhED_5bdFnF2ithKoKDyzEyTOLUlFnz83IibTKCOw3LPOEp8xKM67EYOw1xU6OBBOdLQT-XO5mKMg51JJ4J91IBDwDazDex0D2UQphr7i8gPGP_5FyjlNincP_rToVbYOOzfk9cmnH1-iLmOxxbrsa7-v08Gx12ib-Z-KxKBXBHbxI8uvauVWUVS6A7rl0eAlb6SecSPPQpxQnD1zakA-nGFUbWq5Zx8XqgVZ-VidcGcd7kmhZ-bMy4Z1uGOWmAXHC793v8sbXuRdroZM8kmO0pqQMoE_wmlriIxflFABCa1PPWi5YB87aVF3oIWHYXawZXxRwxevgK.Zm9vdGVy",
version: V1,
payload: []byte("Lorem Ipsum"),
footer: []byte("footer"),
},
"v2.local": {
token: "v2.local.FGVEQLywggpvH0AzKtLXz0QRmGYuC6yvl05z9GIX0cnol6UK94cfV77AXnShlUcNgpDR12FrQiurS8jxBRmvoIKmeMWC5wY9Y6w.Q3VvbiBBbHBpbnVz",
version: V2,
payload: []byte("Love is stronger than hate or fear"),
footer: []byte("Cuon Alpinus"),
},
"v2.public": {
token: "v2.public.RnJhbmsgRGVuaXMgcm9ja3O7MPuu90WKNyvBUUhAGFmi4PiPOr2bN2ytUSU-QWlj8eNefki2MubssfN1b8figynnY0WusRPwIQ-o0HSZOS0F.Q3VvbiBBbHBpbnVz",
version: V2,
payload: []byte("Frank Denis rocks"),
footer: []byte("Cuon Alpinus"),
},
}
b, _ := hex.DecodeString("1eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2")
v2PublicKey := ed25519.PublicKey(b)
for name, test := range cases {
t.Run(name, func(t *testing.T) {
var payload []byte
var footer []byte
if ver, err := Parse(test.token, &payload, &footer, symmetricKey, map[Version]crypto.PublicKey{V1: rsaPublicKey, V2: v2PublicKey}); assert.NoError(t, err) {
assert.Equal(t, test.version, ver)
assert.Equal(t, test.payload, payload)
assert.Equal(t, test.footer, footer)
}
})
}
}
func TestParse_Err(t *testing.T) {
cases := map[string]struct {
token string
error error
}{
"Incorrect token format": {
token: "v1.publiceqreqqereqrqerq",
error: ErrIncorrectTokenFormat,
},
"Unsupported token version": {
token: "v0.local.rElw-WywOuwAqKC9Yao3YokSp7vx",
error: ErrUnsupportedTokenVersion,
},
"Unsupported token type": {
token: "v1.private.rElw",
error: ErrUnsupportedTokenType,
},
"Public key not found": {
token: "v1.public.rElw",
error: ErrPublicKeyNotFound,
},
}
for name, test := range cases {
t.Run(name, func(t *testing.T) {
_, err := Parse(test.token, nil, nil, nil, nil)
assert.Equal(t, test.error, err)
})
}
}
func TestParseFooter(t *testing.T) {
cases := map[string]struct {
token string
footer []byte
version Version
err error
}{
"Non empty footer": {
token: "v1.local.rElw-WywOuwAqKC9Yao3YokSp7vx0YiUB9hLTnsVOYYTojmVaYumJSQt8aggtCaFKWyaodw5k-CUWhYKATopiabAl4OAmTxHCfm2E4NSPvrmMcmi8n-JcZ93HpcxC6rx_ps22vutv7iP7wf8QcSD1Mwx.Q3VvbiBBbHBpbnVz",
footer: []byte("Cuon Alpinus"),
version: V1,
},
"Empty footer": {
token: "v1.local.rElw-WywOuwAqKC9Yao3YokSp7vx0YiUB9hLTnsVOYYTojmVaYumJSQt8aggtCaFKWyaodw5k-CUWhYKATopiabAl4OAmTxHCfm2E4NSPvrmMcmi8n-JcZ93HpcxC6rx_ps22vutv7iP7wf8QcSD1Mwx",
version: V1,
},
"Incorrect token format": {
token: "v1.rElw-WywOuwAqK",
err: ErrIncorrectTokenFormat,
},
}
for name, test := range cases {
t.Run(name, func(t *testing.T) {
var footer []byte
err := ParseFooter(test.token, &footer)
assert.Equal(t, test.err, err)
assert.Equal(t, test.footer, footer)
})
}
}
func TestGetTokenInfo(t *testing.T) {
cases := map[string]struct {
token string
version Version
purpose Purpose
err error
}{
"v1.local": {
token: "v1.local.rElw-WywOuwAqKC9Yao3YokSp7vx0YiUB9hLTnsVOYYTojmVaYumJSQt8aggtCaFKWyaodw5k-CUWhYKATopiabAl4OAmTxHCfm2E4NSPvrmMcmi8n-JcZ93HpcxC6rx_ps22vutv7iP7wf8QcSD1Mwx.Q3VvbiBBbHBpbnVz",
version: V1,
purpose: LOCAL,
},
"v2.local": {
token: "v2.local.driRNhM20GQPvlWfJCepzh6HdijAq-yNUtKpdy5KXjKfpSKrOlqQvQ",
version: V2,
purpose: LOCAL,
},
"v1.public": {
token: "v1.public.rElw-WywOuwAqKC9Yao3YokSp7vx0YiUB9hLTnsVOYYTojmVaYumJSQt8aggtCaFKWyaodw5k-CUWhYKATopiabAl4OAmTxHCfm2E4NSPvrmMcmi8n-JcZ93HpcxC6rx_ps22vutv7iP7wf8QcSD1Mwx.Q3VvbiBBbHBpbnVh",
version: V1,
purpose: PUBLIC,
},
"Unsupported token version": {
token: "v0.public.rElw-WywOuwAqKC9Yao3YokSp7vx0YiUB9hLTnsVOYYTojmVaYumJSQt8aggtCaFKWyaodw5k-CUWhYKATopiabAl4OAmTxHCfm2E4NSPvrmMcmi8n-JcZ93HpcxC6rx_ps22vutv7iP7wf8QcSD1Mwx.Q3VvbiBBbHBpbnVh",
err: ErrUnsupportedTokenVersion,
},
"Unsupported token type": {
token: "v1.private.rElw-WywOuwAqKC9Yao3YokSp7vx0YiUB9hLTnsVOYYTojmVaYumJSQt8aggtCaFKWyaodw5k-CUWhYKATopiabAl4OAmTxHCfm2E4NSPvrmMcmi8n-JcZ93HpcxC6rx_ps22vutv7iP7wf8QcSD1Mwx.Q3VvbiBBbHBpbnVh",
err: ErrUnsupportedTokenType,
},
"Incorrect token format": {
token: "v1.private",
err: ErrIncorrectTokenFormat,
},
}
for name, test := range cases {
t.Run(name, func(t *testing.T) {
version, purpose, err := GetTokenInfo(test.token)
assert.Equal(t, test.err, err)
assert.Equal(t, test.version, version)
assert.Equal(t, test.purpose, purpose)
})
}
}