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
|
|
|
client := &http.Client{Timeout: time.Microsecond} |
21
|
|
|
sut := scan.NewScanner( |
22
|
|
|
client, |
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
|
|
|
client := &http.Client{Timeout: time.Second} |
45
|
|
|
sut := scan.NewScanner( |
46
|
|
|
client, |
47
|
|
|
prod, |
48
|
|
|
producer.NewReProducer(prod), |
49
|
|
|
logger, |
50
|
|
|
) |
51
|
|
|
|
52
|
|
|
testServer, serverAssertion := test.NewServerWithAssertion( |
53
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}), |
54
|
|
|
) |
55
|
|
|
defer testServer.Close() |
56
|
|
|
|
57
|
|
|
results := sut.Scan(test.MustParseUrl(t, testServer.URL), 10) |
58
|
|
|
|
59
|
|
|
for r := range results { |
60
|
|
|
t.Fatalf("No results expected, got %s", r.Target.Path) |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
assert.Contains(t, loggerBuffer.String(), "failed to build request") |
64
|
|
|
assert.Contains(t, loggerBuffer.String(), "invalid method") |
65
|
|
|
assert.Equal(t, 0, serverAssertion.Len()) |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
func TestScannerWillNotRedirectIfStatusCodeIsInvalid(t *testing.T) { |
69
|
|
|
logger, loggerBuffer := test.NewLogger() |
70
|
|
|
|
71
|
|
|
prod := producer.NewDictionaryProducer( |
72
|
|
|
[]string{http.MethodGet}, |
73
|
|
|
[]string{"/home"}, |
74
|
|
|
3, |
75
|
|
|
) |
76
|
|
|
|
77
|
|
|
client := &http.Client{Timeout: time.Second} |
78
|
|
|
sut := scan.NewScanner( |
79
|
|
|
client, |
80
|
|
|
prod, |
81
|
|
|
producer.NewReProducer(prod), |
82
|
|
|
logger, |
83
|
|
|
) |
84
|
|
|
|
85
|
|
|
testServer, serverAssertion := test.NewServerWithAssertion( |
86
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
87
|
|
|
w.Header().Add("location", "/potato") |
88
|
|
|
if r.URL.Path == "/home" { |
89
|
|
|
w.WriteHeader(http.StatusOK) |
90
|
|
|
return |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
w.WriteHeader(http.StatusNotFound) |
94
|
|
|
}), |
95
|
|
|
) |
96
|
|
|
defer testServer.Close() |
97
|
|
|
|
98
|
|
|
results := make([]scan.Result, 0, 2) |
99
|
|
|
resultsChannel := sut.Scan(test.MustParseUrl(t, testServer.URL), 10) |
100
|
|
|
|
101
|
|
|
for r := range resultsChannel { |
102
|
|
|
results = append(results, r) |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
expectedsResults := []scan.Result{ |
106
|
|
|
{ |
107
|
|
|
Target: scan.Target{Path: "/home", Method: http.MethodGet, Depth: 3}, |
108
|
|
|
StatusCode: http.StatusOK, |
109
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/home"), |
110
|
|
|
}, |
111
|
|
|
{ |
112
|
|
|
Target: scan.Target{Path: "/home/home", Method: http.MethodGet, Depth: 2}, |
113
|
|
|
StatusCode: http.StatusNotFound, |
114
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/home/home"), |
115
|
|
|
}, |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
assert.Equal(t, expectedsResults, results) |
119
|
|
|
|
120
|
|
|
assert.Contains(t, loggerBuffer.String(), "/home") |
121
|
|
|
assert.Contains(t, loggerBuffer.String(), "/home/home") |
122
|
|
|
assert.Equal(t, 2, serverAssertion.Len()) |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
func TestScannerWillChangeMethodForRedirect(t *testing.T) { |
126
|
|
|
logger, loggerBuffer := test.NewLogger() |
127
|
|
|
|
128
|
|
|
prod := producer.NewDictionaryProducer( |
129
|
|
|
[]string{http.MethodPatch}, |
130
|
|
|
[]string{"/home"}, |
131
|
|
|
3, |
132
|
|
|
) |
133
|
|
|
|
134
|
|
|
testServer, serverAssertion := test.NewServerWithAssertion( |
135
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
136
|
|
|
if r.URL.Path == "/home" { |
137
|
|
|
http.Redirect(w, r, "/potato", http.StatusMovedPermanently) |
138
|
|
|
return |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
w.WriteHeader(http.StatusNotFound) |
142
|
|
|
}), |
143
|
|
|
) |
144
|
|
|
defer testServer.Close() |
145
|
|
|
|
146
|
|
|
c, err := client.NewClientFromConfig( |
147
|
|
|
1000, |
148
|
|
|
nil, |
149
|
|
|
"", |
150
|
|
|
false, |
151
|
|
|
nil, |
152
|
|
|
nil, |
153
|
|
|
test.MustParseUrl(t, testServer.URL), |
154
|
|
|
) |
155
|
|
|
|
156
|
|
|
assert.NoError(t, err) |
157
|
|
|
sut := scan.NewScanner( |
158
|
|
|
c, |
159
|
|
|
prod, |
160
|
|
|
producer.NewReProducer(prod), |
161
|
|
|
logger, |
162
|
|
|
) |
163
|
|
|
|
164
|
|
|
results := make([]scan.Result, 0, 4) |
165
|
|
|
resultsChannel := sut.Scan(test.MustParseUrl(t, testServer.URL), 1) |
166
|
|
|
|
167
|
|
|
for r := range resultsChannel { |
168
|
|
|
results = append(results, r) |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
expectedResults := []scan.Result{ |
172
|
|
|
{ |
173
|
|
|
Target: scan.Target{Path: "/home", Method: http.MethodPatch, Depth: 3}, |
174
|
|
|
StatusCode: http.StatusMovedPermanently, |
175
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/home"), |
176
|
|
|
}, |
177
|
|
|
{ |
178
|
|
|
Target: scan.Target{Path: "/potato", Method: http.MethodGet, Depth: 2}, |
179
|
|
|
StatusCode: http.StatusNotFound, |
180
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/potato"), |
181
|
|
|
}, |
182
|
|
|
{ |
183
|
|
|
Target: scan.Target{Path: "/home/home", Method: http.MethodPatch, Depth: 2}, |
184
|
|
|
StatusCode: http.StatusNotFound, |
185
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/home/home"), |
186
|
|
|
}, |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
assert.Equal(t, expectedResults, results) |
190
|
|
|
|
191
|
|
|
loggerBufferAsString := loggerBuffer.String() |
192
|
|
|
assert.Contains(t, loggerBufferAsString, "/home") |
193
|
|
|
assert.Contains(t, loggerBufferAsString, "/potato") |
194
|
|
|
assert.Contains(t, loggerBufferAsString, "/home/home") |
195
|
|
|
assert.Equal(t, 3, serverAssertion.Len()) |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
func TestScannerWillSkipRedirectWhenLocationHostIsDifferent(t *testing.T) { |
199
|
|
|
logger, loggerBuffer := test.NewLogger() |
200
|
|
|
|
201
|
|
|
prod := producer.NewDictionaryProducer( |
202
|
|
|
[]string{http.MethodPatch}, |
203
|
|
|
[]string{"/home"}, |
204
|
|
|
3, |
205
|
|
|
) |
206
|
|
|
|
207
|
|
|
testServer, serverAssertion := test.NewServerWithAssertion( |
208
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
209
|
|
|
if r.URL.Path == "/home" { |
210
|
|
|
http.Redirect(w, r, "http://gibberish/potato", http.StatusMovedPermanently) |
211
|
|
|
return |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
w.WriteHeader(http.StatusNotFound) |
215
|
|
|
}), |
216
|
|
|
) |
217
|
|
|
defer testServer.Close() |
218
|
|
|
|
219
|
|
|
c, err := client.NewClientFromConfig( |
220
|
|
|
1000, |
221
|
|
|
nil, |
222
|
|
|
"", |
223
|
|
|
false, |
224
|
|
|
nil, |
225
|
|
|
nil, |
226
|
|
|
test.MustParseUrl(t, testServer.URL), |
227
|
|
|
) |
228
|
|
|
|
229
|
|
|
assert.NoError(t, err) |
230
|
|
|
sut := scan.NewScanner( |
231
|
|
|
c, |
232
|
|
|
prod, |
233
|
|
|
producer.NewReProducer(prod), |
234
|
|
|
logger, |
235
|
|
|
) |
236
|
|
|
|
237
|
|
|
results := make([]scan.Result, 0, 4) |
238
|
|
|
resultsChannel := sut.Scan(test.MustParseUrl(t, testServer.URL), 1) |
239
|
|
|
|
240
|
|
|
for r := range resultsChannel { |
241
|
|
|
results = append(results, r) |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
expectedResults := []scan.Result{ |
245
|
|
|
{ |
246
|
|
|
Target: scan.Target{Path: "/home", Method: http.MethodPatch, Depth: 3}, |
247
|
|
|
StatusCode: http.StatusMovedPermanently, |
248
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/home"), |
249
|
|
|
}, |
250
|
|
|
{ |
251
|
|
|
Target: scan.Target{Path: "/home/home", Method: http.MethodPatch, Depth: 2}, |
252
|
|
|
StatusCode: http.StatusNotFound, |
253
|
|
|
URL: *test.MustParseUrl(t, testServer.URL+"/home/home"), |
254
|
|
|
}, |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
assert.Equal(t, expectedResults, results) |
258
|
|
|
|
259
|
|
|
loggerBufferAsString := loggerBuffer.String() |
260
|
|
|
assert.Contains(t, loggerBufferAsString, "/home") |
261
|
|
|
assert.Contains(t, loggerBufferAsString, "/home/home") |
262
|
|
|
assert.Contains(t, loggerBufferAsString, "skipping redirect, pointing to a different host") |
263
|
|
|
assert.Equal(t, 2, serverAssertion.Len()) |
264
|
|
|
} |
265
|
|
|
|