Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Commands::QUIT;
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 quit_ok => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 };
24
25 sub new {
26 return shift()->SUPER::new(@_);
27 }
28
29 sub list_tests {
30 return testsuite_get_runnable_tests($TESTS);
31 }
32
33 sub quit_ok {
34 my $self = shift;
35 my $tmpdir = $self->{tmpdir};
36
37 my $config_file = "$tmpdir/cmds.conf";
38 my $pid_file = File::Spec->rel2abs("$tmpdir/cmds.pid");
39 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/cmds.scoreboard");
40
41 my $log_file = test_get_logfile();
42
43 my $auth_user_file = File::Spec->rel2abs("$tmpdir/cmds.passwd");
44 my $auth_group_file = File::Spec->rel2abs("$tmpdir/cmds.group");
45
46 my $user = 'proftpd';
47 my $passwd = 'test';
48 my $home_dir = File::Spec->rel2abs($tmpdir);
49 my $uid = 500;
50 my $gid = 500;
51
52 # Make sure that, if we're running as root, that the home directory has
53 # permissions/privs set for the account we create
54 if ($< == 0) {
55 unless (chmod(0755, $home_dir)) {
56 die("Can't set perms on $home_dir to 0755: $!");
57 }
58
59 unless (chown($uid, $gid, $home_dir)) {
60 die("Can't set owner of $home_dir to $uid/$gid: $!");
61 }
62 }
63
64 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
65 '/bin/bash');
66 auth_group_write($auth_group_file, 'ftpd', $gid, $user);
67
68 my $config = {
69 PidFile => $pid_file,
70 ScoreboardFile => $scoreboard_file,
71 SystemLog => $log_file,
72
73 AuthUserFile => $auth_user_file,
74 AuthGroupFile => $auth_group_file,
75 AuthOrder => 'mod_auth_file.c',
76
77 IfModules => {
78 'mod_delay.c' => {
79 DelayEngine => 'off',
80 },
81 },
82 };
83
84 my ($port, $config_user, $config_group) = config_write($config_file, $config);
85
86 # Open pipes, for use between the parent and child processes. Specifically,
87 # the child will indicate when it's done with its test by writing a message
88 # to the parent.
89 my ($rfh, $wfh);
90 unless (pipe($rfh, $wfh)) {
91 die("Can't open pipe: $!");
92 }
93
94 my $ex;
95
96 # Fork child
97 $self->handle_sigchld();
98 defined(my $pid = fork()) or die("Can't fork: $!");
99 if ($pid) {
100 eval {
101 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
102
103 $client->login($user, $passwd);
104
105 my ($resp_code, $resp_msg);
106 ($resp_code, $resp_msg) = $client->quit();
107
108 my $expected;
109
110 $expected = 221;
111 $self->assert($expected == $resp_code,
112 test_msg("Expected $expected, got $resp_code"));
113
114 $expected = "Goodbye.";
115 $self->assert($expected eq $resp_msg,
116 test_msg("Expected '$expected', got '$resp_msg'"));
117 };
118
119 if ($@) {
120 $ex = $@;
121 }
122
123 $wfh->print("done\n");
124 $wfh->flush();
125
126 } else {
127 eval { server_wait($config_file, $rfh) };
128 if ($@) {
129 warn($@);
130 exit 1;
131 }
132
133 exit 0;
134 }
135
136 # Stop server
137 server_stop($pid_file);
138
139 $self->assert_child_ok($pid);
140
141 if ($ex) {
142 test_append_logfile($log_file, $ex);
143 unlink($log_file);
144
145 die($ex);
146 }
147
148 unlink($log_file);
149 }
150
151 1;