Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::DisplayConnect;
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 displayconnect_abs_path => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 displayconnect_rel_path => {
24 order => ++$order,
25 test_class => [qw(forking rootprivs)],
26 },
27
28 displayconnect_client_count => {
29 order => ++$order,
30 test_class => [qw(forking)],
31 },
32
33 displayconnect_class_client_count => {
34 order => ++$order,
35 test_class => [qw(forking)],
36 },
37
38 displayconnect_multiline => {
39 order => ++$order,
40 test_class => [qw(forking)],
41 },
42
43 displayconnect_fs_vars => {
44 order => ++$order,
45 test_class => [qw(forking)],
46 },
47
48 # XXX Add other tests with the various Display variables
49 };
50
51 sub new {
52 return shift()->SUPER::new(@_);
53 }
54
55 sub list_tests {
56 return testsuite_get_runnable_tests($TESTS);
57 }
58
59 sub displayconnect_abs_path {
60 my $self = shift;
61 my $tmpdir = $self->{tmpdir};
62
63 my $config_file = "$tmpdir/config.conf";
64 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
65 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
66
67 my $log_file = File::Spec->rel2abs('tests.log');
68
69 my $connect_file = File::Spec->rel2abs("$tmpdir/connect.txt");
70
71 if (open(my $fh, "> $connect_file")) {
72 print $fh "Hello client!\n";
73
74 unless (close($fh)) {
75 die("Unable to write $connect_file: $!");
76 }
77
78 } else {
79 die("Unable to open $connect_file: $!");
80 }
81
82 my $config = {
83 PidFile => $pid_file,
84 ScoreboardFile => $scoreboard_file,
85 SystemLog => $log_file,
86
87 DisplayConnect => $connect_file,
88
89 IfModules => {
90 'mod_delay.c' => {
91 DelayEngine => 'off',
92 },
93 },
94 };
95
96 my ($port, $config_user, $config_group) = config_write($config_file, $config);
97
98 # Open pipes, for use between the parent and child processes. Specifically,
99 # the child will indicate when it's done with its test by writing a message
100 # to the parent.
101 my ($rfh, $wfh);
102 unless (pipe($rfh, $wfh)) {
103 die("Can't open pipe: $!");
104 }
105
106 my $ex;
107
108 # Fork child
109 $self->handle_sigchld();
110 defined(my $pid = fork()) or die("Can't fork: $!");
111 if ($pid) {
112 eval {
113 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
114
115 my ($resp_code, $resp_msg);
116
117 $resp_code = $client->response_code();
118 $resp_msg = $client->response_msg(0);
119
120 my $expected;
121
122 $expected = 220;
123 $self->assert($expected == $resp_code,
124 test_msg("Expected $expected, got $resp_code"));
125
126 $expected = "Hello client!";
127 $self->assert($expected eq $resp_msg,
128 test_msg("Expected '$expected', got '$resp_msg'"));
129 };
130
131 if ($@) {
132 $ex = $@;
133 }
134
135 $wfh->print("done\n");
136 $wfh->flush();
137
138 } else {
139 eval { server_wait($config_file, $rfh) };
140 if ($@) {
141 warn($@);
142 exit 1;
143 }
144
145 exit 0;
146 }
147
148 # Stop server
149 server_stop($pid_file);
150
151 $self->assert_child_ok($pid);
152
153 if ($ex) {
154 die($ex);
155 }
156
157 unlink($log_file);
158 }
159
160 sub displayconnect_rel_path {
161 my $self = shift;
162 my $tmpdir = $self->{tmpdir};
163
164 my $config_file = "$tmpdir/config.conf";
165 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
166 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
167
168 my $log_file = File::Spec->rel2abs('tests.log');
169
170 my $connect_file = 'connect.txt';
171
172 # Turns out that when a relative DisplayConnect path is used, proftpd
173 # looks for it in /, contrary to the docs.
174 #
175 # This is probably due to the fact that src/main.c:daemonize() does
176 # a chdir("/").
177
178 my $connect_path = "/$connect_file";
179
180 if (open(my $fh, "> $connect_path")) {
181 print $fh "Hello client!\n";
182
183 unless (close($fh)) {
184 die("Unable to write $connect_path: $!");
185 }
186
187 } else {
188 die("Unable to open $connect_path: $!");
189 }
190
191 my $config = {
192 PidFile => $pid_file,
193 ScoreboardFile => $scoreboard_file,
194 SystemLog => $log_file,
195
196 DisplayConnect => $connect_file,
197
198 IfModules => {
199 'mod_delay.c' => {
200 DelayEngine => 'off',
201 },
202 },
203 };
204
205 my ($port, $config_user, $config_group) = config_write($config_file, $config);
206
207 # Open pipes, for use between the parent and child processes. Specifically,
208 # the child will indicate when it's done with its test by writing a message
209 # to the parent.
210 my ($rfh, $wfh);
211 unless (pipe($rfh, $wfh)) {
212 die("Can't open pipe: $!");
213 }
214
215 my $ex;
216
217 # Fork child
218 $self->handle_sigchld();
219 defined(my $pid = fork()) or die("Can't fork: $!");
220 if ($pid) {
221 eval {
222 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
223
224 my ($resp_code, $resp_msg);
225
226 $resp_code = $client->response_code();
227 $resp_msg = $client->response_msg(0);
228
229 my $expected;
230
231 $expected = 220;
232 $self->assert($expected == $resp_code,
233 test_msg("Expected $expected, got $resp_code"));
234
235 $expected = "Hello client!";
236 $self->assert($expected eq $resp_msg,
237 test_msg("Expected '$expected', got '$resp_msg'"));
238 };
239
240 if ($@) {
241 $ex = $@;
242 }
243
244 $wfh->print("done\n");
245 $wfh->flush();
246
247 } else {
248 eval { server_wait($config_file, $rfh) };
249 if ($@) {
250 warn($@);
251 exit 1;
252 }
253
254 exit 0;
255 }
256
257 unlink($connect_path);
258
259 # Stop server
260 server_stop($pid_file);
261
262 $self->assert_child_ok($pid);
263
264 if ($ex) {
265 die($ex);
266 }
267
268 unlink($log_file);
269 }
270
271 sub displayconnect_client_count {
272 my $self = shift;
273 my $tmpdir = $self->{tmpdir};
274
275 my $config_file = "$tmpdir/config.conf";
276 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
277 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
278
279 my $log_file = File::Spec->rel2abs('tests.log');
280
281 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
282 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
283
284 my $connect_file = File::Spec->rel2abs("$tmpdir/connect.txt");
285
286 if (open(my $fh, "> $connect_file")) {
287 print $fh "Client count: %N\n";
288
289 unless (close($fh)) {
290 die("Unable to write $connect_file: $!");
291 }
292
293 } else {
294 die("Unable to open $connect_file: $!");
295 }
296
297 my $user = 'proftpd';
298 my $passwd = 'test';
299 my $home_dir = File::Spec->rel2abs($tmpdir);
300 my $uid = 500;
301 my $gid = 500;
302
303 # Make sure that, if we're running as root, that the home directory has
304 # permissions/privs set for the account we create
305 if ($< == 0) {
306 unless (chmod(0755, $home_dir)) {
307 die("Can't set perms on $home_dir to 0755: $!");
308 }
309
310 unless (chown($uid, $gid, $home_dir)) {
311 die("Can't set owner of $home_dir to $uid/$gid: $!");
312 }
313 }
314
315 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
316 '/bin/bash');
317 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
318
319 my $config = {
320 PidFile => $pid_file,
321 ScoreboardFile => $scoreboard_file,
322 SystemLog => $log_file,
323
324 AuthUserFile => $auth_user_file,
325 AuthGroupFile => $auth_group_file,
326 AuthOrder => 'mod_auth_file.c',
327
328 DisplayConnect => $connect_file,
329
330 IfModules => {
331 'mod_delay.c' => {
332 DelayEngine => 'off',
333 },
334 },
335 };
336
337 my ($port, $config_user, $config_group) = config_write($config_file, $config);
338
339 # Open pipes, for use between the parent and child processes. Specifically,
340 # the child will indicate when it's done with its test by writing a message
341 # to the parent.
342 my ($rfh, $wfh);
343 unless (pipe($rfh, $wfh)) {
344 die("Can't open pipe: $!");
345 }
346
347 my $ex;
348
349 # Fork child
350 $self->handle_sigchld();
351 defined(my $pid = fork()) or die("Can't fork: $!");
352 if ($pid) {
353 eval {
354 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
355 $client1->login($user, $passwd);
356
357 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
358
359 my ($resp_code, $resp_msg);
360
361 $resp_code = $client2->response_code();
362 $resp_msg = $client2->response_msg(0);
363
364 my $expected;
365
366 $expected = 220;
367 $self->assert($expected == $resp_code,
368 test_msg("Expected $expected, got $resp_code"));
369
370 $expected = "Client count: 2";
371 $self->assert($expected eq $resp_msg,
372 test_msg("Expected '$expected', got '$resp_msg'"));
373
374 $client2->quit();
375 $client1->quit();
376 };
377
378 if ($@) {
379 $ex = $@;
380 }
381
382 $wfh->print("done\n");
383 $wfh->flush();
384
385 } else {
386 eval { server_wait($config_file, $rfh) };
387 if ($@) {
388 warn($@);
389 exit 1;
390 }
391
392 exit 0;
393 }
394
395 # Stop server
396 server_stop($pid_file);
397
398 $self->assert_child_ok($pid);
399
400 if ($ex) {
401 die($ex);
402 }
403
404 unlink($log_file);
405 }
406
407 sub displayconnect_class_client_count {
408 my $self = shift;
409 my $tmpdir = $self->{tmpdir};
410
411 my $config_file = "$tmpdir/config.conf";
412 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
413 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
414
415 my $log_file = File::Spec->rel2abs('tests.log');
416
417 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
418 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
419
420 my $connect_file = File::Spec->rel2abs("$tmpdir/connect.txt");
421
422 if (open(my $fh, "> $connect_file")) {
423 print $fh "Client count for class: %y\n";
424
425 unless (close($fh)) {
426 die("Unable to write $connect_file: $!");
427 }
428
429 } else {
430 die("Unable to open $connect_file: $!");
431 }
432
433 my $user = 'proftpd';
434 my $passwd = 'test';
435 my $home_dir = File::Spec->rel2abs($tmpdir);
436 my $uid = 500;
437 my $gid = 500;
438
439 # Make sure that, if we're running as root, that the home directory has
440 # permissions/privs set for the account we create
441 if ($< == 0) {
442 unless (chmod(0755, $home_dir)) {
443 die("Can't set perms on $home_dir to 0755: $!");
444 }
445
446 unless (chown($uid, $gid, $home_dir)) {
447 die("Can't set owner of $home_dir to $uid/$gid: $!");
448 }
449 }
450
451 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
452 '/bin/bash');
453 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
454
455 my $config = {
456 PidFile => $pid_file,
457 ScoreboardFile => $scoreboard_file,
458 SystemLog => $log_file,
459
460 AuthUserFile => $auth_user_file,
461 AuthGroupFile => $auth_group_file,
462 AuthOrder => 'mod_auth_file.c',
463
464 DisplayConnect => $connect_file,
465
466 Class => {
467 test => {
468 From => '127.0.0.1',
469 },
470 },
471
472 IfModules => {
473 'mod_delay.c' => {
474 DelayEngine => 'off',
475 },
476 },
477 };
478
479 my ($port, $config_user, $config_group) = config_write($config_file, $config);
480
481 # Open pipes, for use between the parent and child processes. Specifically,
482 # the child will indicate when it's done with its test by writing a message
483 # to the parent.
484 my ($rfh, $wfh);
485 unless (pipe($rfh, $wfh)) {
486 die("Can't open pipe: $!");
487 }
488
489 my $ex;
490
491 # Fork child
492 $self->handle_sigchld();
493 defined(my $pid = fork()) or die("Can't fork: $!");
494 if ($pid) {
495 eval {
496 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
497 $client1->login($user, $passwd);
498
499 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
500
501 my ($resp_code, $resp_msg);
502
503 $resp_code = $client2->response_code();
504 $resp_msg = $client2->response_msg(0);
505
506 my $expected;
507
508 $expected = 220;
509 $self->assert($expected == $resp_code,
510 test_msg("Expected $expected, got $resp_code"));
511
512 $expected = "Client count for class: 1";
513 $self->assert($expected eq $resp_msg,
514 test_msg("Expected '$expected', got '$resp_msg'"));
515
516 $client2->quit();
517 $client1->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 die($ex);
544 }
545
546 unlink($log_file);
547 }
548
549 sub displayconnect_multiline {
550 my $self = shift;
551 my $tmpdir = $self->{tmpdir};
552
553 my $config_file = "$tmpdir/config.conf";
554 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
555 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
556
557 my $log_file = File::Spec->rel2abs('tests.log');
558
559 my $connect_file = File::Spec->rel2abs("$tmpdir/connect.txt");
560
561 if (open(my $fh, "> $connect_file")) {
562 print $fh "Welcome to my domain\nThe files are restless today\nHello client!\n";
563
564 unless (close($fh)) {
565 die("Unable to write $connect_file: $!");
566 }
567
568 } else {
569 die("Unable to open $connect_file: $!");
570 }
571
572 my $config = {
573 PidFile => $pid_file,
574 ScoreboardFile => $scoreboard_file,
575 SystemLog => $log_file,
576
577 DisplayConnect => $connect_file,
578
579 IfModules => {
580 'mod_delay.c' => {
581 DelayEngine => 'off',
582 },
583 },
584 };
585
586 my ($port, $config_user, $config_group) = config_write($config_file, $config);
587
588 # Open pipes, for use between the parent and child processes. Specifically,
589 # the child will indicate when it's done with its test by writing a message
590 # to the parent.
591 my ($rfh, $wfh);
592 unless (pipe($rfh, $wfh)) {
593 die("Can't open pipe: $!");
594 }
595
596 my $ex;
597
598 # Fork child
599 $self->handle_sigchld();
600 defined(my $pid = fork()) or die("Can't fork: $!");
601 if ($pid) {
602 eval {
603 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
604
605 my ($resp_code, $resp_msg);
606
607 $resp_code = $client->response_code();
608 $resp_msg = $client->response_msg(2);
609
610 my $expected;
611
612 $expected = 220;
613 $self->assert($expected == $resp_code,
614 test_msg("Expected $expected, got $resp_code"));
615
616 $expected = " Hello client!";
617 $self->assert($expected eq $resp_msg,
618 test_msg("Expected '$expected', got '$resp_msg'"));
619 };
620
621 if ($@) {
622 $ex = $@;
623 }
624
625 $wfh->print("done\n");
626 $wfh->flush();
627
628 } else {
629 eval { server_wait($config_file, $rfh) };
630 if ($@) {
631 warn($@);
632 exit 1;
633 }
634
635 exit 0;
636 }
637
638 # Stop server
639 server_stop($pid_file);
640
641 $self->assert_child_ok($pid);
642
643 if ($ex) {
644 die($ex);
645 }
646
647 unlink($log_file);
648 }
649
650 sub displayconnect_fs_vars {
651 my $self = shift;
652 my $tmpdir = $self->{tmpdir};
653 my $setup = test_setup($tmpdir, 'config');
654
655 my $connect_file = File::Spec->rel2abs("$tmpdir/connect.txt");
656
657 if (open(my $fh, "> $connect_file")) {
658 print $fh "Hello client!\n";
659 print $fh "Filesystem: %F %f\n";
660
661 unless (close($fh)) {
662 die("Unable to write $connect_file: $!");
663 }
664
665 } else {
666 die("Unable to open $connect_file: $!");
667 }
668
669 my $config = {
670 PidFile => $setup->{pid_file},
671 ScoreboardFile => $setup->{scoreboard_file},
672 SystemLog => $setup->{log_file},
673 TraceLog => $setup->{log_file},
674 Trace => 'fsio:20',
675
676 DisplayConnect => $connect_file,
677
678 IfModules => {
679 'mod_delay.c' => {
680 DelayEngine => 'off',
681 },
682 },
683 };
684
685 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
686 $config);
687
688 # Open pipes, for use between the parent and child processes. Specifically,
689 # the child will indicate when it's done with its test by writing a message
690 # to the parent.
691 my ($rfh, $wfh);
692 unless (pipe($rfh, $wfh)) {
693 die("Can't open pipe: $!");
694 }
695
696 my $ex;
697
698 # Fork child
699 $self->handle_sigchld();
700 defined(my $pid = fork()) or die("Can't fork: $!");
701 if ($pid) {
702 eval {
703 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
704 my $resp_code = $client->response_code();
705 my $resp_msg = $client->response_msg(1);
706
707 my $expected;
708
709 $expected = 220;
710 $self->assert($expected == $resp_code,
711 test_msg("Expected response code $expected, got $resp_code"));
712
713 $expected = 'Filesystem: (\d+) (\S+)$';
714 $self->assert(qr/$expected/, $resp_msg,
715 test_msg("Expected response message '$expected', got '$resp_msg'"));
716 };
717
718 if ($@) {
719 $ex = $@;
720 }
721
722 $wfh->print("done\n");
723 $wfh->flush();
724
725 } else {
726 eval { server_wait($setup->{config_file}, $rfh) };
727 if ($@) {
728 warn($@);
729 exit 1;
730 }
731
732 exit 0;
733 }
734
735 # Stop server
736 server_stop($setup->{pid_file});
737
738 $self->assert_child_ok($pid);
739
740 test_cleanup($setup->{log_file}, $ex);
741 }
742
743 1;