Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Commands::MODE;
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 mode_stream_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 mode_block_fails => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 mode_compressed_fails => {
29 order => ++$order,
30 test_class => [qw(forking)],
31 },
32
33 mode_other_fails => {
34 order => ++$order,
35 test_class => [qw(forking)],
36 },
37
38 # XXX Add test for mode_deflated_ok if mod_deflate is present
39 };
40
41 sub new {
42 return shift()->SUPER::new(@_);
43 }
44
45 sub list_tests {
46 return testsuite_get_runnable_tests($TESTS);
47 }
48
49 sub mode_stream_ok {
50 my $self = shift;
51 my $tmpdir = $self->{tmpdir};
52
53 my $config_file = "$tmpdir/cmds.conf";
54 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
55 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
56
57 my $log_file = test_get_logfile();
58
59 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
60 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
61
62 my $user = 'proftpd';
63 my $passwd = 'test';
64 my $home_dir = File::Spec->rel2abs($tmpdir);
65 my $uid = 500;
66 my $gid = 500;
67
68 # Make sure that, if we're running as root, that the home directory has
69 # permissions/privs set for the account we create
70 if ($< == 0) {
71 unless (chmod(0755, $home_dir)) {
72 die("Can't set perms on $home_dir to 0755: $!");
73 }
74
75 unless (chown($uid, $gid, $home_dir)) {
76 die("Can't set owner of $home_dir to $uid/$gid: $!");
77 }
78 }
79
80 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
81 '/bin/bash');
82 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
83
84 my $config = {
85 PidFile => $pid_file,
86 ScoreboardFile => $scoreboard_file,
87 SystemLog => $log_file,
88
89 AuthUserFile => $auth_user_file,
90 AuthGroupFile => $auth_group_file,
91 AuthOrder => 'mod_auth_file.c',
92
93 IfModules => {
94 'mod_delay.c' => {
95 DelayEngine => 'off',
96 },
97 },
98 };
99
100 my ($port, $config_user, $config_group) = config_write($config_file, $config);
101
102 # Open pipes, for use between the parent and child processes. Specifically,
103 # the child will indicate when it's done with its test by writing a message
104 # to the parent.
105 my ($rfh, $wfh);
106 unless (pipe($rfh, $wfh)) {
107 die("Can't open pipe: $!");
108 }
109
110 my $ex;
111
112 # Fork child
113 $self->handle_sigchld();
114 defined(my $pid = fork()) or die("Can't fork: $!");
115 if ($pid) {
116 eval {
117 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
118
119 $client->login($user, $passwd);
120
121 my ($resp_code, $resp_msg);
122 ($resp_code, $resp_msg) = $client->mode('stream');
123
124 my $expected;
125
126 $expected = 200;
127 $self->assert($expected == $resp_code,
128 test_msg("Expected $expected, got $resp_code"));
129
130 $expected = "Mode set to S";
131 $self->assert($expected eq $resp_msg,
132 test_msg("Expected '$expected', got '$resp_msg'"));
133 };
134
135 if ($@) {
136 $ex = $@;
137 }
138
139 $wfh->print("done\n");
140 $wfh->flush();
141
142 } else {
143 eval { server_wait($config_file, $rfh) };
144 if ($@) {
145 warn($@);
146 exit 1;
147 }
148
149 exit 0;
150 }
151
152 # Stop server
153 server_stop($pid_file);
154
155 $self->assert_child_ok($pid);
156
157 if ($ex) {
158 test_append_logfile($log_file, $ex);
159 unlink($log_file);
160
161 die($ex);
162 }
163
164 unlink($log_file);
165 }
166
167 sub mode_block_fails {
168 my $self = shift;
169 my $tmpdir = $self->{tmpdir};
170
171 my $config_file = "$tmpdir/cmds.conf";
172 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
173 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
174
175 my $log_file = test_get_logfile();
176
177 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
178 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
179
180 my $user = 'proftpd';
181 my $passwd = 'test';
182 my $home_dir = File::Spec->rel2abs($tmpdir);
183 my $uid = 500;
184 my $gid = 500;
185
186 # Make sure that, if we're running as root, that the home directory has
187 # permissions/privs set for the account we create
188 if ($< == 0) {
189 unless (chmod(0755, $home_dir)) {
190 die("Can't set perms on $home_dir to 0755: $!");
191 }
192
193 unless (chown($uid, $gid, $home_dir)) {
194 die("Can't set owner of $home_dir to $uid/$gid: $!");
195 }
196 }
197
198 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
199 '/bin/bash');
200 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
201
202 my $config = {
203 PidFile => $pid_file,
204 ScoreboardFile => $scoreboard_file,
205 SystemLog => $log_file,
206
207 AuthUserFile => $auth_user_file,
208 AuthGroupFile => $auth_group_file,
209 AuthOrder => 'mod_auth_file.c',
210
211 IfModules => {
212 'mod_delay.c' => {
213 DelayEngine => 'off',
214 },
215 },
216 };
217
218 my ($port, $config_user, $config_group) = config_write($config_file, $config);
219
220 # Open pipes, for use between the parent and child processes. Specifically,
221 # the child will indicate when it's done with its test by writing a message
222 # to the parent.
223 my ($rfh, $wfh);
224 unless (pipe($rfh, $wfh)) {
225 die("Can't open pipe: $!");
226 }
227
228 my $ex;
229
230 # Fork child
231 $self->handle_sigchld();
232 defined(my $pid = fork()) or die("Can't fork: $!");
233 if ($pid) {
234 eval {
235 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
236 $client->login($user, $passwd);
237
238 my ($resp_code, $resp_msg);
239 eval { ($resp_code, $resp_msg) = $client->mode('block') };
240 unless ($@) {
241 die("MODE succeeded unexpectedly");
242
243 } else {
244 $resp_code = $client->response_code();
245 $resp_msg = $client->response_msg();
246 }
247
248 my $expected;
249
250 $expected = 504;
251 $self->assert($expected == $resp_code,
252 test_msg("Expected $expected, got $resp_code"));
253
254 $expected = "'MODE B' unsupported transfer mode";
255 $self->assert($expected eq $resp_msg,
256 test_msg("Expected '$expected', got '$resp_msg'"));
257 };
258
259 if ($@) {
260 $ex = $@;
261 }
262
263 $wfh->print("done\n");
264 $wfh->flush();
265
266 } else {
267 eval { server_wait($config_file, $rfh) };
268 if ($@) {
269 warn($@);
270 exit 1;
271 }
272
273 exit 0;
274 }
275
276 # Stop server
277 server_stop($pid_file);
278
279 $self->assert_child_ok($pid);
280
281 if ($ex) {
282 test_append_logfile($log_file, $ex);
283 unlink($log_file);
284
285 die($ex);
286 }
287
288 unlink($log_file);
289 }
290
291 sub mode_compressed_fails {
292 my $self = shift;
293 my $tmpdir = $self->{tmpdir};
294
295 my $config_file = "$tmpdir/cmds.conf";
296 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
297 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
298
299 my $log_file = test_get_logfile();
300
301 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
302 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
303
304 my $user = 'proftpd';
305 my $passwd = 'test';
306 my $home_dir = File::Spec->rel2abs($tmpdir);
307 my $uid = 500;
308 my $gid = 500;
309
310 # Make sure that, if we're running as root, that the home directory has
311 # permissions/privs set for the account we create
312 if ($< == 0) {
313 unless (chmod(0755, $home_dir)) {
314 die("Can't set perms on $home_dir to 0755: $!");
315 }
316
317 unless (chown($uid, $gid, $home_dir)) {
318 die("Can't set owner of $home_dir to $uid/$gid: $!");
319 }
320 }
321
322 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
323 '/bin/bash');
324 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
325
326 my $config = {
327 PidFile => $pid_file,
328 ScoreboardFile => $scoreboard_file,
329 SystemLog => $log_file,
330
331 AuthUserFile => $auth_user_file,
332 AuthGroupFile => $auth_group_file,
333 AuthOrder => 'mod_auth_file.c',
334
335 IfModules => {
336 'mod_delay.c' => {
337 DelayEngine => 'off',
338 },
339 },
340 };
341
342 my ($port, $config_user, $config_group) = config_write($config_file, $config);
343
344 # Open pipes, for use between the parent and child processes. Specifically,
345 # the child will indicate when it's done with its test by writing a message
346 # to the parent.
347 my ($rfh, $wfh);
348 unless (pipe($rfh, $wfh)) {
349 die("Can't open pipe: $!");
350 }
351
352 my $ex;
353
354 # Fork child
355 $self->handle_sigchld();
356 defined(my $pid = fork()) or die("Can't fork: $!");
357 if ($pid) {
358 eval {
359 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
360 $client->login($user, $passwd);
361
362 my ($resp_code, $resp_msg);
363 eval { ($resp_code, $resp_msg) = $client->mode('compressed') };
364 unless ($@) {
365 die("MODE succeeded unexpectedly");
366
367 } else {
368 $resp_code = $client->response_code();
369 $resp_msg = $client->response_msg();
370 }
371
372 my $expected;
373
374 $expected = 504;
375 $self->assert($expected == $resp_code,
376 test_msg("Expected $expected, got $resp_code"));
377
378 $expected = "'MODE C' unsupported transfer mode";
379 $self->assert($expected eq $resp_msg,
380 test_msg("Expected '$expected', got '$resp_msg'"));
381 };
382
383 if ($@) {
384 $ex = $@;
385 }
386
387 $wfh->print("done\n");
388 $wfh->flush();
389
390 } else {
391 eval { server_wait($config_file, $rfh) };
392 if ($@) {
393 warn($@);
394 exit 1;
395 }
396
397 exit 0;
398 }
399
400 # Stop server
401 server_stop($pid_file);
402
403 $self->assert_child_ok($pid);
404
405 if ($ex) {
406 test_append_logfile($log_file, $ex);
407 unlink($log_file);
408
409 die($ex);
410 }
411
412 unlink($log_file);
413 }
414
415 sub mode_other_fails {
416 my $self = shift;
417 my $tmpdir = $self->{tmpdir};
418
419 my $config_file = "$tmpdir/cmds.conf";
420 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
421 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
422
423 my $log_file = test_get_logfile();
424
425 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
426 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
427
428 my $user = 'proftpd';
429 my $passwd = 'test';
430 my $home_dir = File::Spec->rel2abs($tmpdir);
431 my $uid = 500;
432 my $gid = 500;
433
434 # Make sure that, if we're running as root, that the home directory has
435 # permissions/privs set for the account we create
436 if ($< == 0) {
437 unless (chmod(0755, $home_dir)) {
438 die("Can't set perms on $home_dir to 0755: $!");
439 }
440
441 unless (chown($uid, $gid, $home_dir)) {
442 die("Can't set owner of $home_dir to $uid/$gid: $!");
443 }
444 }
445
446 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
447 '/bin/bash');
448 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
449
450 my $config = {
451 PidFile => $pid_file,
452 ScoreboardFile => $scoreboard_file,
453 SystemLog => $log_file,
454
455 AuthUserFile => $auth_user_file,
456 AuthGroupFile => $auth_group_file,
457 AuthOrder => 'mod_auth_file.c',
458
459 IfModules => {
460 'mod_delay.c' => {
461 DelayEngine => 'off',
462 },
463 },
464 };
465
466 my ($port, $config_user, $config_group) = config_write($config_file, $config);
467
468 # Open pipes, for use between the parent and child processes. Specifically,
469 # the child will indicate when it's done with its test by writing a message
470 # to the parent.
471 my ($rfh, $wfh);
472 unless (pipe($rfh, $wfh)) {
473 die("Can't open pipe: $!");
474 }
475
476 my $ex;
477
478 # Fork child
479 $self->handle_sigchld();
480 defined(my $pid = fork()) or die("Can't fork: $!");
481 if ($pid) {
482 eval {
483 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
484 $client->login($user, $passwd);
485
486 my ($resp_code, $resp_msg);
487 eval { ($resp_code, $resp_msg) = $client->mode('other') };
488 unless ($@) {
489 die("MODE succeeded unexpectedly");
490
491 } else {
492 $resp_code = $client->response_code();
493 $resp_msg = $client->response_msg();
494 }
495
496 my $expected;
497
498 $expected = 501;
499 $self->assert($expected == $resp_code,
500 test_msg("Expected $expected, got $resp_code"));
501
502 $expected = "'MODE other' unrecognized transfer mode";
503 $self->assert($expected eq $resp_msg,
504 test_msg("Expected '$expected', got '$resp_msg'"));
505 };
506
507 if ($@) {
508 $ex = $@;
509 }
510
511 $wfh->print("done\n");
512 $wfh->flush();
513
514 } else {
515 eval { server_wait($config_file, $rfh) };
516 if ($@) {
517 warn($@);
518 exit 1;
519 }
520
521 exit 0;
522 }
523
524 # Stop server
525 server_stop($pid_file);
526
527 $self->assert_child_ok($pid);
528
529 if ($ex) {
530 test_append_logfile($log_file, $ex);
531 unlink($log_file);
532
533 die($ex);
534 }
535
536 unlink($log_file);
537 }
538
539 1;