forked from cristalhq/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algo_hs_test.go
51 lines (41 loc) · 1.28 KB
/
algo_hs_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
package jwt
import (
"errors"
"testing"
)
func TestHS(t *testing.T) {
f := func(alg Algorithm, signKey, verifyKey []byte, wantErr error) {
t.Helper()
signer, errSigner := NewSignerHS(alg, signKey)
if errSigner != nil {
t.Fatalf("NewSignerHS %v", errSigner)
}
verifier, errVerifier := NewVerifierHS(alg, verifyKey)
if errVerifier != nil {
t.Fatalf("NewVerifierHS %v", errVerifier)
}
token, err := NewBuilder(signer).Build(simplePayload)
if err != nil {
t.Fatalf("Build %v", errVerifier)
}
errVerify := verifier.Verify(token)
if !errors.Is(errVerify, wantErr) {
t.Errorf("want %v, got %v", wantErr, errVerify)
}
}
f(HS256, hsKey256, hsKey256, nil)
f(HS384, hsKey384, hsKey384, nil)
f(HS512, hsKey512, hsKey512, nil)
f(HS256, hsKey256, hsKeyAnother256, ErrInvalidSignature)
f(HS384, hsKey384, hsKeyAnother384, ErrInvalidSignature)
f(HS512, hsKey512, hsKeyAnother512, ErrInvalidSignature)
f(HS256, hsKey256, hsKeyAnother256, ErrInvalidSignature)
}
var (
hsKey256 = []byte("hmac-secret-key-256")
hsKey384 = []byte("hmac-secret-key-384")
hsKey512 = []byte("hmac-secret-key-512")
hsKeyAnother256 = []byte("hmac-secret-key-256-another")
hsKeyAnother384 = []byte("hmac-secret-key-384-another")
hsKeyAnother512 = []byte("hmac-secret-key-512-another")
)