Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Commands/PASV.pm

raw
1 package ProFTPD::Tests::Commands::PASV;
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 pasv_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 pasv_fails_login_required => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 pasv_during_xfer_bug3487 => {
29 order => ++$order,
30 test_class => [qw(bug forking)],
31 },
32
33 };
34
35 sub new {
36 return shift()->SUPER::new(@_);
37 }
38
39 sub list_tests {
40 return testsuite_get_runnable_tests($TESTS);
41 }
42
43 sub pasv_ok {
44 my $self = shift;
45 my $tmpdir = $self->{tmpdir};
46
47 my $config_file = "$tmpdir/cmds.conf";
48 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
49 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
50
51 my $log_file = test_get_logfile();
52
53 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
54 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
55
56 my $user = 'proftpd';
57 my $passwd = 'test';
58 my $home_dir = File::Spec->rel2abs($tmpdir);
59 my $uid = 500;
60 my $gid = 500;
61
62 # Make sure that, if we're running as root, that the home directory has
63 # permissions/privs set for the account we create
64 if ($< == 0) {
65 unless (chmod(0755, $home_dir)) {
66 die("Can't set perms on $home_dir to 0755: $!");
67 }
68
69 unless (chown($uid, $gid, $home_dir)) {
70 die("Can't set owner of $home_dir to $uid/$gid: $!");
71 }
72 }
73
74 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
75 '/bin/bash');
76 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
77
78 my $config = {
79 PidFile => $pid_file,
80 ScoreboardFile => $scoreboard_file,
81 SystemLog => $log_file,
82
83 AuthUserFile => $auth_user_file,
84 AuthGroupFile => $auth_group_file,
85 AuthOrder => 'mod_auth_file.c',
86
87 IfModules => {
88 'mod_delay.c' => {
89 DelayEngine => 'off',
90 },
91 },
92 };
93
94 my ($port, $config_user, $config_group) = config_write($config_file, $config);
95
96 # Open pipes, for use between the parent and child processes. Specifically,
97 # the child will indicate when it's done with its test by writing a message
98 # to the parent.
99 my ($rfh, $wfh);
100 unless (pipe($rfh, $wfh)) {
101 die("Can't open pipe: $!");
102 }
103
104 my $ex;
105
106 # Fork child
107 $self->handle_sigchld();
108 defined(my $pid = fork()) or die("Can't fork: $!");
109 if ($pid) {
110 eval {
111 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
112 $client->login($user, $passwd);;
113
114 my ($resp_code, $resp_msg);
115 ($resp_code, $resp_msg) = $client->pasv();
116
117 my $expected;
118
119 $expected = 227;
120 $self->assert($expected == $resp_code,
121 test_msg("Expected $expected, got $resp_code"));
122
123 $expected = '^Entering Passive Mode \(\d+,\d+,\d+,\d+,\d+,\d+\)';
124 $self->assert(qr/$expected/, $resp_msg,
125 test_msg("Expected '$expected', got '$resp_msg'"));
126 };
127
128 if ($@) {
129 $ex = $@;
130 }
131
132 $wfh->print("done\n");
133 $wfh->flush();
134
135 } else {
136 eval { server_wait($config_file, $rfh) };
137 if ($@) {
138 warn($@);
139 exit 1;
140 }
141
142 exit 0;
143 }
144
145 # Stop server
146 server_stop($pid_file);
147
148 $self->assert_child_ok($pid);
149
150 if ($ex) {
151 test_append_logfile($log_file, $ex);
152 unlink($log_file);
153
154 die($ex);
155 }
156
157 unlink($log_file);
158 }
159
160 sub pasv_fails_login_required {
161 my $self = shift;
162 my $tmpdir = $self->{tmpdir};
163
164 my $config_file = "$tmpdir/cmds.conf";
165 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
166 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
167
168 my $log_file = test_get_logfile();
169
170 my $config = {
171 PidFile => $pid_file,
172 ScoreboardFile => $scoreboard_file,
173 SystemLog => $log_file,
174
175 IfModules => {
176 'mod_delay.c' => {
177 DelayEngine => 'off',
178 },
179 },
180 };
181
182 my ($port, $config_user, $config_group) = config_write($config_file, $config);
183
184 # Open pipes, for use between the parent and child processes. Specifically,
185 # the child will indicate when it's done with its test by writing a message
186 # to the parent.
187 my ($rfh, $wfh);
188 unless (pipe($rfh, $wfh)) {
189 die("Can't open pipe: $!");
190 }
191
192 my $ex;
193
194 # Fork child
195 $self->handle_sigchld();
196 defined(my $pid = fork()) or die("Can't fork: $!");
197 if ($pid) {
198 eval {
199 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
200
201 my ($resp_code, $resp_msg);
202 eval { ($resp_code, $resp_msg) = $client->pasv() };
203 unless ($@) {
204 die("PASV succeeded unexpectedly");
205
206 } else {
207 $resp_code = $client->response_code();
208 $resp_msg = $client->response_msg();
209 }
210
211 my $expected;
212
213 $expected = 530;
214 $self->assert($expected == $resp_code,
215 test_msg("Expected $expected, got $resp_code"));
216
217 $expected = "Please login with USER and PASS";
218 $self->assert($expected eq $resp_msg,
219 test_msg("Expected '$expected', got '$resp_msg'"));
220 };
221
222 if ($@) {
223 $ex = $@;
224 }
225
226 $wfh->print("done\n");
227 $wfh->flush();
228
229 } else {
230 eval { server_wait($config_file, $rfh) };
231 if ($@) {
232 warn($@);
233 exit 1;
234 }
235
236 exit 0;
237 }
238
239 # Stop server
240 server_stop($pid_file);
241
242 $self->assert_child_ok($pid);
243
244 if ($ex) {
245 test_append_logfile($log_file, $ex);
246 unlink($log_file);
247
248 die($ex);
249 }
250
251 unlink($log_file);
252 }
253
254 sub pasv_during_xfer_bug3487 {
255 my $self = shift;
256 my $tmpdir = $self->{tmpdir};
257
258 my $config_file = "$tmpdir/cmds.conf";
259 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
260 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
261
262 my $log_file = test_get_logfile();
263
264 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
265 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
266
267 my $user = 'proftpd';
268 my $passwd = 'test';
269 my $home_dir = File::Spec->rel2abs($tmpdir);
270 my $uid = 500;
271 my $gid = 500;
272
273 # Make sure that, if we're running as root, that the home directory has
274 # permissions/privs set for the account we create
275 if ($< == 0) {
276 unless (chmod(0755, $home_dir)) {
277 die("Can't set perms on $home_dir to 0755: $!");
278 }
279
280 unless (chown($uid, $gid, $home_dir)) {
281 die("Can't set owner of $home_dir to $uid/$gid: $!");
282 }
283 }
284
285 # For this test, we need to create many files in the home directory
286 my $test_file_prefix = File::Spec->rel2abs("$tmpdir/test_");
287
288 my $count = 7000;
289 for (my $i = 1; $i <= $count; $i++) {
290 my $test_file = 'test_' . sprintf("%07s", $i);
291 my $test_path = "$home_dir/$test_file";
292
293 if (open(my $fh, "> $test_path")) {
294 close($fh);
295
296 } else {
297 die("Can't open $test_path: $!");
298 }
299 }
300
301 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
302 '/bin/bash');
303 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
304
305 my $config = {
306 PidFile => $pid_file,
307 ScoreboardFile => $scoreboard_file,
308 SystemLog => $log_file,
309
310 AuthUserFile => $auth_user_file,
311 AuthGroupFile => $auth_group_file,
312 AuthOrder => 'mod_auth_file.c',
313
314 IfModules => {
315 'mod_delay.c' => {
316 DelayEngine => 'off',
317 },
318 },
319 };
320
321 my ($port, $config_user, $config_group) = config_write($config_file, $config);
322
323 # Open pipes, for use between the parent and child processes. Specifically,
324 # the child will indicate when it's done with its test by writing a message
325 # to the parent.
326 my ($rfh, $wfh);
327 unless (pipe($rfh, $wfh)) {
328 die("Can't open pipe: $!");
329 }
330
331 my $ex;
332
333 # Fork child
334 $self->handle_sigchld();
335 defined(my $pid = fork()) or die("Can't fork: $!");
336 if ($pid) {
337 eval {
338 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
339 $client->login($user, $passwd);
340
341 my $conn = $client->list_raw();
342 unless ($conn) {
343 die("Failed to LIST: " . $client->response_code() . " " .
344 $client->response_msg());
345 }
346
347 my ($buf, $tmp);
348 $conn->read($tmp, 8, 30);
349 $buf .= $tmp;
350
351 eval { $client->pasv() };
352 unless ($@) {
353 die("PASV command succeeded unexpectedly");
354 }
355
356 my $resp_code = $client->response_code();
357 my $resp_msg = $client->response_msg();
358
359 my $expected;
360
361 $expected = 450;
362 $self->assert($expected == $resp_code,
363 test_msg("Expected $expected, got $resp_code"));
364
365 $expected = 'PASV: data transfer in progress';
366 $self->assert($expected eq $resp_msg,
367 test_msg("Expected '$expected', got '$resp_msg'"));
368
369 # Finish reading the data
370 while ($conn->read($tmp, 32768, 30)) {
371 $buf .= $tmp;
372 }
373 $conn->close();
374
375 $resp_code = $client->response_code();
376 $resp_msg = $client->response_msg();
377
378 $expected = 226;
379 $self->assert($expected == $resp_code,
380 test_msg("Expected $expected, got $resp_code"));
381
382 $expected = 'Transfer complete';
383 $self->assert($expected eq $resp_msg,
384 test_msg("Expected '$expected', got '$resp_msg'"));
385
386 };
387
388 if ($@) {
389 $ex = $@;
390 }
391
392 $wfh->print("done\n");
393 $wfh->flush();
394
395 } else {
396 eval { server_wait($config_file, $rfh) };
397 if ($@) {
398 warn($@);
399 exit 1;
400 }
401
402 exit 0;
403 }
404
405 # Stop server
406 server_stop($pid_file);
407
408 $self->assert_child_ok($pid);
409
410 if ($ex) {
411 test_append_logfile($log_file, $ex);
412 unlink($log_file);
413
414 die($ex);
415 }
416
417 unlink($log_file);
418 }
419
420 1;