Debian Codegraph @

tests/t/lib/ProFTPD/Tests/Logging/ServerLog.pm

raw
1 package ProFTPD::Tests::Logging::ServerLog;
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 serverlog_ifclass_matching_class_bug3832 => {
19 order => ++$order,
20 test_class => [qw(bug forking mod_ifsession)],
21 },
22
23 serverlog_ifclass_not_matching_class_bug3832 => {
24 order => ++$order,
25 test_class => [qw(bug forking mod_ifsession)],
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 serverlog_ifclass_matching_class_bug3832 {
39 my $self = shift;
40 my $tmpdir = $self->{tmpdir};
41
42 my $config_file = "$tmpdir/serverlog.conf";
43 my $pid_file = File::Spec->rel2abs("$tmpdir/serverlog.pid");
44 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/serverlog.scoreboard");
45
46 my $auth_user_file = File::Spec->rel2abs("$tmpdir/serverlog.passwd");
47 my $auth_group_file = File::Spec->rel2abs("$tmpdir/serverlog.group");
48
49 my $test_file = File::Spec->rel2abs($config_file);
50
51 my $user = 'proftpd';
52 my $passwd = 'test';
53 my $group = 'ftpd';
54 my $home_dir = File::Spec->rel2abs($tmpdir);
55 my $uid = 500;
56 my $gid = 500;
57
58 # Make sure that, if we're running as root, that the home directory has
59 # permissions/privs set for the account we create
60 if ($< == 0) {
61 unless (chmod(0755, $home_dir)) {
62 die("Can't set perms on $home_dir to 0755: $!");
63 }
64
65 unless (chown($uid, $gid, $home_dir)) {
66 die("Can't set owner of $home_dir to $uid/$gid: $!");
67 }
68 }
69
70 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
71 '/bin/bash');
72 auth_group_write($auth_group_file, $group, $gid, $user);
73
74 my $server_log = File::Spec->rel2abs("$tmpdir/server.log");
75
76 my $config = {
77 PidFile => $pid_file,
78 ScoreboardFile => $scoreboard_file,
79 ServerLog => $server_log,
80
81 AuthUserFile => $auth_user_file,
82 AuthGroupFile => $auth_group_file,
83 AuthOrder => 'mod_auth_file.c',
84
85 IfModules => {
86 'mod_delay.c' => {
87 DelayEngine => 'off',
88 },
89 },
90 };
91
92 my ($port, $config_user, $config_group) = config_write($config_file, $config);
93
94 # Append the mod_ifsession config to the end of the config file
95 if (open(my $fh, ">> $config_file")) {
96 print $fh <<EOC;
97 <Class local>
98 From 127.0.0.1
99 </Class>
100
101 <IfClass local>
102 ServerLog none
103 </IfClass>
104 EOC
105 unless (close($fh)) {
106 die("Can't write $config_file: $!");
107 }
108
109 } else {
110 die("Can't open $config_file: $!");
111 }
112
113 # Open pipes, for use between the parent and child processes. Specifically,
114 # the child will indicate when it's done with its test by writing a message
115 # to the parent.
116 my ($rfh, $wfh);
117 unless (pipe($rfh, $wfh)) {
118 die("Can't open pipe: $!");
119 }
120
121 my $ex;
122
123 # Fork child
124 $self->handle_sigchld();
125 defined(my $pid = fork()) or die("Can't fork: $!");
126 if ($pid) {
127 eval {
128 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
129 $client->login($user, $passwd);
130 $client->type('ascii');
131
132 my $conn = $client->retr_raw($test_file);
133 unless ($conn) {
134 die("Failed to RETR: " . $client->response_code() . " " .
135 $client->response_msg());
136 }
137
138 my $buf;
139 $conn->read($buf, 8192, 25);
140 eval { $conn->close() };
141 $client->quit();
142 };
143
144 if ($@) {
145 $ex = $@;
146 }
147
148 $wfh->print("done\n");
149 $wfh->flush();
150
151 } else {
152 eval { server_wait($config_file, $rfh) };
153 if ($@) {
154 warn($@);
155 exit 1;
156 }
157
158 exit 0;
159 }
160
161 # Stop server
162 server_stop($pid_file);
163
164 $self->assert_child_ok($pid);
165
166 $self->assert(!-f $server_log,
167 test_msg("Expected ServerLog $server_log to not exist, but it does"));
168
169 if ($ex) {
170 test_append_logfile($server_log, $ex);
171 unlink($server_log);
172
173 die($ex);
174 }
175
176 unlink($server_log);
177 }
178
179 sub serverlog_ifclass_not_matching_class_bug3832 {
180 my $self = shift;
181 my $tmpdir = $self->{tmpdir};
182
183 my $config_file = "$tmpdir/serverlog.conf";
184 my $pid_file = File::Spec->rel2abs("$tmpdir/serverlog.pid");
185 my $scoreboard_file = File::Spec->rel2abs("$tmpdir/serverlog.scoreboard");
186
187 my $auth_user_file = File::Spec->rel2abs("$tmpdir/serverlog.passwd");
188 my $auth_group_file = File::Spec->rel2abs("$tmpdir/serverlog.group");
189
190 my $test_file = File::Spec->rel2abs($config_file);
191
192 my $user = 'proftpd';
193 my $passwd = 'test';
194 my $group = 'ftpd';
195 my $home_dir = File::Spec->rel2abs($tmpdir);
196 my $uid = 500;
197 my $gid = 500;
198
199 # Make sure that, if we're running as root, that the home directory has
200 # permissions/privs set for the account we create
201 if ($< == 0) {
202 unless (chmod(0755, $home_dir)) {
203 die("Can't set perms on $home_dir to 0755: $!");
204 }
205
206 unless (chown($uid, $gid, $home_dir)) {
207 die("Can't set owner of $home_dir to $uid/$gid: $!");
208 }
209 }
210
211 auth_user_write($auth_user_file, $user, $passwd, $uid, $gid, $home_dir,
212 '/bin/bash');
213 auth_group_write($auth_group_file, $group, $gid, $user);
214
215 my $server_log = File::Spec->rel2abs("$tmpdir/server.log");
216
217 my $config = {
218 PidFile => $pid_file,
219 ScoreboardFile => $scoreboard_file,
220 ServerLog => $server_log,
221
222 AuthUserFile => $auth_user_file,
223 AuthGroupFile => $auth_group_file,
224 AuthOrder => 'mod_auth_file.c',
225
226 IfModules => {
227 'mod_delay.c' => {
228 DelayEngine => 'off',
229 },
230 },
231 };
232
233 my ($port, $config_user, $config_group) = config_write($config_file, $config);
234
235 # Append the mod_ifsession config to the end of the config file
236 if (open(my $fh, ">> $config_file")) {
237 print $fh <<EOC;
238 <Class local>
239 From 127.0.0.1
240 </Class>
241
242 <IfClass !local>
243 ServerLog none
244 </IfClass>
245 EOC
246 unless (close($fh)) {
247 die("Can't write $config_file: $!");
248 }
249
250 } else {
251 die("Can't open $config_file: $!");
252 }
253
254 # Open pipes, for use between the parent and child processes. Specifically,
255 # the child will indicate when it's done with its test by writing a message
256 # to the parent.
257 my ($rfh, $wfh);
258 unless (pipe($rfh, $wfh)) {
259 die("Can't open pipe: $!");
260 }
261
262 my $ex;
263
264 # Fork child
265 $self->handle_sigchld();
266 defined(my $pid = fork()) or die("Can't fork: $!");
267 if ($pid) {
268 eval {
269 my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port);
270 $client->login($user, $passwd);
271 $client->type('ascii');
272
273 my $conn = $client->retr_raw($test_file);
274 unless ($conn) {
275 die("Failed to RETR: " . $client->response_code() . " " .
276 $client->response_msg());
277 }
278
279 my $buf;
280 $conn->read($buf, 8192, 25);
281 eval { $conn->close() };
282 $client->quit();
283 };
284
285 if ($@) {
286 $ex = $@;
287 }
288
289 $wfh->print("done\n");
290 $wfh->flush();
291
292 } else {
293 eval { server_wait($config_file, $rfh) };
294 if ($@) {
295 warn($@);
296 exit 1;
297 }
298
299 exit 0;
300 }
301
302 # Stop server
303 server_stop($pid_file);
304
305 $self->assert_child_ok($pid);
306
307 if (open(my $fh, "< $server_log")) {
308 my $sess_nlines = 0;
309 while (my $line = <$fh>) {
310 chomp($line);
311
312 if ($line =~ /session (opened|closed)/) {
313 $sess_nlines++;
314 }
315 }
316
317 close($fh);
318
319 $self->assert($sess_nlines > 0,
320 test_msg("Expected lines in $server_log, got none"));
321
322 } else {
323 die("Can't read $server_log: $!");
324 }
325
326 if ($ex) {
327 test_append_logfile($server_log, $ex);
328 unlink($server_log);
329
330 die($ex);
331 }
332
333 unlink($server_log);
334 }
335
336 1;