forked from arkime/arkime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.pl
executable file
·463 lines (405 loc) · 18.7 KB
/
tests.pl
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#!/usr/bin/perl -I.
use strict;
use HTTP::Request::Common;
use LWP::UserAgent;
use JSON;
use Data::Dumper;
use Test::More;
use Test::Differences;
use Cwd;
use URI::Escape;
use TAP::Harness;
use MolochTest;
use Socket6 qw(AF_INET6 inet_pton);
$main::userAgent = LWP::UserAgent->new(timeout => 20);
my $ELASTICSEARCH = $ENV{ELASTICSEARCH} = "http://127.0.0.1:9200";
#my $ELASTICSEARCH = $ENV{ELASTICSEARCH} = "http://elastic:changeme\@127.0.0.1:9200";
$ENV{'PERL5LIB'} = getcwd();
$ENV{'TZ'} = 'US/Eastern';
my $INSECURE = "";
################################################################################
sub doGeo {
if (! -f "ipv4-address-space.csv") {
system("wget https://s3.amazonaws.com/files.molo.ch/testing/ipv4-address-space.csv");
}
if (! -f "oui.txt") {
system("wget https://s3.amazonaws.com/files.molo.ch/testing/oui.txt");
}
if (! -f "GeoLite2-Country.mmdb") {
system("wget https://s3.amazonaws.com/files.molo.ch/testing/GeoLite2-Country.mmdb");
}
if (! -f "GeoLite2-ASN.mmdb") {
system("wget https://s3.amazonaws.com/files.molo.ch/testing/GeoLite2-ASN.mmdb");
}
if (! -f "plugins/test.so" || (stat('../capture/moloch.h'))[9] > (stat('plugins/test.so'))[9]) {
system("cd plugins ; make");
}
}
################################################################################
sub doFuzz2Pcap {
my @files = @ARGV;
foreach my $file (@files) {
print "$file\n";;
open my $in, '<', "$file" or die "error opening $file: $!";
open my $out, '>', "$file.pcap" or die "error opening $file.pcap: $!";
binmode($in);
binmode($out);
my $buf;
read($in, $buf, 1000000);
my $len = length($buf);
syswrite($out, pack('H*', "d4c3b2a1020004000000000000000000ffff000001000000"));
syswrite($out, pack('H*VV', "1234567800000000", $len, $len));
syswrite($out, $buf);
close($in);
close($out);
}
}
################################################################################
sub sortObj {
my ($parentkey,$obj) = @_;
for my $key (keys %{$obj}) {
my $r = ref $obj->{$key};
if ($r eq "HASH") {
sortObj($key, $obj->{$key});
} elsif ($r eq "ARRAY") {
next if (scalar (@{$obj->{$key}}) < 2);
next if ($key =~ /(packetPos|packetLen|cert)/);
if ("$parentkey.$key" =~ /vlan.id|http.statuscode|icmp.type|icmp.code/) {
my @tmp = sort { $a <=> $b } (@{$obj->{$key}});
$obj->{$key} = \@tmp;
} else {
my @tmp = sort (@{$obj->{$key}});
$obj->{$key} = \@tmp;
}
}
}
}
################################################################################
sub sortJson {
my ($json) = @_;
foreach my $session (@{$json->{sessions3}}) {
sortObj("", $session->{body});
}
return $json;
}
################################################################################
sub doTests {
my @files = @ARGV;
@files = glob ("pcap/*.pcap") if ($#files == -1);
plan tests => scalar @files;
foreach my $filename (@files) {
$filename = substr($filename, 0, -5) if ($filename =~ /\.pcap$/);
die "Missing $filename.test" if (! -f "$filename.test");
open my $fh, '<', "$filename.test" or die "error opening $filename.test: $!";
my $savedData = do { local $/; <$fh> };
my $savedJson = sortJson(from_json($savedData, {relaxed => 1}));
my $cmd = "../capture/capture --tests -c config.test.ini -n test -r $filename.pcap 2>&1 1>/dev/null | ./tests.pl --fix";
if ($main::valgrind) {
$cmd = "G_SLICE=always-malloc valgrind --leak-check=full --log-file=$filename.val " . $cmd;
}
if ($main::debug) {
print "$cmd\n";
}
my $testData = `$cmd`;
my $testJson;
eval {
$testJson = sortJson(from_json($testData, {relaxed => 1}));
1;
} or do {
my $e = $@;
print "$e\n";
print $testData, "\n";
exit 1;
};
eq_or_diff($testJson, $savedJson, "$filename", { context => 3 });
}
}
################################################################################
sub doFix {
my $data = do { local $/; <> };
my $json;
eval {
$json = from_json($data, {relaxed => 1});
1;
} or do {
my $e = $@;
print "$e\n";
print $data, "\n";
exit 1;
};
fix($json);
$json = to_json($json, {pretty => 1, canonical => 1});
print $json, "\n";
}
################################################################################
sub fix {
my ($json) = @_;
my $json = sortJson($json);
foreach my $session (@{$json->{sessions3}}) {
my $body = $session->{body};
delete $session->{header}->{index}->{_id};
if (exists $body->{rootId}) {
$body->{rootId} = "SET";
}
if (exists $body->{timestamp}) {
$body->{timestamp} = "SET";
}
if (exists $body->{"\@timestamp"}) {
$body->{"\@timestamp"} = "SET";
}
if ($body->{srcIp} =~ /:/) {
$body->{srcIp} = join ":", (unpack("H*", inet_pton(AF_INET6, $body->{srcIp})) =~ m/(....)/g );
}
if ($body->{dstIp} =~ /:/) {
$body->{dstIp} = join ":", (unpack("H*", inet_pton(AF_INET6, $body->{dstIp})) =~ m/(....)/g );
}
if (exists $body->{dns} && exists $body->{dns}->{ip}) {
for (my $i = 0; $i < @{$body->{dns}->{ip}}; $i++) {
if ($body->{dns}->{ip}[$i] =~ /:/) {
$body->{dns}->{ip}[$i] = join ":", (unpack("H*", inet_pton(AF_INET6, $body->{dns}->{ip}[$i])) =~ m/(....)/g );
}
}
}
if (exists $body->{dns} && exists $body->{dns}->{nameserverIp}) {
for (my $i = 0; $i < @{$body->{dns}->{nameserverIp}}; $i++) {
if ($body->{dns}->{nameserverIp}[$i] =~ /:/) {
$body->{dns}->{nameserverIp}[$i] = join ":", (unpack("H*", inet_pton(AF_INET6, $body->{dns}->{nameserverIp}[$i])) =~ m/(....)/g );
}
}
}
if (exists $body->{dns} && exists $body->{dns}->{mailserverIp}) {
for (my $i = 0; $i < @{$body->{dns}->{mailserverIp}}; $i++) {
if ($body->{dns}->{mailserverIp}[$i] =~ /:/) {
$body->{dns}->{mailserverIp}[$i] = join ":", (unpack("H*", inet_pton(AF_INET6, $body->{dns}->{mailserverIp}[$i])) =~ m/(....)/g );
}
}
}
if (exists $body->{cert}) {
for (my $i = 0; $i < @{$body->{cert}}; $i++) {
if ($body->{cert}->[$i]->{remainingDays} < 0) {
$body->{cert}->[$i]->{remainingDays} = -1;
} elsif ($body->{cert}->[$i]->{remainingDays} > 0) {
$body->{cert}->[$i]->{remainingDays} = 1;
}
}
}
}
@{$json->{sessions3}} = sort {
return $a->{body}->{firstPacket} <=> $b->{body}->{firstPacket} if ($a->{body}->{firstPacket} != $b->{body}->{firstPacket});
return $a->{body}->{source}->{ip} <=> $b->{body}->{source}->{ip};
} @{$json->{sessions3}};
}
################################################################################
sub doMake {
foreach my $filename (@ARGV) {
$filename = substr($filename, 0, -5) if ($filename =~ /\.pcap$/);
if ($main::debug) {
print("../capture/capture --tests -c config.test.ini -n test -r $filename.pcap 2>&1 1>/dev/null | ./tests.pl --fix > $filename.test\n");
}
system("../capture/capture --tests -c config.test.ini -n test -r $filename.pcap 2>&1 1>/dev/null | ./tests.pl --fix > $filename.test");
}
}
################################################################################
sub ip2bin {
my @parts = split(/\./, $_[0]);
return pack('H*', sprintf("%02x%02x%02x%02x", $parts[0], $parts[1], $parts[2], $parts[3]));
}
################################################################################
sub doReip {
open my $in, '<', "$ARGV[0]" or die "error opening $ARGV[0]: $!";
open my $out, '>', "$ARGV[0].tmp" or die "error opening $ARGV[0].tmp: $!";
binmode($in);
binmode($out);
my $buf;
read($in, $buf, 1000000);
for (my $i = 1; $i < $#ARGV; $i+=2) {
my $src = ip2bin($ARGV[$i]);
my $dst = ip2bin($ARGV[$i+1]);
$buf =~ s/\Q$src\E/$dst/g;
}
syswrite($out, $buf);
close($in);
close($out);
}
################################################################################
sub doViewer {
my ($cmd) = @_;
die "Must run in tests directory" if (! -f "../db/db.pl");
if ($cmd eq "--viewernostart") {
print "Skipping ES Init and PCAP load\n";
$main::userAgent->post("http://localhost:8123/regressionTests/flushCache");
} elsif ($cmd eq "--viewerstart" || $cmd eq "--viewerhang") {
print "Skipping ES Init and PCAP load\n";
$main::userAgent->post("http://localhost:8123/regressionTests/flushCache");
print ("Starting viewer\n");
if ($main::debug) {
system("cd ../wiseService ; node wiseService.js --regressionTests -c ../tests/config.test.json > /tmp/moloch.wise &");
system("cd ../viewer ; node --trace-warnings multies.js -c ../tests/config.test.ini -n all --debug $INSECURE > /tmp/multies.all &");
waitFor($MolochTest::host, 8200, 1);
system("cd ../viewer ; node --trace-warnings viewer.js -c ../tests/config.test.ini -n test --debug $INSECURE > /tmp/moloch.test &");
system("cd ../viewer ; node --trace-warnings viewer.js -c ../tests/config.test.ini -n test2 --debug $INSECURE > /tmp/moloch.test2 &");
system("cd ../viewer ; node --trace-warnings viewer.js -c ../tests/config.test.ini -n test3 --debug $INSECURE > /tmp/moloch.test3 &");
system("cd ../viewer ; node --trace-warnings viewer.js -c ../tests/config.test.ini -n all --debug $INSECURE > /tmp/moloch.all &");
system("cd ../parliament ; node --trace-warnings parliament.js --regressionTests -c /dev/null --debug > /tmp/moloch.parliament 2>&1 &");
} else {
system("cd ../wiseService ; node wiseService.js --regressionTests -c ../tests/config.test.json > /dev/null &");
system("cd ../viewer ; node multies.js -c ../tests/config.test.ini -n all $INSECURE > /dev/null &");
waitFor($MolochTest::host, 8200, 1);
system("cd ../viewer ; node viewer.js -c ../tests/config.test.ini -n test $INSECURE > /dev/null &");
system("cd ../viewer ; node viewer.js -c ../tests/config.test.ini -n test2 $INSECURE > /dev/null &");
system("cd ../viewer ; node viewer.js -c ../tests/config.test.ini -n test3 $INSECURE > /dev/null &");
system("cd ../viewer ; node viewer.js -c ../tests/config.test.ini -n all $INSECURE > /dev/null &");
system("cd ../parliament ; node parliament.js --regressionTests -c /dev/null > /dev/null 2>&1 &");
}
waitFor($MolochTest::host, 8081, 1);
sleep (10000) if ($cmd eq "--viewerhang");
} else {
print ("Initializing ES\n");
if ($main::debug) {
system("../db/db.pl $INSECURE --prefix tests $ELASTICSEARCH initnoprompt");
system("../db/db.pl $INSECURE --prefix tests2 $ELASTICSEARCH initnoprompt");
} else {
system("../db/db.pl $INSECURE --prefix tests $ELASTICSEARCH initnoprompt 2>&1 1>/dev/null");
system("../db/db.pl $INSECURE --prefix tests2 $ELASTICSEARCH initnoprompt 2>&1 1>/dev/null");
}
print ("Loading tagger\n");
system("../capture/plugins/taggerUpload.pl $ELASTICSEARCH ip ip.tagger1.json iptaggertest1");
system("../capture/plugins/taggerUpload.pl $ELASTICSEARCH host host.tagger1.json hosttaggertest1");
system("../capture/plugins/taggerUpload.pl $ELASTICSEARCH md5 md5.tagger1.json md5taggertest1");
system("../capture/plugins/taggerUpload.pl $ELASTICSEARCH ip ip.tagger2.json iptaggertest2");
system("../capture/plugins/taggerUpload.pl $ELASTICSEARCH host host.tagger2.json hosttaggertest2");
system("../capture/plugins/taggerUpload.pl $ELASTICSEARCH md5 md5.tagger2.json md5taggertest2");
system("../capture/plugins/taggerUpload.pl $ELASTICSEARCH email email.tagger2.json emailtaggertest2");
system("../capture/plugins/taggerUpload.pl $ELASTICSEARCH uri uri.tagger2.json uritaggertest2");
# Start Wise
if ($main::debug) {
system("cd ../wiseService ; node wiseService.js --regressionTests -c ../tests/config.test.json > /tmp/moloch.wise &");
} else {
system("cd ../wiseService ; node wiseService.js --regressionTests -c ../tests/config.test.json > /dev/null &");
}
waitFor($MolochTest::host, 8081, 1);
$main::userAgent->get("$ELASTICSEARCH/_flush");
$main::userAgent->get("$ELASTICSEARCH/_refresh");
print ("Loading PCAP\n");
my $mcmd = "../capture/capture $INSECURE $main::copy -c config.test.ini -n test -R pcap --flush";
if (!$main::debug) {
$mcmd .= " 2>&1 1>/dev/null";
} else {
$mcmd .= " --debug 2>&1 1>/tmp/moloch.capture";
}
if ($main::valgrind) {
$mcmd = "G_SLICE=always-malloc valgrind --leak-check=full --log-file=moloch.val " . $mcmd;
}
print "$mcmd\n" if ($main::debug);
system($mcmd);
die "Loaded" if ($cmd eq "--viewerload");
esCopy("tests_fields", "tests2_fields", "field");
print ("Starting viewer\n");
if ($main::debug) {
system("cd ../viewer ; node --trace-warnings multies.js -c ../tests/config.test.ini -n all --debug > /tmp/multies.all &");
waitFor($MolochTest::host, 8200, 1);
system("cd ../viewer ; node --trace-warnings viewer.js -c ../tests/config.test.ini -n test --debug $INSECURE > /tmp/moloch.test &");
system("cd ../viewer ; node --trace-warnings viewer.js -c ../tests/config.test.ini -n test2 --debug $INSECURE > /tmp/moloch.test2 &");
system("cd ../viewer ; node --trace-warnings viewer.js -c ../tests/config.test.ini -n test3 --debug $INSECURE > /tmp/moloch.test3 &");
system("cd ../viewer ; node --trace-warnings viewer.js -c ../tests/config.test.ini -n all --debug $INSECURE > /tmp/moloch.all &");
system("cd ../parliament ; node --trace-warnings parliament.js --regressionTests -c /dev/null --debug > /tmp/moloch.parliament 2>&1 &");
} else {
system("cd ../viewer ; node multies.js -c ../tests/config.test.ini -n all $INSECURE > /dev/null &");
waitFor($MolochTest::host, 8200, 1);
system("cd ../viewer ; node viewer.js -c ../tests/config.test.ini -n test $INSECURE > /dev/null &");
system("cd ../viewer ; node viewer.js -c ../tests/config.test.ini -n test2 $INSECURE > /dev/null &");
system("cd ../viewer ; node viewer.js -c ../tests/config.test.ini -n test3 $INSECURE > /dev/null &");
system("cd ../viewer ; node viewer.js -c ../tests/config.test.ini -n all $INSECURE > /dev/null &");
system("cd ../parliament ; node parliament.js --regressionTests -c /dev/null > /dev/null 2>&1 &");
}
}
waitFor($MolochTest::host, 8123);
waitFor($MolochTest::host, 8124);
waitFor($MolochTest::host, 8125);
waitFor($MolochTest::host, 8008);
sleep 1;
$main::userAgent->get("$ELASTICSEARCH/_flush");
$main::userAgent->get("$ELASTICSEARCH/_refresh");
sleep 1;
my $harness = TAP::Harness->new();
my @tests = @ARGV;
@tests = glob ("*.t") if ($#tests == -1);
my $parser = $harness->runtests(@tests);
# Cleanup
if ($cmd ne "--viewernostart") {
$main::userAgent->post("http://localhost:8123/regressionTests/shutdown");
$main::userAgent->post("http://localhost:8124/regressionTests/shutdown");
$main::userAgent->post("http://localhost:8125/regressionTests/shutdown");
$main::userAgent->post("http://localhost:8126/regressionTests/shutdown");
$main::userAgent->post("http://localhost:8200/regressionTests/shutdown");
$main::userAgent->post("http://localhost:8081/regressionTests/shutdown");
$main::userAgent->post("http://localhost:8008/regressionTests/shutdown");
}
exit(1) if ( $parser->has_errors );
}
################################################################################
$main::debug = 0;
$main::valgrind = 0;
$main::copy = "";
$main::cmd = "--capture";
while (scalar (@ARGV) > 0) {
if ($ARGV[0] eq "--debug") {
$main::debug = 1;
shift @ARGV;
} elsif ($ARGV[0] eq "--insecure") {
$INSECURE = "--insecure";
shift @ARGV;
} elsif ($ARGV[0] eq "--valgrind") {
$main::valgrind = 1;
shift @ARGV;
} elsif ($ARGV[0] eq "--copy") {
$main::copy = "--copy";
shift @ARGV;
} elsif ($ARGV[0] =~ /^--(viewer|fix|make|capture|viewernostart|viewerstart|viewerhang|viewerload|help|reip|fuzz|fuzz2pcap)$/) {
$main::cmd = $ARGV[0];
shift @ARGV;
} elsif ($ARGV[0] =~ /^--/) {
print "Unknown option $ARGV[0]\n";
$main::cmd = "--help";
last;
} else {
last;
}
}
if ($main::cmd eq "--fix") {
doFix();
} elsif ($main::cmd eq "--make") {
doMake();
} elsif ($main::cmd eq "--reip") {
doReip();
} elsif ($main::cmd eq "--fuzz") {
doGeo();
my $cmd = "ASAN_OPTIONS=fast_unwind_on_malloc=0 G_SLICE=always-malloc ../capture/fuzzloch-capture -max_len=8196 -timeout=5 @ARGV";
print "$cmd\n";
system($cmd);
} elsif ($main::cmd eq "--fuzz2pcap") {
doFuzz2Pcap();
} elsif ($main::cmd eq "--help") {
print "$ARGV[0] [OPTIONS] [COMMAND] <pcap> files\n";
print "Options:\n";
print " --debug Turn on debuggin\n";
print " --valgrind Use valgrind on capture\n";
print "\n";
print "Commands:\n";
print " --help This help\n";
print " --make Create a .test file for each .pcap file on command line\n";
print " --reip file ip newip Create file.tmp, replace ip with newip\n";
print " --viewer viewer tests\n";
print " This will init local ES, import data, start a viewer, run tests\n";
print " --viewerstart Viewer tests without reloading pcap\n";
print " --fuzz [fuzzoptions] Run fuzzloch\n";
print " --fuzz2pcap Convert a fuzzloch crash file into a pcap file\n";
print " [default] [pcap files] Run each .pcap (default pcap/*.pcap) file thru ../capture/capture and compare to .test file\n";
} elsif ($main::cmd =~ "^--viewer") {
doGeo();
setpgrp $$, 0;
doViewer($main::cmd);
} else {
doGeo();
doTests();
}