-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaseto_test.go
86 lines (75 loc) · 1.67 KB
/
paseto_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
package paseto
import (
"encoding/hex"
"encoding/json"
"os"
"reflect"
"testing"
)
type GoldenCases struct {
Tests []GoldenCase `json:"tests"`
}
type GoldenCase struct {
Name string `json:"name"`
ExpectFail bool `json:"expect-fail"`
Nonce string `json:"nonce"`
Key string `json:"key"`
PublicKey string `json:"public-key"`
SecretKey string `json:"secret-key"`
SecretKeySeed string `json:"secret-key-seed"`
SecretKeyPem string `json:"secret-key-pem"`
PublicKeyPem string `json:"public-key-pem"`
Token string `json:"token"`
Payload string `json:"payload"`
Footer string `json:"footer"`
ImplicitAssertion string `json:"implicit-assertion"`
}
func loadGoldenFile(filename string) GoldenCases {
f, err := os.Open(filename)
if err != nil {
panic(err)
}
defer f.Close()
var tc GoldenCases
if err := json.NewDecoder(f).Decode(&tc); err != nil {
panic(err)
}
return tc
}
func must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
func mustHex(raw string) []byte {
return must(hex.DecodeString(raw))
}
func mustJSON(raw string) any {
if len(raw) == 0 || string(raw) == "" {
return nil
}
var dst any
if err := json.Unmarshal([]byte(raw), &dst); err != nil {
return string(raw)
}
return dst
}
func mustOk(tb testing.TB, err error) {
tb.Helper()
if err != nil {
tb.Fatal(err)
}
}
func mustFail(tb testing.TB, err error) {
tb.Helper()
if err == nil {
tb.Fatal()
}
}
func mustEqual[T any](tb testing.TB, have, want T) {
tb.Helper()
if !reflect.DeepEqual(have, want) {
tb.Fatalf("\nhave: %+v\nwant: %+v\n", have, want)
}
}