1#!/usr/bin/env perl
2
3system("mkdir -p NEW DIFF");
4
5if(@ARGV != 4) {
6  print "Usage: TESTonce name input output options\n";
7  exit 20;
8}
9
10$name=$ARGV[0];
11$input=$ARGV[1];
12$output=$ARGV[2];
13$options=$ARGV[3];
14
15my $r;
16
17if ($^O eq 'MSWin32') {
18    $r = system "..\\windump -n -t -r $input $options 2>NUL | sed 's/\\r//' | tee NEW/$output | diff $output - >DIFF/$output.diff";
19    # need to do same as below for Cygwin.
20}
21else {
22    # we used to do this as a nice pipeline, but the problem is that $r fails to
23    # to be set properly if the tcpdump core dumps.
24    $r = system "../tcpdump 2>/dev/null -n -t -r $input $options >NEW/$output";
25    if($r != 0) {
26        # this means tcpdump failed.
27        open(OUTPUT, ">>"."NEW/$output") || die "fail to open $output\n";
28        printf OUTPUT "EXIT CODE %08x\n", $r;
29        close(OUTPUT);
30        $r = 0;
31    }
32    if($r == 0) {
33        $r = system "cat NEW/$output | diff $output - >DIFF/$output.diff";
34    }
35    #print sprintf("END: %08x\n", $r);
36}
37
38if($r == 0) {
39  printf "    %-35s: passed\n", $name;
40  unlink "DIFF/$output.diff";
41  exit 0;
42}
43printf "    %-35s: TEST FAILED", $name;
44open FOUT, '>>failure-outputs.txt';
45printf FOUT "Failed test: $name\n\n";
46close FOUT;
47if(-f "DIFF/$output.diff") {
48    system "cat DIFF/$output.diff >> failure-outputs.txt";
49}
50
51if($r == -1) {
52  print " (failed to execute: $!)\n";
53  exit 30;
54}
55
56# this is not working right, $r == 0x8b00 when there is a core dump.
57# clearly, we need some platform specific perl magic to take this apart, so look for "core"
58# too.
59# In particular, on Solaris 10 SPARC an alignment problem results in SIGILL,
60# a core dump and $r set to 0x00008a00 ($? == 138 in the shell).
61if($r & 127 || -f "core") {
62    my $with = ($r & 128) ? 'with' : 'without';
63    if(-f "core") {
64        $with = "with";
65    }
66    printf " (terminated with signal %u, %s coredump)\n", ($r & 127), $with;
67    exit ($r & 128) ? 10 : 20;
68}
69print "\n";
70exit $r >> 8;
71