Debian Codegraph @

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

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