bin/iabtcfv2

filter: Statementstmt 91.9% 264 / 287 Branchbran 69.7% 106 / 152 Conditioncond 47.0% 8 / 17 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 96.0% 24 / 25 Podpod n/a totaltotal 81.9% 403 / 492 CC 80 SCAR 25.9
CC
80
Cov
83%
run_validate
33.1
_validate_string
19.5
run_dump
10.1
CRAP 13.3
« blib/lib/GDPR/IAB/TCFv2/RangeSection.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/Validator.pm »
1 #!/usr/bin/env perl
2  
3 55 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 55 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 55 ▶use Getopt::Long qw(GetOptionsFromArray :config pass_through require_order bundling);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 55 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 55 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 55 ▶eval { require JSON; 1 } or require JSON::PP;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require JSON; 1 } } or require JSON::PP 55 0 55
10 55 ▶use GDPR::IAB::TCFv2;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 55 ▶use constant {EXIT_SUCCESS => 0, EXIT_PARSE_ERROR => 1,};
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 55 my $script_path = File::Spec->rel2abs($0);
15  
16 13 ▶sub _json_false {
Subroutine _json_false: called CC=2 SCAR=7.6
17 13 ▶ return JSON->can('false') ? JSON->false() : JSON::PP::false();
Branch: 1 branch
BranchTrueFalseTotal
'JSON'->can('false') ? : 13 0 13
18 }
19  
20 8 ▶sub _json_true {
Subroutine _json_true: called CC=2 SCAR=7.6
21 8 ▶ return JSON->can('true') ? JSON->true() : JSON::PP::true();
Branch: 1 branch
BranchTrueFalseTotal
'JSON'->can('true') ? : 8 0 8
22 }
23  
24 9 ▶sub _clean_error {
Subroutine _clean_error: called CC=3 SCAR=11.2
25 9 my $err = shift;
26 9 ▶ return "" unless defined $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 0 9 9
27  
28 # If it's a reference (e.g. an exception object), return it as-is
29 9 ▶ return $err if ref $err;
Branch: 1 branch
BranchTrueFalseTotal
if ref $err 0 9 9
30  
31 # Strip Perl/Carp's automatic " at ... line ..." location suffix
32 9 $err =~ s/ at \S+ line \d+\.?\n?\z//;
33 9 chomp $err;
34 9 return $err;
35 }
36  
37 # Subcommand registry: drives dispatch, the short-help summaries, and
38 # the unknown-subcommand error message. New subcommands plug in here
39 # with no other code edits.
40 #
41 # Each `common` row is [short, long, value-placeholder, description].
42 # Rows appear in the short help in the order listed. Niche flags (e.g.
43 # --cmp-validator-timeout) are intentionally omitted from `common` and
44 # documented only in the full POD.
45 55 my %SUBCOMMANDS = (
46 dump => {
47 summary => 'Parse TC strings and emit JSON.',
48 synopsis => 'iabtcfv2 dump [options] [tc_strings...]',
49 runner => \&run_dump,
50 common => [
51 ['-p', '--pretty', '', 'Indent JSON output'],
52 ['-c', '--compact', '', 'Compact JSON (lists of IDs instead of boolean maps)'],
53 ['-v', '--vendor-id', '<ID>', 'Filter output to a single vendor ID'],
54 ['-s', '--strict-legal-basis', '', 'Strict spec validation in the underlying parser'],
55 ['-i', '--ignore-errors', '', 'Skip parse-error JSON output (still bumps exit code)'],
56 ['-f', '--fail-fast', '', 'Exit immediately on the first parse error'],
57 ['-e', '--errors-to-stderr', '', 'Send error JSON objects to STDERR'],
58 ['-w', '--enable-warnings', '', 'Emit human-readable warnings on STDERR'],
59 ['-q', '--quiet', '', 'Suppress STDOUT (exit code only)'],
60 ],
61 },
62 validate => {
63 summary => 'Validate TC strings against a vendor identity and a purpose policy.',
64 synopsis => 'iabtcfv2 validate -v <id> [options] [tc_strings...]',
65 runner => \&run_validate,
66 common => [
67 ['-v', '--vendor-id', '<ID>', 'REQUIRED: vendor ID to validate'],
68 ['-C', '--consent-purposes', '<LIST>', 'Purpose IDs that must be allowed on consent (e.g. 1,3)'],
69 ['-L', '--legitimate-interest-purposes', '<LIST>', 'Purpose IDs that must be allowed on legitimate interest'],
70 ['-F', '--flexible-purposes', '<LIST>', 'Purpose IDs declared flexible by the vendor'],
71 ['-d', '--verify-disclosed-vendors', '', 'Require vendor in the Disclosed Vendors segment'],
72 ['-s', '--strict-legal-basis', '', 'Strict spec validation in the underlying parser'],
73 ['-m', '--min-tcf-policy-version', '<N>', 'Reject TC strings whose policy version is below N'],
74 ['', '--cmp-validator', '<PATH-OR-URL>', 'Validate cmp_id against an IAB CMP registry snapshot'],
75 ['-a', '--all', '', 'Accumulate every failing rule (reasons array)'],
76 ['-p', '--pretty', '', 'Indent JSON output'],
77 ['-t', '--text', '', 'Human-readable text output instead of JSON'],
78 ['-i', '--ignore-errors', '', 'Skip parse-error JSON output'],
79 ['-f', '--fail-fast', '', 'Exit on first parse error or invalid TC string'],
80 ['-e', '--errors-to-stderr', '', 'Send error JSON objects to STDERR'],
81 ['-w', '--enable-warnings', '', 'Emit human-readable warnings on STDERR'],
82 ['-q', '--quiet', '', 'Suppress STDOUT (exit code only)'],
83 ],
84 },
85 );
86  
87 55 my %global_opts;
88 GetOptions(
89 'help' => \$global_opts{full_help},
90 'h' => \$global_opts{short_help},
91 'man' => \$global_opts{man},
92 'version|V' => \$global_opts{version},
93 55 ▶) or pod2usage(-input => $script_path, -exitval => 2, -verbose => 0);
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("help", \$global_opts{"full_help"}, "h", \$global_opts{"short_help"}, "man", \$global_opts{"man"}, "version|V", \$global_opts{"version"}) or pod2usage("-input", $script_path, "-exitval", 2, "-verbose", 0) 55 0 55
94  
95 55 ▶if ($global_opts{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global_opts{"version"}) 2 53 55
96 2 print "iabtcfv2 version $GDPR::IAB::TCFv2::VERSION\n";
97 2 exit EXIT_SUCCESS;
98 }
99  
100 53 ▶if ($global_opts{short_help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global_opts{"short_help"}) 1 52 53
101 1 _show_short_help($ARGV[0]);
102 }
103 52 ▶if ($global_opts{full_help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($global_opts{"full_help"}) 1 51 52
104 1 _show_help($ARGV[0]);
105 }
106 51 ▶pod2usage(-input => $script_path, -exitval => 1, -verbose => 2) if $global_opts{man};
Branch: 1 branch
BranchTrueFalseTotal
if $global_opts{"man"} 0 51 51
107  
108 51 my $subcommand = shift @ARGV;
109  
110 # Bare invocation: brief overview, exit 1 (mirrors `--help` behavior).
111 51 ▶_show_short_help() unless defined $subcommand;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subcommand 1 50 51
112  
113 # `iabtcfv2 help [<sub>]` keeps showing the full POD, same as before.
114 50 ▶if ($subcommand eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($subcommand eq "help") 1 49 50
115 1 _show_help(shift @ARGV);
116 }
117  
118 49 my $entry = $SUBCOMMANDS{$subcommand};
119 49 ▶_unknown_subcommand($subcommand) unless $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless $entry 1 48 49
120 48 $entry->{runner}->(@ARGV);
121  
122 27 ▶sub run_dump {
Subroutine run_dump: called CC=10 SCAR=23.2
123 27 my @args = @_;
124 27 my %opts = (
125 pretty => 0,
126 compact => 0,
127 'ignore-errors' => 0,
128 'fail-fast' => 0,
129 'errors-to-stderr' => 0,
130 'quiet' => 0,
131 'enable-warnings' => 0,
132 'vendor-id' => undef,
133 'strict-legal-basis' => 0,
134 );
135  
136 27 require Getopt::Long;
137 27 my $parser = Getopt::Long::Parser->new(config => [qw(no_pass_through require_order bundling)]);
138  
139 27 my $getopt_ok;
140 {
141 27 local @ARGV = @args;
142 $getopt_ok = $parser->getoptions(
143 'pretty|p' => \$opts{pretty},
144 'compact|c' => \$opts{compact},
145 'ignore-errors|i' => \$opts{'ignore-errors'},
146 'fail-fast|f' => \$opts{'fail-fast'},
147 'errors-to-stderr|e' => \$opts{'errors-to-stderr'},
148 'quiet|q' => \$opts{'quiet'},
149 'enable-warnings|w' => \$opts{'enable-warnings'},
150 'vendor-id|v=i' => \$opts{'vendor-id'},
151 'strict-legal-basis|s' => \$opts{'strict-legal-basis'},
152 1 ▶ 'help' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
153 1 pod2usage(-input => $script_path, -exitval => 1, -verbose => 99, -sections => "DUMP|BUGS");
154 },
155 1 ▶ 'h' => sub { _show_subcommand_short_help('dump'); exit 1; },
Subroutine __ANON__: called CC=1 SCAR=0.0
156 27 );
157 25 @args = @ARGV;
158 }
159 25 ▶ $getopt_ok or pod2usage(-input => $script_path, -exitval => 2, -verbose => 99, -sections => "DUMP/Usage");
Branch: 1 branch
BranchTrueFalseTotal
$getopt_ok or pod2usage("-input", $script_path, "-exitval", 2, "-verbose", 99, "-sections", "DUMP/Usage") 25 0 25
160  
161 25 ▶ my $json_pkg = JSON->can('new') ? 'JSON' : 'JSON::PP';
Branch: 1 branch
BranchTrueFalseTotal
'JSON'->can('new') ? : 25 0 25
162 25 my $json = $json_pkg->new->utf8;
163 25 ▶ if ($opts{pretty}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"pretty"}) 7 18 25
164 7 $json->pretty(1);
165 7 ▶ $json->indent_length(4) if $json->isa('JSON::PP');
Branch: 1 branch
BranchTrueFalseTotal
if $json->isa("JSON::PP") 0 7 7
166 }
167  
168 25 my $state = {count => 0, line_num => 0,};
169  
170 25 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 24 1 25
171 24 foreach my $str (@args) {
172 25 $state->{line_num}++;
173 25 _process_string($str, $state, \%opts, $json);
174 }
175 }
176 else {
177 1 ▶ binmode(STDIN, ':encoding(UTF-8)') if -t STDIN; ## no critic (InputOutput::ProhibitInteractiveTest, InputOutput::RequireEncodingWithUTF8Layer)
Branch: 1 branch
BranchTrueFalseTotal
if -t STDIN 0 1 1
178 1 ▶ while (my $line = <STDIN>) { ## no critic (InputOutput::ProhibitExplicitStdin)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline STDIN)) 1 1 2
179 1 $state->{line_num}++;
180 1 chomp $line;
181 1 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/u 0 1 1
182 1 _process_string($line, $state, \%opts, $json);
183 }
184 }
185  
186 24 exit EXIT_SUCCESS;
187 }
188  
189 26 ▶sub _process_string {
Subroutine _process_string: called CC=8 SCAR=20.9
190 26 my ($str, $state, $o, $j) = @_;
191 26 my $output_data;
192  
193 26 eval {
194 my $tcf = GDPR::IAB::TCFv2->Parse(
195 $str,
196 json => {compact => $o->{compact}, vendor_id => $o->{'vendor-id'},},
197 26 strict => $o->{'strict-legal-basis'},
198 );
199 19 $output_data = $tcf->TO_JSON;
200 };
201 26 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 7 19 26
202 7 ▶ if ($o->{'fail-fast'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"fail-fast"}) 1 6 7
203 1 ▶ warn "Fatal: Failed to parse TC string '$str' at line $state->{line_num}: $err\n" if $o->{'enable-warnings'};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"enable-warnings"} 0 1 1
204 1 exit EXIT_PARSE_ERROR;
205 }
206  
207 6 ▶ warn "Warning: Failed to parse TC string '$str' at line $state->{line_num}: $err\n" if $o->{'enable-warnings'};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"enable-warnings"} 0 6 6
208  
209 6 ▶ return if $o->{'ignore-errors'};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"ignore-errors"} 1 5 6
210  
211 5 $err = _clean_error($err);
212 5 $output_data = {tc_string => $str, error => $err, success => _json_false(),};
213  
214 5 ▶ if ($o->{'errors-to-stderr'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"errors-to-stderr"}) 1 4 5
215 1 warn $j->encode($output_data) . "\n";
216 1 return;
217 }
218 }
219  
220 23 ▶ return if $o->{'quiet'};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"quiet"} 0 23 23
221  
222 23 print $j->encode($output_data) . "\n";
223  
224 23 $state->{count}++;
225 }
226  
227 2 ▶sub _show_help {
Subroutine _show_help: called CC=5 SCAR=17.5
228 2 my $topic = shift;
229  
230 2 ▶ if ($topic && $topic eq 'dump') {
Branch: 1 branch
BranchTrueFalseTotal
if ($topic and $topic eq "dump") 1 1 2
Condition66%2/3
$topic and $topic eq "dump"
!ll&&!rl&&r
101
MC/DC50%1/2
$topic and $topic eq "dump"
$topic $topic eq "dump"
Truth table66%2/3
A and B
A= $topicB= $topic eq "dump"
ABresult
0X0
100
111
231 1 pod2usage(-input => $script_path, -exitval => 1, -verbose => 99, -sections => "DUMP|BUGS");
232 }
233 1 ▶ if ($topic && $topic eq 'validate') {
Branch: 1 branch
BranchTrueFalseTotal
if ($topic and $topic eq "validate") 0 1 1
Condition33%1/3
$topic and $topic eq "validate"
!ll&&!rl&&r
100
MC/DC0%0/2
$topic and $topic eq "validate"
$topic $topic eq "validate"
Truth table33%1/3
A and B
A= $topicB= $topic eq "validate"
ABresult
0X0
100
111
234 0 pod2usage(-input => $script_path, -exitval => 1, -verbose => 99, -sections => "VALIDATE|BUGS");
235 }
236 1 pod2usage(-input => $script_path, -exitval => 1, -verbose => 99, -sections => "SYNOPSIS|OPTIONS|SUBCOMMANDS|BUGS");
237 }
238  
239 # Brief overview shown by `iabtcfv2 -h`, by `iabtcfv2 -h <subcommand>`,
240 # by a bare `iabtcfv2`, or as a fallback path. Always exits 1 so the
241 # caller's shell sees "no command actually ran".
242 2 ▶sub _show_short_help {
Subroutine _show_short_help: called CC=4 SCAR=14.2
243 2 my $topic = shift;
244  
245 2 ▶ if (defined $topic && $SUBCOMMANDS{$topic}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $topic and $SUBCOMMANDS{$topic}) 0 2 2
Condition33%1/3
defined $topic and $SUBCOMMANDS{$topic}
!ll&&!rl&&r
200
MC/DC0%0/2
defined $topic and $SUBCOMMANDS{$topic}
defined $topic $SUBCOMMANDS{$topic}
Truth table33%1/3
A and B
A= defined $topicB= $SUBCOMMANDS{$topic}
ABresult
0X0
100
111
246 0 _show_subcommand_short_help($topic);
247 0 exit 1;
248 }
249  
250 2 print {*STDERR} "iabtcfv2 - CLI tool for GDPR IAB TCF v2 strings\n\n";
251 2 print {*STDERR} "Usage:\n iabtcfv2 <subcommand> [options] [tc_strings...]\n\n";
252 2 print {*STDERR} "Subcommands:\n";
253 2 for my $name (sort keys %SUBCOMMANDS) {
254 4 printf {*STDERR} " %-10s %s\n", $name, $SUBCOMMANDS{$name}{summary};
255 }
256 2 print {*STDERR} "\n";
257 2 print {*STDERR} "Run 'iabtcfv2 <subcommand> -h' for a short summary of that subcommand.\n";
258 2 print {*STDERR} "Run 'iabtcfv2 --help' for the full manual.\n";
259 2 exit 1;
260 }
261  
262 2 ▶sub _show_subcommand_short_help {
Subroutine _show_subcommand_short_help: called CC=7 SCAR=19.5
263 2 my $name = shift;
264 2 my $sub_entry = $SUBCOMMANDS{$name};
265  
266 2 print {*STDERR} "iabtcfv2 $name - $sub_entry->{summary}\n\n";
267 2 print {*STDERR} "Usage:\n $sub_entry->{synopsis}\n\n";
268 2 print {*STDERR} "Options:\n";
269  
270 # `-h` / `--help` are appended as if they were registry rows so they
271 # share the two-pass alignment below and do not require hardcoded
272 # column widths.
273 my @rows
274 2 = (@{$sub_entry->{common}}, ['-h', '', '', 'Show this short help'], ['', '--help', '', 'Show full documentation'],);
275  
276 # Two-pass formatting so the description column is left-aligned across
277 # rows regardless of long-option / value-placeholder width.
278 2 my @lines;
279 2 my $left_width = 0;
280 2 for my $row (@rows) {
281 29 my ($s, $l, $v, $desc) = @{$row};
282 29 my $left_col;
283 29 ▶ if (length $l) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $l) { } 27 2 29
284  
285 # `-x, --long-name [<VAL>]` — comma only when both forms exist.
286 27 ▶ my $short_col = length($s) ? "$s," : ' ';
Branch: 1 branch
BranchTrueFalseTotal
length $s ? : 24 3 27
287 27 ▶ $left_col = sprintf " %-3s %s%s", $short_col, $l, length($v) ? " $v" : '';
Branch: 1 branch
BranchTrueFalseTotal
length $v ? : 7 20 27
288 }
289 else {
290 # Short-only row (e.g. the `-h` line in the appended help block).
291 2 $left_col = sprintf " %s", $s;
292 }
293 29 ▶ $left_width = length($left_col) if length($left_col) > $left_width;
Branch: 1 branch
BranchTrueFalseTotal
if length $left_col > $left_width 7 22 29
294 29 push @lines, [$left_col, $desc];
295 }
296  
297 2 for my $r (@lines) {
298 29 printf {*STDERR} "%-${left_width}s %s\n", $r->[0], $r->[1];
299 }
300  
301 2 print {*STDERR} "\nReads TC strings from positional args, or one per line from STDIN.\n";
302 2 print {*STDERR} "\nRun 'iabtcfv2 $name --help' for the full documentation.\n";
303 }
304  
305 1 ▶sub _unknown_subcommand {
Subroutine _unknown_subcommand: called CC=2 SCAR=6.9
306 1 my $name = shift;
307  
308 1 print {*STDERR} "iabtcfv2: unknown subcommand '$name'\n\n";
309 1 print {*STDERR} "Available subcommands:\n";
310 1 for my $sc (sort keys %SUBCOMMANDS) {
311 2 printf {*STDERR} " %-10s %s\n", $sc, $SUBCOMMANDS{$sc}{summary};
312 }
313 1 print {*STDERR} "\n";
314 1 print {*STDERR} "Run 'iabtcfv2 -h' for a short overview, or 'iabtcfv2 --help' for the full manual.\n";
315 1 exit 2;
316 }
317  
318 7 ▶sub _parse_id_list {
Subroutine _parse_id_list: called CC=3 SCAR=11.8
319 7 my $s = shift;
320 7 ▶ return [] unless defined $s && length $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and length $s 0 7 7
Condition33%1/3
defined $s and length $s
!ll&&!rl&&r
007
MC/DC0%0/2
defined $s and length $s
defined $s length $s
Truth table33%1/3
A and B
A= defined $sB= length $s
ABresult
0X0
100
111
321 7 return [map { 0 + $_ } grep { length $_ } split /\s*,\s*/, $s];
322 }
323  
324 21 ▶sub run_validate { ## no critic (Subroutines::ProhibitExcessComplexity)
Subroutine run_validate: called CC=22 SCAR=35.0
325 21 my @args = @_;
326 21 my %opts = (
327 pretty => 0,
328 'ignore-errors' => 0,
329 'fail-fast' => 0,
330 'errors-to-stderr' => 0,
331 quiet => 0,
332 'enable-warnings' => 0,
333 'vendor-id' => undef,
334 'consent-purposes' => undef,
335 'legitimate-interest-purposes' => undef,
336 'flexible-purposes' => undef,
337 'verify-disclosed-vendors' => 0,
338 'strict-legal-basis' => 0,
339 'min-tcf-policy-version' => undef,
340 'cmp-validator' => undef,
341 'cmp-validator-network-ok' => 0,
342 'cmp-validator-verify-ssl' => 1,
343 'cmp-validator-timeout' => 30,
344 all => 0,
345 text => 0,
346 );
347  
348 21 require Getopt::Long;
349 21 my $parser = Getopt::Long::Parser->new(config => [qw(no_pass_through require_order bundling)]);
350  
351 21 my $getopt_ok;
352 {
353 21 local @ARGV = @args;
354 $getopt_ok = $parser->getoptions(
355 'pretty|p' => \$opts{pretty},
356 'ignore-errors|i' => \$opts{'ignore-errors'},
357 'fail-fast|f' => \$opts{'fail-fast'},
358 'errors-to-stderr|e' => \$opts{'errors-to-stderr'},
359 'quiet|q' => \$opts{quiet},
360 'enable-warnings|w' => \$opts{'enable-warnings'},
361 'vendor-id|v=i' => \$opts{'vendor-id'},
362 'consent-purposes|C=s' => \$opts{'consent-purposes'},
363 'legitimate-interest-purposes|L=s' => \$opts{'legitimate-interest-purposes'},
364 'flexible-purposes|F=s' => \$opts{'flexible-purposes'},
365 'verify-disclosed-vendors|d' => \$opts{'verify-disclosed-vendors'},
366 'strict-legal-basis|s' => \$opts{'strict-legal-basis'},
367 'min-tcf-policy-version|m=i' => \$opts{'min-tcf-policy-version'},
368 'cmp-validator=s' => \$opts{'cmp-validator'},
369 'cmp-validator-network-ok' => \$opts{'cmp-validator-network-ok'},
370 'cmp-validator-verify-ssl!' => \$opts{'cmp-validator-verify-ssl'},
371 'cmp-validator-timeout=i' => \$opts{'cmp-validator-timeout'},
372 'all|a' => \$opts{all},
373 'text|t' => \$opts{text},
374 0 ▶ 'help' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
375 0 pod2usage(-input => $script_path, -exitval => 1, -verbose => 99, -sections => "VALIDATE|BUGS");
376 },
377 1 ▶ 'h' => sub { _show_subcommand_short_help('validate'); exit 1; },
Subroutine __ANON__: called CC=1 SCAR=0.0
378 21 );
379 20 @args = @ARGV;
380 }
381 20 ▶ $getopt_ok or pod2usage(-input => $script_path, -exitval => 2, -verbose => 99, -sections => "VALIDATE/Usage");
Branch: 1 branch
BranchTrueFalseTotal
$getopt_ok or pod2usage("-input", $script_path, "-exitval", 2, "-verbose", 99, "-sections", "VALIDATE/Usage") 20 0 20
382  
383 20 ▶ unless (defined $opts{'vendor-id'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{"vendor-id"}) 1 19 20
384 1 pod2usage(
385 -input => $script_path,
386 -exitval => 2,
387 -message => "validate: --vendor-id|-v is required",
388 -verbose => 99,
389 -sections => "VALIDATE/Usage"
390 );
391 }
392  
393 19 require GDPR::IAB::TCFv2::Validator;
394  
395 my %vargs = (
396 vendor_id => $opts{'vendor-id'},
397 verify_disclosed_vendors => $opts{'verify-disclosed-vendors'},
398 19 strict_legal_basis => $opts{'strict-legal-basis'},
399 );
400 19 ▶ $vargs{consent_purpose_ids} = _parse_id_list($opts{'consent-purposes'}) if defined $opts{'consent-purposes'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"consent-purposes"} 5 14 19
401 $vargs{legitimate_interest_purpose_ids} = _parse_id_list($opts{'legitimate-interest-purposes'})
402 19 ▶ if defined $opts{'legitimate-interest-purposes'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"legitimate-interest-purposes"} 2 17 19
403 19 ▶ $vargs{flexible_purpose_ids} = _parse_id_list($opts{'flexible-purposes'}) if defined $opts{'flexible-purposes'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"flexible-purposes"} 0 19 19
404 19 ▶ $vargs{min_tcf_policy_version} = $opts{'min-tcf-policy-version'} if defined $opts{'min-tcf-policy-version'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"min-tcf-policy-version"} 0 19 19
405  
406 19 ▶ if (defined $opts{'cmp-validator'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"cmp-validator"}) 8 11 19
407 8 require GDPR::IAB::TCFv2::CMPValidator;
408  
409 8 my $src = $opts{'cmp-validator'};
410 8 my %cmp_args = (verify_ssl => $opts{'cmp-validator-verify-ssl'}, timeout => $opts{'cmp-validator-timeout'},);
411  
412 8 ▶ if ($src =~ m{^https?://}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src =~ m[^https?://]iu) { } 1 7 8
413 1 $cmp_args{url} = $src;
414 1 ▶ $cmp_args{network_ok} = 1 if $opts{'cmp-validator-network-ok'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"cmp-validator-network-ok"} 0 1 1
415 }
416 else {
417 7 $cmp_args{file} = $src;
418 }
419  
420 # The CMPValidator emits a `carp` when the registry is older
421 # than 28 days. Surface that warning only when the user has
422 # opted into warnings (--enable-warnings); otherwise filter it
423 # out so routine CLI use stays quiet. Other warnings flow
424 # through unchanged.
425 8 my $cmp_validator;
426 {
427 8 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
428 5 ▶ my $msg = defined($_[0]) ? $_[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 5 0 5
429 5 ▶ return if $msg =~ /CMP list is older than 28 days/;
Branch: 1 branch
BranchTrueFalseTotal
if $msg =~ /CMP list is older than 28 days/u 5 0 5
430 0 warn @_; ## no critic (ErrorHandling::RequireCarping)
431 }
432 8 ▶ if !$opts{'enable-warnings'};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"enable-warnings"} 7 1 8
433  
434 8 $cmp_validator = eval { GDPR::IAB::TCFv2::CMPValidator->new(%cmp_args) };
435 }
436  
437 8 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 2 6 8
438 2 $err = _clean_error($err);
439 2 warn "validate: $err\n";
440 2 exit 2;
441 }
442  
443 6 $vargs{cmp_validator} = $cmp_validator;
444 }
445  
446 17 my $validator = eval { GDPR::IAB::TCFv2::Validator->new(%vargs) };
447 17 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 17 17
448 0 $err = _clean_error($err);
449 0 warn "validate: $err\n";
450 0 exit 2;
451 }
452  
453 17 ▶ my $json_pkg = JSON->can('new') ? 'JSON' : 'JSON::PP';
Branch: 1 branch
BranchTrueFalseTotal
'JSON'->can('new') ? : 17 0 17
454 17 my $json = $json_pkg->new->utf8;
455 17 ▶ if ($opts{pretty}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"pretty"}) 0 17 17
456 0 $json->pretty(1);
457 0 ▶ $json->indent_length(4) if $json->isa('JSON::PP');
Branch: 1 branch
BranchTrueFalseTotal
if $json->isa("JSON::PP") 0 0 0
458 }
459  
460 17 my $state = {count => 0, line_num => 0, any_fail => 0,};
461  
462 17 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 17 0 17
463 17 foreach my $str (@args) {
464 17 $state->{line_num}++;
465 17 _validate_string($str, $state, \%opts, $json, $validator);
466 }
467 }
468 else {
469 0 ▶ binmode(STDIN, ':encoding(UTF-8)') if -t STDIN; ## no critic (InputOutput::ProhibitInteractiveTest, InputOutput::RequireEncodingWithUTF8Layer)
Branch: 1 branch
BranchTrueFalseTotal
if -t STDIN 0 0 0
470 0 ▶ while (my $line = <STDIN>) { ## no critic (InputOutput::ProhibitExplicitStdin)
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline STDIN)) 0 0 0
471 0 $state->{line_num}++;
472 0 chomp $line;
473 0 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/u 0 0 0
474 0 _validate_string($line, $state, \%opts, $json, $validator);
475 }
476 }
477  
478 17 ▶ exit($state->{any_fail} ? EXIT_PARSE_ERROR : EXIT_SUCCESS);
Branch: 1 branch
BranchTrueFalseTotal
$state->{'any_fail'} ? : 9 8 17
479 }
480  
481 17 ▶sub _validate_string {
Subroutine _validate_string: called CC=15 SCAR=29.7
482 17 my ($str, $state, $o, $j, $validator) = @_;
483  
484 17 ▶ my $result = eval { $o->{all} ? $validator->validate_all($str) : $validator->validate($str); };
Branch: 1 branch
BranchTrueFalseTotal
$o->{'all'} ? : 2 15 17
485 17 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 3 14 17
486 3 $state->{any_fail} = 1;
487  
488 3 ▶ if ($o->{'fail-fast'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"fail-fast"}) 0 3 3
489 0 ▶ warn "Fatal: Failed to parse TC string '$str' at line $state->{line_num}: $err\n" if $o->{'enable-warnings'};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"enable-warnings"} 0 0 0
490 0 exit EXIT_PARSE_ERROR;
491 }
492  
493 3 ▶ warn "Warning: Failed to parse TC string '$str' at line $state->{line_num}: $err\n" if $o->{'enable-warnings'};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"enable-warnings"} 0 3 3
494  
495 3 ▶ return if $o->{'ignore-errors'};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"ignore-errors"} 1 2 3
496  
497 2 $err = _clean_error($err);
498 2 my $output_data = {tc_string => $str, error => $err, success => _json_false(),};
499  
500 2 ▶ if ($o->{'errors-to-stderr'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"errors-to-stderr"}) 0 2 2
501 0 ▶ if ($o->{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{'text'}) { } 0 0 0
502 0 warn "ERROR $str: $err\n";
503 }
504 else {
505 0 warn $j->encode($output_data) . "\n";
506 }
507 0 return;
508 }
509  
510 2 ▶ return if $o->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"quiet"} 0 2 2
511 2 _emit_validate($output_data, $state, $o, $j);
512 2 return;
513 }
514  
515 14 my $output_data;
516 14 ▶ if ($result->is_valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->is_valid) { } 8 6 14
517 8 $output_data = {tc_string => $str, vendor_id => $o->{'vendor-id'}, valid => _json_true(),};
518 }
519 else {
520 6 $state->{any_fail} = 1;
521 6 my @reasons = $result->reasons;
522 $output_data = {
523 tc_string => $str,
524 vendor_id => $o->{'vendor-id'},
525 valid => _json_false(),
526 6 ▶ $o->{all} ? (reasons => \@reasons) : (reason => $reasons[0]),
Branch: 1 branch
BranchTrueFalseTotal
$o->{'all'} ? : 2 4 6
527 };
528 }
529  
530 14 ▶ return if $o->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"quiet"} 2 12 14
531  
532 12 _emit_validate($output_data, $state, $o, $j);
533  
534 12 ▶ exit EXIT_PARSE_ERROR if !$result->is_valid && $o->{'fail-fast'};
Branch: 1 branch
BranchTrueFalseTotal
if not $result->is_valid and $o->{"fail-fast"} 0 12 12
Condition66%2/3
not $result->is_valid and $o->{"fail-fast"}
!ll&&!rl&&r
750
MC/DC0%0/2
not $result->is_valid and $o->{"fail-fast"}
not $result->is_valid $o->{"fail-fast"}
Truth table66%2/3
not(A) and B
A= not $result->is_validB= $o->{"fail-fast"}
ABresult
0X0
100
111
535  
536 12 return;
537 }
538  
539 14 ▶sub _emit_validate {
Subroutine _emit_validate: called CC=7 SCAR=19.5
540 14 my ($output_data, $state, $o, $j) = @_;
541  
542 14 ▶ if ($o->{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"text"}) 3 11 14
543 3 my $tc = $output_data->{tc_string};
544 3 my $vid = $output_data->{vendor_id};
545 3 my $line;
546 3 ▶ if (exists $output_data->{error}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $output_data->{'error'}) { } 0 3 3
elsif ($output_data->{'valid'}) { } 1 2 3
547 0 $line = "ERROR $tc: $output_data->{error}";
548 }
549 elsif ($output_data->{valid}) {
550 1 $line = "OK $tc vendor $vid";
551 }
552 else {
553 2 ▶ my @reasons = $o->{all} ? @{$output_data->{reasons} || []} : ($output_data->{reason});
Branch: 1 branch
BranchTrueFalseTotal
$o->{'all'} ? : 1 1 2
Condition50%1/2
$output_data->{'reasons'} || []
l!l
10
MC/DC0%0/1
$output_data->{'reasons'} || []
$output_data->{'reasons'}
Truth table50%1/2
A
A= $output_data->{'reasons'}
Aresult
11
00
554 2 ▶ if (@reasons == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@reasons == 1) { } 1 1 2
555 1 $line = "FAIL $tc vendor $vid: $reasons[0]";
556 }
557 else {
558 1 $line = join "\n", "FAIL $tc vendor $vid:", map {" - $_"} @reasons;
559 }
560 }
561 3 print "$line\n";
562 3 $state->{count}++;
563 3 return;
564 }
565  
566 11 print $j->encode($output_data) . "\n";
567 11 $state->{count}++;
568  
569 11 return;
570 }
571  
572 __END__
« blib/lib/GDPR/IAB/TCFv2/RangeSection.pm ↑ Summary blib/lib/GDPR/IAB/TCFv2/Validator.pm »