forked from fsamin/go-wsqueue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacl_test.go
167 lines (140 loc) · 4.46 KB
/
acl_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
package wsqueue
import (
"fmt"
"net/http"
"testing"
"github.com/phayes/freeport"
"github.com/stretchr/testify/assert"
)
func TestCheckACLShouldAuthorizeEveryoneWhenACEIsSetToWord(t *testing.T) {
var port = freeport.GetPort()
wait := make(chan bool, 1)
//setup ACL
acl := ACL{
&ACEWorld{},
}
//setup server
handler := func(w http.ResponseWriter, r *http.Request) {
assert.True(t, checkACL(acl, w, r), "check should return true")
wait <- true
}
http.HandleFunc(fmt.Sprintf("/%d", port), handler)
//Run the server
t.Logf("Starting test server on port %d", port)
go http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
//Run the test
t.Logf("Calling the test server")
client := http.DefaultClient
res, err := client.Get(fmt.Sprintf("http://localhost:%d/%d", port, port))
assert.NoError(t, err)
assert.Equal(t, 200, res.StatusCode, "status code should be 200")
<-wait
}
func TestCheckACLShouldAuthorizeLocalIPWhenACEIsSetLocalIP(t *testing.T) {
var port = freeport.GetPort()
wait := make(chan bool, 1)
//setup ACL
acl := ACL{
&ACEIP{"localhost0"},
}
//setup server
handler := func(w http.ResponseWriter, r *http.Request) {
assert.True(t, checkACL(acl, w, r), "check should return true")
wait <- true
}
http.HandleFunc(fmt.Sprintf("/%d", port), handler)
//Run the server
t.Logf("Starting test server on port %d", port)
go http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
//Run the test
t.Logf("Calling the test server")
client := http.DefaultClient
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d/%d", port, port), nil)
req.Header.Set("X-Forwarded-For", "localhost0")
res, err := client.Do(req)
assert.NoError(t, err)
assert.Equal(t, 200, res.StatusCode, "status code should be 200")
<-wait
}
func TestCheckACLShouldUnauthorizeLocalIPWhenACEIPIsSetToFooBar(t *testing.T) {
var port = freeport.GetPort()
wait := make(chan bool, 2)
//setup ACL
acl := ACL{
&ACEIP{"FooBar"},
}
//setup server
handler := func(w http.ResponseWriter, r *http.Request) {
assert.False(t, checkACL(acl, w, r), "check should return false")
wait <- true
}
http.HandleFunc(fmt.Sprintf("/%d", port), handler)
//Run the server
t.Logf("Starting test server on port %d", port)
go http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
//Run the test
t.Logf("Calling the test server")
client := http.DefaultClient
req, _ := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d/%d", port, port), nil)
req.Header.Set("X-Forwarded-For", "localhost")
res, err := client.Do(req)
assert.NoError(t, err)
assert.Equal(t, http.StatusUnauthorized, res.StatusCode, "status code should be "+string(http.StatusUnauthorized))
wait <- true
<-wait
<-wait
}
func TestCheckACLShouldAuthorizeFooBarWhenACEDigetIsSetToFooBar(t *testing.T) {
var port = freeport.GetPort()
wait := make(chan bool, 1)
//setup ACL
acl := ACL{
&ACEDigest{Username: "Foo", Password: "Bar"},
}
//setup server
handler := func(w http.ResponseWriter, r *http.Request) {
assert.True(t, checkACL(acl, w, r), "check should return true")
wait <- true
}
http.HandleFunc(fmt.Sprintf("/%d", port), handler)
//Run the server
t.Logf("Starting test server on port %d", port)
go http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
//Run the test
t.Logf("Calling the test server")
client := http.DefaultClient
req, err := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d/%d", port, port), nil)
req.SetBasicAuth("Foo", "Bar")
res, err := client.Do(req)
assert.NoError(t, err)
assert.Equal(t, 200, res.StatusCode, "status code should be 200")
<-wait
}
func TestCheckACLShouldUnauthorizeFooBarWhenACEDigetIsSetToXXXXX(t *testing.T) {
var port = freeport.GetPort()
wait := make(chan bool, 2)
//setup ACL
acl := ACL{
&ACEDigest{Username: "XXX", Password: "XXX"},
}
//setup server
handler := func(w http.ResponseWriter, r *http.Request) {
assert.False(t, checkACL(acl, w, r), "check should return false")
wait <- true
}
http.HandleFunc(fmt.Sprintf("/%d", port), handler)
//Run the server
t.Logf("Starting test server on port %d", port)
go http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
//Run the test
t.Logf("Calling the test server")
client := http.DefaultClient
req, _ := http.NewRequest("GET", fmt.Sprintf("http://localhost:%d/%d", port, port), nil)
req.SetBasicAuth("Foo", "Bar")
res, err := client.Do(req)
assert.NoError(t, err)
assert.Equal(t, http.StatusUnauthorized, res.StatusCode, "status code should be "+string(http.StatusUnauthorized))
wait <- true
<-wait
<-wait
}