Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::TimeoutIdle;
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 timeoutidle_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 timeoutidle_exceeded => {
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 timeoutidle_ok {
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 # Make sure that, if we're running as root, that the home directory has
59 # permissions/privs set for the account we create
60 if ($< == 0) {
61 unless (chmod(0755, $home_dir)) {
62 die("Can't set perms on $home_dir to 0755: $!");
63 }
64
65 unless (chown($uid, $gid, $home_dir)) {
66 die("Can't set owner of $home_dir to $uid/$gid: $!");
67 }
68 }
69
70 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
71 '/bin/bash');
72 auth_group_write($auth_group_file, $group, $gid, $user);
73
74 my $timeout_idle = 4;
75
76 my $config = {
77 PidFile => $pid_file,
78 ScoreboardFile => $scoreboard_file,
79 SystemLog => $log_file,
80
81 AuthUserFile => $auth_user_file,
82 AuthGroupFile => $auth_group_file,
83 AuthOrder => 'mod_auth_file.c',
84
85 TimeoutIdle => $timeout_idle,
86
87 IfModules => {
88 'mod_delay.c' => {
89 DelayEngine => 'off',
90 },
91 },
92 };
93
94 my ($port, $config_user, $config_group) = config_write($config_file, $config);
95
96 # Open pipes, for use between the parent and child processes. Specifically,
97 # the child will indicate when it's done with its test by writing a message
98 # to the parent.
99 my ($rfh, $wfh);
100 unless (pipe($rfh, $wfh)) {
101 die("Can't open pipe: $!");
102 }
103
104 my $ex;
105
106 # Fork child
107 $self->handle_sigchld();
108 defined(my $pid = fork()) or die("Can't fork: $!");
109 if ($pid) {
110 eval {
111 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
112
113 $client->login($user, $passwd);
114 $client->noop();
115
116 # Wait for one second less than the idle period
117 sleep($timeout_idle - 1);
118
119 my ($resp_code, $resp_msg);
120 ($resp_code, $resp_msg) = $client->noop();
121
122 my $expected;
123
124 $expected = 200;
125 $self->assert($expected == $resp_code,
126 test_msg("Expected $expected, got $resp_code"));
127
128 $expected = "NOOP command successful";
129 $self->assert($expected eq $resp_msg,
130 test_msg("Expected '$expected', got '$resp_msg'"));
131
132 ($resp_code, $resp_msg) = $client->quit();
133
134 $expected = 221;
135 $self->assert($expected == $resp_code,
136 test_msg("Expected $expected, got $resp_code"));
137
138 $expected = "Goodbye.";
139 $self->assert($expected eq $resp_msg,
140 test_msg("Expected '$expected', got '$resp_msg'"));
141 };
142
143 if ($@) {
144 $ex = $@;
145 }
146
147 $wfh->print("done\n");
148 $wfh->flush();
149
150 } else {
151 eval { server_wait($config_file, $rfh) };
152 if ($@) {
153 warn($@);
154 exit 1;
155 }
156
157 exit 0;
158 }
159
160 # Stop server
161 server_stop($pid_file);
162
163 $self->assert_child_ok($pid);
164
165 if ($ex) {
166 test_append_logfile($log_file, $ex);
167 unlink($log_file);
168
169 die($ex);
170 }
171
172 unlink($log_file);
173 }
174
175 sub timeoutidle_exceeded {
176 my $self = shift;
177 my $tmpdir = $self->{tmpdir};
178
179 my $config_file = "$tmpdir/config.conf";
180 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
181 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
182
183 my $log_file = test_get_logfile();
184
185 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
186 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
187
188 my $user = 'proftpd';
189 my $passwd = 'test';
190 my $group = 'ftpd';
191 my $home_dir = File::Spec->rel2abs($tmpdir);
192 my $uid = 500;
193 my $gid = 500;
194
195 # Make sure that, if we're running as root, that the home directory has
196 # permissions/privs set for the account we create
197 if ($< == 0) {
198 unless (chmod(0755, $home_dir)) {
199 die("Can't set perms on $home_dir to 0755: $!");
200 }
201
202 unless (chown($uid, $gid, $home_dir)) {
203 die("Can't set owner of $home_dir to $uid/$gid: $!");
204 }
205 }
206
207 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
208 '/bin/bash');
209 auth_group_write($auth_group_file, $group, $gid, $user);
210
211 my $timeout_idle = 4;
212 my $timeout_delay = $timeout_idle + 2;
213
214 my $config = {
215 PidFile => $pid_file,
216 ScoreboardFile => $scoreboard_file,
217 SystemLog => $log_file,
218 TraceLog => $log_file,
219 Trace => 'DEFAULT:10',
220
221 AuthUserFile => $auth_user_file,
222 AuthGroupFile => $auth_group_file,
223 AuthOrder => 'mod_auth_file.c',
224
225 TimeoutIdle => $timeout_idle,
226
227 IfModules => {
228 'mod_delay.c' => {
229 DelayEngine => 'off',
230 },
231 },
232 };
233
234 my ($port, $config_user, $config_group) = config_write($config_file, $config);
235
236 # Open pipes, for use between the parent and child processes. Specifically,
237 # the child will indicate when it's done with its test by writing a message
238 # to the parent.
239 my ($rfh, $wfh);
240 unless (pipe($rfh, $wfh)) {
241 die("Can't open pipe: $!");
242 }
243
244 my $ex;
245
246 # Fork child
247 $self->handle_sigchld();
248 defined(my $pid = fork()) or die("Can't fork: $!");
249 if ($pid) {
250 eval {
251 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
252
253 $client->login($user, $passwd);
254 $client->noop();
255
256 # Wait for more than the idle period
257 sleep($timeout_delay);
258
259 my ($resp_code, $resp_msg);
260
261 eval { $client->noop() };
262 unless ($@) {
263 die("NOOP succeeded unexpectedly");
264
265 } else {
266 $resp_code = $client->response_code();
267 $resp_msg = $client->response_msg();
268 }
269
270 my $expected;
271
272 $expected = 421;
273 $self->assert($expected == $resp_code,
274 test_msg("Expected $expected, got $resp_code"));
275
276 $expected = "Idle timeout ($timeout_idle seconds): closing control connection";
277 $self->assert($expected eq $resp_msg,
278 test_msg("Expected '$expected', got '$resp_msg'"));
279 };
280
281 if ($@) {
282 $ex = $@;
283 }
284
285 $wfh->print("done\n");
286 $wfh->flush();
287
288 } else {
289 eval { server_wait($config_file, $rfh) };
290 if ($@) {
291 warn($@);
292 exit 1;
293 }
294
295 exit 0;
296 }
297
298 # Stop server
299 server_stop($pid_file);
300
301 $self->assert_child_ok($pid);
302
303 if ($ex) {
304 test_append_logfile($log_file, $ex);
305 unlink($log_file);
306
307 die($ex);
308 }
309
310 unlink($log_file);
311 }
312
313 1;