bin/extract-schemas

filter: Statementstmt 81.7% 148 / 181 Branchbran 58.7% 47 / 80 Conditioncond 48.0% 12 / 25 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 71.1% 222 / 312 CC 10 SCAR 14.6
CC
10
Cov
72%
_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 # Downstream all-fuzz runs otherwise just see a module stop being fuzzed.
266 126 ▶if(grep { ref($_) eq 'HASH' && ($_->{skip} // '') =~ /^multi-package file/ } values %$schemas) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {ref $_ eq "HASH" and ($_->{"skip"} // "") =~ /^multi-package file/;} values %$schemas) 0 126 126
Condition60%3/5
ref $_ eq 'HASH' && ($_->{'skip'} // '') =~ /^multi-package file/
!ll&&!rl&&r
02910
$_->{"skip"} // ""
l!l
10281
MC/DC50%1/2
ref $_ eq 'HASH' && ($_->{'skip'} // '') =~ /^multi-package file/
ref $_ eq 'HASH' $_->{"skip"}
Truth table66%2/3
A && B
A= ref $_ eq 'HASH'B= $_->{"skip"}
ABresult
0X0
100
111
267 0 print STDERR "Note: $input_file declares more than one package; its schemas are marked skip and will not be fuzzed\n";
268 }
269  
270 # ---------------------------------------------------------------------------
271 # Optional: restrict to a single routine
272 # ---------------------------------------------------------------------------
273  
274 126 ▶if (defined $function_filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $function_filter) 0 126 126
275 0 ▶ unless (exists $schemas->{$function_filter}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $schemas->{$function_filter}) 0 0 0
276 0 print STDERR "Error: routine '$function_filter' not found in $input_file\n";
277 0 print STDERR "Available routines: ", join(', ', sort keys %$schemas), "\n";
278 0 exit 1;
279 }
280  
281 # Remove every other schema and its on-disk YAML file so that callers
282 # (e.g. t/self-fuzz.t) see exactly one .yml in the output directory.
283 0 for my $name (keys %$schemas) {
284 0 ▶ next if $name eq $function_filter;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $function_filter 0 0 0
285 0 delete $schemas->{$name};
286 0 my $yml = File::Spec->catfile($extractor_opts{output_dir}, "$name.yml");
287 0 ▶ unlink $yml if -f $yml;
Branch: 1 branch
BranchTrueFalseTotal
if -f $yml 0 0 0
288 }
289  
290 0 print "Restricted to routine: $function_filter\n\n";
291 }
292  
293 # ---------------------------------------------------------------------------
294 # Optional: coverage-guided fuzzing
295 # ---------------------------------------------------------------------------
296  
297 126 my %fuzz_results; # method_name => report hashref
298  
299 126 ▶if ($fuzz) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fuzz) 3 123 126
300 3 require App::Test::Generator::CoverageGuidedFuzzer;
301 3 ▶ make_path($corpus_dir) unless -d $corpus_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $corpus_dir 2 1 3
302  
303 # Load the target module once so all methods are callable
304 3 my $package = _load_target_module($input_file, $schemas);
305  
306 # Try to build a default instance for object method calls.
307 # Most OO modules need a $self as the first argument.
308 # We try new() with no args, then new({}), then give up and fuzz as functions.
309 3 my $instance = _try_construct($package);
310 3 ▶ if ($instance) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance) { } 0 3 3
311 0 print "Constructed $package instance for method calls.\n";
312 } else {
313 3 print "Could not construct $package instance; fuzzing as functions.\n";
314 }
315  
316 3 ▶ print "Fuzzing with $fuzz_iters iterations per method",
Branch: 1 branch
BranchTrueFalseTotal
$fuzz_all ? : 0 3 3
317 ($fuzz_all ? ' (all methods)' : ' (methods with known inputs)'),
318 "...\n\n";
319  
320 3 foreach my $method (sort keys %$schemas) {
321 3 my $schema = $schemas->{$method};
322 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
323  
324 3 ▶ unless ($fuzz_all) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fuzz_all) 3 0 3
325 # Skip methods with no input schema at all — there is nothing to fuzz
326 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
327 }
328  
329 3 my $sub_ref = $package->can($method);
330 3 ▶ unless ($sub_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub_ref) 0 3 3
331 0 warn " Skipping $method: not callable in $package\n";
332 0 next;
333 }
334  
335 # Skip constructors and AUTOLOAD — not suitable for direct fuzzing
336 3 ▶ if ($method =~ /^(new|AUTOLOAD|DESTROY|import)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(new|AUTOLOAD|DESTROY|import)$/) 0 3 3
337 print " Skipping $method (constructor/special method)\n"
338 0 ▶ if $extractor_opts{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $extractor_opts{"verbose"} 0 0 0
339 0 next;
340 }
341  
342 3 my $corpus_file = File::Spec->catfile($corpus_dir, "$method.json");
343  
344 3 print " Fuzzing $method ($iconf confidence)... ";
345  
346 3 my $fuzzer = App::Test::Generator::CoverageGuidedFuzzer->new(
347 schema => $schema,
348 target_sub => $sub_ref,
349 instance => $instance,
350 iterations => $fuzz_iters,
351 );
352  
353 3 ▶ $fuzzer->load_corpus($corpus_file) if -f $corpus_file;
Branch: 1 branch
BranchTrueFalseTotal
if -f $corpus_file 1 2 3
354  
355 3 my $report = $fuzzer->run();
356  
357 3 ▶ if ($minimize_corpus) {
Branch: 1 branch
BranchTrueFalseTotal
if ($minimize_corpus) 1 2 3
358 1 my $stats = $fuzzer->minimize_corpus();
359 printf "%d bugs, %d branches covered, corpus %d->%d entries\n",
360 $report->{bugs_found},
361 $report->{branches_covered},
362 $stats->{before},
363 1 $stats->{after};
364 }
365  
366 3 $fuzzer->save_corpus($corpus_file);
367  
368 3 $fuzz_results{$method} = $report;
369  
370 printf "%d bugs, %d branches covered\n",
371 $report->{bugs_found},
372 $report->{branches_covered}
373 3 ▶ unless $minimize_corpus;
Branch: 1 branch
BranchTrueFalseTotal
unless $minimize_corpus 2 1 3
374 }
375  
376 3 print "\n";
377 }
378  
379 # ---------------------------------------------------------------------------
380 # Summary report
381 # ---------------------------------------------------------------------------
382  
383 126 print '=' x 70, "\n",
384 "EXTRACTION SUMMARY\n",
385 '=' x 70, "\n\n";
386  
387 126 my %input_confidence_counts = (high => 0, medium => 0, low => 0, none => 0);
388 126 my %output_confidence_counts = (high => 0, medium => 0, low => 0, none => 0);
389  
390 126 foreach my $method (sort keys %$schemas) {
391 291 my $schema = $schemas->{$method};
392 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
393 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
394 291 $input_confidence_counts{$iconf}++;
395 291 $output_confidence_counts{$oconf}++;
396  
397 291 my $param_count = scalar grep { $_ !~ /^_/ } keys %{ $schema->{input} };
398  
399 291 my $fuzz_col = '';
400 291 ▶ if (exists $fuzz_results{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fuzz_results{$method}) 3 288 291
401 3 my $r = $fuzz_results{$method};
402 $fuzz_col = $r->{bugs_found}
403 ? sprintf(' BUGS: %d', $r->{bugs_found})
404 3 ▶ : ' fuzz: ok';
Branch: 1 branch
BranchTrueFalseTotal
$r->{'bugs_found'} ? : 0 3 3
405 }
406  
407 291 printf "%-30s %d params [%s input confidence] [%s output confidence]%s\n",
408 $method, $param_count, uc($iconf), uc($oconf), $fuzz_col;
409 }
410  
411 126 print "\n";
412 126 print 'Total methods: ', (scalar keys %$schemas), "\n";
413 126 print " Input:\n";
414 126 print " High confidence: $input_confidence_counts{high}\n";
415 126 print " Medium confidence: $input_confidence_counts{medium}\n";
416 126 print " Low confidence: $input_confidence_counts{low}\n";
417 126 print " Output:\n";
418 126 print " High confidence: $output_confidence_counts{high}\n";
419 126 print " Medium confidence: $output_confidence_counts{medium}\n";
420 126 print " Low confidence: $output_confidence_counts{low}\n";
421 126 print "\n";
422  
423 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
424 42 print "RECOMMENDATION:\n",
425 "Review the generated schemas in $extractor_opts{output_dir}/\n",
426 "Focus on methods with medium/low confidence ratings.\n\n";
427 }
428  
429 # Fuzz bug detail
430 126 ▶if (%fuzz_results) {
Branch: 1 branch
BranchTrueFalseTotal
if (%fuzz_results) 3 123 126
431 3 my $total_bugs = 0;
432 3 $total_bugs += $_->{bugs_found} for values %fuzz_results;
433  
434 3 ▶ if ($total_bugs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($total_bugs) { } 0 3 3
435 0 print '=' x 70, "\n",
436 "FUZZING BUGS FOUND ($total_bugs total)\n",
437 '=' x 70, "\n\n";
438  
439 0 foreach my $method (sort keys %fuzz_results) {
440 0 my $r = $fuzz_results{$method};
441 0 ▶ next unless $r->{bugs_found};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"bugs_found"} 0 0 0
442 0 print " $method:\n";
443 0 for my $i (0 .. $#{ $r->{bugs} }) {
444 0 my $bug = $r->{bugs}[$i];
445 0 ▶ my $inp = defined($bug->{input}) ? qq("$bug->{input}") : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $bug->{'input'} ? : 0 0 0
446 printf " Bug %d: input=%-30s error=%s\n",
447 0 $i + 1, $inp, $bug->{error};
448 }
449 0 print "\n";
450 }
451 0 print "Corpora saved to: $corpus_dir/\n\n";
452 } else {
453 3 print "Fuzzing complete: no bugs found across ",
454 scalar(keys %fuzz_results), " methods.\n\n";
455 }
456 }
457  
458 126 ▶if ($extractor_opts{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extractor_opts{"verbose"}) 1 125 126
459 1 print "Schemas:\n\t", Dumper($schemas);
460 }
461  
462 126 print "Schema files written to: $extractor_opts{output_dir}/\n";
463  
464 # ---------------------------------------------------------------------------
465 # Helper: load the target module so methods become callable
466 # ---------------------------------------------------------------------------
467  
468 3 ▶sub _load_target_module {
Subroutine _load_target_module: called CC=6 SCAR=18.1
469 3 my ($input_file, $schemas) = @_;
470  
471 # Derive the package name from the first schema entry that has 'module' set
472 3 my ($package) = map { $schemas->{$_}{module} }
473 3 grep { $schemas->{$_}{module} }
474 keys %$schemas;
475  
476 3 ▶ die 'Could not determine package name from extracted schemas' unless $package;
Branch: 1 branch
BranchTrueFalseTotal
unless $package 0 3 3
477  
478 # Reject anything that is not a syntactically valid Perl package name
479 # before it is used to build a require path — guards against this
480 # becoming a code-injection vector if $package is ever sourced from
481 # something less constrained than a PPI-parsed 'package' statement.
482 3 ▶ die "Invalid package name: $package"
Branch: 1 branch
BranchTrueFalseTotal
unless $package =~ /^[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/ 0 3 3
483 unless $package =~ /^[A-Za-z_]\w*(?:::[A-Za-z_]\w*)*\z/;
484  
485 # Add the module's containing lib dir to @INC
486 # Walks up from the file looking for a 'lib' directory
487 3 my $abs = File::Spec->rel2abs($input_file);
488 3 my ($volume, $directory) = File::Spec->splitpath($abs);
489 3 my @dirs = File::Spec->splitdir($directory);
490  
491 3 ▶ while (@dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 9 0 9
492 # catpath() (not catdir()) keeps $volume attached, so this still
493 # resolves on Windows when the temp dir and the checkout live on
494 # different drive letters (a bare catdir() result is interpreted
495 # as relative to the *current* drive, not $volume's).
496 9 my $candidate = File::Spec->catpath($volume, File::Spec->catdir(@dirs, 'lib'), '');
497 9 ▶ if (-d $candidate) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $candidate) 3 6 9
498 3 lib->import($candidate);
499 3 last;
500 }
501 6 pop @dirs;
502 }
503  
504 # require() the module by file path rather than via string eval, so
505 # $package is never compiled as Perl source.
506 3 (my $module_file = $package) =~ s{::}{/}g;
507 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
508 or die "Could not load $package for fuzzing: $@";
509  
510 3 return $package;
511 }
512  
513 # Try to construct a default instance of the target package for method calls.
514 # Attempts new() with progressively more forgiving argument lists.
515 # Returns the instance on success, undef if nothing works.
516 3 ▶sub _try_construct {
Subroutine _try_construct: called CC=5 SCAR=21.7
517 3 my ($package) = @_;
518  
519 3 for my $args ([], [{}], [undef]) {
520 9 my $obj = eval { $package->new(@$args) };
521 9 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 9 0 9
522 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
523 0 return $obj;
524 }
525  
526 3 return undef;
527 }
528  
529 __END__
« blib/lib/App/Test/Generator/LCSAJ/Coverage.pm ↑ Summary lib/App/Test/Generator/Mutation/Base.pm »