forked from ant1441/ldap_proxy
-
Notifications
You must be signed in to change notification settings - Fork 4
/
options_test.go
254 lines (205 loc) · 6.09 KB
/
options_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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"crypto"
"crypto/tls"
"fmt"
"net/url"
"reflect"
"strings"
"testing"
"time"
)
func testOptions() *Options {
o := NewOptions()
o.Upstreams = append(o.Upstreams, "http://127.0.0.1:8080/")
o.CookieSecret = "foobar"
o.EmailDomains = []string{"*"}
return o
}
func errorMsg(msgs []string) string {
result := make([]string, 0)
result = append(result, "Invalid configuration:")
result = append(result, msgs...)
return strings.Join(result, "\n ")
}
func TestNewOptions(t *testing.T) {
o := NewOptions()
o.EmailDomains = []string{"*"}
err := o.Validate()
if err == nil {
t.Error("expected error")
}
expected := errorMsg([]string{
"missing setting: upstream",
"missing setting: cookie-secret"})
if expected != err.Error() {
t.Errorf("unexpected error: %+v", err)
}
}
func TestInitializedOptions(t *testing.T) {
o := testOptions()
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
}
func TestProxyURLs(t *testing.T) {
o := testOptions()
o.Upstreams = append(o.Upstreams, "http://127.0.0.1:8081")
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
expected := []*url.URL{
{Scheme: "http", Host: "127.0.0.1:8080", Path: "/"},
// note the '/' was added
{Scheme: "http", Host: "127.0.0.1:8081", Path: "/"},
}
if !reflect.DeepEqual(expected, o.proxyURLs) {
t.Errorf("unexpected value: %+v, expected %+v", expected, o.proxyURLs)
}
}
func TestCompiledRegex(t *testing.T) {
o := testOptions()
regexps := []string{"/foo/.*", "/ba[rz]/quux"}
o.SkipAuthRegex = regexps
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
actual := make([]string, 0)
for _, regex := range o.CompiledPathRegex {
actual = append(actual, regex.String())
}
if !reflect.DeepEqual(regexps, actual) {
t.Errorf("unexpected value: %+v, expected %+v", actual, regexps)
}
}
func TestCompiledRegexError(t *testing.T) {
o := testOptions()
o.SkipAuthRegex = []string{"(foobaz", "barquux)"}
err := o.Validate()
if err == nil {
t.Error("expected error")
return
}
expected := errorMsg([]string{
"error compiling regex=\"(foobaz\" error parsing regexp: " +
"missing closing ): `(foobaz`",
"error compiling regex=\"barquux)\" error parsing regexp: " +
"unexpected ): `barquux)`"})
if expected != err.Error() {
t.Errorf("unexpected error message: %+v, expected %+v", expected, err)
}
}
func TestCookieRefreshMustBeLessThanCookieExpire(t *testing.T) {
o := testOptions()
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
o.CookieSecret = "0123456789abcdefabcd"
o.CookieRefresh = o.CookieExpire
if err := o.Validate(); err == nil {
t.Errorf("expected error, got %+v", err)
}
o.CookieRefresh -= time.Duration(1)
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
}
func TestBase64CookieSecret(t *testing.T) {
o := testOptions()
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
// 32 byte, base64 (urlsafe) encoded key
o.CookieSecret = "yHBw2lh2Cvo6aI_jn_qMTr-pRAjtq0nzVgDJNb36jgQ="
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
// 32 byte, base64 (urlsafe) encoded key, w/o padding
o.CookieSecret = "yHBw2lh2Cvo6aI_jn_qMTr-pRAjtq0nzVgDJNb36jgQ"
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
// 24 byte, base64 (urlsafe) encoded key
o.CookieSecret = "Kp33Gj-GQmYtz4zZUyUDdqQKx5_Hgkv3"
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
// 16 byte, base64 (urlsafe) encoded key
o.CookieSecret = "LFEqZYvYUwKwzn0tEuTpLA=="
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
// 16 byte, base64 (urlsafe) encoded key, w/o padding
o.CookieSecret = "LFEqZYvYUwKwzn0tEuTpLA"
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
}
func TestValidateSignatureKey(t *testing.T) {
o := testOptions()
o.SignatureKey = "sha1:secret"
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
if o.signatureData.hash != crypto.SHA1 {
t.Errorf("expected %+v to be equal to %+v", o.signatureData.hash, crypto.SHA1)
}
if o.signatureData.key != "secret" {
t.Errorf("expected %+v to be equal to %+v", o.signatureData.key, "secret")
}
}
func TestValidateSignatureKeyInvalidSpec(t *testing.T) {
o := testOptions()
o.SignatureKey = "invalid spec"
err := o.Validate()
if err.Error() != "Invalid configuration:\n invalid signature hash:key spec: "+o.SignatureKey {
t.Error("unexpected error", err)
}
}
func TestValidateSignatureKeyUnsupportedAlgorithm(t *testing.T) {
o := testOptions()
o.SignatureKey = "unsupported:default secret"
err := o.Validate()
if err.Error() != "Invalid configuration:\n unsupported signature hash algorithm: "+o.SignatureKey {
t.Error("unexpected error", err)
}
}
func TestValidateCookie(t *testing.T) {
o := testOptions()
o.CookieName = "_valid_cookie_name"
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
}
func TestValidateCookieBadName(t *testing.T) {
o := testOptions()
o.CookieName = "_bad_cookie_name{}"
err := o.Validate()
if err.Error() != fmt.Sprintf("Invalid configuration:\n invalid cookie name: %q", o.CookieName) {
t.Error("unexpected error", err)
}
}
func TestValidateCiphersBadName(t *testing.T) {
o := testOptions()
o.CiphersSuites = "bad_cipher"
err := o.Validate()
if err.Error() != fmt.Sprintf("Invalid configuration:\n unsupported cipher %q", o.CiphersSuites) {
t.Error("unexpected error", err)
}
}
func TestValidateCipher(t *testing.T) {
o := testOptions()
o.CiphersSuites = "TLS_RSA_WITH_RC4_128_SHA,TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305"
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
if err := o.Validate(); err != nil {
t.Errorf("unexpected error: %+v", err)
}
if tls.TLS_RSA_WITH_RC4_128_SHA != o.ciphersSuites[0] {
t.Errorf("unexpected cipher: %+v", o.ciphersSuites[0])
}
if tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 != o.ciphersSuites[1] {
t.Errorf("unexpected cipher: %+v", o.ciphersSuites[1])
}
}