Debian Codegraph @

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

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