-
Notifications
You must be signed in to change notification settings - Fork 19
/
gcm_test.go
90 lines (76 loc) · 2.55 KB
/
gcm_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
package jwt
import (
"bytes"
"reflect"
"testing"
)
func TestGCM(t *testing.T) {
// Get a plain, we will compare it later on.
plainToken, err := Sign(testAlg, testSecret, Map{"foo": "bar", "age": 27}, Claims{Issuer: "issuer"})
if err != nil {
t.Fatal(err)
}
var (
key = MustGenerateRandom(32)
addtionalData = []byte("adata")
// constant-time because of AES.
expectedToken = []byte("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.QmT4pOOamAy7TwdqeF6RpHplP21yMNulO5fVQqva2arIZExIerYfVtLwihoKShSTMtCXeKFlLRNlrRa7hav3DZg.en_w-8wf5nL_s7J1qiG3l0HasomYCe7qme4UfhDYOiw")
)
encrypt, decrypt, err := GCM(key, addtionalData)
if err != nil {
t.Fatal(err)
}
encryptedToken, err := SignEncrypted(testAlg, testSecret, encrypt, Map{"foo": "bar", "age": 27}, Claims{Issuer: "issuer"})
if err != nil {
t.Fatal(err)
}
t.Logf("Encrypted Token: %s", string(encryptedToken))
if bytes.Equal(expectedToken, encryptedToken) {
t.Fatalf("expected token to be: %q but got: %q", expectedToken, encryptedToken)
}
if bytes.Equal(plainToken, encryptedToken) {
t.Fatalf("expected plain and encrypted token to be different: %s", string(plainToken))
}
// Test if encryption & decryption work as expected.
verifiedToken, err := VerifyEncrypted(testAlg, testSecret, decrypt, encryptedToken)
if err != nil {
t.Fatal(err)
}
type claims struct {
Foo string `json:"foo"`
Age int `json:"age"`
Issuer string `json:"iss"`
}
expectedClaims := claims{"bar", 27, "issuer"}
var gotClaims claims
err = verifiedToken.Claims(&gotClaims)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(expectedClaims, gotClaims) {
t.Fatalf("expected claims to be:\n%#+v\n\nbut got:\n%#+v", expectedClaims, gotClaims)
}
// Test try to decrypt unencrypted (failure).
verifiedToken, err = VerifyEncrypted(testAlg, testSecret, decrypt, plainToken)
if err != ErrDecrypt {
t.Fatalf("expected error: %v but got: %v", ErrDecrypt, err)
}
// Test encrypted but different key (same additionalData).
encrypt, decrypt, err = GCM(MustGenerateRandom(32), []byte("data"))
if err != nil {
t.Fatal(err)
}
verifiedToken, err = VerifyEncrypted(testAlg, testSecret, decrypt, encryptedToken)
if err != ErrDecrypt {
t.Fatalf("expected error: %v but got: %v", ErrDecrypt, err)
}
// Test fail because of different additionalData.
encrypt, decrypt, err = GCM(key, []byte("a_data"))
if err != nil {
t.Fatal(err)
}
verifiedToken, err = VerifyEncrypted(testAlg, testSecret, decrypt, encryptedToken)
if err != ErrDecrypt {
t.Fatalf("expected error: %v but got: %v", ErrDecrypt, err)
}
}