| 1 |
package ProFTPD::Tests::Config::TraceOptions; |
| 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 |
traceoptions_conn_ips => { |
| 19 |
order => ++$order, |
| 20 |
test_class => [qw(feature_trace forking)], |
| 21 |
}, |
| 22 |
|
| 23 |
traceoptions_timestamp_millis => { |
| 24 |
order => ++$order, |
| 25 |
test_class => [qw(feature_trace forking)], |
| 26 |
}, |
| 27 |
|
| 28 |
traceoptions_no_timestamp => { |
| 29 |
order => ++$order, |
| 30 |
test_class => [qw(feature_trace forking)], |
| 31 |
}, |
| 32 |
}; |
| 33 |
|
| 34 |
sub new { |
| 35 |
return shift()->SUPER::new(@_); |
| 36 |
} |
| 37 |
|
| 38 |
sub list_tests { |
| 39 |
return testsuite_get_runnable_tests($TESTS); |
| 40 |
} |
| 41 |
|
| 42 |
sub traceoptions_conn_ips { |
| 43 |
my $self = shift; |
| 44 |
my $tmpdir = $self->{tmpdir}; |
| 45 |
my $setup = test_setup($tmpdir, 'trace'); |
| 46 |
|
| 47 |
my $trace_log = File::Spec->rel2abs("$tmpdir/trace.log"); |
| 48 |
|
| 49 |
my $config = { |
| 50 |
PidFile => $setup->{pid_file}, |
| 51 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 52 |
SystemLog => $setup->{log_file}, |
| 53 |
TraceLog => $trace_log, |
| 54 |
Trace => 'DEFAULT:10', |
| 55 |
TraceOptions => '+ConnIPs', |
| 56 |
|
| 57 |
AuthUserFile => $setup->{auth_user_file}, |
| 58 |
AuthGroupFile => $setup->{auth_group_file}, |
| 59 |
AuthOrder => 'mod_auth_file.c', |
| 60 |
|
| 61 |
IfModules => { |
| 62 |
'mod_delay.c' => { |
| 63 |
DelayEngine => 'off', |
| 64 |
}, |
| 65 |
}, |
| 66 |
}; |
| 67 |
|
| 68 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 69 |
$config); |
| 70 |
|
| 71 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 72 |
# the child will indicate when it's done with its test by writing a message |
| 73 |
# to the parent. |
| 74 |
my ($rfh, $wfh); |
| 75 |
unless (pipe($rfh, $wfh)) { |
| 76 |
die("Can't open pipe: $!"); |
| 77 |
} |
| 78 |
|
| 79 |
my $ex; |
| 80 |
|
| 81 |
# Fork child |
| 82 |
$self->handle_sigchld(); |
| 83 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 84 |
if ($pid) { |
| 85 |
eval { |
| 86 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 87 |
$client->login($setup->{user}, $setup->{passwd}); |
| 88 |
$client->quit(); |
| 89 |
}; |
| 90 |
if ($@) { |
| 91 |
$ex = $@; |
| 92 |
} |
| 93 |
|
| 94 |
$wfh->print("done\n"); |
| 95 |
$wfh->flush(); |
| 96 |
|
| 97 |
} else { |
| 98 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 99 |
if ($@) { |
| 100 |
warn($@); |
| 101 |
exit 1; |
| 102 |
} |
| 103 |
|
| 104 |
exit 0; |
| 105 |
} |
| 106 |
|
| 107 |
# Stop server |
| 108 |
server_stop($setup->{pid_file}); |
| 109 |
$self->assert_child_ok($pid); |
| 110 |
|
| 111 |
eval { |
| 112 |
if (open(my $fh, "< $trace_log")) { |
| 113 |
my $ok = 0; |
| 114 |
|
| 115 |
while (my $line = <$fh>) { |
| 116 |
chomp($line); |
| 117 |
|
| 118 |
my $expected = '\[\d+\]\s+\(client\s+(\S+), server\s+\S+\)\s+<(\S+):(\d+)>: (.*?)$'; |
| 119 |
|
| 120 |
if ($line =~ /$expected/) { |
| 121 |
my $client_ip = $1; |
| 122 |
my $trace_channel = $2; |
| 123 |
my $trace_level = $3; |
| 124 |
my $trace_msg = $4; |
| 125 |
|
| 126 |
next unless $trace_channel eq 'command'; |
| 127 |
|
| 128 |
my $expected = 'command'; |
| 129 |
$self->assert($expected eq $trace_channel, |
| 130 |
test_msg("Expected '$expected', got '$trace_channel'")); |
| 131 |
|
| 132 |
$expected = 7; |
| 133 |
$self->assert($expected >= $trace_level, |
| 134 |
test_msg("Expected >= $expected, got $trace_level")); |
| 135 |
|
| 136 |
$expected = '127.0.0.1'; |
| 137 |
$self->assert($expected eq $client_ip, |
| 138 |
test_msg("Expected '$expected', got '$client_ip'")); |
| 139 |
|
| 140 |
$ok = 1; |
| 141 |
last; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
close($fh); |
| 146 |
|
| 147 |
$self->assert($ok, test_msg("Missing expected TraceLog messages")); |
| 148 |
|
| 149 |
} else { |
| 150 |
die("Can't read $trace_log: $!"); |
| 151 |
} |
| 152 |
}; |
| 153 |
if ($@) { |
| 154 |
$ex = $@; |
| 155 |
} |
| 156 |
|
| 157 |
test_cleanup($setup->{log_file}, $ex); |
| 158 |
} |
| 159 |
|
| 160 |
sub traceoptions_timestamp_millis { |
| 161 |
my $self = shift; |
| 162 |
my $tmpdir = $self->{tmpdir}; |
| 163 |
my $setup = test_setup($tmpdir, 'trace'); |
| 164 |
|
| 165 |
my $trace_log = File::Spec->rel2abs("$tmpdir/trace.log"); |
| 166 |
|
| 167 |
my $config = { |
| 168 |
PidFile => $setup->{pid_file}, |
| 169 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 170 |
SystemLog => $setup->{log_file}, |
| 171 |
TraceLog => $trace_log, |
| 172 |
Trace => 'DEFAULT:10', |
| 173 |
TraceOptions => '+TimestampMillis', |
| 174 |
|
| 175 |
AuthUserFile => $setup->{auth_user_file}, |
| 176 |
AuthGroupFile => $setup->{auth_group_file}, |
| 177 |
AuthOrder => 'mod_auth_file.c', |
| 178 |
|
| 179 |
IfModules => { |
| 180 |
'mod_delay.c' => { |
| 181 |
DelayEngine => 'off', |
| 182 |
}, |
| 183 |
}, |
| 184 |
}; |
| 185 |
|
| 186 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 187 |
$config); |
| 188 |
|
| 189 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 190 |
# the child will indicate when it's done with its test by writing a message |
| 191 |
# to the parent. |
| 192 |
my ($rfh, $wfh); |
| 193 |
unless (pipe($rfh, $wfh)) { |
| 194 |
die("Can't open pipe: $!"); |
| 195 |
} |
| 196 |
|
| 197 |
my $ex; |
| 198 |
|
| 199 |
# Fork child |
| 200 |
$self->handle_sigchld(); |
| 201 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 202 |
if ($pid) { |
| 203 |
eval { |
| 204 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 205 |
$client->login($setup->{user}, $setup->{passwd}); |
| 206 |
$client->quit(); |
| 207 |
}; |
| 208 |
if ($@) { |
| 209 |
$ex = $@; |
| 210 |
} |
| 211 |
|
| 212 |
$wfh->print("done\n"); |
| 213 |
$wfh->flush(); |
| 214 |
|
| 215 |
} else { |
| 216 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 217 |
if ($@) { |
| 218 |
warn($@); |
| 219 |
exit 1; |
| 220 |
} |
| 221 |
|
| 222 |
exit 0; |
| 223 |
} |
| 224 |
|
| 225 |
# Stop server |
| 226 |
server_stop($setup->{pid_file}); |
| 227 |
$self->assert_child_ok($pid); |
| 228 |
|
| 229 |
eval { |
| 230 |
if (open(my $fh, "< $trace_log")) { |
| 231 |
my $ok = 0; |
| 232 |
|
| 233 |
while (my $line = <$fh>) { |
| 234 |
chomp($line); |
| 235 |
|
| 236 |
if ($ENV{TEST_VERBOSE}) { |
| 237 |
print STDERR "# $line\n"; |
| 238 |
} |
| 239 |
|
| 240 |
my $expected = '\d+:\d+:\d+,\d+\s+\[\d+\]\s+<(\S+):(\d+)>: (.*?)$'; |
| 241 |
|
| 242 |
if ($line =~ /$expected/) { |
| 243 |
my $trace_channel = $1; |
| 244 |
my $trace_level = $2; |
| 245 |
my $trace_msg = $3; |
| 246 |
|
| 247 |
next unless $trace_channel eq 'command'; |
| 248 |
|
| 249 |
my $expected = 'command'; |
| 250 |
$self->assert($expected eq $trace_channel, |
| 251 |
test_msg("Expected '$expected', got '$trace_channel'")); |
| 252 |
|
| 253 |
$expected = 7; |
| 254 |
$self->assert($expected >= $trace_level, |
| 255 |
test_msg("Expected >= $expected, got $trace_level")); |
| 256 |
|
| 257 |
$ok = 1; |
| 258 |
last; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
close($fh); |
| 263 |
|
| 264 |
$self->assert($ok, test_msg("Missing expected TraceLog messages")); |
| 265 |
|
| 266 |
} else { |
| 267 |
die("Can't read $trace_log: $!"); |
| 268 |
} |
| 269 |
}; |
| 270 |
if ($@) { |
| 271 |
$ex = $@; |
| 272 |
} |
| 273 |
|
| 274 |
test_cleanup($setup->{log_file}, $ex); |
| 275 |
} |
| 276 |
|
| 277 |
sub traceoptions_no_timestamp { |
| 278 |
my $self = shift; |
| 279 |
my $tmpdir = $self->{tmpdir}; |
| 280 |
my $setup = test_setup($tmpdir, 'trace'); |
| 281 |
|
| 282 |
my $trace_log = File::Spec->rel2abs("$tmpdir/trace.log"); |
| 283 |
|
| 284 |
my $config = { |
| 285 |
PidFile => $setup->{pid_file}, |
| 286 |
ScoreboardFile => $setup->{scoreboard_file}, |
| 287 |
SystemLog => $setup->{log_file}, |
| 288 |
TraceLog => $trace_log, |
| 289 |
Trace => 'DEFAULT:10', |
| 290 |
TraceOptions => '-Timestamp', |
| 291 |
|
| 292 |
AuthUserFile => $setup->{auth_user_file}, |
| 293 |
AuthGroupFile => $setup->{auth_group_file}, |
| 294 |
AuthOrder => 'mod_auth_file.c', |
| 295 |
|
| 296 |
IfModules => { |
| 297 |
'mod_delay.c' => { |
| 298 |
DelayEngine => 'off', |
| 299 |
}, |
| 300 |
}, |
| 301 |
}; |
| 302 |
|
| 303 |
my ($port, $config_user, $config_group) = config_write($setup->{config_file}, |
| 304 |
$config); |
| 305 |
|
| 306 |
# Open pipes, for use between the parent and child processes. Specifically, |
| 307 |
# the child will indicate when it's done with its test by writing a message |
| 308 |
# to the parent. |
| 309 |
my ($rfh, $wfh); |
| 310 |
unless (pipe($rfh, $wfh)) { |
| 311 |
die("Can't open pipe: $!"); |
| 312 |
} |
| 313 |
|
| 314 |
my $ex; |
| 315 |
|
| 316 |
# Fork child |
| 317 |
$self->handle_sigchld(); |
| 318 |
defined(my $pid = fork()) or die("Can't fork: $!"); |
| 319 |
if ($pid) { |
| 320 |
eval { |
| 321 |
my $client = ProFTPD::TestSuite::FTP->new('127.0.0.1', $port); |
| 322 |
$client->login($setup->{user}, $setup->{passwd}); |
| 323 |
$client->quit(); |
| 324 |
}; |
| 325 |
if ($@) { |
| 326 |
$ex = $@; |
| 327 |
} |
| 328 |
|
| 329 |
$wfh->print("done\n"); |
| 330 |
$wfh->flush(); |
| 331 |
|
| 332 |
} else { |
| 333 |
eval { server_wait($setup->{config_file}, $rfh) }; |
| 334 |
if ($@) { |
| 335 |
warn($@); |
| 336 |
exit 1; |
| 337 |
} |
| 338 |
|
| 339 |
exit 0; |
| 340 |
} |
| 341 |
|
| 342 |
# Stop server |
| 343 |
server_stop($setup->{pid_file}); |
| 344 |
$self->assert_child_ok($pid); |
| 345 |
|
| 346 |
eval { |
| 347 |
if (open(my $fh, "< $trace_log")) { |
| 348 |
my $ok = 0; |
| 349 |
|
| 350 |
while (my $line = <$fh>) { |
| 351 |
chomp($line); |
| 352 |
|
| 353 |
if ($ENV{TEST_VERBOSE}) { |
| 354 |
print STDERR "# $line\n"; |
| 355 |
} |
| 356 |
|
| 357 |
my $expected = '\[\d+\]\s+<(\S+):(\d+)>: (.*?)$'; |
| 358 |
|
| 359 |
if ($line =~ /$expected/) { |
| 360 |
my $trace_channel = $1; |
| 361 |
my $trace_level = $2; |
| 362 |
my $trace_msg = $3; |
| 363 |
|
| 364 |
next unless $trace_channel eq 'command'; |
| 365 |
|
| 366 |
my $expected = 'command'; |
| 367 |
$self->assert($expected eq $trace_channel, |
| 368 |
test_msg("Expected '$expected', got '$trace_channel'")); |
| 369 |
|
| 370 |
$expected = 7; |
| 371 |
$self->assert($expected >= $trace_level, |
| 372 |
test_msg("Expected >= $expected, got $trace_level")); |
| 373 |
|
| 374 |
$ok = 1; |
| 375 |
last; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
close($fh); |
| 380 |
|
| 381 |
$self->assert($ok, test_msg("Missing expected TraceLog messages")); |
| 382 |
|
| 383 |
} else { |
| 384 |
die("Can't read $trace_log: $!"); |
| 385 |
} |
| 386 |
}; |
| 387 |
if ($@) { |
| 388 |
$ex = $@; |
| 389 |
} |
| 390 |
|
| 391 |
test_cleanup($setup->{log_file}, $ex); |
| 392 |
} |
| 393 |
|
| 394 |
1; |