Debian Codegraph @

tests/t/lib/ProFTPD/Tests/HTTP.pm

raw
1 package ProFTPD::Tests::HTTP;
2
3 use lib qw(t/lib);
4 use base qw(ProFTPD::TestSuite::Child);
5 use strict;
6
7 use File::Spec;
8 use IO::Handle;
9
10 use ProFTPD::TestSuite::FTP;
11 use ProFTPD::TestSuite::Utils qw(:auth :config :running :test :testsuite);
12
13 $| = 1;
14
15 my $order = 0;
16
17 my $TESTS = {
18 http_connect => {
19 order => ++$order,
20 test_class => [qw(bug forking)],
21 },
22
23 http_delete => {
24 order => ++$order,
25 test_class => [qw(bug forking)],
26 },
27
28 http_get => {
29 order => ++$order,
30 test_class => [qw(bug forking)],
31 },
32
33 http_head => {
34 order => ++$order,
35 test_class => [qw(bug forking)],
36 },
37
38 http_options => {
39 order => ++$order,
40 test_class => [qw(bug forking)],
41 },
42
43 http_patch => {
44 order => ++$order,
45 test_class => [qw(bug forking)],
46 },
47
48 http_post => {
49 order => ++$order,
50 test_class => [qw(bug forking)],
51 },
52
53 http_put => {
54 order => ++$order,
55 test_class => [qw(bug forking)],
56 },
57
58 };
59
60 sub new {
61 return shift()->SUPER::new(@_);
62 }
63
64 sub list_tests {
65 # Check for the required Perl modules:
66 #
67 # HTTP-Request
68 # LWP-UserAgent
69
70 my $required = [qw(
71 HTTP::Request
72 LWP::UserAgent
73 )];
74
75 foreach my $req (@$required) {
76 eval "use $req";
77 if ($@) {
78 print STDERR "\nWARNING:\n + Module '$req' not found, skipping all tests\n";
79
80 if ($ENV{TEST_VERBOSE}) {
81 print STDERR "Unable to load $req: $@\n";
82 }
83
84 return qw(testsuite_empty_test);
85 }
86 }
87
88 return testsuite_get_runnable_tests($TESTS);
89 }
90
91 sub test_http_req {
92 my $self = shift;
93 my $tmpdir = shift;
94 my $req = shift;
95
96 my $config_file = "$tmpdir/http.conf";
97 my $pid_file = File::Spec->rel2abs("$tmpdir/http.pid");
98 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/http.scoreboard");
99
100 my $log_file = test_get_logfile();
101
102 my $auth_user_file = File::Spec->rel2abs("$tmpdir/http.passwd");
103 my $auth_group_file = File::Spec->rel2abs("$tmpdir/http.group");
104
105 my $user = 'proftpd';
106 my $passwd = 'test';
107 my $group = 'ftpd';
108 my $home_dir = File::Spec->rel2abs($tmpdir);
109 my $uid = 500;
110 my $gid = 500;
111
112 # Make sure that, if we're running as root, that the home directory has
113 # permissions/privs set for the account we create
114 if ($< == 0) {
115 unless (chmod(0755, $home_dir)) {
116 die("Can't set perms on $home_dir to 0755: $!");
117 }
118
119 unless (chown($uid, $gid, $home_dir)) {
120 die("Can't set owner of $home_dir to $uid/$gid: $!");
121 }
122 }
123
124 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
125 '/bin/bash');
126 auth_group_write($auth_group_file, $group, $gid, $user);
127
128 my $config = {
129 PidFile => $pid_file,
130 ScoreboardFile => $scoreboard_file,
131 SystemLog => $log_file,
132
133 AuthUserFile => $auth_user_file,
134 AuthGroupFile => $auth_group_file,
135 AuthOrder => 'mod_auth_file.c',
136
137 IfModules => {
138 'mod_delay.c' => {
139 DelayEngine => 'off',
140 },
141 },
142 };
143
144 my ($port, $config_user, $config_group) = config_write($config_file, $config);
145
146 # Open pipes, for use between the parent and child processes. Specifically,
147 # the child will indicate when it's done with its test by writing a message
148 # to the parent.
149 my ($rfh, $wfh);
150 unless (pipe($rfh, $wfh)) {
151 die("Can't open pipe: $!");
152 }
153
154 require LWP::UserAgent;
155 require HTTP::Request;
156
157 my $ex;
158
159 # Fork child
160 $self->handle_sigchld();
161 defined(my $pid = fork()) or die("Can't fork: $!");
162 if ($pid) {
163 eval {
164 sleep(1);
165
166 # To reproduce Bug#4143, we only need to connect to the server,
167 # then issue an HTTP request.
168
169 my $client = LWP::UserAgent->new(
170 keep_alive => 1,
171 timeout => 10,
172 );
173 $client->default_header('Host' => "127.0.0.1:$port");
174
175 my $req = HTTP::Request->new($req => "http://127.0.0.1:$port/path/to/some/file");
176 my $resp = $client->request($req);
177
178 if ($ENV{TEST_VERBOSE}) {
179 print STDERR "# response: ", $resp->as_string, "\n";
180 }
181
182 my $conn_count = $client->conn_cache->get_connections();
183
184 $self->assert($conn_count == 0,
185 test_msg("Expected connection cache count 0, got $conn_count"));
186 };
187
188 if ($@) {
189 $ex = $@;
190 }
191
192 $wfh->print("done\n");
193 $wfh->flush();
194
195 } else {
196 eval { server_wait($config_file, $rfh) };
197 if ($@) {
198 warn($@);
199 exit 1;
200 }
201
202 exit 0;
203 }
204
205 # Stop server
206 server_stop($pid_file);
207
208 $self->assert_child_ok($pid);
209
210 if ($ex) {
211 test_append_logfile($log_file, $ex);
212 unlink($log_file);
213
214 die($ex);
215 }
216
217 unlink($log_file);
218 }
219
220 sub http_connect {
221 my $self = shift;
222 my $tmpdir = $self->{tmpdir};
223 test_http_req($self, $tmpdir, 'CONNECT');
224 }
225
226 sub http_delete {
227 my $self = shift;
228 my $tmpdir = $self->{tmpdir};
229 test_http_req($self, $tmpdir, 'DELETE');
230 }
231
232 sub http_get {
233 my $self = shift;
234 my $tmpdir = $self->{tmpdir};
235 test_http_req($self, $tmpdir, 'GET');
236 }
237
238 sub http_head {
239 my $self = shift;
240 my $tmpdir = $self->{tmpdir};
241 test_http_req($self, $tmpdir, 'HEAD');
242 }
243
244 sub http_options {
245 my $self = shift;
246 my $tmpdir = $self->{tmpdir};
247 test_http_req($self, $tmpdir, 'OPTIONS');
248 }
249
250 sub http_patch {
251 my $self = shift;
252 my $tmpdir = $self->{tmpdir};
253 test_http_req($self, $tmpdir, 'PATCH');
254 }
255
256 sub http_post {
257 my $self = shift;
258 my $tmpdir = $self->{tmpdir};
259 test_http_req($self, $tmpdir, 'POST');
260 }
261
262 sub http_put {
263 my $self = shift;
264 my $tmpdir = $self->{tmpdir};
265 test_http_req($self, $tmpdir, 'PUT');
266 }
267
268 1;