Debian Codegraph @

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

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