bin/fuzz-harness-generator

filter: Statementstmt 52.6% 108 / 205 Branchbran 39.1% 36 / 92 Conditioncond 42.8% 15 / 35 MC/DCmcdc 14.2% 3 / 21 Subroutinesub 78.9% 15 / 19 Podpod n/a totaltotal 47.5% 177 / 372 CC 35 SCAR 46.6
CC
35
Cov
48%
_generate_replay_tap
306.0
_infer_module_from_schema
42.0
_format_input
12.0
CRAP 105.5
« lib/App/Test/Generator.pm ↑ Summary blib/lib/App/Test/Generator/SchemaExtractor.pm »
1 #!/usr/bin/env perl
2  
3 182 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 182 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 182 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 182 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 182 ▶use lib "$FindBin::Bin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 182 ▶use App::Test::Generator;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 182 ▶use App::Test::Generator::Mutation::Feedback;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 182 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 182 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 182 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 182 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 182 ▶use YAML::XS qw(LoadFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Derive prove from $^X so we always use the same perl installation that
19 # invoked this script, even when $PATH resolves to a different system perl.
20 182 my ($prove_vol, $prove_dir) = (File::Spec->splitpath($^X))[0, 1];
21 182 my $prove = File::Spec->catpath($prove_vol, $prove_dir, 'prove');
22  
23 =head1 NAME
24  
25 fuzz-harness-generator - Generate fuzzing + corpus-based test harnesses from test schemas
26  
27 =head1 SYNOPSIS
28  
29 fuzz-harness-generator [-r] [--strict-documented] [-o output_file] input.yaml
30 fuzz-harness-generator --mutation-results mutation.json -o t/fuzz.t input.yaml
31 fuzz-harness-generator --dry-run input.yaml
32 fuzz-harness-generator --replay-corpus schemas/corpus/ -o t/fuzz_replay.t
33 fuzz-harness-generator --replay-corpus schemas/corpus/translate.json -o t/fuzz_replay.t
34  
35 =head1 DESCRIPTION
36  
37 This tool generates a test file that fuzzes and validates a target module's function or method,
38 using both randomized fuzz cases and a static corpus cases (Perl or YAML).
39  
40 It can also generate regression test files from corpus JSON files previously
41 written by C<extract-schemas --fuzz>, using C<--replay-corpus>.
42  
43 A starter C<input.yaml> can be created using C<extract-schemas> which is also in this package.
44  
45 =head1 OPTIONS
46  
47 =over 4
48  
49 =item B<--help>
50  
51 Show this help.
52  
53 =item B<--input>
54  
55 The input configuration file
56  
57 =item B<--output>
58  
59 The (optional) output file.
60  
61 =item B<--dry-run>
62  
63 Validate the input configuration and schema extraction without writing any output files or running tests.
64  
65 =item B<--run>
66  
67 Call C<prove> on the output file.
68  
69 C<fuzz-harness-generator -r t/conf/data_text_append.conf> will, therefore, dynamically create and run tests on the C<append> method of L<Data::Text>
70  
71 =item B<--strict-documented>
72  
73 Fail the generated harness when fuzzing finds a routine that contradicts a
74 POD-derived constraint: a bound, a C<memberof> list or a C<matches> pattern.
75 By default the harness runs in advisory mode: an outside value is still
76 tried, and a routine that accepts it is reported as accepting a value its
77 POD says is invalid, but the run does not fail. This option makes each
78 recorded contradiction a failing test.
79  
80 =item B<--mutation-results> FILE
81  
82 Feed surviving mutants back into harness generation. FILE is the JSON report
83 written by C<test-generator-mutate --json>. A surviving numeric-boundary
84 mutant is traced to the comparison it mutated, and that comparison becomes a
85 C<min> or C<max> constraint on the matching input field, so the generated
86 harness exercises the boundary the test suite missed. Existing constraints
87 are never replaced, and survivors that do not describe a numeric comparison
88 are listed for review. Only survivors belonging to the schema's C<module>
89 are used.
90  
91 =item B<--replay-corpus> PATH
92  
93 Instead of generating a fuzz harness, generate a regression test file from
94 one or more corpus JSON files previously written by C<extract-schemas --fuzz>.
95  
96 PATH may be either:
97  
98 =over 4
99  
100 =item * A single corpus file, e.g. C<schemas/corpus/translate.json>
101  
102 =item * A directory, e.g. C<schemas/corpus/> - all C<*.json> files in that
103 directory will be included
104  
105 =back
106  
107 The generated test file contains one failing test per bug recorded in the
108 corpus. Each test calls the target method with the exact input that previously
109 caused a crash and expects it B<not> to die. Tests will be red until the
110 underlying bug is fixed, at which point they go green and stay green -
111 acting as permanent regression tests.
112  
113 Only corpus entries with recorded bugs are included. Clean corpus entries
114 (inputs that did not cause a bug) are ignored.
115  
116 =item B<--version>
117  
118 Prints the version of L<App::Test::Generator>
119  
120 =back
121  
122 =cut
123  
124 182 my $infile;
125 my $outfile;
126 182 my $help;
127 182 my $run;
128 182 my $verbose;
129 182 my $version;
130 182 my $dry_run;
131 182 my $replay_corpus;
132 182 my $strict_documented;
133 182 my $mutation_results;
134  
135 182 Getopt::Long::Configure('bundling');
136  
137 182 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("help|h", \$help, "input|i=s", \$infile, "dry-run|n", \$dry_run, "output|o=s", \$outfile, "run|r", \$run, "strict-documented", \$strict_documented, "mutation-results=s", \$mutation_results, "verbose|v", \$verbose, "version|V", \$version, "replay-corpus|R=s", \$replay_corpus) or pod2usage(2) 182 0 182
138 'help|h' => \$help,
139 'input|i=s' => \$infile,
140 'dry-run|n' => \$dry_run,
141 'output|o=s' => \$outfile,
142 'run|r' => \$run,
143 'strict-documented' => \$strict_documented,
144 'mutation-results=s' => \$mutation_results,
145 'verbose|v' => \$verbose,
146 'version|V' => \$version,
147 'replay-corpus|R=s' => \$replay_corpus,
148 ) or pod2usage(2);
149  
150 182 ▶pod2usage(-exitval => 0, -verbose => 1) if($help);
Branch: 1 branch
BranchTrueFalseTotal
if $help 2 180 182
151  
152 180 ▶if($version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version) 1 179 180
153 1 print $App::Test::Generator::VERSION, "\n";
154 1 exit 0;
155 }
156  
157 # ---------------------------------------------------------------------------
158 # --replay-corpus mode: generate a regression .t from corpus bug entries
159 # ---------------------------------------------------------------------------
160  
161 179 ▶if($replay_corpus) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replay_corpus) 0 179 179
162 0 ▶ pod2usage('--replay-corpus cannot be combined with --dry-run') if $dry_run;
Branch: 1 branch
BranchTrueFalseTotal
if $dry_run 0 0 0
163 0 ▶ pod2usage('--replay-corpus cannot be combined with --input') if $infile;
Branch: 1 branch
BranchTrueFalseTotal
if $infile 0 0 0
164  
165 0 my @corpus_files = _collect_corpus_files($replay_corpus);
166 0 ▶ die "No corpus JSON files found at: $replay_corpus\n" unless @corpus_files;
Branch: 1 branch
BranchTrueFalseTotal
unless @corpus_files 0 0 0
167  
168 0 my $tap = _generate_replay_tap(@corpus_files);
169  
170 0 ▶ if($outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile) { } 0 0 0
171 0 ▶ open(my $fh, '>', $outfile)
Branch: 1 branch
BranchTrueFalseTotal
&main::open(my $fh, ">", $outfile) or die "Cannot write to ${outfile}: $!" 0 0 0
172 or die "Cannot write to $outfile: $!";
173 0 print $fh $tap;
174 0 close $fh;
175 0 chmod 0755, $outfile;
176 0 print "Replay test written to: $outfile\n";
177 0 ▶ if($run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run) 0 0 0
178 0 exit system($prove, '-l', $outfile) >> 8;
179 }
180 } else {
181 0 print $tap;
182 }
183 0 exit 0;
184 }
185  
186 179 ▶if($infile && @ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if ($infile and @ARGV) 0 179 179
Condition66%2/3
$infile and @ARGV
!ll&&!rl&&r
17540
MC/DC0%0/2
$infile and @ARGV
$infile @ARGV
Truth table66%2/3
A and B
A= $infileB= @ARGV
ABresult
0X0
100
111
187 0 pod2usage('Specify input file either as argument or via --input, not both');
188 }
189  
190 179 ▶if($infile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($infile) 4 175 179
191 4 my $schema = eval { LoadFile($infile) };
192 4 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 3 4
193 1 die "Cannot parse '$infile' as YAML: $@";
194 }
195 3 ▶ unless(ref($schema) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $schema eq "HASH") 0 3 3
196 0 die "Input file '$infile' does not contain a YAML hash";
197 }
198 3 ▶ unless($schema->{function}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($schema->{"function"}) 0 3 3
199 0 die "Input file '$infile' is missing required 'function' key";
200 }
201 }
202  
203 178 ▶$infile ||= shift @ARGV or pod2usage('No config file given');
Branch: 1 branch
BranchTrueFalseTotal
$infile ||= shift @ARGV or pod2usage("No config file given") 178 0 178
Condition66%2/3
$infile ||= shift @ARGV
l!l&&r!l&&!r
31750
MC/DC0%0/2
$infile ||= shift @ARGV
$infile shift @ARGV
Truth table66%2/3
A ||= B
A= $infileB= shift @ARGV
ABresult
1X1
011
000
204  
205 178 ▶if($dry_run && $run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dry_run and $run) 0 178 178
Condition66%2/3
$dry_run and $run
!ll&&!rl&&r
17620
MC/DC0%0/2
$dry_run and $run
$dry_run $run
Truth table66%2/3
A and B
A= $dry_runB= $run
ABresult
0X0
100
111
206 0 pod2usage('--dry-run cannot be used with --run');
207 }
208  
209 178 ▶if($dry_run && $outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dry_run and $outfile) 1 177 178
Condition100%3/3
$dry_run and $outfile
!ll&&!rl&&r
17611
MC/DC100%2/2
$dry_run and $outfile
$dry_run $outfile
Truth table100%3/3
A and B
A= $dry_runB= $outfile
ABresult
0X0
100
111
210 1 warn '--dry-run specified; --output will be ignored';
211 }
212  
213 178 ▶if($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 178 178
214 0 $ENV{'TEST_VERBOSE'} = 1;
215 }
216  
217 178 ▶if($run && !$outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run and not $outfile) 126 52 178
Condition66%2/3
$run and not $outfile
!ll&&!rl&&r
520126
MC/DC50%1/2
$run and not $outfile
$run not $outfile
Truth table66%2/3
A and not(B)
A= $runB= not $outfile
ABresult
0X0
100
111
218 126 my ($fh, $tmp) = File::Temp::tempfile();
219 126 close $fh;
220  
221 126 _generate_harness($infile, $tmp, $strict_documented, $mutation_results);
222  
223 126 exit system($prove, '-l', $tmp) >> 8;
224 }
225  
226 52 ▶if($dry_run) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dry_run) { } 2 50 52
elsif ($outfile and -e $outfile and not $run) { } 0 100 100
227 2 my ($fh, $tmp) = File::Temp::tempfile();
228 2 close $fh;
229  
230 eval {
231 2 _generate_harness($infile, $tmp, $strict_documented, $mutation_results);
232 2 1;
233 2 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { _generate_harness($infile, $tmp, $strict_documented, $mutation_results); 1 } } or do { die "Dry-run failed for ${infile}: $@" } 2 0 2
234 0 die "Dry-run failed for $infile: $@";
235 };
236  
237 2 unlink $tmp;
238 2 print "Dry-run OK: $infile parsed and validated successfully\n";
239 2 exit 0;
240 ▶} elsif($outfile && -e $outfile && !$run) {
Condition33%2/6
$outfile and -e $outfile and not $run
!ll&&!rl&&r
5000
$outfile and -e $outfile
!ll&&!rl&&r
0500
MC/DC0%0/3
$outfile and -e $outfile and not $run
$outfile -e $outfile not $run
Truth table25%1/4
A and B and not(C)
A= $outfileB= -e $outfileC= not $run
ABCresult
0XX0
10X0
1100
1111
241 0 warn "Overwriting existing file: $outfile";
242 }
243  
244 50 _generate_harness($infile, $outfile, $strict_documented, $mutation_results);
245  
246 50 ▶if($outfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outfile) 50 0 50
247 50 ▶ chmod 0755, $outfile if($outfile =~ /\.(pl|cgi)$/);
Branch: 1 branch
BranchTrueFalseTotal
if $outfile =~ /\.(pl|cgi)$/ 0 50 50
248 50 ▶ if($run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($run) 0 50 50
249 # Use list form to avoid shell interpolation of $outfile
250 0 system($prove, '-l', $outfile);
251 }
252 }
253  
254 50 exit 0;
255  
256 # --------------------------------------------------
257 # _generate_harness
258 #
259 # Generate a fuzz harness, carrying the CLI's
260 # --strict-documented choice into the generated
261 # test file so that later prove runs behave
262 # the same way.
263 #
264 # Entry: $infile - schema file.
265 # $outfile - where to write the harness.
266 # $strict - true to fail on documented-bound
267 # contradictions.
268 # $mutation_results - optional path to the JSON
269 # written by test-generator-mutate;
270 # surviving boundary mutants become
271 # schema constraints, and what was
272 # applied is summarised here.
273 #
274 # Exit: Returns nothing; generate() may croak.
275 # --------------------------------------------------
276 178 ▶sub _generate_harness {
Subroutine _generate_harness: called CC=4 SCAR=13.9
277 178 my ($infile, $outfile, $strict, $mutation_results) = @_;
278  
279 178 ▶ if(defined($mutation_results)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mutation_results) 1 177 178
280 1 _report_mutation_feedback($infile, $mutation_results);
281 }
282  
283 178 ▶ return App::Test::Generator->generate({
Branch: 2 branches
BranchTrueFalseTotal
$strict ? : 4 174 178
defined $mutation_results ? : 1 177 178
284 input_file => $infile,
285 output_file => $outfile,
286 strict_documented => $strict ? 1 : 0,
287 (defined($mutation_results) ? (mutation_results => $mutation_results) : ()),
288 });
289 }
290  
291 # --------------------------------------------------
292 # _report_mutation_feedback
293 #
294 # Print what the survivors propose before the harness is
295 # written, so a run that adds no constraints says so.
296 #
297 # Entry: $infile - schema file.
298 # $mutation_results - mutation results JSON.
299 #
300 # Exit: Returns nothing; reads both files.
301 # --------------------------------------------------
302 1 ▶sub _report_mutation_feedback {
Subroutine _report_mutation_feedback: called CC=7 SCAR=19.8
303 1 my ($infile, $mutation_results) = @_;
304  
305 1 my $schema = LoadFile($infile);
306 my $feedback = App::Test::Generator::Mutation::Feedback->new(
307 mutation_results => $mutation_results,
308 1 ▶ (defined($schema->{module}) ? (module => $schema->{module}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $schema->{'module'} ? : 1 0 1
309 );
310  
311 1 my $proposals = $feedback->proposals;
312 1 my $applied = $feedback->apply_to_schema($schema);
313  
314 printf "Mutation feedback: %d survivor(s), %d constraint(s) applied\n",
315 1 scalar(@{$proposals}), scalar(@{$applied});
316  
317 1 foreach my $change (@{$applied}) {
318 printf " %s: %s = %s (survivor %s)\n",
319 $change->{field}, $change->{constraint}, $change->{value},
320 2 $change->{survivor_id};
321 }
322  
323 1 foreach my $proposal (@{$proposals}) {
324 3 ▶ next unless $proposal->{kind} eq 'manual';
Branch: 1 branch
BranchTrueFalseTotal
unless $proposal->{"kind"} eq "manual" 2 1 3
325 printf " manual: %s line %s - %s\n",
326 $proposal->{file}, $proposal->{line} // '?',
327 1 ▶ $proposal->{description} // $proposal->{survivor_id};
Condition40%2/5
$proposal->{"line"} // "?"
l!l
10
$proposal->{"description"} // $proposal->{"survivor_id"}
l!l&&r!l&&!r
100
MC/DC0%0/1
$proposal->{"line"} // "?"
$proposal->{"line"}
MC/DC0%0/2
$proposal->{"description"} // $proposal->{"survivor_id"}
$proposal->{"description"} $proposal->{"survivor_id"}
Truth table50%1/2
A
A= $proposal->{"line"}
Aresult
11
00
Truth table33%1/3
A // B
A= $proposal->{"description"}B= $proposal->{"survivor_id"}
ABresult
1X1
011
000
328 }
329 }
330  
331 # ---------------------------------------------------------------------------
332 # Helpers for --replay-corpus
333 # ---------------------------------------------------------------------------
334  
335 # --------------------------------------------------
336 # _collect_corpus_files
337 #
338 # Collect the list of corpus JSON files
339 # to process for --replay-corpus mode.
340 # Accepts either a single file path or
341 # a directory, returning all *.json files
342 # found in the directory case.
343 #
344 # Entry: $path - filesystem path to either a
345 # single .json file or a directory
346 # containing .json files.
347 #
348 # Exit: Returns a sorted list of file paths.
349 # Returns an empty list if the path does
350 # not exist or contains no .json files.
351 #
352 # Notes: Directory globbing matches only *.json
353 # files at the top level of the directory;
354 # subdirectories are not recursed into.
355 # --------------------------------------------------
356 0 ▶sub _collect_corpus_files {
Subroutine _collect_corpus_files: not called CC=3 SCAR=19.6
357 0 my ($path) = @_;
358  
359 0 ▶ if(-f $path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $path) { } 0 0 0
elsif (-d $path) { } 0 0 0
360 0 return ($path);
361 } elsif(-d $path) {
362 0 ▶ my @files = glob(File::Spec->catfile($path, '*.json'));
Subroutine BEGIN: called CC=1 SCAR=0.2
363 0 return sort @files;
364 }
365  
366 0 return ();
367 }
368  
369 # --------------------------------------------------
370 # _generate_replay_tap
371 #
372 # Purpose: Read one or more corpus JSON files and
373 # produce a complete .t file as a string.
374 # Each bug entry in the corpus becomes
375 # one lives_ok test that calls the target
376 # method with the exact input that
377 # previously caused a crash, asserting
378 # that it no longer dies.
379 #
380 # Entry: @corpus_files - list of paths to corpus
381 # JSON files as returned
382 # by _collect_corpus_files.
383 #
384 # Exit: Returns the complete .t file content as
385 # a string. Never returns undef.
386 # Returns a skip_all plan if no bugs are
387 # found across all corpus files.
388 #
389 # Side effects: Reads corpus JSON files from disk.
390 # Attempts to load JSON::MaybeXS or
391 # JSON via block eval.
392 #
393 # Notes: Corpus files that cannot be parsed are
394 # skipped with a warning rather than
395 # aborting the entire run.
396 # Clean corpus entries (those without
397 # recorded bugs) are silently ignored —
398 # only entries with a 'bugs' array are
399 # processed.
400 # The module name for each test is
401 # inferred from the YAML schema file
402 # alongside the corpus file via
403 # _infer_module_from_schema. Falls back
404 # to 'UNKNOWN::Module' if not found.
405 # --------------------------------------------------
406 0 ▶sub _generate_replay_tap {
Subroutine _generate_replay_tap: not called CC=17 SCAR=57.2
407 0 my (@corpus_files) = @_;
408  
409 # Prefer JSON::MaybeXS for correctness; fall back to JSON
410 0 my $json_module;
411 0 for my $mod (qw(JSON::MaybeXS JSON)) {
412 0 ▶ eval { require $mod; 1 } and $json_module = $mod and last;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require $mod; 1 } } and $json_module = $mod and last 0 0 0
Condition0%0/3
eval { do { require $mod; 1 } } and $json_module = $mod
!ll&&!rl&&r
000
MC/DC0%0/2
eval { do { require $mod; 1 } } and $json_module = $mod
eval { do { require $mod; 1 } } $json_module = $mod
Truth table0%0/3
A and B
A= eval { do { require $mod; 1 } }B= $json_module = $mod
ABresult
0X0
100
111
413 }
414 0 ▶ die "No JSON module available; install JSON or JSON::MaybeXS\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $json_module 0 0 0
415 unless $json_module;
416  
417 # Collect all bugs across all corpus files into a flat list
418 0 my @tests;
419  
420 0 for my $file (@corpus_files) {
421 0 ▶ open(my $fh, '<', $file)
Branch: 1 branch
BranchTrueFalseTotal
&main::open(my $fh, "<", $file) or die "Cannot read ${file}: $!" 0 0 0
422 or die "Cannot read $file: $!";
423 0 my $data = eval {
424 0 $json_module->new->decode(do { local $/; <$fh> })
425 };
426 0 close $fh;
427  
428 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
429 0 warn "Skipping $file: could not parse JSON: $@\n";
430 0 next;
431 }
432  
433 0 ▶ my $bugs = $data->{'bugs'} // [];
Condition0%0/2
$data->{"bugs"} // []
l!l
00
MC/DC0%0/1
$data->{"bugs"} // []
$data->{"bugs"}
Truth table0%0/2
A
A= $data->{"bugs"}
Aresult
11
00
434 0 ▶ next unless @{$bugs};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$bugs;} 0 0 0
435  
436 # Derive method name from filename: translate.json -> translate
437 0 my (undef, undef, $fname) = File::Spec->splitpath($file);
438 0 (my $method = $fname) =~ s/\.json$//;
439  
440 # Look up the module name from the companion schema file;
441 # fall back to a placeholder if the schema cannot be found
442 0 ▶ my $module = _infer_module_from_schema($file, $method)
Condition0%0/2
_infer_module_from_schema($file, $method) // "UNKNOWN::Module"
l!l
00
MC/DC0%0/1
_infer_module_from_schema($file, $method) // "UNKNOWN::Module"
_infer_module_from_schema($file, $method)
Truth table0%0/2
A
A= _infer_module_from_schema($file, $method)
Aresult
11
00
443 // 'UNKNOWN::Module';
444  
445 0 for my $bug (@{$bugs}) {
446 push @tests, {
447 module => $module,
448 method => $method,
449 input => $bug->{'input'},
450 0 error => $bug->{'error'},
451 file => $file,
452 };
453 }
454 }
455  
456 # Build the .t header — include Test::Exception up front since
457 # lives_ok is always needed when there are tests to emit
458 0 my $t = <<'HEADER';
459 #!/usr/bin/env perl
460 # Auto-generated by fuzz-harness-generator --replay-corpus
461 # DO NOT EDIT - regenerate from corpus files instead
462 use strict;
463 use warnings;
464 use Test::More;
465 use Test::Exception;
466 HEADER
467  
468 0 my $test_count = scalar @tests;
469  
470 0 ▶ if($test_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test_count == 0) 0 0 0
471 0 $t .= "\nplan skip_all => 'No bugs recorded in corpus files';\n";
472 0 return $t;
473 }
474  
475 # Emit one use statement per unique module (excluding the placeholder)
476 0 my %modules = map { $_->{'module'} => 1 } @tests;
477 0 for my $mod (sort keys %modules) {
478 0 ▶ next if $mod eq 'UNKNOWN::Module';
Branch: 1 branch
BranchTrueFalseTotal
if $mod eq "UNKNOWN::Module" 0 0 0
479 0 $t .= "use $mod;\n";
480 }
481  
482 0 $t .= "\nplan tests => $test_count;\n\n";
483  
484 0 for my $i (0 .. $#tests) {
485 0 my $test = $tests[$i];
486 0 my $n = $i + 1;
487 0 my $input = _format_input($test->{'input'});
488 0 my $label = "$test->{'method'} does not die on input from $test->{'file'}";
489  
490 # Flatten and escape the original error for use as a comment
491 0 ▶ (my $orig_error = $test->{'error'} // '') =~ s/\n/ /g;
Condition0%0/2
$test->{"error"} // ""
l!l
00
MC/DC0%0/1
$test->{"error"} // ""
$test->{"error"}
Truth table0%0/2
A
A= $test->{"error"}
Aresult
11
00
492 0 $orig_error =~ s/'/\\'/g;
493  
494 0 $t .= "# Corpus bug: $orig_error\n";
495 0 $t .= "lives_ok { $test->{'module'}\->$test->{'method'}($input) }\n";
496 0 $t .= " '$label';\n\n";
497 }
498  
499 0 return $t;
500 }
501  
502 # --------------------------------------------------
503 # _format_input
504 #
505 # Purpose: Format a scalar input value as a Perl
506 # literal string suitable for embedding
507 # directly in generated test source code.
508 #
509 # Entry: $input - the input value to format.
510 # May be undef, a numeric string,
511 # or an arbitrary string.
512 #
513 # Exit: Returns a Perl literal string:
514 # 'undef' if $input is undef
515 # bare number if $input looks numeric
516 # single-quoted string otherwise, with
517 # backslashes and single quotes escaped.
518 #
519 # Side effects: None.
520 #
521 # Notes: Only scalar inputs are handled — corpus
522 # entries with arrayref or hashref inputs
523 # are not currently supported and will be
524 # formatted as a single-quoted string of
525 # the stringified reference, which will
526 # not reproduce the original input.
527 # --------------------------------------------------
528 0 ▶sub _format_input {
Subroutine _format_input: not called CC=3 SCAR=24.8
529 0 my ($input) = @_;
530  
531 0 ▶ return 'undef' unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 0 0
532  
533 # Emit bare numeric literals without quoting
534 0 ▶ return $input if $input =~ /^-?(?:\d+\.?\d*|\.\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $input =~ /^-?(?:\d+\.?\d*|\.\d+)$/ 0 0 0
535  
536 # Escape backslashes first, then single quotes, to avoid
537 # double-escaping when both appear in the same string
538 0 (my $escaped = $input) =~ s/\\/\\\\/g;
539 0 $escaped =~ s/'/\\'/g;
540  
541 0 return "'$escaped'";
542 }
543  
544 # --------------------------------------------------
545 # _infer_module_from_schema
546 #
547 # Purpose: Attempt to determine the Perl module
548 # name for a given corpus method by
549 # locating and reading the companion YAML
550 # schema file that sits alongside the
551 # corpus directory.
552 #
553 # Entry: $corpus_file - path to the corpus JSON
554 # file, e.g.
555 # schemas/corpus/translate.json
556 # $method - the method name derived
557 # from the corpus filename,
558 # e.g. 'translate'
559 #
560 # Exit: Returns the module name string if found,
561 # or undef if no companion schema file
562 # exists or the schema contains no
563 # 'module:' line.
564 #
565 # Side effects: Reads schema files from disk.
566 #
567 # Notes: The corpus is expected to live one
568 # directory below the schemas directory,
569 # e.g. schemas/corpus/ alongside
570 # schemas/translate.yaml. This function
571 # walks up one level from the corpus
572 # directory to find the schema.
573 # Both .yaml and .yml extensions are
574 # tried, in that order.
575 # --------------------------------------------------
576 0 ▶sub _infer_module_from_schema {
Subroutine _infer_module_from_schema: not called CC=6 SCAR=37.4
577 0 my ($corpus_file, $method) = @_;
578  
579 0 my (undef, $corpus_dir) = File::Spec->splitpath($corpus_file);
580  
581 # Walk up one directory from corpus/ to reach the schemas/ dir
582 0 my $schema_dir = File::Spec->catdir($corpus_dir, File::Spec->updir());
583  
584 0 for my $ext (qw(yaml yml)) {
585 0 my $schema_file = File::Spec->catfile($schema_dir, "$method.$ext");
586 0 ▶ next unless -f $schema_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $schema_file 0 0 0
587  
588 0 ▶ open(my $fh, '<', $schema_file) or next;
Branch: 1 branch
BranchTrueFalseTotal
&main::open(my $fh, "<", $schema_file) or next 0 0 0
589 0 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
590 0 ▶ if(/^module:\s*(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^module:\s*(\S+)/) 0 0 0
591 0 close $fh;
592 0 return $1;
593 }
594 }
595 0 close $fh;
596 }
597  
598 0 return undef;
599 }
600  
601 __END__
« lib/App/Test/Generator.pm ↑ Summary blib/lib/App/Test/Generator/SchemaExtractor.pm »