Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Commands::STRU;
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 stru_file_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 stru_record_fails => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 stru_page_fails => {
29 order => ++$order,
30 test_class => [qw(forking)],
31 },
32
33 stru_other_fails => {
34 order => ++$order,
35 test_class => [qw(forking)],
36 },
37
38 };
39
40 sub new {
41 return shift()->SUPER::new(@_);
42 }
43
44 sub list_tests {
45 return testsuite_get_runnable_tests($TESTS);
46 }
47
48 sub stru_file_ok {
49 my $self = shift;
50 my $tmpdir = $self->{tmpdir};
51
52 my $config_file = "$tmpdir/cmds.conf";
53 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
54 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
55
56 my $log_file = test_get_logfile();
57
58 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
59 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
60
61 my $user = 'proftpd';
62 my $passwd = 'test';
63 my $home_dir = File::Spec->rel2abs($tmpdir);
64 my $uid = 500;
65 my $gid = 500;
66
67 # Make sure that, if we're running as root, that the home directory has
68 # permissions/privs set for the account we create
69 if ($< == 0) {
70 unless (chmod(0755, $home_dir)) {
71 die("Can't set perms on $home_dir to 0755: $!");
72 }
73
74 unless (chown($uid, $gid, $home_dir)) {
75 die("Can't set owner of $home_dir to $uid/$gid: $!");
76 }
77 }
78
79 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
80 '/bin/bash');
81 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
82
83 my $config = {
84 PidFile => $pid_file,
85 ScoreboardFile => $scoreboard_file,
86 SystemLog => $log_file,
87
88 AuthUserFile => $auth_user_file,
89 AuthGroupFile => $auth_group_file,
90 AuthOrder => 'mod_auth_file.c',
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 ($resp_code, $resp_msg);
120 ($resp_code, $resp_msg) = $client->stru('file');
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 = "Structure set to F";
129 $self->assert($expected eq $resp_msg,
130 test_msg("Expected '$expected', got '$resp_msg'"));
131 };
132
133 if ($@) {
134 $ex = $@;
135 }
136
137 $wfh->print("done\n");
138 $wfh->flush();
139
140 } else {
141 eval { server_wait($config_file, $rfh) };
142 if ($@) {
143 warn($@);
144 exit 1;
145 }
146
147 exit 0;
148 }
149
150 # Stop server
151 server_stop($pid_file);
152
153 $self->assert_child_ok($pid);
154
155 if ($ex) {
156 test_append_logfile($log_file, $ex);
157 unlink($log_file);
158
159 die($ex);
160 }
161
162 unlink($log_file);
163 }
164
165 sub stru_record_fails {
166 my $self = shift;
167 my $tmpdir = $self->{tmpdir};
168
169 my $config_file = "$tmpdir/cmds.conf";
170 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
171 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
172
173 my $log_file = test_get_logfile();
174
175 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
176 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
177
178 my $user = 'proftpd';
179 my $passwd = 'test';
180 my $home_dir = File::Spec->rel2abs($tmpdir);
181 my $uid = 500;
182 my $gid = 500;
183
184 # Make sure that, if we're running as root, that the home directory has
185 # permissions/privs set for the account we create
186 if ($< == 0) {
187 unless (chmod(0755, $home_dir)) {
188 die("Can't set perms on $home_dir to 0755: $!");
189 }
190
191 unless (chown($uid, $gid, $home_dir)) {
192 die("Can't set owner of $home_dir to $uid/$gid: $!");
193 }
194 }
195
196 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
197 '/bin/bash');
198 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
199
200 my $config = {
201 PidFile => $pid_file,
202 ScoreboardFile => $scoreboard_file,
203 SystemLog => $log_file,
204
205 AuthUserFile => $auth_user_file,
206 AuthGroupFile => $auth_group_file,
207 AuthOrder => 'mod_auth_file.c',
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 $client->login($user, $passwd);
235
236 my ($resp_code, $resp_msg);
237 eval { ($resp_code, $resp_msg) = $client->stru('record') };
238 unless ($@) {
239 die("STRU succeeded unexpectedly");
240
241 } else {
242 $resp_code = $client->response_code();
243 $resp_msg = $client->response_msg();
244 }
245
246 my $expected;
247
248 $expected = 504;
249 $self->assert($expected == $resp_code,
250 test_msg("Expected $expected, got $resp_code"));
251
252 $expected = "'STRU R' unsupported structure type";
253 $self->assert($expected eq $resp_msg,
254 test_msg("Expected '$expected', got '$resp_msg'"));
255 };
256
257 if ($@) {
258 $ex = $@;
259 }
260
261 $wfh->print("done\n");
262 $wfh->flush();
263
264 } else {
265 eval { server_wait($config_file, $rfh) };
266 if ($@) {
267 warn($@);
268 exit 1;
269 }
270
271 exit 0;
272 }
273
274 # Stop server
275 server_stop($pid_file);
276
277 $self->assert_child_ok($pid);
278
279 if ($ex) {
280 test_append_logfile($log_file, $ex);
281 unlink($log_file);
282
283 die($ex);
284 }
285
286 unlink($log_file);
287 }
288
289 sub stru_page_fails {
290 my $self = shift;
291 my $tmpdir = $self->{tmpdir};
292
293 my $config_file = "$tmpdir/cmds.conf";
294 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
295 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
296
297 my $log_file = test_get_logfile();
298
299 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
300 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
301
302 my $user = 'proftpd';
303 my $passwd = 'test';
304 my $home_dir = File::Spec->rel2abs($tmpdir);
305 my $uid = 500;
306 my $gid = 500;
307
308 # Make sure that, if we're running as root, that the home directory has
309 # permissions/privs set for the account we create
310 if ($< == 0) {
311 unless (chmod(0755, $home_dir)) {
312 die("Can't set perms on $home_dir to 0755: $!");
313 }
314
315 unless (chown($uid, $gid, $home_dir)) {
316 die("Can't set owner of $home_dir to $uid/$gid: $!");
317 }
318 }
319
320 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
321 '/bin/bash');
322 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
323
324 my $config = {
325 PidFile => $pid_file,
326 ScoreboardFile => $scoreboard_file,
327 SystemLog => $log_file,
328
329 AuthUserFile => $auth_user_file,
330 AuthGroupFile => $auth_group_file,
331 AuthOrder => 'mod_auth_file.c',
332
333 IfModules => {
334 'mod_delay.c' => {
335 DelayEngine => 'off',
336 },
337 },
338 };
339
340 my ($port, $config_user, $config_group) = config_write($config_file, $config);
341
342 # Open pipes, for use between the parent and child processes. Specifically,
343 # the child will indicate when it's done with its test by writing a message
344 # to the parent.
345 my ($rfh, $wfh);
346 unless (pipe($rfh, $wfh)) {
347 die("Can't open pipe: $!");
348 }
349
350 my $ex;
351
352 # Fork child
353 $self->handle_sigchld();
354 defined(my $pid = fork()) or die("Can't fork: $!");
355 if ($pid) {
356 eval {
357 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
358 $client->login($user, $passwd);
359
360 my ($resp_code, $resp_msg);
361 eval { ($resp_code, $resp_msg) = $client->stru('page') };
362 unless ($@) {
363 die("STRU succeeded unexpectedly");
364
365 } else {
366 $resp_code = $client->response_code();
367 $resp_msg = $client->response_msg();
368 }
369
370 my $expected;
371
372 $expected = 504;
373 $self->assert($expected == $resp_code,
374 test_msg("Expected $expected, got $resp_code"));
375
376 $expected = "'STRU P' unsupported structure type";
377 $self->assert($expected eq $resp_msg,
378 test_msg("Expected '$expected', got '$resp_msg'"));
379 };
380
381 if ($@) {
382 $ex = $@;
383 }
384
385 $wfh->print("done\n");
386 $wfh->flush();
387
388 } else {
389 eval { server_wait($config_file, $rfh) };
390 if ($@) {
391 warn($@);
392 exit 1;
393 }
394
395 exit 0;
396 }
397
398 # Stop server
399 server_stop($pid_file);
400
401 $self->assert_child_ok($pid);
402
403 if ($ex) {
404 test_append_logfile($log_file, $ex);
405 unlink($log_file);
406
407 die($ex);
408 }
409
410 unlink($log_file);
411 }
412
413 sub stru_other_fails {
414 my $self = shift;
415 my $tmpdir = $self->{tmpdir};
416
417 my $config_file = "$tmpdir/cmds.conf";
418 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
419 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
420
421 my $log_file = test_get_logfile();
422
423 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
424 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
425
426 my $user = 'proftpd';
427 my $passwd = 'test';
428 my $home_dir = File::Spec->rel2abs($tmpdir);
429 my $uid = 500;
430 my $gid = 500;
431
432 # Make sure that, if we're running as root, that the home directory has
433 # permissions/privs set for the account we create
434 if ($< == 0) {
435 unless (chmod(0755, $home_dir)) {
436 die("Can't set perms on $home_dir to 0755: $!");
437 }
438
439 unless (chown($uid, $gid, $home_dir)) {
440 die("Can't set owner of $home_dir to $uid/$gid: $!");
441 }
442 }
443
444 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
445 '/bin/bash');
446 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
447
448 my $config = {
449 PidFile => $pid_file,
450 ScoreboardFile => $scoreboard_file,
451 SystemLog => $log_file,
452
453 AuthUserFile => $auth_user_file,
454 AuthGroupFile => $auth_group_file,
455 AuthOrder => 'mod_auth_file.c',
456
457 IfModules => {
458 'mod_delay.c' => {
459 DelayEngine => 'off',
460 },
461 },
462 };
463
464 my ($port, $config_user, $config_group) = config_write($config_file, $config);
465
466 # Open pipes, for use between the parent and child processes. Specifically,
467 # the child will indicate when it's done with its test by writing a message
468 # to the parent.
469 my ($rfh, $wfh);
470 unless (pipe($rfh, $wfh)) {
471 die("Can't open pipe: $!");
472 }
473
474 my $ex;
475
476 # Fork child
477 $self->handle_sigchld();
478 defined(my $pid = fork()) or die("Can't fork: $!");
479 if ($pid) {
480 eval {
481 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
482 $client->login($user, $passwd);
483
484 my ($resp_code, $resp_msg);
485 eval { ($resp_code, $resp_msg) = $client->stru('other') };
486 unless ($@) {
487 die("STRU succeeded unexpectedly");
488
489 } else {
490 $resp_code = $client->response_code();
491 $resp_msg = $client->response_msg();
492 }
493
494 my $expected;
495
496 $expected = 501;
497 $self->assert($expected == $resp_code,
498 test_msg("Expected $expected, got $resp_code"));
499
500 $expected = "'STRU other' unrecognized structure type";
501 $self->assert($expected eq $resp_msg,
502 test_msg("Expected '$expected', got '$resp_msg'"));
503 };
504
505 if ($@) {
506 $ex = $@;
507 }
508
509 $wfh->print("done\n");
510 $wfh->flush();
511
512 } else {
513 eval { server_wait($config_file, $rfh) };
514 if ($@) {
515 warn($@);
516 exit 1;
517 }
518
519 exit 0;
520 }
521
522 # Stop server
523 server_stop($pid_file);
524
525 $self->assert_child_ok($pid);
526
527 if ($ex) {
528 test_append_logfile($log_file, $ex);
529 unlink($log_file);
530
531 die($ex);
532 }
533
534 unlink($log_file);
535 }
536
537 1;