-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_test.go
103 lines (88 loc) · 1.93 KB
/
token_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
package mock
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"testing"
"time"
)
func TestTokenExpired(t *testing.T) {
token := &Token{
IssuedAt: time.Now().Unix(),
}
if token.Expired() {
t.Error("Token must be valid")
}
token.IssuedAt = time.Now().Unix() - 3601
if !token.Expired() {
t.Error("Token must be expired")
}
}
func TestAuthKeyFromBytes(t *testing.T) {
pem := `-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgodDhM6dpwS6cNtS3
nPA41Vs3vqloEKwSYAi5ILTdm3ygCgYIKoZIzj0DAQehRANCAATqkU98jtUIDH7n
dW8vbVTfCuY9zw3CFkmeMzncdymE0lPdlpAVh/Np78DNWHaAQ5gXhR27LhLE6cYr
NJ7qj9gT
-----END PRIVATE KEY-----`
key, err := AuthKeyFromBytes([]byte(pem))
if err != nil {
t.Error("Error must be nil")
}
if key == nil {
t.Error("Key must be not nil")
}
}
func TestAuthKeyFromBytes_EmptyBlockBytes(t *testing.T) {
pem := `-----BEGIN PRIVATE KEY-----
-----END PRIVATE KEY-----`
key, err := AuthKeyFromBytes([]byte(pem))
if err == nil {
t.Error("Error must be not nil")
}
if key != nil {
t.Error("Key must be nil")
}
}
func TestAuthKeyFromBytes_EmptyPEM(t *testing.T) {
key, err := AuthKeyFromBytes([]byte(""))
if err == nil {
t.Error("Error must be not nil")
}
if key != nil {
t.Error("Key must be nil")
}
}
func TestAuthKeyFromFile(t *testing.T) {
_, err := AuthKeyFromFile("test/AuthKey_82M5U9676G.p8")
if err != nil {
t.Error(err)
}
}
func TestAuthKeyFromFile_BadPath(t *testing.T) {
key, err := AuthKeyFromFile("")
if err == nil {
t.Error("Error must be not nil")
}
if key != nil {
t.Error("Key must be nil")
}
}
func TestGenerateAuthKeyPEM(t *testing.T) {
pemKey, err := GenerateAuthKeyPEM()
if err != nil {
t.Error(err)
}
block, _ := pem.Decode(pemKey)
if block == nil {
t.Error("Cannot parse PEM")
}
p8, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
t.Error(err)
}
_, ok := p8.(*ecdsa.PrivateKey)
if !ok {
t.Error("Not ECDSA private key")
}
}