Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Config/Order.pm

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