File: run-test.pl

package info (click to toggle)
libapache-mod-security 1.8.7-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,132 kB
  • ctags: 664
  • sloc: ansic: 7,641; perl: 199; makefile: 59; sh: 44
file content (189 lines) | stat: -rwxr-xr-x 4,165 bytes parent folder | download
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
#!/usr/bin/perl -w

use IO::Socket;

$USAGE = "Usage: $0 host[:port] [-d] testfile1, testfile2, ...\n";
$USERAGENT = "mod_security regression test utility";

$debug = 0;

$count = 0;

sub perform_test {
    my $filename = shift(@_);

    if (!open(FILE, $filename)) {
        print("Could not open file $filename.\n");
        return;
    }

    $socket = IO::Socket::INET->new(Proto => 'tcp', PeerAddr => $server, PeerPort => $port, Timeout => 10);

    if(!$socket) {
        # this is treated as a serious problem and we
        # die if we cannot connect
        die("Could not connect to $server:$port");
    }

    $socket->autoflush(1);

    $mode = 1;
    $expected_status = 500;
    $test_name_found = 0;
    $test_name = '';

    $send_host_header = 1;
    $send_useragent_header = 1;
    $send_connection_header = 1;

    LINE: while(<FILE>) {

        $send_line = 1;
    
        if ($mode == 1) {

            # discard comments
            if (/^#/) {

                if ($test_name_found == 0) {
                    chomp;
                    $test_name = substr($_, 2);
                    $test_name_found = 1;

                    if ($test_name =~ /([0-9]{3})/) {
                        $expected_status = $1;
                    }

                    print "Test \"${test_name}\": ";
                    if ($debug) { print "\n"; }
                }
                next LINE;
            }

            $mode = 2;
    
            # execution flow continues as
            # mode is 2 now
        }        

        if ($mode == 2) {

            # check for the "Host" header
            if (/^(Host: )(.*)$/i) {
                $send_host_header = 0;
                if ($2 eq "-") {
                    $send_line = 0;
                }
            }

            # check for the "User-Agent" header
            if (/^(User-Agent: )(.*)$/i) {
                $send_useragent_header = 0;
                if ($2 eq "-") {
                    $send_line = 0;
                }
            }

            # check for the "Connection" header
            if (/^(Connection: )(.*)$/i) {
                $send_connection_header = 0;
                if ($2 eq "-") {
                    $send_line = 0;
                }
            }

            if (/^$/) {
                # empty line detected, the body follows
                $mode = 3;

                # send additional headers
                if ($send_host_header) {
                    print $socket "Host: $server:$port\n";
                    if ($debug) { print "> Host: $server:$port\n"; }
                }

                if ($send_useragent_header) {
                    print $socket "User-Agent: $USERAGENT\n";
                    if ($debug) { print "> User-Agent: $USERAGENT\n"; }
                }

                if  ($send_connection_header) {
                    print $socket "Connection: Close\n";
                    if ($debug) { print "> Connection: Close\n"; }
                }
            }

            if ($send_line) {
                print $socket $_;
                if ($debug) { print "> $_"; }
            }

            next LINE;
        }

        if ($mode == 3) {
            print $socket $_;
            if ($debug) { print "> $_"; }
        }   
    }

    close(FILE);

    $status = 0;
    while (<$socket>) {
        
        if (($status == 0) && (/^HTTP\/[0-9]\.[0-9] ([0-9]+).+$/)) {
            $status = $1;
        }

        if ($debug) { print "< $_"; }
    }

    if ($status == 0) {
        print "Failed (status not available)\n";
    }

    if ($status == $expected_status) {
        print "OK\n";
    } else {
        print "Failed (status = $status)\n";
    }

}


# -- main -------------------------------------------

if(!@ARGV) {
    print $USAGE;
    exit;
}

if ($#ARGV < 1) {
    print $USAGE;
    exit;
}

$_ = shift(@ARGV);

if (/(.+):(.+)/) {
    $server = $1;
    $port = $2;
} else {
    $server = $_;
    $port = 80;
}

foreach $filename (@ARGV) {
    if ($filename =~ /^-d$/) {
        $debug = 1;
    } else {
        perform_test($filename);
        $count++;
    }
}

if ($count == 0) {
    print $USAGE;
}