Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::TimeoutNoTransfer;
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 timeoutnoxfer_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 timeoutnoxfer_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 timeoutnoxfer_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_noxfer = 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 TimeoutNoTransfer => $timeout_noxfer,
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 no-transfer period
115 sleep($timeout_noxfer - 1);
116
117 my $conn = $client->list_raw($home_dir);
118 unless ($conn) {
119 die("LIST failed: " . $client->response_code() . " " .
120 $client->response_msg());
121 }
122
123 my $buf;
124 $conn->read($buf, 8192, 30);
125 eval { $conn->close() };
126 };
127
128 if ($@) {
129 $ex = $@;
130 }
131
132 $wfh->print("done\n");
133 $wfh->flush();
134
135 } else {
136 eval { server_wait($config_file, $rfh) };
137 if ($@) {
138 warn($@);
139 exit 1;
140 }
141
142 exit 0;
143 }
144
145 # Stop server
146 server_stop($pid_file);
147
148 $self->assert_child_ok($pid);
149
150 if ($ex) {
151 die($ex);
152 }
153
154 unlink($log_file);
155 }
156
157 sub timeoutnoxfer_exceeded {
158 my $self = shift;
159 my $tmpdir = $self->{tmpdir};
160
161 my $config_file = "$tmpdir/config.conf";
162 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
163 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
164
165 my $log_file = File::Spec->rel2abs('tests.log');
166
167 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
168 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
169
170 my $user = 'proftpd';
171 my $passwd = 'test';
172 my $home_dir = File::Spec->rel2abs($tmpdir);
173 my $uid = 500;
174 my $gid = 500;
175
176 # Make sure that, if we're running as root, that the home directory has
177 # permissions/privs set for the account we create
178 if ($< == 0) {
179 unless (chmod(0755, $home_dir)) {
180 die("Can't set perms on $home_dir to 0755: $!");
181 }
182
183 unless (chown($uid, $gid, $home_dir)) {
184 die("Can't set owner of $home_dir to $uid/$gid: $!");
185 }
186 }
187
188 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
189 '/bin/bash');
190 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
191
192 my $timeout_noxfer = 1;
193
194 my $config = {
195 PidFile => $pid_file,
196 ScoreboardFile => $scoreboard_file,
197 SystemLog => $log_file,
198
199 AuthUserFile => $auth_user_file,
200 AuthGroupFile => $auth_group_file,
201 AuthOrder => 'mod_auth_file.c',
202
203 TimeoutNoTransfer => $timeout_noxfer,
204
205 IfModules => {
206 'mod_delay.c' => {
207 DelayEngine => 'off',
208 },
209 },
210 };
211
212 my ($port, $config_user, $config_group) = config_write($config_file, $config);
213
214 # Open pipes, for use between the parent and child processes. Specifically,
215 # the child will indicate when it's done with its test by writing a message
216 # to the parent.
217 my ($rfh, $wfh);
218 unless (pipe($rfh, $wfh)) {
219 die("Can't open pipe: $!");
220 }
221
222 my $ex;
223
224 # Fork child
225 $self->handle_sigchld();
226 defined(my $pid = fork()) or die("Can't fork: $!");
227 if ($pid) {
228 eval {
229 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
230
231 $client->login($user, $passwd);
232
233 # Wait for one second more than the no-transfer period
234 sleep($timeout_noxfer + 1);
235
236 my ($resp_code, $resp_msg);
237
238 my $conn = $client->list_raw($home_dir);
239 if ($conn) {
240 die("LIST succeeded unexpectedly");
241
242 } else {
243 $resp_code = $client->response_code();
244 $resp_msg = $client->response_msg();
245 }
246
247 my $expected;
248
249 $expected = 421;
250 $self->assert($expected == $resp_code,
251 test_msg("Expected $expected, got $resp_code"));
252
253 $expected = "No transfer timeout ($timeout_noxfer seconds): closing control connection";
254 $self->assert($expected eq $resp_msg,
255 test_msg("Expected '$expected', got '$resp_msg'"));
256 };
257
258 if ($@) {
259 $ex = $@;
260 }
261
262 $wfh->print("done\n");
263 $wfh->flush();
264
265 } else {
266 eval { server_wait($config_file, $rfh) };
267 if ($@) {
268 warn($@);
269 exit 1;
270 }
271
272 exit 0;
273 }
274
275 # Stop server
276 server_stop($pid_file);
277
278 $self->assert_child_ok($pid);
279
280 if ($ex) {
281 die($ex);
282 }
283
284 unlink($log_file);
285 }
286
287 1;