Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::DirFakeMode;
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 dirfakemode_list => {
19 order => ++$order,
20 test_class => [qw(bug forking)],
21 },
22
23 dirfakemode_mlsd_bug3604 => {
24 order => ++$order,
25 test_class => [qw(bug forking)],
26 },
27
28 dirfakemode_mlst_bug3604 => {
29 order => ++$order,
30 test_class => [qw(bug forking)],
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 dirfakemode_list {
44 my $self = shift;
45 my $tmpdir = $self->{tmpdir};
46
47 my $config_file = "$tmpdir/config.conf";
48 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
49 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
50
51 my $log_file = File::Spec->rel2abs('tests.log');
52
53 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
54 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
55
56 my $user = 'proftpd';
57 my $passwd = 'test';
58 my $group = 'ftpd';
59 my $home_dir = File::Spec->rel2abs($tmpdir);
60 my $uid = 500;
61 my $gid = 500;
62
63 # Make sure that, if we're running as root, that the home directory has
64 # permissions/privs set for the account we create
65 if ($< == 0) {
66 unless (chmod(0755, $home_dir)) {
67 die("Can't set perms on $home_dir to 0755: $!");
68 }
69
70 unless (chown($uid, $gid, $home_dir)) {
71 die("Can't set owner of $home_dir to $uid/$gid: $!");
72 }
73 }
74
75 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
76 '/bin/bash');
77 auth_group_write($auth_group_file, $group, $gid, $user);
78
79 my $test_file = File::Spec->rel2abs("$tmpdir/test.txt");
80
81 my $config = {
82 PidFile => $pid_file,
83 ScoreboardFile => $scoreboard_file,
84 SystemLog => $log_file,
85
86 AuthUserFile => $auth_user_file,
87 AuthGroupFile => $auth_group_file,
88 AuthOrder => 'mod_auth_file.c',
89
90 DirFakeMode => '0311',
91
92 IfModules => {
93 'mod_delay.c' => {
94 DelayEngine => 'off',
95 },
96 },
97 };
98
99 my ($port, $config_user, $config_group) = config_write($config_file, $config);
100
101 # Open pipes, for use between the parent and child processes. Specifically,
102 # the child will indicate when it's done with its test by writing a message
103 # to the parent.
104 my ($rfh, $wfh);
105 unless (pipe($rfh, $wfh)) {
106 die("Can't open pipe: $!");
107 }
108
109 my $ex;
110
111 # Fork child
112 $self->handle_sigchld();
113 defined(my $pid = fork()) or die("Can't fork: $!");
114 if ($pid) {
115 eval {
116 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
117 $client->login($user, $passwd);
118
119 my $conn = $client->list_raw();
120 unless ($conn) {
121 die("Failed to LIST: " . $client->response_code() . " " .
122 $client->response_msg());
123 }
124
125 my $buf;
126 $conn->read($buf, 16384, 25);
127 eval { $conn->close() };
128
129 my $resp_code = $client->response_code();
130 my $resp_msg = $client->response_msg();
131
132 my $expected;
133
134 $expected = 226;
135 $self->assert($expected == $resp_code,
136 test_msg("Expected $expected, got $resp_code"));
137
138 $expected = "Transfer complete";
139 $self->assert($expected eq $resp_msg,
140 test_msg("Expected '$expected', got '$resp_msg'"));
141
142 $client->quit();
143
144 # We have to be careful of the fact that readdir returns directory
145 # entries in an unordered fashion.
146 my $res = {};
147 my $lines = [split(/\n/, $buf)];
148 foreach my $line (@$lines) {
149 if ($line =~ /(^\S+)\s+\d+\s+\S+\s+\S+\s+.*?\s+(\S+)$/) {
150 $res->{$2} = $1;
151 }
152 }
153
154 $expected = {
155 'config.conf' => '--wx--x--x',
156 'config.group' => '--wx--x--x',
157 'config.passwd' => '--wx--x--x',
158 'config.pid' => '--wx--x--x',
159 'config.scoreboard' => '--wx--x--x',
160 'config.scoreboard.lck' => '--wx--x--x',
161 };
162
163 my $file_ok = 1;
164 my $mode_ok = 1;
165
166 my $mismatch;
167 foreach my $name (keys(%$res)) {
168 unless (defined($expected->{$name})) {
169 $mismatch = $name;
170 $file_ok = 0;
171 last;
172 }
173
174 unless ($res->{$name} eq $expected->{$name}) {
175 $mismatch = "$name: $res->{$name}";
176 $mode_ok = 0;
177 last;
178 }
179 }
180
181 unless ($file_ok) {
182 die("Unexpected name '$mismatch' appeared in LIST data")
183 }
184
185 unless ($mode_ok) {
186 die("Unexpected mode '$mismatch' appeared in LIST data")
187 }
188
189 };
190
191 if ($@) {
192 $ex = $@;
193 }
194
195 $wfh->print("done\n");
196 $wfh->flush();
197
198 } else {
199 eval { server_wait($config_file, $rfh) };
200 if ($@) {
201 warn($@);
202 exit 1;
203 }
204
205 exit 0;
206 }
207
208 # Stop server
209 server_stop($pid_file);
210
211 $self->assert_child_ok($pid);
212
213 if ($ex) {
214 die($ex);
215 }
216
217 unlink($log_file);
218 }
219
220 sub dirfakemode_mlsd_bug3604 {
221 my $self = shift;
222 my $tmpdir = $self->{tmpdir};
223
224 my $config_file = "$tmpdir/config.conf";
225 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
226 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
227
228 my $log_file = File::Spec->rel2abs('tests.log');
229
230 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
231 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
232
233 my $user = 'proftpd';
234 my $passwd = 'test';
235 my $group = 'ftpd';
236 my $home_dir = File::Spec->rel2abs($tmpdir);
237 my $uid = 500;
238 my $gid = 500;
239
240 # Make sure that, if we're running as root, that the home directory has
241 # permissions/privs set for the account we create
242 if ($< == 0) {
243 unless (chmod(0755, $home_dir)) {
244 die("Can't set perms on $home_dir to 0755: $!");
245 }
246
247 unless (chown($uid, $gid, $home_dir)) {
248 die("Can't set owner of $home_dir to $uid/$gid: $!");
249 }
250 }
251
252 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
253 '/bin/bash');
254 auth_group_write($auth_group_file, $group, $gid, $user);
255
256 my $fake_mode = '0311';
257
258 my $config = {
259 PidFile => $pid_file,
260 ScoreboardFile => $scoreboard_file,
261 SystemLog => $log_file,
262
263 AuthUserFile => $auth_user_file,
264 AuthGroupFile => $auth_group_file,
265 AuthOrder => 'mod_auth_file.c',
266
267 DirFakeMode => $fake_mode,
268
269 IfModules => {
270 'mod_delay.c' => {
271 DelayEngine => 'off',
272 },
273 },
274 };
275
276 my ($port, $config_user, $config_group) = config_write($config_file, $config);
277
278 # Open pipes, for use between the parent and child processes. Specifically,
279 # the child will indicate when it's done with its test by writing a message
280 # to the parent.
281 my ($rfh, $wfh);
282 unless (pipe($rfh, $wfh)) {
283 die("Can't open pipe: $!");
284 }
285
286 my $ex;
287
288 # Fork child
289 $self->handle_sigchld();
290 defined(my $pid = fork()) or die("Can't fork: $!");
291 if ($pid) {
292 eval {
293 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
294 $client->login($user, $passwd);
295
296 my $conn = $client->mlsd_raw();
297 unless ($conn) {
298 die("Failed to MLSD: " . $client->response_code() . " " .
299 $client->response_msg());
300 }
301
302 my $buf;
303 $conn->read($buf, 16384, 25);
304 eval { $conn->close() };
305
306 my $resp_code = $client->response_code();
307 my $resp_msg = $client->response_msg();
308
309 my $expected;
310
311 $expected = 226;
312 $self->assert($expected == $resp_code,
313 test_msg("Expected $expected, got $resp_code"));
314
315 $expected = "Transfer complete";
316 $self->assert($expected eq $resp_msg,
317 test_msg("Expected '$expected', got '$resp_msg'"));
318
319 $client->quit();
320
321 # We have to be careful of the fact that readdir returns directory
322 # entries in an unordered fashion.
323 my $res = {};
324 my $lines = [split(/\n/, $buf)];
325 foreach my $line (@$lines) {
326 if ($line =~ /^modify=\S+;perm=\S+;type=\S+;unique=\S+;UNIX\.group=\d+;UNIX\.mode=(\d+);UNIX.owner=\d+; (.*?)$/) {
327 $res->{$2} = $1;
328 }
329 }
330
331 $expected = {
332 '.' => $fake_mode,
333 '..' => $fake_mode,
334 'config.conf' => $fake_mode,
335 'config.group' => $fake_mode,
336 'config.passwd' => $fake_mode,
337 'config.pid' => $fake_mode,
338 'config.scoreboard' => $fake_mode,
339 'config.scoreboard.lck' => $fake_mode,
340 };
341
342 my $file_ok = 1;
343 my $mode_ok = 1;
344
345 my $mismatch;
346 foreach my $name (keys(%$res)) {
347 unless (defined($expected->{$name})) {
348 $mismatch = $name;
349 $file_ok = 0;
350 last;
351 }
352
353 unless ($res->{$name} == $expected->{$name}) {
354 $mismatch = "$name: $res->{$name}";
355 $mode_ok = 0;
356 last;
357 }
358 }
359
360 unless ($file_ok) {
361 die("Unexpected name '$mismatch' appeared in LIST data")
362 }
363
364 unless ($mode_ok) {
365 die("Unexpected mode '$mismatch' appeared in LIST data")
366 }
367 };
368
369 if ($@) {
370 $ex = $@;
371 }
372
373 $wfh->print("done\n");
374 $wfh->flush();
375
376 } else {
377 eval { server_wait($config_file, $rfh) };
378 if ($@) {
379 warn($@);
380 exit 1;
381 }
382
383 exit 0;
384 }
385
386 # Stop server
387 server_stop($pid_file);
388
389 $self->assert_child_ok($pid);
390
391 if ($ex) {
392 die($ex);
393 }
394
395 unlink($log_file);
396 }
397
398 sub dirfakemode_mlst_bug3604 {
399 my $self = shift;
400 my $tmpdir = $self->{tmpdir};
401 my $setup = test_setup($tmpdir, 'config');
402
403 my $fake_mode = '0311';
404
405 my $config = {
406 PidFile => $setup->{pid_file},
407 ScoreboardFile => $setup->{scoreboard_file},
408 SystemLog => $setup->{log_file},
409
410 AuthUserFile => $setup->{auth_user_file},
411 AuthGroupFile => $setup->{auth_group_file},
412 AuthOrder => 'mod_auth_file.c',
413
414 DirFakeMode => $fake_mode,
415
416 IfModules => {
417 'mod_delay.c' => {
418 DelayEngine => 'off',
419 },
420 },
421 };
422
423 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
424 $config);
425
426 # Open pipes, for use between the parent and child processes. Specifically,
427 # the child will indicate when it's done with its test by writing a message
428 # to the parent.
429 my ($rfh, $wfh);
430 unless (pipe($rfh, $wfh)) {
431 die("Can't open pipe: $!");
432 }
433
434 my $ex;
435
436 # Fork child
437 $self->handle_sigchld();
438 defined(my $pid = fork()) or die("Can't fork: $!");
439 if ($pid) {
440 eval {
441 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
442 $client->login($setup->{user}, $setup->{passwd});
443
444 my ($resp_code, $resp_msg) = $client->mlst('config.conf');
445
446 my $expected = 250;
447 $self->assert($expected == $resp_code,
448 test_msg("Expected response code $expected, got $resp_code"));
449
450 $expected = 'modify=\d+;perm=adfr(w)?;size=\d+;type=file;unique=\S+;UNIX\.group=\d+;UNIX\.groupname=\S+;UNIX\.mode=(\d+);UNIX\.owner=\d+;UNIX\.ownername=\S+; (.*?)\/config\.conf$';
451 $self->assert(qr/$expected/, $resp_msg,
452 test_msg("Expected response message '$expected', got '$resp_msg'"));
453
454 $client->quit();
455
456 $resp_msg =~ /$expected/;
457 my $file_mode = $2;
458
459 $self->assert($file_mode == $fake_mode,
460 test_msg("Expected $fake_mode, got $file_mode"));
461 };
462 if ($@) {
463 $ex = $@;
464 }
465
466 $wfh->print("done\n");
467 $wfh->flush();
468
469 } else {
470 eval { server_wait($setup->{config_file}, $rfh) };
471 if ($@) {
472 warn($@);
473 exit 1;
474 }
475
476 exit 0;
477 }
478
479 # Stop server
480 server_stop($setup->{pid_file});
481 $self->assert_child_ok($pid);
482
483 test_cleanup($setup->{log_file}, $ex);
484 }
485
486 1;