Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::MaxConnectionsPerHost;
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 maxconnsperhost_one_multi_conns => {
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 maxconnsperhost_one_multi_conns {
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 $group = 'ftpd';
49 my $home_dir = File::Spec->rel2abs($tmpdir);
50 my $uid = 500;
51 my $gid = 500;
52
53 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
54 '/bin/bash');
55 auth_group_write($auth_group_file, $group, $gid, $user);
56
57 my $max_conns_per_host = 1;
58
59 my $config = {
60 PidFile => $pid_file,
61 ScoreboardFile => $scoreboard_file,
62 SystemLog => $log_file,
63
64 AuthUserFile => $auth_user_file,
65 AuthGroupFile => $auth_group_file,
66 AuthOrder => 'mod_auth_file.c',
67
68 MaxConnectionsPerHost => $max_conns_per_host,
69 TimeoutIdle => 30,
70
71 IfModules => {
72 'mod_delay.c' => {
73 DelayEngine => 'off',
74 },
75 },
76 };
77
78 my ($port, $config_user, $config_group) = config_write($config_file, $config);
79
80 # Open pipes, for use between the parent and child processes. Specifically,
81 # the child will indicate when it's done with its test by writing a message
82 # to the parent.
83 my ($rfh, $wfh);
84 unless (pipe($rfh, $wfh)) {
85 die("Can't open pipe: $!");
86 }
87
88 my $ex;
89
90 # Fork child
91 $self->handle_sigchld();
92 defined(my $pid = fork()) or die("Can't fork: $!");
93 if ($pid) {
94 eval {
95 # First client should be able to connect and log in...
96 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
97 $client1->login($user, $passwd);
98
99 # ...but the next clients shouldn't be able to connect at all.
100 for (my $i = 0; $i < 10; $i++) {
101 eval {
102 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
103 };
104 unless ($@) {
105 die("Connect succeeded unexpectedly");
106 }
107 }
108
109 $client1->quit();
110 };
111
112 if ($@) {
113 $ex = $@;
114 }
115
116 $wfh->print("done\n");
117 $wfh->flush();
118
119 } else {
120 eval { server_wait($config_file, $rfh, 30) };
121 if ($@) {
122 warn($@);
123 exit 1;
124 }
125
126 exit 0;
127 }
128
129 # Stop server
130 server_stop($pid_file);
131
132 $self->assert_child_ok($pid);
133
134 if ($ex) {
135 die($ex);
136 }
137
138 unlink($log_file);
139 }
140
141 sub maxclientsperhost_one_multi_conns {
142 my $self = shift;
143 my $tmpdir = $self->{tmpdir};
144
145 my $config_file = "$tmpdir/config.conf";
146 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
147 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
148
149 my $log_file = File::Spec->rel2abs('tests.log');
150
151 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
152 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
153
154 my $user = 'proftpd';
155 my $passwd = 'test';
156 my $group = 'ftpd';
157 my $home_dir = File::Spec->rel2abs($tmpdir);
158 my $uid = 500;
159 my $gid = 500;
160
161 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
162 '/bin/bash');
163 auth_group_write($auth_group_file, $group, $gid, $user);
164
165 my $max_clients_per_host = 1;
166
167 my $config = {
168 PidFile => $pid_file,
169 ScoreboardFile => $scoreboard_file,
170 SystemLog => $log_file,
171
172 AuthUserFile => $auth_user_file,
173 AuthGroupFile => $auth_group_file,
174 AuthOrder => 'mod_auth_file.c',
175
176 MaxClientsPerHost => $max_clients_per_host,
177
178 IfModules => {
179 'mod_delay.c' => {
180 DelayEngine => 'off',
181 },
182 },
183 };
184
185 my ($port, $config_user, $config_group) = config_write($config_file, $config);
186
187 # Open pipes, for use between the parent and child processes. Specifically,
188 # the child will indicate when it's done with its test by writing a message
189 # to the parent.
190 my ($rfh, $wfh);
191 unless (pipe($rfh, $wfh)) {
192 die("Can't open pipe: $!");
193 }
194
195 my $ex;
196
197 # Fork child
198 $self->handle_sigchld();
199 defined(my $pid = fork()) or die("Can't fork: $!");
200 if ($pid) {
201 eval {
202 # First client should be able to connect and log in...
203 my $client1 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
204 $client1->login($user, $passwd);
205
206 # ...but the second client should be able to connect, but not login.
207 my $client2 = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
208
209 eval { $client2->login($user, $passwd) };
210 unless ($@) {
211 die("Login succeeded unexpectedly");
212 }
213
214 # Even though we can't log in, we should be able to connect quite
215 # a few more times
216
217 my $clients = [];
218 for (my $i = 0; $i < 10; $i++) {
219 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
220 push(@$clients, $client);
221 }
222
223 $client1->quit();
224 };
225
226 if ($@) {
227 $ex = $@;
228 }
229
230 $wfh->print("done\n");
231 $wfh->flush();
232
233 } else {
234 eval { server_wait($config_file, $rfh) };
235 if ($@) {
236 warn($@);
237 exit 1;
238 }
239
240 exit 0;
241 }
242
243 # Stop server
244 server_stop($pid_file);
245
246 $self->assert_child_ok($pid);
247
248 if ($ex) {
249 die($ex);
250 }
251
252 unlink($log_file);
253 }
254
255 1;