-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_test.go
137 lines (116 loc) · 3.62 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
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDelim(t *testing.T) {
assert.True(t, validDelim(oauthDecEncDelim))
}
func TestCrypto(t *testing.T) {
var (
message = "Hello World"
secret = "123456789_123456789_123456789_12"
delim = "%&%&%&"
)
assert.True(t, validSecret(secret))
assert.True(t, validDelim(delim))
t.Run("Correct usage", func(t *testing.T) {
encrypted, err := Encrypt(secret, message)
assert.Nil(t, err)
assert.NotEqual(t, message, encrypted)
decrypted, err := Decrypt(secret, encrypted)
assert.Nil(t, err)
assert.Equal(t, decrypted, message)
})
t.Run("Decrypt with wrong key", func(t *testing.T) {
encrypted, err := Encrypt(secret, message)
assert.Nil(t, err)
assert.NotEqual(t, message, encrypted)
decrypted, err := Decrypt("wrong-secret-key", encrypted)
assert.NotNil(t, err)
assert.NotEqual(t, decrypted, message)
})
t.Run("Test OAuthDecEncoder", func(t *testing.T) {
var (
emailListID string = "abcdefgh"
providerName ProviderName = ProviderNameGoogle
redirectUrls []string = testingUrls()
outputIDs []string = []string{
"1234",
"5678",
}
)
de := NewOAuthDecEncoder(secret, delim)
for _, redirectUrl := range redirectUrls {
encrypted, err := de.Encode(emailListID, providerName, outputIDs, redirectUrl)
assert.Nil(t, err)
assert.NotEqual(t, emailListID, encrypted)
assert.NotEqual(t, providerName, encrypted)
assert.NotEqual(t, string(providerName), encrypted)
for _, outputID := range outputIDs {
assert.NotEqual(t, outputID, encrypted)
}
assert.NotEqual(t, redirectUrl, encrypted)
assert.NotEqual(t, emailListID+de.delim+string(providerName), encrypted)
decEmailListID, decProvider, decOutputIDs, decRedirectUrl, err := de.Decode(encrypted)
assert.Nil(t, err)
assert.Equal(t, emailListID, decEmailListID)
assert.Equal(t, providerName, decProvider.Name())
assert.Equal(t, outputIDs, decOutputIDs)
assert.Equal(t, redirectUrl, decRedirectUrl)
}
})
}
func TestUrls(t *testing.T) {
var (
secret = "123456789_123456789_123456789_12"
urls = testingUrls()
)
assert.True(t, validSecret(secret))
t.Run("Test encodePart() and decodePart()", func(t *testing.T) {
for _, url := range urls {
encodedUrl := encodePart(url)
assert.NotEqual(t, url, encodedUrl)
decodedUrl, err := decodePart(encodedUrl)
assert.Nil(t, err)
assert.Equal(t, url, decodedUrl)
}
})
t.Run("Test Encrpyt() and Decrpyt()", func(t *testing.T) {
for _, url := range urls {
encryptedUrl, err := Encrypt(secret, url)
assert.Nil(t, err)
assert.NotEqual(t, secret, encryptedUrl)
assert.NotEqual(t, url, encryptedUrl)
decryptedUrl, err := Decrypt(secret, encryptedUrl)
assert.Nil(t, err)
assert.NotEqual(t, secret, decryptedUrl)
assert.Equal(t, url, decryptedUrl)
}
})
}
func TestUUID(t *testing.T) {
uuid := NewUUID()
assert.NotContains(t, uuid, oauthDecEncDelim)
assert.NotContains(t, uuid, outputCookieDelim)
}
func testingUrls() []string {
return []string{
"http://bing.com",
"http://bing.com/1/2/3",
"https://bing.com",
"https://bing.com/1/2/3",
"http://subdoamin.bing.com",
"http://subdoamin.bing.com/1/2/3",
"https://subdoamin.bing.com",
"https://subdoamin.bing.com/1/2/3",
"http://bing.com?one=1&hello=true",
"http://bing.com/1/2/3?one=1&hello=true",
"https://bing.com?one=1&hello=true",
"https://bing.com/1/2/3?one=1&hello=true",
"http://subdoamin.bing.com?one=1&hello=true",
"http://subdoamin.bing.com/1/2/3?one=1&hello=true",
"https://subdoamin.bing.com?one=1&hello=true",
"https://subdoamin.bing.com/1/2/3?one=1&hello=true",
}
}