-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathhttp_test.go
292 lines (258 loc) · 7.21 KB
/
http_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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright 2021 Jeff Foley. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package http
import (
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"github.com/caffix/resolve"
"github.com/caffix/stringset"
"github.com/miekg/dns"
)
func TestCopyCookies(t *testing.T) {
u, _ := url.Parse("http://owasp.org")
DefaultClient.Jar.SetCookies(u, []*http.Cookie{{
Name: "Test",
Value: "Cookie",
}})
CopyCookies("http://owasp.org", "http://example.com")
u2, _ := url.Parse("http://example.com")
if c := DefaultClient.Jar.Cookies(u2); len(c) == 0 || c[0].Value != "Cookie" {
t.Errorf("Failed to copy the cookie")
}
}
func TestCheckCookie(t *testing.T) {
type args struct {
urlString string
cookieName string
}
tests := []struct {
name string
init func()
args args
want bool
}{
{
name: "basic-success",
init: func() {
sampleURL, err := url.Parse("http://owasp.org")
if err != nil {
t.Errorf("CheckCookie() parse error: got error = %v", err)
}
cookies := []*http.Cookie{{Name: "cookie1", Value: "sample cookie value"}}
DefaultClient.Jar.SetCookies(sampleURL, cookies)
},
args: args{
urlString: "https://owasp.org",
cookieName: "cookie1",
},
want: true,
},
{
name: "basic-failure",
init: func() {},
args: args{
urlString: "http://domain.local",
cookieName: "cookie2",
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.init()
if got := CheckCookie(tt.args.urlString, tt.args.cookieName); got != tt.want {
t.Errorf("CheckCookie() = %v, want %v", got, tt.want)
}
})
}
}
func TestRequestWebPage(t *testing.T) {
name := "caffix"
pass := "OWASP"
hkey := "OWASP-Leader"
post := "Test Body"
succ := "Success"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if username, password, ok := r.BasicAuth(); !ok || username != name || password != pass {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprint(w, "Authentication Failed")
return
}
if val := r.Header.Get(hkey); val != name {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Header value was missing")
return
}
if r.Method != http.MethodPost {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "Method was not POST")
return
}
if in, err := ioutil.ReadAll(r.Body); err != nil || string(in) != post {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprint(w, "POST body did not match")
return
}
fmt.Fprint(w, succ)
}))
defer ts.Close()
resp, err := RequestWebPage(context.TODO(), ts.URL, "get", nil, nil, &BasicAuth{name, pass})
if err == nil || resp == succ {
t.Errorf("Failed to detect the bad request")
}
body := strings.NewReader(post)
var headers = map[string]string{hkey: name}
resp, err = RequestWebPage(context.TODO(), ts.URL, "post", body, headers, &BasicAuth{name, pass})
if err != nil || resp != succ {
t.Errorf(resp)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
resp, err = RequestWebPage(ctx, ts.URL, "get", nil, nil, nil)
if err == nil || resp != "" {
t.Errorf("Failed to detect the expired context")
}
}
func TestCrawl(t *testing.T) {
tests := []struct {
name string
depth int
want []string
}{
{
name: "first page only",
depth: 1,
want: []string{"example.com", "127.0.0.1"},
},
{
name: "first and second page",
depth: 2,
want: []string{"example.com", "127.0.0.1", "xml.owasp.org",
"www.owasp.org", "www.example.com", "sub.example.com", "www.google.com"},
},
{
name: "all of the pages",
depth: 0,
want: []string{"example.com", "127.0.0.1", "xml.owasp.org",
"www.owasp.org", "www.example.com", "sub.example.com",
"www.google.com", "owasp.org", "img.owasp.org",
"media.owasp.org", "static.owasp.org", "blogs.oracle.com"},
},
}
ts := httptest.NewServer(http.FileServer(http.Dir("./static")))
defer ts.Close()
for _, test := range tests {
set := stringset.New(test.want...)
defer set.Close()
got, err := Crawl(context.Background(), ts.URL, []string{"127.0.0.1"}, test.depth)
if err != nil {
t.Errorf("Failed to crawl the static web content: %s: %v", test.name, err)
continue
}
for _, w := range got {
set.Remove(w)
}
if set.Len() != 0 {
fmt.Printf("Test %s with max %d failed to discover the following names: %v\n", test.name, test.depth, set.Slice())
}
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err := Crawl(ctx, ts.URL, []string{"127.0.0.1"}, 0)
if err.Error() != "the context expired during the crawl of "+ts.URL {
t.Errorf("Failed to catch the expired context during the crawl")
}
_, err = Crawl(ctx, ts.URL, []string{"127.0.0.1"}, 0)
if err.Error() != "the context expired" {
t.Errorf("Failed to catch the expired context before the crawl")
}
next := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`<html>
<head>
<title>Sample page with links for testing Crawl</title>
</head>
<body>
<p>Just text as a placeholder.</p>
</body>
</html>`))
}))
defer next.Close()
_, err = Crawl(context.Background(), next.URL, []string{"127.0.0.1"}, 0)
if err.Error() != "no DNS names were discovered during the crawl of "+next.URL {
t.Errorf("Failed to detect the crawl returned zero names")
}
}
func TestPullCertificateNames(t *testing.T) {
r := resolve.NewResolvers()
if r == nil {
t.Errorf("Failed to setup the DNS resolver")
}
_ = r.AddResolvers(10, "8.8.8.8")
defer r.Stop()
msg := resolve.QueryMsg("www.utica.edu", dns.TypeA)
resp, err := r.QueryBlocking(context.Background(), msg)
if err != nil && resp == nil && len(resp.Answer) > 0 {
t.Errorf("Failed to obtain the IP address")
}
ans := resolve.ExtractAnswers(resp)
if len(ans) == 0 {
t.Errorf("Failed to obtain answers to the DNS query")
}
rr := resolve.AnswersByType(ans, dns.TypeA)
if len(rr) == 0 {
t.Errorf("Failed to obtain the answers of the correct type")
}
ip := net.ParseIP(strings.TrimSpace(rr[0].Data))
if ip == nil {
t.Errorf("Failed to extract a valid IP address from the DNS response")
}
if names := PullCertificateNames(context.Background(), ip.String(), []int{443}); len(names) == 0 {
t.Errorf("Failed to obtain names from a certificate from address %s", ip.String())
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if names := PullCertificateNames(ctx, ip.String(), []int{443}); len(names) != 0 {
t.Errorf("Failed to detect the expired context")
}
}
func TestCleanName(t *testing.T) {
tests := []struct {
data string
want string
}{
{
data: "3aowasp.org",
want: "owasp.org",
},
{
data: "http://www.owasp.org/index.html",
want: "www.owasp.org",
},
{
data: "-Sub.OWASP.org.",
want: "sub.owasp.org",
},
{
data: "http://www.owasp.org/index.html",
want: "www.owasp.org",
},
{
data: "http://blackhat2018.owasp.org/index.html",
want: "blackhat2018.owasp.org",
},
}
for _, test := range tests {
if got := CleanName(test.data); got != test.want {
t.Errorf("Got: %s, Want: %s", got, test.want)
}
}
}