Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Commands/ALLO.pm

raw
1 package ProFTPD::Tests::Commands::ALLO;
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 allo_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 allo_chrooted_bug3996 => {
24 order => ++$order,
25 test_class => [qw(bug forking rootprivs)],
26 },
27
28 allo_anon_bug3996 => {
29 order => ++$order,
30 test_class => [qw(bug forking rootprivs)],
31 },
32
33 };
34
35 sub new {
36 return shift()->SUPER::new(@_);
37 }
38
39 sub list_tests {
40 return testsuite_get_runnable_tests($TESTS);
41 }
42
43 sub allo_ok {
44 my $self = shift;
45 my $tmpdir = $self->{tmpdir};
46 my $setup = test_setup($tmpdir, 'cmds');
47
48 my $config = {
49 PidFile => $setup->{pid_file},
50 ScoreboardFile => $setup->{scoreboard_file},
51 SystemLog => $setup->{log_file},
52
53 AuthUserFile => $setup->{auth_user_file},
54 AuthGroupFile => $setup->{auth_group_file},
55 AuthOrder => 'mod_auth_file.c',
56
57 IfModules => {
58 'mod_delay.c' => {
59 DelayEngine => 'off',
60 },
61 },
62 };
63
64 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
65 $config);
66
67 # Open pipes, for use between the parent and child processes. Specifically,
68 # the child will indicate when it's done with its test by writing a message
69 # to the parent.
70 my ($rfh, $wfh);
71 unless (pipe($rfh, $wfh)) {
72 die("Can't open pipe: $!");
73 }
74
75 my $ex;
76
77 # Fork child
78 $self->handle_sigchld();
79 defined(my $pid = fork()) or die("Can't fork: $!");
80 if ($pid) {
81 eval {
82 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
83 $client->login($setup->{user}, $setup->{passwd});
84
85 my ($resp_code, $resp_msg) = $client->allo(8192);
86
87 my $expected;
88
89 $expected = 200;
90 $self->assert($expected == $resp_code,
91 test_msg("Expected response code $expected, got $resp_code"));
92
93 $expected = "ALLO command successful";
94 $self->assert($expected eq $resp_msg,
95 test_msg("Expected response message '$expected', got '$resp_msg'"));
96 };
97
98 if ($@) {
99 $ex = $@;
100 }
101
102 $wfh->print("done\n");
103 $wfh->flush();
104
105 } else {
106 eval { server_wait($setup->{config_file}, $rfh) };
107 if ($@) {
108 warn($@);
109 exit 1;
110 }
111
112 exit 0;
113 }
114
115 # Stop server
116 server_stop($setup->{pid_file});
117
118 $self->assert_child_ok($pid);
119
120 if ($ex) {
121 test_append_logfile($setup->{log_file}, $ex);
122 unlink($setup->{log_file});
123
124 die($ex);
125 }
126
127 unlink($setup->{log_file});
128 }
129
130 sub allo_chrooted_bug3996 {
131 my $self = shift;
132 my $tmpdir = $self->{tmpdir};
133 my $setup = test_setup($tmpdir, 'cmds');
134
135 my $config = {
136 PidFile => $setup->{pid_file},
137 ScoreboardFile => $setup->{scoreboard_file},
138 SystemLog => $setup->{log_file},
139
140 AuthUserFile => $setup->{auth_user_file},
141 AuthGroupFile => $setup->{auth_group_file},
142 AuthOrder => 'mod_auth_file.c',
143
144 DefaultRoot => '~',
145
146 IfModules => {
147 'mod_delay.c' => {
148 DelayEngine => 'off',
149 },
150 },
151 };
152
153 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
154 $config);
155
156 # Open pipes, for use between the parent and child processes. Specifically,
157 # the child will indicate when it's done with its test by writing a message
158 # to the parent.
159 my ($rfh, $wfh);
160 unless (pipe($rfh, $wfh)) {
161 die("Can't open pipe: $!");
162 }
163
164 my $ex;
165
166 # Fork child
167 $self->handle_sigchld();
168 defined(my $pid = fork()) or die("Can't fork: $!");
169 if ($pid) {
170 eval {
171 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
172 $client->login($setup->{user}, $setup->{passwd});
173
174 my ($resp_code, $resp_msg) = $client->allo(8192);
175
176 my $expected;
177
178 $expected = 202;
179 $self->assert($expected == $resp_code,
180 test_msg("Expected response code $expected, got $resp_code"));
181
182 $expected = "No storage allocation necessary";
183 $self->assert($expected eq $resp_msg,
184 test_msg("Expected response message '$expected', got '$resp_msg'"));
185 };
186
187 if ($@) {
188 $ex = $@;
189 }
190
191 $wfh->print("done\n");
192 $wfh->flush();
193
194 } else {
195 eval { server_wait($setup->{config_file}, $rfh) };
196 if ($@) {
197 warn($@);
198 exit 1;
199 }
200
201 exit 0;
202 }
203
204 # Stop server
205 server_stop($setup->{pid_file});
206
207 $self->assert_child_ok($pid);
208
209 if ($ex) {
210 test_append_logfile($setup->{log_file}, $ex);
211 unlink($setup->{log_file});
212
213 die($ex);
214 }
215
216 unlink($setup->{log_file});
217 }
218
219 sub allo_anon_bug3996 {
220 my $self = shift;
221 my $tmpdir = $self->{tmpdir};
222
223 my $config_file = "$tmpdir/cmds.conf";
224 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
225 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
226
227 my $log_file = test_get_logfile();
228
229 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
230 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
231
232 my ($config_user, $config_group) = config_get_identity();
233
234 my $anon_dir = File::Spec->rel2abs($tmpdir);
235 my $uid = 500;
236 my $gid = 500;
237
238 # Make sure that, if we're running as root, that the home directory has
239 # permissions/privs set for the account we create
240 if ($< == 0) {
241 unless (chmod(0755, $anon_dir)) {
242 die("Can't set perms on $anon_dir to 0755: $!");
243 }
244
245 unless (chown($uid, $gid, $anon_dir)) {
246 die("Can't set owner of $anon_dir to $uid/$gid: $!");
247 }
248 }
249
250 auth_user_write($auth_user_file, $config_user, 'test', $uid, $gid, '/tmp',
251 '/bin/bash');
252 auth_group_write($auth_group_file, $config_group, $gid, $config_user);
253
254 my $config = {
255 PidFile => $pid_file,
256 ScoreboardFile => $scoreboard_file,
257 SystemLog => $log_file,
258
259 AuthUserFile => $auth_user_file,
260 AuthGroupFile => $auth_group_file,
261 AuthOrder => 'mod_auth_file.c',
262
263 IfModules => {
264 'mod_delay.c' => {
265 DelayEngine => 'off',
266 },
267 },
268 };
269
270 my ($port, $user, $group) = config_write($config_file, $config);
271
272 if (open(my $fh, ">> $config_file")) {
273 print $fh <<EOC;
274 <Anonymous $anon_dir>
275 User $config_user
276 Group $config_group
277 UserAlias anonymous $config_user
278 RequireValidShell off
279 </Anonymous>
280 EOC
281 unless (close($fh)) {
282 die("Can't write $config_file: $!");
283 }
284
285 } else {
286 die("Can't open $config_file: $!");
287 }
288
289 # Open pipes, for use between the parent and child processes. Specifically,
290 # the child will indicate when it's done with its test by writing a message
291 # to the parent.
292 my ($rfh, $wfh);
293 unless (pipe($rfh, $wfh)) {
294 die("Can't open pipe: $!");
295 }
296
297 my $ex;
298
299 # Fork child
300 $self->handle_sigchld();
301 defined(my $pid = fork()) or die("Can't fork: $!");
302 if ($pid) {
303 eval {
304 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
305 $client->login('anonymous', '[email protected]');
306
307 my ($resp_code, $resp_msg) = $client->allo(8192);
308
309 my $expected;
310
311 $expected = 202;
312 $self->assert($expected == $resp_code,
313 test_msg("Expected response code $expected, got $resp_code"));
314
315 $expected = "No storage allocation necessary";
316 $self->assert($expected eq $resp_msg,
317 test_msg("Expected response message '$expected', got '$resp_msg'"));
318 };
319
320 if ($@) {
321 $ex = $@;
322 }
323
324 $wfh->print("done\n");
325 $wfh->flush();
326
327 } else {
328 eval { server_wait($config_file, $rfh) };
329 if ($@) {
330 warn($@);
331 exit 1;
332 }
333
334 exit 0;
335 }
336
337 # Stop server
338 server_stop($pid_file);
339
340 $self->assert_child_ok($pid);
341
342 if ($ex) {
343 test_append_logfile($log_file, $ex);
344 unlink($log_file);
345
346 die($ex);
347 }
348
349 unlink($log_file);
350 }
351
352 1;