bin/extract-schemas

filter: Statementstmt 82.0% 146 / 178 Branchbran 58.9% 46 / 78 Conditioncond 45.0% 9 / 20 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 71.6% 215 / 300 CC 10 SCAR 14.6
CC
10
Cov
73%
_try_construct
8.8
_load_target_module
6.1
BEGIN
1.0
CRAP 4.3
« blib/lib/App/Test/Generator/LCSAJ/Coverage.pm ↑ Summary lib/App/Test/Generator/Mutation/Base.pm »
1 #!/usr/bin/env perl
2  
3 129 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 129 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 129 $| = 1; # unbuffered stdout so messages appear in execution order
7  
8 129 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 129 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 129 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 129 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 129 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 129 ▶use lib "$FindBin::Bin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
14 129 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 129 ▶use App::Test::Generator::SchemaExtractor;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 extract-schemas - Extract test schemas from Perl modules
21  
22 =head1 SYNOPSIS
23  
24 extract-schemas [options] <module.pm>
25  
26 Options:
27 --output-dir DIR Output directory for schema files (default: schemas/)
28 --strict-pod=off|warn|fatal
29 --function NAME Only extract and test this one routine (useful for
30 debugging a single failure in t/self-fuzz.t)
31 --verbose Show detailed analysis
32 --version Show the version of App::Test::Generator::SchemaExtractor
33 --fuzz Run coverage-guided fuzzing on extracted schemas
34 --fuzz-iters N Iterations per method when fuzzing (default: 100)
35 (no short form, to avoid conflict with --fuzz/-f)
36 --fuzz-all Fuzz all methods, including those with no input schema
37 --corpus-dir DIR Directory to persist fuzz corpora (default: schemas/corpus/)
38 --minimize-corpus After fuzzing, trim each corpus to the minimum subset
39 that still covers all discovered branches (greedy set-cover).
40 Keeps the corpus file small across many CI runs.
41 --help Show this help message
42 --man Show full documentation
43  
44 Examples:
45 extract-schemas lib/MyModule.pm
46 extract-schemas --output-dir my_schemas --verbose lib/MyModule.pm
47 extract-schemas --function greet lib/MyModule.pm
48 extract-schemas --fuzz lib/MyModule.pm
49 extract-schemas --fuzz --function greet lib/MyModule.pm
50 extract-schemas --fuzz --fuzz-iters 300 --corpus-dir t/corpus lib/MyModule.pm
51 extract-schemas --fuzz --fuzz-all lib/MyModule.pm
52 extract-schemas --fuzz --minimize-corpus lib/MyModule.pm
53  
54 =head1 QUICK START
55  
56 Run C<extract-schemas --strict-pod=warn -v --fuzz lib/MyModule.pm> to analyse your module and
57 automatically probe each method with hundreds of fuzzed inputs,
58 looking for
59 crashes caused by inputs that should be valid.
60 Anything suspicious is saved to C<schemas/corpus/>.
61  
62 If genuine bugs are found,
63 run C<fuzz-harness-generator --replay-corpus schemas/corpus/ -o t/fuzz_replay.t>
64 to turn them into regression tests that will fail until you fix the underlying code and pass forever after.
65 Run C<extract-schemas --fuzz> regularly - each
66 run builds on the last, probing deeper into your code each time.
67  
68 Otherwise, for each of the functions in MyModule.pm,
69 C<fuzz-harness-generator -r schemas/function.yml>
70  
71 =head1 OPTIONS
72  
73 =over 4
74  
75 =item B<--output-dir|-o> I<DIR>
76  
77 Directory to write generated YAML schema files. Defaults to C<schemas/>.
78  
79 =item B<--strict-pod|-s> I<MODE>
80  
81 How to handle disagreements between POD and code analysis.
82 C<0> (off), C<1> (warn), or C<fatal> (croak on disagreement).
83  
84 =item B<--function|-F> I<NAME>
85  
86 Extract schema for only the named function; delete all other generated YAMLs
87 and restrict the fuzz run to that one routine.
88  
89 =item B<--verbose|-v>
90  
91 Print extra diagnostic information during extraction and fuzzing.
92  
93 =item B<--version|-V>
94  
95 Print the C<App::Test::Generator::SchemaExtractor> version and exit.
96  
97 =item B<--fuzz|-f>
98  
99 After extraction, run C<App::Test::Generator::CoverageGuidedFuzzer> against
100 each method using the generated schema.
101  
102 =item B<--fuzz-all>
103  
104 Like C<--fuzz>, but also fuzzes methods that have no extractable schema.
105  
106 =item B<--fuzz-iters> I<N>
107  
108 Number of fuzzing iterations per method (default: 100).
109  
110 =item B<--corpus-dir|-c> I<DIR>
111  
112 Directory for loading and saving the fuzzing corpus between runs.
113  
114 =item B<--minimize-corpus>
115  
116 After fuzzing, run C<minimize_corpus()> to reduce the saved corpus to the
117 smallest subset that still covers every branch discovered in this run.
118  
119 =item B<--help|-h>
120  
121 Print a short usage summary and exit.
122  
123 =item B<--man|-m>
124  
125 Print the full manual page and exit.
126  
127 =back
128  
129 =head1 DESCRIPTION
130  
131 This tool analyzes a Perl module and generates YAML schema files for each
132 method, suitable for use with L<App::Test::Generator>
133 using the C<fuzz-harness-generator> program which will create the C<.t> file to run through C<prove>.
134  
135 The extractor uses three sources of information:
136  
137 =over 4
138  
139 =item 1. POD Documentation
140  
141 Parses parameter descriptions from POD to extract types and constraints.
142  
143 =item 2. Code Analysis
144  
145 Analyzes validation patterns in the code (ref checks, length checks, etc.)
146  
147 =item 3. Method Signatures
148  
149 Extracts parameter names from method signatures.
150  
151 =back
152  
153 The tool assigns a confidence level (high/medium/low) to each schema based
154 on how much information it could infer.
155  
156 =head1 FUZZING
157  
158 When C<--fuzz> is specified, the tool will additionally run
159 C<App::Test::Generator::CoverageGuidedFuzzer> against each method after
160 schema extraction.
161  
162 By default all methods with at least one known input parameter are fuzzed,
163 regardless of confidence level. Use C<--fuzz-all> to also attempt fuzzing
164 methods with no input schema (these will use purely random generation).
165  
166 The fuzzer will:
167  
168 =over 4
169  
170 =item * Load and C<require> the target module at runtime
171  
172 =item * Run coverage-guided fuzzing using the extracted schema as input spec
173  
174 =item * Report any crashes or unexpected errors found
175  
176 =item * Persist a corpus to C<--corpus-dir> for incremental improvement across runs
177  
178 =item * Optionally minimise the corpus with C<--minimize-corpus> to keep file sizes bounded
179  
180 =back
181  
182 Corpus files are named C<< <corpus-dir>/<method>.json >> and are automatically
183 loaded on subsequent runs, so each run builds on the last. Without
184 C<--minimize-corpus> the corpus grows by several entries per run; with it, only
185 the entries that provide unique branch coverage are retained (greedy set-cover),
186 plus a deduplicated set of entries from prior runs that cannot be re-evaluated.
187 Bug-triggering inputs are always kept regardless of coverage contribution.
188  
189 =cut
190  
191 # ---------------------------------------------------------------------------
192 # Option parsing
193 # ---------------------------------------------------------------------------
194  
195 129 my %cli_opts = (
196 help => 0,
197 man => 0,
198 );
199  
200 129 my %extractor_opts = (
201 output_dir => 'schemas',
202 strict_pod => 'warn',
203 verbose => 0,
204 version => 0,
205 );
206  
207 129 my $fuzz = 0;
208 129 my $fuzz_all = 0;
209 129 my $fuzz_iters = 100;
210 129 my $minimize_corpus = 0;
211 129 my $corpus_dir; # default set after output_dir is known
212 my $function_filter; # when set, restrict to this one routine
213  
214 129 Getopt::Long::Configure('bundling');
215  
216 GetOptions(
217 'output-dir|o=s' => \$extractor_opts{output_dir},
218 'strict-pod|s=s' => \$extractor_opts{strict_pod},
219 'function|F=s' => \$function_filter,
220 'verbose|v' => \$extractor_opts{verbose},
221 'version|V' => \$extractor_opts{version},
222 'fuzz|f' => \$fuzz,
223 'fuzz-all' => \$fuzz_all,
224 'fuzz-iters=i' => \$fuzz_iters,
225 'corpus-dir|c=s' => \$corpus_dir,
226 'minimize-corpus' => \$minimize_corpus,
227 'help|h' => \$cli_opts{help},
228 'man|m' => \$cli_opts{man},
229 129 ▶) or pod2usage(2);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("output-dir|o=s", \$extractor_opts{"output_dir"}, "strict-pod|s=s", \$extractor_opts{"strict_pod"}, "function|F=s", \$function_filter, "verbose|v", \$extractor_opts{"verbose"}, "version|V", \$extractor_opts{"version"}, "fuzz|f", \$fuzz, "fuzz-all", \$fuzz_all, "fuzz-iters=i", \$fuzz_iters, "corpus-dir|c=s", \$corpus_dir, "minimize-corpus", \$minimize_corpus, "help|h", \$cli_opts{"help"}, "man|m", \$cli_opts{"man"}) or pod2usage(2) 129 0 129
230  
231 129 ▶pod2usage(-exitval => 0, -verbose => 1) if $cli_opts{help};
Branch: 1 branch
BranchTrueFalseTotal
if $cli_opts{"help"} 1 128 129
232 128 ▶pod2usage(-exitval => 0, -verbose => 2) if $cli_opts{man};
Branch: 1 branch
BranchTrueFalseTotal
if $cli_opts{"man"} 0 128 128
233  
234 128 ▶if($extractor_opts{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extractor_opts{"version"}) 0 128 128
235 0 print $App::Test::Generator::SchemaExtractor::VERSION, "\n";
236 0 exit 0;
237 }
238  
239 128 ▶if ($extractor_opts{strict_pod} !~ /^(off|warn|fatal)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($extractor_opts{"strict_pod"} =~ /^(off|warn|fatal)$/) 1 127 128
240 1 die "Invalid --strict-pod value '$extractor_opts{strict_pod}'. Expected off, warn, or fatal";
241 }
242  
243 127 ▶my $input_file = shift @ARGV or pod2usage('Error: No input file specified');
Branch: 1 branch
BranchTrueFalseTotal
my $input_file = shift @ARGV or pod2usage("Error: No input file specified") 126 1 127
244 126 ▶die "Error: File not found: $input_file" unless -f $input_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $input_file 0 126 126
245  
246 # Default corpus dir sits under the output dir
247 126 ▶$corpus_dir //= File::Spec->catdir($extractor_opts{output_dir}, 'corpus');
Condition66%2/3
$corpus_dir //= "File::Spec"->catdir($extractor_opts{"output_dir"}, "corpus")
l!l&&r!l&&!r
31230
MC/DC0%0/2
$corpus_dir //= "File::Spec"->catdir($extractor_opts{"output_dir"}, "corpus")
$corpus_dir "File::Spec"->catdir($extractor_opts{"output_dir"}, "corpus")
Truth table66%2/3
A //= B
A= $corpus_dirB= "File::Spec"->catdir($extractor_opts{"output_dir"}, "corpus")
ABresult
1X1
011
000
248  
249 # ---------------------------------------------------------------------------
250 # Schema extraction
251 # ---------------------------------------------------------------------------
252  
253 126 print "Extracting schemas from: $input_file\n";
254 126 print "Output directory: $extractor_opts{output_dir}\n\n";
255  
256 126 ▶make_path($extractor_opts{output_dir}) unless -d $extractor_opts{output_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $extractor_opts{"output_dir"} 5 121 126
257  
258 126 my $extractor = App::Test::Generator::SchemaExtractor->new(
259 input_file => $input_file,
260 %extractor_opts,
261 );
262  
263 126 my $schemas = $extractor->extract_all();
264  
265 # ---------------------------------------------------------------------------
266 # Optional: restrict to a single routine
267 # ---------------------------------------------------------------------------
268  
269 126 ▶if (defined $function_filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $function_filter) 0 126 126
270 0 ▶ unless (exists $schemas->{$function_filter}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $schemas->{$function_filter}) 0 0 0
271 0 print STDERR "Error: routine '$function_filter' not found in $input_file\n";
272 0 print STDERR "Available routines: ", join(', ', sort keys %$schemas), "\n";
273 0 exit 1;
274 }
275  
276 # Remove every other schema and its on-disk YAML file so that callers
277 # (e.g. t/self-fuzz.t) see exactly one .yml in the output directory.
278 0 for my $name (keys %$schemas) {
279 0 ▶ next if $name eq $function_filter;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $function_filter 0 0 0
280 0 delete $schemas->{$name};
281 0 my $yml = File::Spec->catfile($extractor_opts{output_dir}, "$name.yml");
282 0 ▶ unlink $yml if -f $yml;
Branch: 1 branch
BranchTrueFalseTotal
if -f $yml 0 0 0
283 }
284  
285 0 print "Restricted to routine: $function_filter\n\n";
286 }
287  
288 # ---------------------------------------------------------------------------
289 # Optional: coverage-guided fuzzing
290 # ---------------------------------------------------------------------------
291  
292 126 my %fuzz_results; # method_name => report hashref
293  
294 126 ▶if ($fuzz) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fuzz) 3 123 126
295 3 require App::Test::Generator::CoverageGuidedFuzzer;
296 3 ▶ make_path($corpus_dir) unless -d $corpus_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $corpus_dir 2 1 3
297  
298 # Load the target module once so all methods are callable
299 3 my $package = _load_target_module($input_file, $schemas);
300  
301 # Try to build a default instance for object method calls.
302 # Most OO modules need a $self as the first argument.
303 # We try new() with no args, then new({}), then give up and fuzz as functions.
304 3 my $instance = _try_construct($package);
305 3 ▶ if ($instance) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance) { } 0 3 3
306 0 print "Constructed $package instance for method calls.\n";
307 } else {
308 3 print "Could not construct $package instance; fuzzing as functions.\n";
309 }
310  
311 3 ▶ print "Fuzzing with $fuzz_iters iterations per method",
Branch: 1 branch
BranchTrueFalseTotal
$fuzz_all ? : 0 3 3
312 ($fuzz_all ? ' (all methods)' : ' (methods with known inputs)'),
313 "...\n\n";
314  
315 3 foreach my $method (sort keys %$schemas) {
316 3 my $schema = $schemas->{$method};
317 3 ▶ my $iconf = $schema->{_confidence}{input}{level} // 'low';
Condition50%1/2
$schema->{"_confidence"}{"input"}{"level"} // "low"
l!l
30
MC/DC0%0/1
$schema->{"_confidence"}{"input"}{"level"} // "low"
$schema->{"_confidence"}{"input"}{"level"}
Truth table50%1/2
A
A= $schema->{"_confidence"}{"input"}{"level"}
Aresult
11
00
318  
319 3 ▶ unless ($fuzz_all) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fuzz_all) 3 0 3
320 # Skip methods with no input schema at all — there is nothing to fuzz
321 0 ▶ next if $iconf eq 'none' && !%{ $schema->{input} // {} };
Branch: 1 branch
BranchTrueFalseTotal
if $iconf eq "none" and not %{$schema->{"input"} // {};} 0 3 3
Condition20%1/5
$iconf eq "none" and not %{$schema->{"input"} // {};}
!ll&&!rl&&r
300
$schema->{"input"} // {}
l!l
00
MC/DC0%0/2
$iconf eq "none" and not %{$schema->{"input"} // {};}
$iconf eq "none" not %{$schema->{"input"} // {};}
MC/DC0%0/1
$schema->{"input"} // {}
$schema->{"input"}
Truth table33%1/3
A and not(B)
A= $iconf eq "none"B= not %{$schema->{"input"} // {};}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $schema->{"input"}
Aresult
11
00
322 }
323  
324 3 my $sub_ref = $package->can($method);
325 3 ▶ unless ($sub_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub_ref) 0 3 3
326 0 warn " Skipping $method: not callable in $package\n";
327 0 next;
328 }
329  
330 # Skip constructors and AUTOLOAD — not suitable for direct fuzzing
331 3 ▶ if ($method =~ /^(new|AUTOLOAD|DESTROY|import)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(new|AUTOLOAD|DESTROY|import)$/) 0 3 3
332 print " Skipping $method (constructor/special method)\n"
333 0 ▶ if $extractor_opts{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $extractor_opts{"verbose"} 0 0 0
334 0 next;
335 }
336  
337 3 my $corpus_file = File::Spec->catfile($corpus_dir, "$method.json");
338  
339 3 print " Fuzzing $method ($iconf confidence)... ";
340  
341 3 my $fuzzer = App::Test::Generator::CoverageGuidedFuzzer->new(
342 schema => $schema,
343 target_sub => $sub_ref,
344 instance => $instance,
345 iterations => $fuzz_iters,
346 );
347  
348 3 ▶ $fuzzer->load_corpus($corpus_file) if -f $corpus_file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $corpus_file 1 2 3
349  
350 3 my $report = $fuzzer->run();
351  
352 3 ▶ if ($minimize_corpus) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minimize_corpus) 1 2 3
353 1 my $stats = $fuzzer->minimize_corpus();
354 printf "%d bugs, %d branches covered, corpus %d->%d entries\n",
355 $report->{bugs_found},
356 $report->{branches_covered},
357 $stats->{before},
358 1 $stats->{after};
359 }
360  
361 3 $fuzzer->save_corpus($corpus_file);
362  
363 3 $fuzz_results{$method} = $report;
364  
365 printf "%d bugs, %d branches covered\n",
366 $report->{bugs_found},
367 $report->{branches_covered}
368 3 ▶ unless $minimize_corpus;
Branch: 1 branch
BranchTrueFalseTotal
unless $minimize_corpus 2 1 3
369 }
370  
371 3 print "\n";
372 }
373  
374 # ---------------------------------------------------------------------------
375 # Summary report
376 # ---------------------------------------------------------------------------
377  
378 126 print '=' x 70, "\n",
379 "EXTRACTION SUMMARY\n",
380 '=' x 70, "\n\n";
381  
382 126 my %input_confidence_counts = (high => 0, medium => 0, low => 0, none => 0);
383 126 my %output_confidence_counts = (high => 0, medium => 0, low => 0, none => 0);
384  
385 126 foreach my $method (sort keys %$schemas) {
386 291 my $schema = $schemas->{$method};
387 291 ▶ my $iconf = $schema->{_confidence}{input}{level} // 'low';
Condition50%1/2
$schema->{"_confidence"}{"input"}{"level"} // "low"
l!l
2910
MC/DC0%0/1
$schema->{"_confidence"}{"input"}{"level"} // "low"
$schema->{"_confidence"}{"input"}{"level"}
Truth table50%1/2
A
A= $schema->{"_confidence"}{"input"}{"level"}
Aresult
11
00
388 291 ▶ my $oconf = $schema->{_confidence}{output}{level} // 'low';
Condition50%1/2
$schema->{"_confidence"}{"output"}{"level"} // "low"
l!l
2910
MC/DC0%0/1
$schema->{"_confidence"}{"output"}{"level"} // "low"
$schema->{"_confidence"}{"output"}{"level"}
Truth table50%1/2
A
A= $schema->{"_confidence"}{"output"}{"level"}
Aresult
11
00
389 291 $input_confidence_counts{$iconf}++;
390 291 $output_confidence_counts{$oconf}++;
391  
392 291 my $param_count = scalar grep { $_ !~ /^_/ } keys %{ $schema->{input} };
393  
394 291 my $fuzz_col = '';
395 291 ▶ if (exists $fuzz_results{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fuzz_results{$method}) 3 288 291
396 3 my $r = $fuzz_results{$method};
397 $fuzz_col = $r->{bugs_found}
398 ? sprintf(' BUGS: %d', $r->{bugs_found})
399 3 ▶ : ' fuzz: ok';
Branch: 1 branch
BranchTrueFalseTotal
$r->{'bugs_found'} ? : 0 3 3
400 }
401  
402 291 printf "%-30s %d params [%s input confidence] [%s output confidence]%s\n",
403 $method, $param_count, uc($iconf), uc($oconf), $fuzz_col;
404 }
405  
406 126 print "\n";
407 126 print 'Total methods: ', (scalar keys %$schemas), "\n";
408 126 print " Input:\n";
409 126 print " High confidence: $input_confidence_counts{high}\n";
410 126 print " Medium confidence: $input_confidence_counts{medium}\n";
411 126 print " Low confidence: $input_confidence_counts{low}\n";
412 126 print " Output:\n";
413 126 print " High confidence: $output_confidence_counts{high}\n";
414 126 print " Medium confidence: $output_confidence_counts{medium}\n";
415 126 print " Low confidence: $output_confidence_counts{low}\n";
416 126 print "\n";
417  
418 126 ▶if ($input_confidence_counts{low} > 0 || $input_confidence_counts{medium} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_confidence_counts{"low"} > 0 or $input_confidence_counts{"medium"} > 0) 42 84 126
Condition100%3/3
$input_confidence_counts{"low"} > 0 or $input_confidence_counts{"medium"} > 0
l!l&&r!l&&!r
152784
MC/DC100%2/2
$input_confidence_counts{"low"} > 0 or $input_confidence_counts{"medium"} > 0
$input_confidence_counts{"low"} > 0 $input_confidence_counts{"medium"} > 0
Truth table100%3/3
A or B
A= $input_confidence_counts{"low"} > 0B= $input_confidence_counts{"medium"} > 0
ABresult
1X1
011
000
419 42 print "RECOMMENDATION:\n",
420 "Review the generated schemas in $extractor_opts{output_dir}/\n",
421 "Focus on methods with medium/low confidence ratings.\n\n";
422 }
423  
424 # Fuzz bug detail
425 126 ▶if (%fuzz_results) {
Branch: 1 branch
BranchTrueFalseTotal
if (%fuzz_results) 3 123 126
426 3 my $total_bugs = 0;
427 3 $total_bugs += $_->{bugs_found} for values %fuzz_results;
428  
429 3 ▶ if ($total_bugs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_bugs) { } 0 3 3
430 0 print '=' x 70, "\n",
431 "FUZZING BUGS FOUND ($total_bugs total)\n",
432 '=' x 70, "\n\n";
433  
434 0 foreach my $method (sort keys %fuzz_results) {
435 0 my $r = $fuzz_results{$method};
436 0 ▶ next unless $r->{bugs_found};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"bugs_found"} 0 0 0
437 0 print " $method:\n";
438 0 for my $i (0 .. $#{ $r->{bugs} }) {
439 0 my $bug = $r->{bugs}[$i];
440 0 ▶ my $inp = defined($bug->{input}) ? qq("$bug->{input}") : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $bug->{'input'} ? : 0 0 0
441 printf " Bug %d: input=%-30s error=%s\n",
442 0 $i + 1, $inp, $bug->{error};
443 }
444 0 print "\n";
445 }
446 0 print "Corpora saved to: $corpus_dir/\n\n";
447 } else {
448 3 print "Fuzzing complete: no bugs found across ",
449 scalar(keys %fuzz_results), " methods.\n\n";
450 }
451 }
452  
453 126 ▶if ($extractor_opts{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extractor_opts{"verbose"}) 1 125 126
454 1 print "Schemas:\n\t", Dumper($schemas);
455 }
456  
457 126 print "Schema files written to: $extractor_opts{output_dir}/\n";
458  
459 # ---------------------------------------------------------------------------
460 # Helper: load the target module so methods become callable
461 # ---------------------------------------------------------------------------
462  
463 3 ▶sub _load_target_module {
Subroutine _load_target_module: called CC=6 SCAR=18.1
464 3 my ($input_file, $schemas) = @_;
465  
466 # Derive the package name from the first schema entry that has 'module' set
467 3 my ($package) = map { $schemas->{$_}{module} }
468 3 grep { $schemas->{$_}{module} }
469 keys %$schemas;
470  
471 3 ▶ die 'Could not determine package name from extracted schemas' unless $package;
Branch: 1 branch
BranchTrueFalseTotal
unless $package 0 3 3
472  
473 # Reject anything that is not a syntactically valid Perl package name
474 # before it is used to build a require path — guards against this
475 # becoming a code-injection vector if $package is ever sourced from
476 # something less constrained than a PPI-parsed 'package' statement.
477 3 ▶ die "Invalid package name: $package"
Branch: 1 branch
BranchTrueFalseTotal
unless $package =~ /^[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ 0 3 3
478 unless $package =~ /^[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/;
479  
480 # Add the module's containing lib dir to @INC
481 # Walks up from the file looking for a 'lib' directory
482 3 my $abs = File::Spec->rel2abs($input_file);
483 3 my ($volume, $directory) = File::Spec->splitpath($abs);
484 3 my @dirs = File::Spec->splitdir($directory);
485  
486 3 ▶ while (@dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 9 0 9
487 # catpath() (not catdir()) keeps $volume attached, so this still
488 # resolves on Windows when the temp dir and the checkout live on
489 # different drive letters (a bare catdir() result is interpreted
490 # as relative to the *current* drive, not $volume's).
491 9 my $candidate = File::Spec->catpath($volume, File::Spec->catdir(@dirs, 'lib'), '');
492 9 ▶ if (-d $candidate) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $candidate) 3 6 9
493 3 lib->import($candidate);
494 3 last;
495 }
496 6 pop @dirs;
497 }
498  
499 # require() the module by file path rather than via string eval, so
500 # $package is never compiled as Perl source.
501 3 (my $module_file = $package) =~ s{::}{/}g;
502 3 ▶ eval { require "$module_file.pm" }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require "$module_file.pm" } } or die "Could not load $package for fuzzing: $@" 3 0 3
503 or die "Could not load $package for fuzzing: $@";
504  
505 3 return $package;
506 }
507  
508 # Try to construct a default instance of the target package for method calls.
509 # Attempts new() with progressively more forgiving argument lists.
510 # Returns the instance on success, undef if nothing works.
511 3 ▶sub _try_construct {
Subroutine _try_construct: called CC=5 SCAR=21.7
512 3 my ($package) = @_;
513  
514 3 for my $args ([], [{}], [undef]) {
515 9 my $obj = eval { $package->new(@$args) };
516 9 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 9 0 9
517 0 ▶ next unless defined $obj && ref $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj and ref $obj 0 0 0
Condition0%0/3
defined $obj and ref $obj
!ll&&!rl&&r
000
MC/DC0%0/2
defined $obj and ref $obj
defined $obj ref $obj
Truth table0%0/3
A and B
A= defined $objB= ref $obj
ABresult
0X0
100
111
518 0 return $obj;
519 }
520  
521 3 return undef;
522 }
523  
524 __END__
« blib/lib/App/Test/Generator/LCSAJ/Coverage.pm ↑ Summary lib/App/Test/Generator/Mutation/Base.pm »