Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::MaxInstances;
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 maxinstances_one => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 };
24
25 sub new {
26 return shift()->SUPER::new(@_);
27 }
28
29 sub list_tests {
30 return testsuite_get_runnable_tests($TESTS);
31 }
32
33 sub maxinstances_one {
34 my $self = shift;
35 my $tmpdir = $self->{tmpdir};
36
37 my $config_file = "$tmpdir/config.conf";
38 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
39 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
40
41 my $log_file = File::Spec->rel2abs('tests.log');
42
43 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
44 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
45
46 my $user = 'proftpd';
47 my $passwd = 'test';
48 my $home_dir = File::Spec->rel2abs($tmpdir);
49
50 auth_user_write($auth_user_file, $user, $passwd, 500, 500, $home_dir,
51 '/bin/bash');
52 auth_group_write($auth_group_file, 'ftpd', 500, $user);
53
54 my $max_instances = 1;
55
56 my $config = {
57 PidFile => $pid_file,
58 ScoreboardFile => $scoreboard_file,
59 SystemLog => $log_file,
60
61 AuthUserFile => $auth_user_file,
62 AuthGroupFile => $auth_group_file,
63 AuthOrder => 'mod_auth_file.c',
64
65 MaxInstances => $max_instances,
66
67 IfModules => {
68 'mod_delay.c' => {
69 DelayEngine => 'off',
70 },
71 },
72 };
73
74 my ($port, $config_user, $config_group) = config_write($config_file, $config);
75
76 # Open pipes, for use between the parent and child processes. Specifically,
77 # the child will indicate when it's done with its test by writing a message
78 # to the parent.
79 my ($rfh, $wfh);
80 unless (pipe($rfh, $wfh)) {
81 die("Can't open pipe: $!");
82 }
83
84 my $ex;
85
86 # Fork child
87 $self->handle_sigchld();
88 defined(my $pid = fork()) or die("Can't fork: $!");
89 if ($pid) {
90 eval {
91 # First client should be able to connect and log in...
92 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
93 $client1->login($user, $passwd);
94
95 # ...but the second client should not be able to connect.
96 eval { my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port,
97 undef, 1) };
98 unless ($@) {
99 die("Connect succeeded unexpectedly");
100 }
101
102 $client1->quit();
103 };
104
105 if ($@) {
106 $ex = $@;
107 }
108
109 $wfh->print("done\n");
110 $wfh->flush();
111
112 } else {
113 eval { server_wait($config_file, $rfh) };
114 if ($@) {
115 warn($@);
116 exit 1;
117 }
118
119 exit 0;
120 }
121
122 # Stop server
123 server_stop($pid_file);
124
125 $self->assert_child_ok($pid);
126
127 if ($ex) {
128 die($ex);
129 }
130
131 unlink($log_file);
132 }
133
134 1;