1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
# $OpenBSD: funcs.pl,v 1.35 2018/05/22 15:01:16 bluhm Exp $
# Copyright (c) 2010-2015 Alexander Bluhm <bluhm@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use strict;
use warnings;
no warnings 'experimental::smartmatch';
use feature 'switch';
use Errno;
use List::Util qw(first);
use Socket;
use Socket6;
use Sys::Syslog qw(:standard :extended :macros);
use Time::HiRes 'sleep';
use IO::Socket;
use IO::Socket::INET6;
my $firstlog = "syslogd regress test first message";
my $secondlog = "syslogd regress test second message";
my $thirdlog = "syslogd regress test third message";
my $testlog = "syslogd regress test log message";
my $downlog = "syslogd regress client shutdown";
my $charlog = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
sub find_ports {
my %args = @_;
my $num = delete $args{num} // 1;
my $domain = delete $args{domain} // AF_INET;
my $addr = delete $args{addr} // "127.0.0.1";
my $proto = delete $args{proto} // "udp";
$proto = "tcp" if $proto eq "tls";
my @sockets = (1..$num);
foreach my $s (@sockets) {
$s = IO::Socket::INET6->new(
Domain => $domain,
LocalAddr => $addr,
Proto => $proto,
) or die "find_ports: create and bind socket failed: $!";
}
my @ports = map { $_->sockport() } @sockets;
return wantarray ? @ports : $ports[0];
}
########################################################################
# Client funcs
########################################################################
sub write_log {
my $self = shift;
write_message($self, $testlog);
IO::Handle::flush(\*STDOUT);
${$self->{syslogd}}->loggrep($testlog, 2);
write_shutdown($self);
}
sub write_between2logs {
my $self = shift;
my $func = shift;
write_message($self, $firstlog);
$func->($self, @_);
write_message($self, $testlog);
IO::Handle::flush(\*STDOUT);
${$self->{syslogd}}->loggrep($testlog, 2);
write_shutdown($self);
}
sub write_message {
my $self = shift;
if (defined($self->{connectdomain})) {
my $msg = join("", @_);
if ($self->{connectdomain} eq "sendsyslog") {
my $flags = $self->{connect}{flags} || 0;
sendsyslog($msg, $flags)
or die ref($self), " sendsyslog failed: $!";
} elsif ($self->{connectproto} eq "udp") {
# writing UDP packets works only with syswrite()
defined(my $n = syswrite(STDOUT, $msg))
or die ref($self), " write log line failed: $!";
$n == length($msg)
or die ref($self), " short UDP write";
} else {
print $msg;
print "\n" if $self->{connectproto} =~ /^(tcp|tls)$/;
}
print STDERR "<<< $msg\n";
} else {
syslog(LOG_INFO, @_);
}
}
sub sendsyslog {
my $msg = shift;
my $flags = shift;
require 'sys/syscall.ph';
return syscall(&SYS_sendsyslog, $msg, length($msg), $flags) != -1;
}
sub write_shutdown {
my $self = shift;
setlogsock("native")
or die ref($self), " setlogsock native failed: $!";
syslog(LOG_NOTICE, $downlog);
}
sub write_lines {
my $self = shift;
my ($lines, $length) = @_;
foreach (1..$lines) {
write_chars($self, $length, " $_");
}
}
sub write_lengths {
my $self = shift;
my ($lengths, $tail) = ref $_[0] ? @_ : [@_];
write_chars($self, $lengths, $tail);
}
sub generate_chars {
my ($len) = @_;
my $msg = "";
my $char = '0';
for (my $i = 0; $i < $len; $i++) {
$msg .= $char;
given ($char) {
when(/9/) { $char = 'A' }
when(/Z/) { $char = 'a' }
when(/z/) { $char = '0' }
default { $char++ }
}
}
return $msg;
}
sub write_chars {
my $self = shift;
my ($length, $tail) = @_;
foreach my $len (ref $length ? @$length : $length) {
my $t = $tail // "";
substr($t, 0, length($t) - $len, "")
if length($t) && length($t) > $len;
my $msg = generate_chars($len - length($t));
$msg .= $t if length($t);
write_message($self, $msg);
# if client is sending too fast, syslogd will not see everything
sleep .01;
}
}
sub write_unix {
my $self = shift;
my $path = shift || "/dev/log";
my $id = shift // $path;
my $u = IO::Socket::UNIX->new(
Type => SOCK_DGRAM,
Peer => $path,
) or die ref($self), " connect to $path unix socket failed: $!";
my $msg = "id $id unix socket: $testlog";
print $u $msg;
print STDERR "<<< $msg\n";
}
sub write_tcp {
my $self = shift;
my $fh = shift || \*STDOUT;
my $id = shift // $fh;
my $msg = "id $id tcp socket: $testlog";
print $fh "$msg\n";
print STDERR "<<< $msg\n";
}
sub redo_connect {
my $self = shift;
my $func = shift;
$func->($self, @_);
if ($self->{cs}) {
# wait for possible icmp errors, port is open
sleep .1;
close(delete $self->{cs})
or die ref($self), " close failed: $!";
}
if (my $redo = shift @{$self->{redo}}) {
if (my $connect = $redo->{connect}) {
delete $self->{logsock};
$self->{connectdomain} = $connect->{domain};
$self->{connectaddr} = $connect->{addr};
$self->{connectproto} = $connect->{proto};
$self->{connectport} = $connect->{port};
} elsif (my $logsock = $redo->{logsock}) {
delete $self->{connectdomain};
delete $self->{connectaddr};
delete $self->{connectproto};
delete $self->{connectport};
$self->{logsock} = $logsock;
} else {
die ref($self), " no connect or logsock in redo";
}
} else {
delete $self->{connectdomain};
delete $self->{connectaddr};
delete $self->{connectproto};
delete $self->{connectport};
$self->{logsock} = { type => "native" };
setlogsock($self->{logsock})
or die ref($self), " setlogsock failed: $!";
sleep .1;
write_log($self);
undef $self->{redo};
}
}
########################################################################
# Server funcs
########################################################################
sub read_log {
my $self = shift;
read_message($self, $downlog);
}
sub read_between2logs {
my $self = shift;
my $func = shift;
read_message($self, $firstlog);
$func->($self, @_);
read_message($self, $testlog);
read_message($self, $downlog);
}
sub accept_between2logs {
my $self = shift;
my $func = shift;
unless ($self->{redo}) {
read_message($self, $firstlog);
$func->($self, @_);
$self->{redo} = 1;
} else {
$self->{redo} = 0;
read_message($self, $testlog);
read_message($self, $downlog);
}
}
sub read_message {
my $self = shift;
my $regex = shift;
local $_;
for (;;) {
if ($self->{listenproto} eq "udp") {
# reading UDP packets works only with sysread()
defined(my $n = sysread(STDIN, $_, 8194))
or die ref($self), " read log line failed: $!";
last if $n == 0;
} else {
defined($_ = <STDIN>)
or last;
}
chomp;
print STDERR ">>> $_\n";
last if /$regex/;
}
}
########################################################################
# Script funcs
########################################################################
sub get_testlog {
return $testlog;
}
sub get_testgrep {
return qr/$testlog\r*$/;
}
sub get_firstlog {
return $firstlog;
}
sub get_secondlog {
return $secondlog;
}
sub get_thirdlog {
return $thirdlog;
}
sub get_charlog {
# add a space so that we match at the beginning of the message
return " $charlog";
}
sub get_between2loggrep {
return (
qr/$firstlog/ => 1,
qr/$testlog/ => 1,
);
}
sub get_downlog {
return $downlog;
}
sub selector2config {
my %s2m = @_;
my $conf = "";
my $i = 0;
foreach my $sel (sort keys %s2m) {
$conf .= "$sel\t\$objdir/file-$i.log\n";
$i++;
}
return $conf;
}
sub selector2loggrep {
my %s2m = @_;
my %allmsg;
@allmsg{map { @$_} values %s2m} = ();
my @loggrep;
foreach my $sel (sort keys %s2m) {
my @m = @{$s2m{$sel}};
my %msg;
$msg{$_}++ foreach (@m);
my %nomsg = %allmsg;
delete @nomsg{@m};
push @loggrep, {
(map { qr/: $_$/ => $msg{$_} } sort keys %msg),
(map { qr/: $_$/ => 0 } sort keys %nomsg),
};
}
return @loggrep;
}
sub check_logs {
my ($c, $r, $s, $m, %args) = @_;
return if $args{nocheck};
check_log($c, $r, $s, @$m);
check_out($r, %args);
check_fstat($c, $r, $s);
check_ktrace($c, $r, $s);
if (my $file = $s->{"outfile"}) {
my $pattern = $s->{filegrep} || get_testgrep();
check_pattern(ref $s, $file, $pattern, \&filegrep);
}
check_multifile(@{$args{multifile} || []});
}
sub compare($$) {
local $_ = $_[1];
if (/^\d+/) {
return $_[0] == $_;
} elsif (/^==(\d+)/) {
return $_[0] == $1;
} elsif (/^!=(\d+)/) {
return $_[0] != $1;
} elsif (/^>=(\d+)/) {
return $_[0] >= $1;
} elsif (/^<=(\d+)/) {
return $_[0] <= $1;
} elsif (/^~(\d+)/) {
return $1 * 0.8 <= $_[0] && $_[0] <= $1 * 1.2;
}
die "bad compare operator: $_";
}
sub check_pattern {
my ($name, $proc, $pattern, $func) = @_;
$pattern = [ $pattern ] unless ref($pattern) eq 'ARRAY';
foreach my $pat (@$pattern) {
if (ref($pat) eq 'HASH') {
foreach my $re (sort keys %$pat) {
my $num = $pat->{$re};
my @matches = $func->($proc, $re);
compare(@matches, $num)
or die "$name matches '@matches': ",
"'$re' => $num";
}
} else {
$func->($proc, $pat)
or die "$name log missing pattern: $pat";
}
}
}
sub check_log {
foreach my $proc (@_) {
next unless $proc && !$proc->{nocheck};
my $pattern = $proc->{loggrep} || get_testgrep();
check_pattern(ref $proc, $proc, $pattern, \&loggrep);
}
}
sub loggrep {
my ($proc, $pattern) = @_;
return $proc->loggrep($pattern);
}
sub check_out {
my ($r, %args) = @_;
unless ($args{pipe}{nocheck}) {
$r->loggrep("bytes transferred", 1) or sleep 1;
}
foreach my $dev (qw(console user)) {
$args{$dev}{nocheck} ||= $args{tty}{nocheck};
$args{$dev}{loggrep} ||= $args{tty}{loggrep};
next if $args{$dev}{nocheck};
my $ctl = $r->{"ctl$dev"};
close($ctl);
my $file = $r->{"out$dev"};
open(my $fh, '<', $file)
or die "Open file $file for reading failed: $!";
grep { /^logout/ or /^console .* off/ } <$fh> or sleep 1;
close($fh);
}
foreach my $name (qw(file pipe console user)) {
next if $args{$name}{nocheck};
my $file = $r->{"out$name"} or die;
my $pattern = $args{$name}{loggrep} || get_testgrep();
check_pattern($name, $file, $pattern, \&filegrep);
}
}
sub check_fstat {
foreach my $proc (@_) {
my $pattern = $proc && $proc->{fstat} or next;
my $file = $proc->{fstatfile} or die;
check_pattern("fstat", $file, $pattern, \&filegrep);
}
}
sub filegrep {
my ($file, $pattern) = @_;
open(my $fh, '<', $file)
or die "Open file $file for reading failed: $!";
return wantarray ?
grep { /$pattern/ } <$fh> : first { /$pattern/ } <$fh>;
}
sub check_ktrace {
foreach my $proc (@_) {
my $pattern = $proc && $proc->{ktrace} or next;
my $file = $proc->{ktracefile} or die;
check_pattern("ktrace", $file, $pattern, \&kdumpgrep);
}
}
sub kdumpgrep {
my ($file, $pattern) = @_;
my @sudo = ! -r $file && $ENV{SUDO} ? $ENV{SUDO} : ();
my @cmd = (@sudo, "kdump", "-f", $file);
open(my $fh, '-|', @cmd)
or die "Open pipe from '@cmd' failed: $!";
my @matches = grep { /$pattern/ } <$fh>;
close($fh) or die $! ?
"Close pipe from '@cmd' failed: $!" :
"Command '@cmd' failed: $?";
return wantarray ? @matches : $matches[0];
}
sub create_multifile {
for (my $i = 0; $i < @_; $i++) {
my $file = "file-$i.log";
open(my $fh, '>', $file)
or die "Create $file failed: $!";
}
}
sub check_multifile {
for (my $i = 0; $i < @_; $i++) {
my $file = "file-$i.log";
my $pattern = $_[$i]{loggrep} or die;
check_pattern("multifile $i", $file, $pattern, \&filegrep);
}
}
1;
|