#!/usr/bin/env perl # # libconfig-general-perl (Config::General 2.65) — brintos-selftests payload. # # Prints one `RNN-tag value` line per measurement; 36-libconfig-general-perl.test # compares each against a value MEASURED on an x86-64 perl running this exact # distribution (sha256 4d6d5754...fc58f, the one the recipe fetches). Pure Perl # with no XS and no backend to select, so the host is a legitimate control: the # target's perl 5.38.4 and the control's 5.38.2 run identical source. # # Config files are written into $BST_CG_DIR (the suite creates it and reads the # artifacts back afterwards) rather than a scratch file this deletes, because the # suite drives the image's own tools against the saved config in its section 5. use strict; use warnings; my $D = $ENV{BST_CG_DIR} || '/tmp'; my $warnings = 0; $SIG{__WARN__} = sub { $warnings++; print "W: $_[0]" }; sub emit { printf "%s %s\n", $_[0], (defined $_[1] ? $_[1] : '') } sub spit { my ($f,$t)=@_; open(my $h,'>',"$D/$f") or die "write $f: $!"; print $h $t; close $h } # ---------------------------------------------------------------- load -------- require Config::General; Config::General->import(qw(ParseConfig SaveConfigString)); emit('R1-version', $Config::General::VERSION); emit('R2-inc-path', $INC{'Config/General.pm'}); # --------------------------------------------------- the config under test ---- # Every feature the ticket names, in one file: nested blocks (server/primary/ # limits), a repeated key (listen, three times, one of them arriving from the # include), a line continuation (motd) and an <>. spit('extra.conf', <<'EOF'); host backup.example.net port 8081 listen 10.0.0.9 EOF spit('main.conf', <<'EOF'); # brintOS selftest configuration name brintos listen 10.0.0.1 listen 10.0.0.2 motd welcome to \ the machine host a.example.net port 8080 max 100 min 1 <> EOF my $c = Config::General->new(-ConfigFile => "$D/main.conf", -IncludeRelative => 1); my %h = $c->getall; # ------------------------------- 1. WHAT THE TICKET ASKS: the parsed hash ----- # Values, not merely "getall returned something": a parser that dropped the # include, flattened the nesting or kept only the last `listen` would still # return a populated hash. emit('R10-name', $h{name}); emit('R11-scalar-keys', scalar keys %h); emit('R12-keys', join(',', sort keys %h)); # repeated keys collapse to an ARRAY REF, in file order, and the include's value # is the third element -- which is simultaneously the repeated-key assertion and # the proof the include was merged into the SAME key rather than shadowing it. emit('R13-listen-reftype', ref $h{listen}); emit('R14-listen-count', ref $h{listen} eq 'ARRAY' ? scalar @{$h{listen}} : -1); emit('R15-listen-joined', ref $h{listen} eq 'ARRAY' ? join('|', @{$h{listen}}) : ''); # the continuation: the backslash-newline is joined AND the leading whitespace of # the continued line collapses to one space. Both halves matter -- a parser that # joined without collapsing gives 'welcome to the machine'. emit('R16-motd', $h{motd}); # nesting: three levels deep, reached by name at every level emit('R20-server-reftype', ref $h{server}); emit('R21-server-names', ref $h{server} eq 'HASH' ? join(',', sort keys %{$h{server}}) : ''); emit('R22-primary-host', $h{server}{primary}{host}); emit('R23-primary-port', $h{server}{primary}{port}); emit('R24-limits-max', $h{server}{primary}{limits}{max}); emit('R25-limits-min', $h{server}{primary}{limits}{min}); emit('R26-limits-reftype', ref $h{server}{primary}{limits}); # the include's own block landed as a sibling of the one in main.conf emit('R27-backup-host', $h{server}{backup}{host}); emit('R28-backup-port', $h{server}{backup}{port}); # files() names both, which is the include being READ rather than the values # happening to be present my @files = $c->files; emit('R29-files-count', scalar @files); # SORTED, and that is not cosmetic: files() ends `return keys %{$this->{files}}`, # so its order is perl's hash order and flips between runs -- measured 21/19 # across 40 fresh interpreters. Pinning the order this run happened to produce # would have reddened roughly half of all boots for no reason a reader could see. emit('R30-files-base', join(',', sort map { my $x=$_; $x =~ s{.*/}{}; $x } @files)); # ------------------------- 2. WHAT THE TICKET ASKS: write out, re-parse ------- # save_string -> new(-String) -> getall must reach the SAME structure. Compared # by a canonical serialisation of the whole tree, not key by key: a round trip # that lost one leaf three levels down would pass a spot check. my $saved = $c->save_string(\%h); spit('saved.conf', $saved); emit('R40-saved-len', length $saved); my %h2 = Config::General->new(-String => $saved)->getall; sub canon { # deterministic, ref-aware, no Data::Dumper my $v = shift; if (ref $v eq 'HASH') { return '{' . join(',', map { "$_=" . canon($v->{$_}) } sort keys %$v) . '}' } if (ref $v eq 'ARRAY') { return '[' . join(',', map { canon($_) } @$v) . ']' } return defined $v ? "$v" : ''; } my $c1 = canon(\%h); my $c2 = canon(\%h2); emit('R41-roundtrip-identical', $c1 eq $c2 ? 1 : 0); emit('R42-canon-len', length $c1); # a spot value THROUGH the round trip, so a canon() that silently returned '' on # both sides could not make R41 pass emit('R43-rt-limits-max', $h2{server}{primary}{limits}{max}); emit('R44-rt-listen-count', ref $h2{listen} eq 'ARRAY' ? scalar @{$h2{listen}} : -1); # ...and a THIRD generation, which is where the writer's one real quirk shows up. # save_string emits in HASH-ITERATION order, and %h2 is a different hash from %h, # so the two saves are not byte-equal even though they carry identical content. # All three are pinned rather than just the convenient one: the raw inequality is # the measured behaviour (a suite asserting 1 there would be asserting a fiction), # and the sorted-line and length equalities are the invariant that actually holds # -- which is also the one the ticket's "re-parses to the same structure" needs. my $g2 = Config::General->new(-String => $saved); my $saved2 = $g2->save_string(\%h2); sub lines { join "\n", sort grep { /\S/ } split /\n/, $_[0] } # Saving THE SAME hash twice is deterministic (100/100 runs) -- one process, one # hash, one iteration order -- so this is the writer-repeatability assertion. emit('R45-save-same-hash-stable', $g2->save_string(\%h2) eq $saved2 ? 1 : 0); # Across a re-parse it is NOT: %h2 is a different hash from %h, save_string emits # in hash-iteration order, and the two agree only by luck -- measured 18 times in # 200 runs. So the RAW comparison is deliberately not asserted in either # direction; what is asserted is the invariant that actually holds and is what # the ticket's "re-parses to the same structure" needs. emit('R45b-save-sorted-stable',lines($saved2) eq lines($saved) ? 1 : 0); emit('R45c-save-len-stable', length($saved2) == length($saved)? 1 : 0); # the file-writing half of the same API $c->save_file("$D/saved-file.conf", \%h); emit('R46-save-file-exists', -f "$D/saved-file.conf" ? 1 : 0); my %h3 = Config::General->new(-ConfigFile => "$D/saved-file.conf")->getall; emit('R47-save-file-identical', canon(\%h3) eq $c1 ? 1 : 0); # the functional interface, which is what most callers actually use my %hf = ParseConfig(-ConfigFile => "$D/main.conf", -IncludeRelative => 1); emit('R48-parseconfig-identical', canon(\%hf) eq $c1 ? 1 : 0); emit('R49-saveconfigstring-eq', SaveConfigString(\%h) eq $saved ? 1 : 0); # --------------- 3. WHAT THE TICKET ASKS: broken input is REPORTED ------------ # Each case is `does it die, and with which message` -- the ticket's criterion is # that a broken file is REPORTED rather than silently half-parsed, so the message # is asserted too: a die that said something else would mean the parser noticed a # different problem than the one in the file. sub parse_err { my (%o) = @_; my $ok = eval { my $g = Config::General->new(%o); my %x = $g->getall; 1 }; return $ok ? '' : do { my $e = $@; $e =~ s/\n.*//s; $e =~ s/ at \S+ line \d+\.?$//; $e }; } sub died { parse_err(@_) ne '' ? 1 : 0 } emit('R60-unclosed-died', died(-String => "\n host x\n")); emit('R61-unclosed-msg', parse_err(-String => "\n host x\n")); emit('R62-orphan-close-died',died(-String => "\n host x\n")); emit('R63-orphan-close-msg', parse_err(-String => "\n host x\n")); # a duplicate option with -AllowMultiOptions off: the parser must refuse rather # than keep one of the two silently emit('R64-dup-strict-died', died(-String => "x 1\nx 2\n", -AllowMultiOptions => 0)); emit('R65-dup-strict-msg', parse_err(-String => "x 1\nx 2\n", -AllowMultiOptions => 0)); # a missing include is an error, not an empty merge emit('R66-missing-inc-died', died(-String => "<>\n", -ConfigPath => [$D])); # the broken file ON DISK, which is the ticket's phrasing spit('broken.conf', "name brintos\n\n host a\n"); emit('R67-broken-file-died', died(-ConfigFile => "$D/broken.conf")); # ...and the half-parse test proper: nothing is returned, not a partial hash my %partial; my $lived = eval { %partial = Config::General->new(-ConfigFile => "$D/broken.conf")->getall; 1 }; emit('R68-broken-no-partial', (!$lived && !keys %partial) ? 1 : 0); # a good file still parses AFTER the failures above -- the module is not left # in a broken state by a die (it keeps package-level state) emit('R69-recovers', canon({ Config::General->new(-ConfigFile => "$D/main.conf", -IncludeRelative => 1)->getall }) eq $c1 ? 1 : 0); # ------------------- 4. behaviour worth PINNING, measured not assumed --------- # A MISMATCHED CLOSE TAG IS ACCEPTED. ` ... ` parses as a block named `a`: # the parser tracks nesting DEPTH, not tag names, and its own POD says so # ("it does not use the block names to check if a block ends, instead it uses an # internal counter"). This is the one place the ticket's "broken files are # reported" premise does NOT hold, so it is pinned as measured upstream # behaviour rather than left as an unstated gap. { my %x = Config::General->new(-String => "\n x 1\n\n")->getall; emit('R70-mismatched-close-accepted', (exists $x{a} && $x{a}{x} eq '1') ? 1 : 0); } # duplicate BLOCKS become an array of hashes by default... { my %x = Config::General->new(-String => "\n x 1\n\n\n y 2\n\n")->getall; emit('R71-dup-block-reftype', ref $x{a}); emit('R72-dup-block-count', ref $x{a} eq 'ARRAY' ? scalar @{$x{a}} : -1); } # ...and merge into one hash when asked { my %x = Config::General->new(-String => "\n x 1\n\n\n y 2\n\n", -MergeDuplicateBlocks => 1)->getall; emit('R73-merged-block', canon($x{a})); } # an option with no value is undef, not the empty string -- the distinction a # caller testing `defined` depends on { my %x = Config::General->new(-String => "x\n")->getall; emit('R74-empty-value-undef', defined $x{x} ? 0 : 1); } # `#` starts a comment even mid-line, so an unquoted value cannot contain one { my %x = Config::General->new(-String => "x a#b\n")->getall; emit('R75-hash-ends-value', $x{x}); } # ...and quoting keeps the SPACES but does NOT protect the hash: `#` still starts # a comment inside double quotes, so the closing quote is eaten with the comment # and the value keeps its opening one. Measured, and pinned in all three forms -- # this is the trap a caller writing a password or a URL fragment falls into, and # the working escape is the backslash, not the quotes. { my %x = Config::General->new(-String => qq{x "a b"\n})->getall; emit('R76-quoted-spaces', $x{x}); } { my %x = Config::General->new(-String => qq{x "a b#c"\n})->getall; emit('R76b-quote-no-hash-escape', $x{x}); } { my %x = Config::General->new(-String => "x a\\#b\n")->getall; emit('R76c-backslash-escapes-hash', $x{x}); } # heredocs, the other multi-line form beside the backslash continuation { my %x = Config::General->new(-String => "x <getall; emit('R77-heredoc', join('/', split /\n/, $x{x})); } # `=` is an accepted separator beside whitespace { my %x = Config::General->new(-String => "x = 1\n")->getall; emit('R78-equals-split', $x{x}); } # -LowerCaseNames, the option most callers reach for first { my %x = Config::General->new(-String => "\n HostName A\n\n", -LowerCaseNames => 1)->getall; emit('R79-lowercase', canon(\%x)); } emit('R99-warnings', $warnings); print "ALL-CONFIG-GENERAL-OK\n";