Debian Codegraph @

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

raw
1 package ProFTPD::Tests::Config::ServerAdmin;
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 serveradmin_default => {
19 order => ++$order,
20 test_class => [qw(forking)],
21 },
22
23 serveradmin_explicit => {
24 order => ++$order,
25 test_class => [qw(forking)],
26 },
27
28 };
29
30 sub new {
31 return shift()->SUPER::new(@_);
32 }
33
34 sub list_tests {
35 return testsuite_get_runnable_tests($TESTS);
36 }
37
38 sub serveradmin_default {
39 my $self = shift;
40 my $tmpdir = $self->{tmpdir};
41
42 my $config_file = "$tmpdir/config.conf";
43 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
44 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
45
46 my $log_file = File::Spec->rel2abs('tests.log');
47
48 my $connect_file = File::Spec->rel2abs("$tmpdir/connect.txt");
49 if (open(my $fh, "> $connect_file")) {
50 print $fh "Report issues to %E\n";
51
52 unless (close($fh)) {
53 die("Can't write $connect_file: $!");
54 }
55
56 } else {
57 die("Can't open $connect_file: $!");
58 }
59
60 my $config = {
61 PidFile => $pid_file,
62 ScoreboardFile => $scoreboard_file,
63 SystemLog => $log_file,
64
65 DisplayConnect => $connect_file,
66
67 IfModules => {
68 'mod_delay.c' => {
69 DelayEngine => 'off',
70 },
71 },
72 };
73
74 my ($port, $config_user, $config_group) = config_write($config_file, $config);
75
76 # Open pipes, for use between the parent and child processes. Specifically,
77 # the child will indicate when it's done with its test by writing a message
78 # to the parent.
79 my ($rfh, $wfh);
80 unless (pipe($rfh, $wfh)) {
81 die("Can't open pipe: $!");
82 }
83
84 my $ex;
85
86 # Fork child
87 $self->handle_sigchld();
88 defined(my $pid = fork()) or die("Can't fork: $!");
89 if ($pid) {
90 eval {
91 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
92
93 my ($resp_code, $resp_msg);
94
95 $resp_code = $client->response_code();
96 $resp_msg = $client->response_msg(0);
97
98 my $expected;
99
100 $expected = 220;
101 $self->assert($expected == $resp_code,
102 test_msg("Expected $expected, got $resp_code"));
103
104 $expected = "Report issues to root\@127.0.0.1";
105 $self->assert($expected eq $resp_msg,
106 test_msg("Expected '$expected', got '$resp_msg'"));
107 };
108
109 if ($@) {
110 $ex = $@;
111 }
112
113 $wfh->print("done\n");
114 $wfh->flush();
115
116 } else {
117 eval { server_wait($config_file, $rfh) };
118 if ($@) {
119 warn($@);
120 exit 1;
121 }
122
123 exit 0;
124 }
125
126 # Stop server
127 server_stop($pid_file);
128
129 $self->assert_child_ok($pid);
130
131 if ($ex) {
132 die($ex);
133 }
134
135 unlink($log_file);
136 }
137
138 sub serveradmin_explicit {
139 my $self = shift;
140 my $tmpdir = $self->{tmpdir};
141
142 my $config_file = "$tmpdir/config.conf";
143 my $pid_file = File::Spec->rel2abs("$tmpdir/config.pid");
144 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/config.scoreboard");
145
146 my $log_file = File::Spec->rel2abs('tests.log');
147
148 my $connect_file = File::Spec->rel2abs("$tmpdir/connect.txt");
149 if (open(my $fh, "> $connect_file")) {
150 print $fh "Report issues to %E\n";
151
152 unless (close($fh)) {
153 die("Can't write $connect_file: $!");
154 }
155
156 } else {
157 die("Can't open $connect_file: $!");
158 }
159
160 my $server_admin = '[email protected]';
161
162 my $config = {
163 PidFile => $pid_file,
164 ScoreboardFile => $scoreboard_file,
165 SystemLog => $log_file,
166
167 DisplayConnect => $connect_file,
168 ServerAdmin => $server_admin,
169
170 IfModules => {
171 'mod_delay.c' => {
172 DelayEngine => 'off',
173 },
174 },
175 };
176
177 my ($port, $config_user, $config_group) = config_write($config_file, $config);
178
179 # Open pipes, for use between the parent and child processes. Specifically,
180 # the child will indicate when it's done with its test by writing a message
181 # to the parent.
182 my ($rfh, $wfh);
183 unless (pipe($rfh, $wfh)) {
184 die("Can't open pipe: $!");
185 }
186
187 my $ex;
188
189 # Fork child
190 $self->handle_sigchld();
191 defined(my $pid = fork()) or die("Can't fork: $!");
192 if ($pid) {
193 eval {
194 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
195
196 my ($resp_code, $resp_msg);
197
198 $resp_code = $client->response_code();
199 $resp_msg = $client->response_msg(0);
200
201 my $expected;
202
203 $expected = 220;
204 $self->assert($expected == $resp_code,
205 test_msg("Expected $expected, got $resp_code"));
206
207 $expected = "Report issues to $server_admin";
208 $self->assert($expected eq $resp_msg,
209 test_msg("Expected '$expected', got '$resp_msg'"));
210 };
211
212 if ($@) {
213 $ex = $@;
214 }
215
216 $wfh->print("done\n");
217 $wfh->flush();
218
219 } else {
220 eval { server_wait($config_file, $rfh) };
221 if ($@) {
222 warn($@);
223 exit 1;
224 }
225
226 exit 0;
227 }
228
229 # Stop server
230 server_stop($pid_file);
231
232 $self->assert_child_ok($pid);
233
234 if ($ex) {
235 die($ex);
236 }
237
238 unlink($log_file);
239 }
240
241 1;