Debian Codegraph @

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

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