forked from pusher/pusher-http-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_test.go
153 lines (136 loc) · 4.56 KB
/
crypto_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
package pusher
import (
"encoding/hex"
"testing"
"gopkg.in/stretchr/testify.v1/assert"
)
func TestHmacSignature(t *testing.T) {
expected := "64e3f44166575febbc5de88c9476325ea7d4b3684752158d9fdb31fce34b980d"
toSign := "Hello!"
secret := "supersecret"
hmac := hmacSignature(toSign, secret)
assert.Equal(t, expected, hmac)
}
func TestHmacBytes(t *testing.T) {
expectedHex := "64e3f44166575febbc5de88c9476325ea7d4b3684752158d9fdb31fce34b980d"
expectedBytes, _ := hex.DecodeString(expectedHex)
toSign := "Hello!"
secret := "supersecret"
hmacBytes := hmacBytes([]byte(toSign), []byte(secret))
assert.Equal(t, expectedBytes, hmacBytes)
}
func TestCheckValidSignature(t *testing.T) {
signature := "64e3f44166575febbc5de88c9476325ea7d4b3684752158d9fdb31fce34b980d"
secret := "supersecret"
body := "Hello!"
validSignature := checkSignature(signature, secret, []byte(body))
assert.Equal(t, true, validSignature)
}
func TestCheckInvalidSignature(t *testing.T) {
signature := "no"
secret := "supersecret"
body := "Hello!"
validSignature := checkSignature(signature, secret, []byte(body))
assert.Equal(t, false, validSignature)
}
func TestCreateAuthMapNoE2E(t *testing.T) {
signature := "64e3f44166575febbc5de88c9476325ea7d4b3684752158d9fdb31fce34b980d"
key := "key"
secret := "supersecret"
stringToSign := "Hello!"
sharedSecret := ""
authMap := createAuthMap(key, secret, stringToSign, sharedSecret)
// The [4:] here removes the prefix of key: from the string.
assert.Equal(t, signature, authMap["auth"][4:])
assert.Equal(t, "", authMap["shared_secret"])
}
func TestCreateAuthMapE2E(t *testing.T) {
signature := "64e3f44166575febbc5de88c9476325ea7d4b3684752158d9fdb31fce34b980d"
key := "key"
secret := "supersecret"
stringToSign := "Hello!"
sharedSecret := "This is a string that is 32 chars"
authMap := createAuthMap(key, secret, stringToSign, sharedSecret)
// The [4:] here removes the prefix of key: from the string.
assert.Equal(t, signature, authMap["auth"][4:])
assert.Equal(t, sharedSecret, authMap["shared_secret"])
}
func TestMD5Signature(t *testing.T) {
expected := "952d2c56d0485958336747bcdd98590d"
actual := md5Signature([]byte("Hello!"))
assert.Equal(t, expected, actual)
}
func TestEncrypt(t *testing.T) {
channel := "private-encrypted-bla"
body := []byte("Hello!")
encryptionKey := []byte("This is a string that is 32 chars")
cipherText := encrypt(channel, body, encryptionKey)
assert.NotNil(t, cipherText)
assert.NotEqual(t, body, cipherText)
}
func TestFormatMessage(t *testing.T) {
nonce := "a"
cipherText := "b"
formatted := formatMessage(nonce, cipherText)
assert.Equal(t, `{"nonce":"a","ciphertext":"b"}`, formatted)
}
func TestGenerateSharedSecret(t *testing.T) {
channel := "private-encrypted-bla"
encryptionKey := []byte("This is a string that is 32 chars")
sharedSecret := generateSharedSecret(channel, encryptionKey)
t.Log(hex.EncodeToString(sharedSecret[:]))
expected := "004831f99d2a4e86723e893caded3a2897deeddbed9514fe9497dcddc52bd50b"
assert.Equal(t, expected, hex.EncodeToString(sharedSecret[:]))
}
func TestDecryptValidKey(t *testing.T) {
channel := "private-encrypted-bla"
plaintext := "Hello!"
cipherText := `{"nonce":"sjklahvpWWQgAjTx5FfYHCCxd2AmaL9T","ciphertext":"zoDEe8dA3nDXKsybAWce/hXGW4szJw=="}`
encryptionKey := []byte("This is a string that is 32 chars")
encryptedWebhookData := &Webhook{
TimeMs: 1,
Events: []WebhookEvent{
WebhookEvent{
Name: "event",
Channel: channel,
Event: "event",
Data: cipherText,
SocketID: "44610.7511910",
},
},
}
expectedWebhookData := &Webhook{
TimeMs: 1,
Events: []WebhookEvent{
WebhookEvent{
Name: "event",
Channel: channel,
Event: "event",
Data: plaintext,
SocketID: "44610.7511910",
},
},
}
decryptedWebhooks, _ := decryptEvents(*encryptedWebhookData, encryptionKey)
assert.Equal(t, expectedWebhookData, decryptedWebhooks)
}
func TestDecryptInvalidKey(t *testing.T) {
channel := "private-encrypted-bla"
cipherText := `{"nonce":"sjklahvpWWQgAjTx5FfYHCCxd2AmaL9T","ciphertext":"zoDEe8dA3nDXKsybAWce/hXGW4szJw=="}`
encryptionKey := []byte("This is an invalid key 32 chars!!")
encryptedWebhookData := &Webhook{
TimeMs: 1,
Events: []WebhookEvent{
WebhookEvent{
Name: "event",
Channel: channel,
Event: "event",
Data: cipherText,
SocketID: "44610.7511910",
},
},
}
decryptedWebhooks, err := decryptEvents(*encryptedWebhookData, encryptionKey)
assert.Equal(t, []WebhookEvent(nil), decryptedWebhooks.Events)
assert.EqualError(t, err, "Failed to decrypt event, possibly wrong key?")
}