Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::DisplayQuit;
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 displayquit_abs_path => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 displayquit_rel_path => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 displayquit_multiline => {
29 order => ++$order,
30 test_class => [qw(forking)],
31 },
32
33 displayquit_non_path => {
34 order => ++$order,
35 test_class => [qw(forking)],
36 },
37
38 # XXX Add other tests with the various Display variables
39 };
40
41 sub new {
42 return shift()->SUPER::new(@_);
43 }
44
45 sub list_tests {
46 return testsuite_get_runnable_tests($TESTS);
47 }
48
49 sub displayquit_abs_path {
50 my $self = shift;
51 my $tmpdir = $self->{tmpdir};
52
53 my $config_file = "$tmpdir/config.conf";
54 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
55 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
56
57 my $log_file = File::Spec->rel2abs('tests.log');
58
59 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
60 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
61
62 my $user = 'proftpd';
63 my $passwd = 'test';
64 my $home_dir = File::Spec->rel2abs($tmpdir);
65
66 auth_user_write($auth_user_file, $user, $passwd, 500, 500, $home_dir,
67 '/bin/bash');
68 auth_group_write($auth_group_file, 'ftpd', 500, $user);
69
70 my $quit_file = File::Spec->rel2abs("$tmpdir/quit.txt");
71
72 if (open(my $fh, "> $quit_file")) {
73 print $fh "Goodbye user!\n";
74
75 unless (close($fh)) {
76 die("Unable to write $quit_file: $!");
77 }
78
79 } else {
80 die("Unable to open $quit_file: $!");
81 }
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 DisplayQuit => $quit_file,
93
94 IfModules => {
95 'mod_delay.c' => {
96 DelayEngine => 'off',
97 },
98 },
99 };
100
101 my ($port, $config_user, $config_group) = config_write($config_file, $config);
102
103 # Open pipes, for use between the parent and child processes. Specifically,
104 # the child will indicate when it's done with its test by writing a message
105 # to the parent.
106 my ($rfh, $wfh);
107 unless (pipe($rfh, $wfh)) {
108 die("Can't open pipe: $!");
109 }
110
111 my $ex;
112
113 # Fork child
114 $self->handle_sigchld();
115 defined(my $pid = fork()) or die("Can't fork: $!");
116 if ($pid) {
117 eval {
118 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
119
120 $client->login($user, $passwd);
121
122 my ($resp_code, $resp_msg) = $client->quit();
123
124 $resp_code = $client->response_code();
125 $resp_msg = $client->response_msg(0);
126
127 my $expected;
128
129 $expected = 221;
130 $self->assert($expected == $resp_code,
131 test_msg("Expected $expected, got $resp_code"));
132
133 $expected = "Goodbye user!";
134 $self->assert($expected eq $resp_msg,
135 test_msg("Expected '$expected', got '$resp_msg'"));
136 };
137
138 if ($@) {
139 $ex = $@;
140 }
141
142 $wfh->print("done\n");
143 $wfh->flush();
144
145 } else {
146 eval { server_wait($config_file, $rfh) };
147 if ($@) {
148 warn($@);
149 exit 1;
150 }
151
152 exit 0;
153 }
154
155 # Stop server
156 server_stop($pid_file);
157
158 $self->assert_child_ok($pid);
159
160 if ($ex) {
161 die($ex);
162 }
163
164 unlink($log_file);
165 }
166
167 sub displayquit_rel_path {
168 my $self = shift;
169 my $tmpdir = $self->{tmpdir};
170
171 my $config_file = "$tmpdir/config.conf";
172 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
173 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
174
175 my $log_file = File::Spec->rel2abs('tests.log');
176
177 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
178 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
179
180 my $user = 'proftpd';
181 my $passwd = 'test';
182 my $home_dir = File::Spec->rel2abs($tmpdir);
183
184 auth_user_write($auth_user_file, $user, $passwd, 500, 500, $home_dir,
185 '/bin/bash');
186 auth_group_write($auth_group_file, 'ftpd', 500, $user);
187
188 my $quit_file = 'quit.txt';
189
190 my $quit_path = File::Spec->rel2abs("$tmpdir/$quit_file");
191
192 if (open(my $fh, "> $quit_path")) {
193 print $fh "Goodbye user!\n";
194
195 unless (close($fh)) {
196 die("Unable to write $quit_path: $!");
197 }
198
199 } else {
200 die("Unable to open $quit_path: $!");
201 }
202
203 my $config = {
204 PidFile => $pid_file,
205 ScoreboardFile => $scoreboard_file,
206 SystemLog => $log_file,
207
208 AuthUserFile => $auth_user_file,
209 AuthGroupFile => $auth_group_file,
210 AuthOrder => 'mod_auth_file.c',
211
212 DisplayQuit => $quit_file,
213
214 IfModules => {
215 'mod_delay.c' => {
216 DelayEngine => 'off',
217 },
218 },
219 };
220
221 my ($port, $config_user, $config_group) = config_write($config_file, $config);
222
223 # Open pipes, for use between the parent and child processes. Specifically,
224 # the child will indicate when it's done with its test by writing a message
225 # to the parent.
226 my ($rfh, $wfh);
227 unless (pipe($rfh, $wfh)) {
228 die("Can't open pipe: $!");
229 }
230
231 my $ex;
232
233 # Fork child
234 $self->handle_sigchld();
235 defined(my $pid = fork()) or die("Can't fork: $!");
236 if ($pid) {
237 eval {
238 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
239 $client->login($user, $passwd);
240
241 my ($resp_code, $resp_msg);
242
243 $client->quit();
244
245 $resp_code = $client->response_code();
246 $resp_msg = $client->response_msg(0);
247
248 my $expected;
249
250 $expected = 221;
251 $self->assert($expected == $resp_code,
252 test_msg("Expected $expected, got $resp_code"));
253
254 $expected = "Goodbye user!";
255 $self->assert($expected eq $resp_msg,
256 test_msg("Expected '$expected', got '$resp_msg'"));
257 };
258
259 if ($@) {
260 $ex = $@;
261 }
262
263 $wfh->print("done\n");
264 $wfh->flush();
265
266 } else {
267 eval { server_wait($config_file, $rfh) };
268 if ($@) {
269 warn($@);
270 exit 1;
271 }
272
273 exit 0;
274 }
275
276 # Stop server
277 server_stop($pid_file);
278
279 $self->assert_child_ok($pid);
280
281 if ($ex) {
282 die($ex);
283 }
284
285 unlink($log_file);
286 }
287
288 sub displayquit_multiline {
289 my $self = shift;
290 my $tmpdir = $self->{tmpdir};
291
292 my $config_file = "$tmpdir/config.conf";
293 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
294 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
295
296 my $log_file = File::Spec->rel2abs('tests.log');
297
298 my $auth_user_file = File::Spec->rel2abs("$tmpdir/config.passwd");
299 my $auth_group_file = File::Spec->rel2abs("$tmpdir/config.group");
300
301 my $user = 'proftpd';
302 my $passwd = 'test';
303 my $home_dir = File::Spec->rel2abs($tmpdir);
304
305 auth_user_write($auth_user_file, $user, $passwd, 500, 500, $home_dir,
306 '/bin/bash');
307 auth_group_write($auth_group_file, 'ftpd', 500, $user);
308
309 my $quit_file = File::Spec->rel2abs("$tmpdir/quit.txt");
310
311 if (open(my $fh, "> $quit_file")) {
312 print $fh "It has been fun...\n...but now it's time to go\nGoodbye user!\n";
313
314 unless (close($fh)) {
315 die("Unable to write $quit_file: $!");
316 }
317
318 } else {
319 die("Unable to open $quit_file: $!");
320 }
321
322 my $config = {
323 PidFile => $pid_file,
324 ScoreboardFile => $scoreboard_file,
325 SystemLog => $log_file,
326
327 AuthUserFile => $auth_user_file,
328 AuthGroupFile => $auth_group_file,
329 AuthOrder => 'mod_auth_file.c',
330
331 DisplayQuit => $quit_file,
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
362 $client->quit();
363
364 $resp_code = $client->response_code();
365 $resp_msg = $client->response_msg(2);
366
367 my $expected;
368
369 $expected = 221;
370 $self->assert($expected == $resp_code,
371 test_msg("Expected $expected, got $resp_code"));
372
373 $expected = "Goodbye user!";
374 $self->assert($expected eq $resp_msg,
375 test_msg("Expected '$expected', got '$resp_msg'"));
376 };
377
378 if ($@) {
379 $ex = $@;
380 }
381
382 $wfh->print("done\n");
383 $wfh->flush();
384
385 } else {
386 eval { server_wait($config_file, $rfh) };
387 if ($@) {
388 warn($@);
389 exit 1;
390 }
391
392 exit 0;
393 }
394
395 # Stop server
396 server_stop($pid_file);
397
398 $self->assert_child_ok($pid);
399
400 if ($ex) {
401 die($ex);
402 }
403
404 unlink($log_file);
405 }
406
407 sub displayquit_non_path {
408 my $self = shift;
409 my $tmpdir = $self->{tmpdir};
410 my $setup = test_setup($tmpdir, 'config');
411
412 my $quit_file = '"GOODBYE: Ya\'ll come son now! Ya hear?"';
413
414 my $config = {
415 PidFile => $setup->{pid_file},
416 ScoreboardFile => $setup->{scoreboard_file},
417 SystemLog => $setup->{log_file},
418
419 AuthUserFile => $setup->{auth_user_file},
420 AuthGroupFile => $setup->{auth_group_file},
421 AuthOrder => 'mod_auth_file.c',
422
423 DisplayQuit => $quit_file,
424
425 IfModules => {
426 'mod_delay.c' => {
427 DelayEngine => 'off',
428 },
429 },
430 };
431
432 my ($port, $config_user, $config_group) = config_write($setup->{config_file},
433 $config);
434
435 # Open pipes, for use between the parent and child processes. Specifically,
436 # the child will indicate when it's done with its test by writing a message
437 # to the parent.
438 my ($rfh, $wfh);
439 unless (pipe($rfh, $wfh)) {
440 die("Can't open pipe: $!");
441 }
442
443 my $ex;
444
445 # Fork child
446 $self->handle_sigchld();
447 defined(my $pid = fork()) or die("Can't fork: $!");
448 if ($pid) {
449 eval {
450 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port, 0, 1);
451 $client->login($setup->{user}, $setup->{passwd});
452 $client->quit();
453
454 my $resp_code = $client->response_code();
455 my $resp_msg = $client->response_msg(0);
456
457 my $expected = 221;
458 $self->assert($expected == $resp_code,
459 test_msg("Expected response code $expected, got $resp_code"));
460
461 $expected = "Goodbye.";
462 $self->assert($expected eq $resp_msg,
463 test_msg("Expected response message '$expected', got '$resp_msg'"));
464 };
465
466 if ($@) {
467 $ex = $@;
468 }
469
470 $wfh->print("done\n");
471 $wfh->flush();
472
473 } else {
474 eval { server_wait($setup->{config_file}, $rfh) };
475 if ($@) {
476 warn($@);
477 exit 1;
478 }
479
480 exit 0;
481 }
482
483 # Stop server
484 server_stop($setup->{pid_file});
485 $self->assert_child_ok($pid);
486
487 test_cleanup($setup->{log_file}, $ex);
488 }
489
490 1;