-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathnetmap.test.js
444 lines (362 loc) · 14 KB
/
netmap.test.js
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import NetMap from './netmap'
test('_checkPort() returns a promise and a result', () => {
expect.assertions(1)
const netmap = new NetMap()
return netmap._checkPort('localhost', 443, {
timeout: 1
}).then(result => {
expect(result).toBeDefined()
})
})
test('_checkPort() returns a host', () => {
expect.assertions(1)
const netmap = new NetMap()
return netmap._checkPort('localhost', 443, {
timeout: 1
}).then(result => {
expect(result.host).toBeDefined()
})
})
test('_checkPort() returns a port', () => {
expect.assertions(1)
const netmap = new NetMap()
return netmap._checkPort('localhost', 443, {
timeout: 1
}).then(result => {
expect(result.port).toBeDefined()
})
})
test('_checkPort() returns a delta', () => {
expect.assertions(1)
const netmap = new NetMap()
return netmap._checkPort('localhost', 443, {
timeout: 1
}).then(result => {
expect(result.delta).toBeDefined()
})
})
test('_checkPort() timeout parameter works', () => {
expect.assertions(2)
const netmap = new NetMap()
return netmap._checkPort('non-existing-host', 443, {
timeout: 10
}).then(result => {
expect(result.delta).toBeGreaterThanOrEqual(10)
expect(result.delta).toBeLessThan(20)
})
})
test('_scan() returns a promise and hosts', () => {
expect.assertions(1)
const netmap = new NetMap({timeout: 1})
return netmap._scan(['localhost'], [443]).then(hosts => {
expect(hosts).toBeDefined()
})
})
test('_scan() results contain hosts and ports', () => {
expect.assertions(2)
const portResults = jest.fn()
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 80, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 443, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 8080, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.2', port: 80, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.2', port: 443, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.2', port: 8080, delta: 1})
const netmap = new NetMap({timeout: 1})
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap._scan(['192.168.1.1', '192.168.1.2'], [80, 443, 8080])
.then(hosts => {
expect(hosts.length).toEqual(2)
expect(hosts[0].ports.length).toEqual(3)
})
})
test('_scan() portCallback() is called', () => {
expect.assertions(1)
const portResults = jest.fn()
portResults.mockReturnValue({host: '192.168.1.1', port: 80, delta: 1})
const netmap = new NetMap({timeout: 1})
const portCallback = jest.fn()
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap._scan(['192.168.1.1', '192.168.1.2'], [80, 443, 8080], {
portCallback: portCallback
}).then(hosts => {
expect(portCallback.mock.calls.length).toEqual(6)
})
})
test('_scan() - portCallback() result parameter has expected values', () => {
expect.assertions(2)
const portResults = jest.fn()
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 80, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 443, delta: 1})
const netmap = new NetMap({timeout: 1})
const portCallback = jest.fn()
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap._scan(['192.168.1.1'], [80, 443], {
portCallback: portCallback
}).then(hosts => {
const desiredResult = {
host: '192.168.1.1',
port: expect.any(Number),
delta: expect.any(Number)
}
expect(portCallback.mock.calls[0][0]).toMatchObject(desiredResult)
expect(portCallback.mock.calls[1][0]).toMatchObject(desiredResult)
})
})
test('_scan() hosts contain ports with port and delta', () => {
expect.assertions(2)
const portResults = jest.fn()
portResults.mockReturnValue({host: '192.168.1.1', port: 80, delta: 1})
const netmap = new NetMap({timeout: 1})
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap._scan(['192.168.1.1'], [80]).then(hosts => {
expect(hosts[0].ports[0].port).toBeDefined()
expect(hosts[0].ports[0].delta).toBeDefined()
})
})
test('tcpScan() returns a promise and a result', () => {
expect.assertions(1)
const netmap = new NetMap({timeout: 1})
return netmap.tcpScan(['localhost'], [443]).then(results => {
expect(results).toBeDefined()
})
})
test('tcpScan() results contain meta, hosts and ports', () => {
expect.assertions(3)
const portResults = jest.fn()
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 80, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 443, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 8080, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.2', port: 80, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.2', port: 443, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.2', port: 8080, delta: 1})
// control port results
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 45000, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.2', port: 45000, delta: 1})
const netmap = new NetMap({timeout: 1})
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap.tcpScan(['192.168.1.1', '192.168.1.2'], [80, 443, 8080], {
controlPorts: []
}).then(results => {
expect(results.meta).toBeDefined()
expect(results.hosts.length).toEqual(2)
expect(results.hosts[0].ports.length).toEqual(3)
})
})
test('tcpScan() meta has default controlPort and controlRatio', () => {
expect.assertions(2)
const portResults = jest.fn()
portResults.mockReturnValue({host: '192.168.1.1', port: 80, delta: 1})
const netmap = new NetMap({timeout: 1})
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap.tcpScan(['192.168.1.1'], [80]).then(results => {
expect(results.meta.controlPort).toEqual(45000)
expect(results.meta.controlRatio).toEqual(0.8)
})
})
test('tcpScan() meta has user defined controlPort and controlRatio', () => {
expect.assertions(2)
const portResults = jest.fn()
portResults.mockReturnValue({host: '192.168.1.1', port: 80, delta: 1})
const netmap = new NetMap({timeout: 1})
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap.tcpScan(['192.168.1.1'], [80], {
controlPort: 10000,
controlRatio: 0.5
}).then(results => {
expect(results.meta.controlPort).toEqual(10000)
expect(results.meta.controlRatio).toEqual(0.5)
})
})
test('tcpScan() hosts contain control value', () => {
expect.assertions(1)
const portResults = jest.fn()
portResults.mockReturnValue({host: '192.168.1.1', port: 80, delta: 1})
const netmap = new NetMap({timeout: 1})
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap.tcpScan(['192.168.1.1'], [80]).then(results => {
expect(results.hosts[0].control).toBeDefined()
})
})
test('tcpScan() hosts contain ports with port, delta and open', () => {
expect.assertions(3)
const portResults = jest.fn()
portResults.mockReturnValue({host: '192.168.1.1', port: 80, delta: 1})
const netmap = new NetMap({timeout: 1})
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap.tcpScan(['192.168.1.1'], [80]).then(results => {
expect(results.hosts[0].ports[0].port).toBeDefined()
expect(results.hosts[0].ports[0].delta).toBeDefined()
expect(results.hosts[0].ports[0].open).toBeDefined()
})
})
test('tcpScan() hosts correctly marks ports as open and closed with default controlRatio', () => {
expect.assertions(3)
const portResults = jest.fn()
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 80, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 443, delta: 6})
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 8080, delta: 10})
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 45000, delta: 10})
const netmap = new NetMap({timeout: 1})
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap.tcpScan(['192.168.1.1'], [80, 443, 8080], {
controlPort: 45000
}).then(results => {
for (let i in results.hosts[0].ports) {
switch (results.hosts[0].ports[i].port) {
case 80:
expect(results.hosts[0].ports[i].open).toEqual(true)
break
case 443:
expect(results.hosts[0].ports[i].open).toEqual(true)
break
case 8080:
expect(results.hosts[0].ports[i].open).toEqual(false)
break
}
}
})
})
test('tcpScan() hosts correctly marks ports as open and closed with user-defined controlRatio', () => {
expect.assertions(3)
const portResults = jest.fn()
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 80, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 443, delta: 6})
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 8080, delta: 10})
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 45000, delta: 10})
const netmap = new NetMap({timeout: 1})
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap.tcpScan(['192.168.1.1'], [80, 443, 8080], {
controlPort: 45000,
controlRatio: 0.5
}).then(results => {
for (let i in results.hosts[0].ports) {
switch (results.hosts[0].ports[i].port) {
case 80:
expect(results.hosts[0].ports[i].open).toEqual(true)
break
case 443:
expect(results.hosts[0].ports[i].open).toEqual(false)
break
case 8080:
expect(results.hosts[0].ports[i].open).toEqual(false)
break
}
}
})
})
test('tcpScan() portCallback() is called', () => {
expect.assertions(1)
const portResults = jest.fn()
portResults.mockReturnValue({host: '192.168.1.1', port: 80, delta: 1})
const netmap = new NetMap({timeout: 1})
const portCallback = jest.fn()
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap.tcpScan(['192.168.1.1'], [80, 443], {
portCallback: portCallback
}).then(results => {
expect(portCallback.mock.calls.length).toEqual(2)
})
})
test('tcpScan() - portCallback() result parameter has expected values', () => {
expect.assertions(2)
const portResults = jest.fn()
portResults.mockReturnValue({host: '192.168.1.1', port: 80, delta: 1})
const netmap = new NetMap({timeout: 1})
const portCallback = jest.fn()
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap.tcpScan(['192.168.1.1'], [80, 443], {
portCallback: portCallback
}).then(results => {
const desiredResult = {
host: '192.168.1.1',
port: expect.any(Number),
delta: expect.any(Number)
}
expect(portCallback.mock.calls[0][0]).toMatchObject(desiredResult)
expect(portCallback.mock.calls[1][0]).toMatchObject(desiredResult)
})
})
test('pingSweep() returns a promise and a result', () => {
expect.assertions(1)
const netmap = new NetMap({timeout: 1})
return netmap.pingSweep(['localhost']).then(results => {
expect(results).toBeDefined()
})
})
test('pingSweep() results contain hosts and meta', () => {
expect.assertions(2)
const portResults = jest.fn()
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 80, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.2', port: 80, delta: 1})
const netmap = new NetMap({timeout: 1})
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap.pingSweep(['192.168.1.1', '192.168.1.2']).then(results => {
expect(results.hosts).toBeDefined()
expect(results.meta).toBeDefined()
})
})
test('pingSweep() meta contains params', () => {
expect.assertions(1)
const portResults = jest.fn()
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 80, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.2', port: 80, delta: 1})
const netmap = new NetMap({timeout: 1})
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap.pingSweep(['192.168.1.1', '192.168.1.2']).then(results => {
const desiredMeta = {
hosts: ['192.168.1.1', '192.168.1.2'],
ports: [45000],
maxConnections: /17|10/,
startTime: expect.any(Number),
endTime: expect.any(Number),
scanDuration: expect.any(Number)
}
expect(results.meta).toMatchObject(desiredMeta)
})
})
test('pingSweep() timeout parameter works', () => {
expect.assertions(2)
const netmap = new NetMap({
timeout: 10
})
return netmap.pingSweep(['non-existing-host'], 443).then(results => {
expect(results.meta.scanDuration).toBeGreaterThanOrEqual(10)
expect(results.meta.scanDuration).toBeLessThan(20)
})
})
test('pingSweep() default port set to 45000', () => {
expect.assertions(1)
const portResults = jest.fn()
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 80, delta: 1})
const netmap = new NetMap({timeout: 1})
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap.pingSweep(['192.168.1.1']).then(results => {
expect(results.meta.ports).toEqual([45000])
})
})
test('pingSweep() results hosts contain expected parameters', () => {
expect.assertions(3)
const portResults = jest.fn()
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 80, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.2', port: 80, delta: 1})
const netmap = new NetMap({timeout: 1})
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap.pingSweep(['192.168.1.1', '192.168.1.2']).then(results => {
expect(results.hosts[0].host).toBeDefined()
expect(results.hosts[0].live).toBeDefined()
expect(results.hosts[0].delta).toBeDefined()
})
})
test('pingSweep() marks hosts that timeout as !live', () => {
expect.assertions(1)
const portResults = jest.fn()
portResults.mockReturnValueOnce({host: '192.168.1.1', port: 80, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.2', port: 80, delta: 1})
portResults.mockReturnValueOnce({host: '192.168.1.3', port: 80, delta: 20})
const netmap = new NetMap({timeout: 20})
netmap._checkPort = _ => Promise.resolve(portResults())
return netmap.pingSweep(['192.168.1.1', '192.168.1.2', '192.168.1.3']).then(results => {
const liveHosts = results.hosts.filter(entry => entry.live)
expect(liveHosts.length).toEqual(2)
})
})