1
|
|
|
package scan_test |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"net/http" |
5
|
|
|
"testing" |
6
|
|
|
"time" |
7
|
|
|
|
8
|
|
|
"github.com/stefanoj3/dirstalk/pkg/scan/client" |
9
|
|
|
|
10
|
|
|
"github.com/stefanoj3/dirstalk/pkg/common/test" |
11
|
|
|
"github.com/stefanoj3/dirstalk/pkg/scan" |
12
|
|
|
"github.com/stefanoj3/dirstalk/pkg/scan/producer" |
13
|
|
|
"github.com/stretchr/testify/assert" |
14
|
|
|
) |
15
|
|
|
|
16
|
|
|
func TestScanningWithEmptyProducerWillProduceNoResults(t *testing.T) { |
17
|
|
|
logger, _ := test.NewLogger() |
18
|
|
|
|
19
|
|
|
prod := producer.NewDictionaryProducer([]string{}, []string{}, 1) |
20
|
|
|
c := &http.Client{Timeout: time.Microsecond} |
21
|
|
|
sut := scan.NewScanner( |
22
|
|
|
c, |
23
|
|
|
prod, |
24
|
|
|
producer.NewReProducer(prod), |
25
|
|
|
logger, |
26
|
|
|
) |
27
|
|
|
|
28
|
|
|
results := sut.Scan(test.MustParseUrl(t, "http://localhost/"), 10) |
29
|
|
|
|
30
|
|
|
for r := range results { |
31
|
|
|
t.Fatalf("No results expected, got %s", r.Target.Path) |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
func TestScannerWillLogAnErrorWithInvalidDictionary(t *testing.T) { |
36
|
|
|
logger, loggerBuffer := test.NewLogger() |
37
|
|
|
|
38
|
|
|
prod := producer.NewDictionaryProducer( |
39
|
|
|
[]string{"\n"}, |
40
|
|
|
[]string{"/home"}, |
41
|
|
|
1, |
42
|
|
|
) |
43
|
|
|
|
44
|
|
|
testServer, serverAssertion := test.NewServerWithAssertion( |
45
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}), |
46
|
|
|
) |
47
|
|
|
defer testServer.Close() |
48
|
|
|
|
49
|
|
|
c, err := client.NewClientFromConfig( |
50
|
|
|
1000, |
51
|
|
|
nil, |
52
|
|
|
"", |
53
|
|
|
false, |
54
|
|
|
nil, |
55
|
|
|
nil, |
56
|
|
|
test.MustParseUrl(t, testServer.URL), |
57
|
|
|
) |
58
|
|
|
assert.NoError(t, err) |
59
|
|
|
sut := scan.NewScanner( |
60
|
|
|
c, |
61
|
|
|
prod, |
62
|
|
|
producer.NewReProducer(prod), |
63
|
|
|
logger, |
64
|
|
|
) |
65
|
|
|
|
66
|
|
|
results := sut.Scan(test.MustParseUrl(t, testServer.URL), 10) |
67
|
|
|
|
68
|
|
|
for r := range results { |
69
|
|
|
t.Fatalf("No results expected, got %s", r.Target.Path) |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
assert.Contains(t, loggerBuffer.String(), "failed to build request") |
73
|
|
|
assert.Contains(t, loggerBuffer.String(), "invalid method") |
74
|
|
|
assert.Equal(t, 0, serverAssertion.Len()) |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
func TestScannerWillNotRedirectIfStatusCodeIsInvalid(t *testing.T) { |
78
|
|
|
logger, loggerBuffer := test.NewLogger() |
79
|
|
|
|
80
|
|
|
prod := producer.NewDictionaryProducer( |
81
|
|
|
[]string{http.MethodGet}, |
82
|
|
|
[]string{"/home"}, |
83
|
|
|
3, |
84
|
|
|
) |
85
|
|
|
|
86
|
|
|
testServer, serverAssertion := test.NewServerWithAssertion( |
87
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
88
|
|
|
w.Header().Add("location", "/potato") |
89
|
|
|
if r.URL.Path == "/home" { |
90
|
|
|
w.WriteHeader(http.StatusOK) |
91
|
|
|
return |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
w.WriteHeader(http.StatusNotFound) |
95
|
|
|
}), |
96
|
|
|
) |
97
|
|
|
defer testServer.Close() |
98
|
|
|
|
99
|
|
|
c, err := client.NewClientFromConfig( |
100
|
|
|
1000, |
101
|
|
|
nil, |
102
|
|
|
"", |
103
|
|
|
false, |
104
|
|
|
nil, |
105
|
|
|
nil, |
106
|
|
|
test.MustParseUrl(t, testServer.URL), |
107
|
|
|
) |
108
|
|
|
assert.NoError(t, err) |
109
|
|
|
|
110
|
|
|
sut := scan.NewScanner( |
111
|
|
|
c, |
112
|
|
|
prod, |
113
|
|
|
producer.NewReProducer(prod), |
114
|
|
|
logger, |
115
|
|
|
) |
116
|
|
|
|
117
|
|
|
results := make([]scan.Result, 0, 2) |
118
|
|
|
resultsChannel := sut.Scan(test.MustParseUrl(t, testServer.URL), 10) |
119
|
|
|
|
120
|
|
|
for r := range resultsChannel { |
121
|
|
|
results = append(results, r) |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
expectedsResults := []scan.Result{ |
125
|
|
|
{ |
126
|
|
|
Target: scan.Target{Path: "/home", Method: http.MethodGet, Depth: 3}, |
127
|
|
|
StatusCode: http.StatusOK, |
128
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/home"), |
129
|
|
|
}, |
130
|
|
|
{ |
131
|
|
|
Target: scan.Target{Path: "/home/home", Method: http.MethodGet, Depth: 2}, |
132
|
|
|
StatusCode: http.StatusNotFound, |
133
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/home/home"), |
134
|
|
|
}, |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
assert.Equal(t, expectedsResults, results) |
138
|
|
|
|
139
|
|
|
assert.Contains(t, loggerBuffer.String(), "/home") |
140
|
|
|
assert.Contains(t, loggerBuffer.String(), "/home/home") |
141
|
|
|
assert.Equal(t, 2, serverAssertion.Len()) |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
func TestScannerWillChangeMethodForRedirect(t *testing.T) { |
145
|
|
|
logger, loggerBuffer := test.NewLogger() |
146
|
|
|
|
147
|
|
|
prod := producer.NewDictionaryProducer( |
148
|
|
|
[]string{http.MethodPatch}, |
149
|
|
|
[]string{"/home"}, |
150
|
|
|
3, |
151
|
|
|
) |
152
|
|
|
|
153
|
|
|
testServer, serverAssertion := test.NewServerWithAssertion( |
154
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
155
|
|
|
if r.URL.Path == "/home" { |
156
|
|
|
http.Redirect(w, r, "/potato", http.StatusMovedPermanently) |
157
|
|
|
return |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
w.WriteHeader(http.StatusNotFound) |
161
|
|
|
}), |
162
|
|
|
) |
163
|
|
|
defer testServer.Close() |
164
|
|
|
|
165
|
|
|
c, err := client.NewClientFromConfig( |
166
|
|
|
1000, |
167
|
|
|
nil, |
168
|
|
|
"", |
169
|
|
|
false, |
170
|
|
|
nil, |
171
|
|
|
nil, |
172
|
|
|
test.MustParseUrl(t, testServer.URL), |
173
|
|
|
) |
174
|
|
|
assert.NoError(t, err) |
175
|
|
|
|
176
|
|
|
sut := scan.NewScanner( |
177
|
|
|
c, |
178
|
|
|
prod, |
179
|
|
|
producer.NewReProducer(prod), |
180
|
|
|
logger, |
181
|
|
|
) |
182
|
|
|
|
183
|
|
|
results := make([]scan.Result, 0, 4) |
184
|
|
|
resultsChannel := sut.Scan(test.MustParseUrl(t, testServer.URL), 1) |
185
|
|
|
|
186
|
|
|
for r := range resultsChannel { |
187
|
|
|
results = append(results, r) |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
expectedResults := []scan.Result{ |
191
|
|
|
{ |
192
|
|
|
Target: scan.Target{Path: "/home", Method: http.MethodPatch, Depth: 3}, |
193
|
|
|
StatusCode: http.StatusMovedPermanently, |
194
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/home"), |
195
|
|
|
}, |
196
|
|
|
{ |
197
|
|
|
Target: scan.Target{Path: "/potato", Method: http.MethodGet, Depth: 2}, |
198
|
|
|
StatusCode: http.StatusNotFound, |
199
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/potato"), |
200
|
|
|
}, |
201
|
|
|
{ |
202
|
|
|
Target: scan.Target{Path: "/home/home", Method: http.MethodPatch, Depth: 2}, |
203
|
|
|
StatusCode: http.StatusNotFound, |
204
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/home/home"), |
205
|
|
|
}, |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
assert.Equal(t, expectedResults, results) |
209
|
|
|
|
210
|
|
|
loggerBufferAsString := loggerBuffer.String() |
211
|
|
|
assert.Contains(t, loggerBufferAsString, "/home") |
212
|
|
|
assert.Contains(t, loggerBufferAsString, "/potato") |
213
|
|
|
assert.Contains(t, loggerBufferAsString, "/home/home") |
214
|
|
|
assert.NotContains(t, loggerBufferAsString, "error") |
215
|
|
|
assert.Equal(t, 3, serverAssertion.Len()) |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
func TestScannerWhenOutOfDepthWillNotFollowRedirect(t *testing.T) { |
219
|
|
|
logger, loggerBuffer := test.NewLogger() |
220
|
|
|
|
221
|
|
|
prod := producer.NewDictionaryProducer( |
222
|
|
|
[]string{http.MethodPatch}, |
223
|
|
|
[]string{"/home"}, |
224
|
|
|
0, |
225
|
|
|
) |
226
|
|
|
|
227
|
|
|
testServer, serverAssertion := test.NewServerWithAssertion( |
228
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
229
|
|
|
if r.URL.Path == "/home" { |
230
|
|
|
http.Redirect(w, r, "/potato", http.StatusMovedPermanently) |
231
|
|
|
return |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
w.WriteHeader(http.StatusNotFound) |
235
|
|
|
}), |
236
|
|
|
) |
237
|
|
|
defer testServer.Close() |
238
|
|
|
|
239
|
|
|
c, err := client.NewClientFromConfig( |
240
|
|
|
1000, |
241
|
|
|
nil, |
242
|
|
|
"", |
243
|
|
|
false, |
244
|
|
|
nil, |
245
|
|
|
nil, |
246
|
|
|
test.MustParseUrl(t, testServer.URL), |
247
|
|
|
) |
248
|
|
|
assert.NoError(t, err) |
249
|
|
|
|
250
|
|
|
sut := scan.NewScanner( |
251
|
|
|
c, |
252
|
|
|
prod, |
253
|
|
|
producer.NewReProducer(prod), |
254
|
|
|
logger, |
255
|
|
|
) |
256
|
|
|
|
257
|
|
|
results := make([]scan.Result, 0, 1) |
258
|
|
|
resultsChannel := sut.Scan(test.MustParseUrl(t, testServer.URL), 1) |
259
|
|
|
|
260
|
|
|
for r := range resultsChannel { |
261
|
|
|
results = append(results, r) |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
expectedResults := []scan.Result{ |
265
|
|
|
{ |
266
|
|
|
Target: scan.Target{Path: "/home", Method: http.MethodPatch, Depth: 0}, |
267
|
|
|
StatusCode: http.StatusMovedPermanently, |
268
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/home"), |
269
|
|
|
}, |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
assert.Equal(t, expectedResults, results) |
273
|
|
|
|
274
|
|
|
loggerBufferAsString := loggerBuffer.String() |
275
|
|
|
assert.Contains(t, loggerBufferAsString, "/home") |
276
|
|
|
assert.Contains(t, loggerBufferAsString, "depth is 0, not following any redirect") |
277
|
|
|
assert.NotContains(t, loggerBufferAsString, "error") |
278
|
|
|
assert.Equal(t, 1, serverAssertion.Len()) |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
func TestScannerWillSkipRedirectWhenLocationHostIsDifferent(t *testing.T) { |
282
|
|
|
logger, loggerBuffer := test.NewLogger() |
283
|
|
|
|
284
|
|
|
prod := producer.NewDictionaryProducer( |
285
|
|
|
[]string{http.MethodPatch}, |
286
|
|
|
[]string{"/home"}, |
287
|
|
|
3, |
288
|
|
|
) |
289
|
|
|
|
290
|
|
|
testServer, serverAssertion := test.NewServerWithAssertion( |
291
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
292
|
|
|
if r.URL.Path == "/home" { |
293
|
|
|
http.Redirect(w, r, "http://gibberish/potato", http.StatusMovedPermanently) |
294
|
|
|
return |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
w.WriteHeader(http.StatusNotFound) |
298
|
|
|
}), |
299
|
|
|
) |
300
|
|
|
defer testServer.Close() |
301
|
|
|
|
302
|
|
|
c, err := client.NewClientFromConfig( |
303
|
|
|
1000, |
304
|
|
|
nil, |
305
|
|
|
"", |
306
|
|
|
false, |
307
|
|
|
nil, |
308
|
|
|
nil, |
309
|
|
|
test.MustParseUrl(t, testServer.URL), |
310
|
|
|
) |
311
|
|
|
assert.NoError(t, err) |
312
|
|
|
|
313
|
|
|
sut := scan.NewScanner( |
314
|
|
|
c, |
315
|
|
|
prod, |
316
|
|
|
producer.NewReProducer(prod), |
317
|
|
|
logger, |
318
|
|
|
) |
319
|
|
|
|
320
|
|
|
results := make([]scan.Result, 0, 4) |
321
|
|
|
resultsChannel := sut.Scan(test.MustParseUrl(t, testServer.URL), 1) |
322
|
|
|
|
323
|
|
|
for r := range resultsChannel { |
324
|
|
|
results = append(results, r) |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
expectedResults := []scan.Result{ |
328
|
|
|
{ |
329
|
|
|
Target: scan.Target{Path: "/home", Method: http.MethodPatch, Depth: 3}, |
330
|
|
|
StatusCode: http.StatusMovedPermanently, |
331
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/home"), |
332
|
|
|
}, |
333
|
|
|
{ |
334
|
|
|
Target: scan.Target{Path: "/home/home", Method: http.MethodPatch, Depth: 2}, |
335
|
|
|
StatusCode: http.StatusNotFound, |
336
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/home/home"), |
337
|
|
|
}, |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
assert.Equal(t, expectedResults, results) |
341
|
|
|
|
342
|
|
|
loggerBufferAsString := loggerBuffer.String() |
343
|
|
|
assert.Contains(t, loggerBufferAsString, "/home") |
344
|
|
|
assert.Contains(t, loggerBufferAsString, "/home/home") |
345
|
|
|
assert.Contains(t, loggerBufferAsString, "skipping redirect, pointing to a different host") |
346
|
|
|
assert.NotContains(t, loggerBufferAsString, "error") |
347
|
|
|
assert.Equal(t, 2, serverAssertion.Len()) |
348
|
|
|
} |
349
|
|
|
|