Profile/Payload/Types/Validation.pm

filter: Statementstmt 86.7% 111 / 128 Branchbran 75.7% 53 / 70 Conditioncond 77.7% 7 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 92.5% 25 / 27 Podpod 100% 10 / 10 totaltotal 84.0% 210 / 250 CC 39 SCAR 18.4
CC
39
Cov
83%
validate_boolean
20.0
validate
12.3
validate_class
12.0
CRAP 6.3
« Profile/Payload/WiFi.pm ↑ Summary Profile/Payload/Tie/Root.pm »
1 package Config::Apple::Profile::Payload::Types::Validation;
2  
3 16 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 16 our $VERSION = '0.87.1';
8  
9 16 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use DateTime::Format::Flexible;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use Exporter::Easy (
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 OK => [qw(
14 validate
15 validate_string validate_number validate_real validate_date
16 validate_boolean validate_data validate_identifier validate_uuid
17 validate_class
18 )],
19 TAGS => [
20 'all' => [qw(
21 validate
22 validate_string validate_number validate_real validate_date
23 validate_boolean validate_data validate_identifier validate_uuid
24 validate_class
25 )],
26 'types' => [qw(
27 validate_string validate_number validate_real validate_date
28 validate_boolean validate_data validate_identifier validate_uuid
29 validate_class
30 )],
31 ],
32 16 );
33 16 ▶use Fcntl qw(F_GETFL O_RDONLY O_RDWR :seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 16 ▶use Regexp::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 16 ▶use Scalar::Util qw(openhandle blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 16 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 16 ▶use Config::Apple::Profile::Payload::Types qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39  
40 =encoding utf8
41  
42 =head1 NAME
43  
44 Config::Apple::Profile::Payload::Types::Validation - Validation of common
45 payload types.
46  
47 =head1 DESCRIPTION
48  
49 Apple Configuration Profiles contain one or more I<payloads>. Each payload
50 contains a dictionary, which can be thought of like a Perl hash. Within a
51 payload's dictionary, each key's value is restricted to a specific type.
52 One key might require a number; a different key might require a string, or some
53 binary data.
54  
55 Provided in this module are a number of Readonly scalars that will be used
56 (instead of strings) to identify the data types for configuration profile keys.
57 The scalars are all OK for import into your local namespace, or you can simply
58 import C<:all> to get all of them at once.
59  
60  
61 =head1 FUNCTIONS
62  
63 =head2 validate
64  
65 my $validated_value = validate($type, $value);
66  
67 Validates C<$value> as a valid C<$type>. If valid, returns the de-tainted
68 C<$value>. If invalid, returns C<undef>.
69  
70 C<$type> is one of the values from L<Config::Apple::Profile::Payload::Types>.
71 C<$value> is the value to be validated.
72  
73 IF C<$type> is not valid, or C<$value> is C<undef>, then C<undef> is returned.
74  
75 =cut
76  
77 681 ▶sub validate {
Subroutine validate: called CC=11 SCAR=25.1
Pod documented
78 681 my ($type, $value) = @_;
79
80 # We recognize String types
81 681 ▶ if ($type == $ProfileString) {
Branch: 9 branches
BranchTrueFalseTotal
if ($type == $Config::Apple::Profile::Payload::Types::Validation::ProfileString) { } 64 617 681
elsif ($type == $Config::Apple::Profile::Payload::Types::Validation::ProfileNumber) { } 184 433 617
elsif ($type == $Config::Apple::Profile::Payload::Types::Validation::ProfileReal) { } 49 384 433
elsif ($type == $Config::Apple::Profile::Payload::Types::Validation::ProfileBool) { } 0 384 384
elsif ($type == $Config::Apple::Profile::Payload::Types::Validation::ProfileData or $type == $Config::Apple::Profile::Payload::Types::Validation::ProfileNSDataBlob) { } 16 368 384
elsif ($type == $Config::Apple::Profile::Payload::Types::Validation::ProfileDate) { } 15 353 368
elsif ($type == $Config::Apple::Profile::Payload::Types::Validation::ProfileIdentifier) { } 31 322 353
elsif ($type == $Config::Apple::Profile::Payload::Types::Validation::ProfileUUID) { } 322 0 322
elsif ($type == $Config::Apple::Profile::Payload::Types::Validation::ProfileClass) { } 0 0 0
82 64 return validate_string($value);
83 }
84
85 # We recognize Number types
86 elsif ($type == $ProfileNumber) {
87 184 return validate_number($value);
88 }
89
90 # We recognize Real (floating-point number) types
91 elsif ($type == $ProfileReal) {
92 49 return validate_real($value);
93 }
94
95 # We recognize Boolean types
96 elsif ($type == $ProfileBool) {
97 0 return validate_boolean($value);
98 }
99
100 # We recognize Data types
101 ▶ elsif ( ($type == $ProfileData)
Condition66%2/3
$type == $Config::Apple::Profile::Payload::Types::Validation::ProfileData or $type == $Config::Apple::Profile::Payload::Types::Validation::ProfileNSDataBlob
l!l&&r!l&&!r
160368
MC/DC50%1/2
$type == $Config::Apple::Profile::Payload::Types::Validation::ProfileData or $type == $Config::Apple::Profile::Payload::Types::Validation::ProfileNSDataBlob
$type == $Config::Apple::Profile::Payload::Types::Validation::ProfileData $type == $Config::Apple::Profile::Payload::Types::Validation::ProfileNSDataBlob
Truth table66%2/3
A or B
A= $type == $Config::Apple::Profile::Payload::Types::Validation::ProfileDataB= $type == $Config::Apple::Profile::Payload::Types::Validation::ProfileNSDataBlob
ABresult
1X1
011
000
102 || ($type == $ProfileNSDataBlob)
103 ) {
104 16 return validate_data($value);
105 }
106
107 # We recognize Date types
108 elsif ($type == $ProfileDate) {
109 15 return validate_date($value);
110 }
111
112 # We recognize Identifier types
113 elsif ($type == $ProfileIdentifier) {
114 31 return validate_identifier($value);
115 }
116
117 # We recognize UUID types
118 elsif ($type == $ProfileUUID) {
119 322 return validate_uuid($value);
120 }
121
122 # We recognize classes
123 elsif ($type == $ProfileClass) {
124 0 return validate_class($value);
125 }
126 }
127  
128  
129 =head2 validate_string
130  
131 my $valid_string = validate_string($value);
132  
133 Returns a de-tained C<$value> if it is a defined, non-empty scalar, and can be
134 encoded as UTF-8 by L<Encode>.
135  
136 =cut
137  
138 64 ▶sub validate_string {
Subroutine validate_string: called CC=3 SCAR=11.0
Pod documented
139 64 my ($value) = @_;
140
141 # References aren't allowed here
142 ## no critic (ProhibitExplicitReturnUndef)
143 64 ▶ return undef if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 6 58 64
144 ##use critic
145
146 # Empty strings aren't allowed, either.
147 58 ▶ if ($value =~ m/^(.+)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(.+)$/s) { } 51 6 57
148 51 $value = $1;
149
150 }
151 else {
152 ## no critic (ProhibitExplicitReturnUndef)
153 6 return undef;
154 ##use critic
155 }
156
157 # Try to encode as UTF-8, to make sure it's safe
158 51 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
159 51 encode('UTF-8', $value, Encode::FB_CROAK | Encode::LEAVE_SRC);
160 }
161 2 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
162 2 $value = undef;
163 51 };
164
165 # If we're here, then we are valid!
166 51 return $value;
167 }
168  
169  
170 =head2 validate_number
171  
172 my $number = validate_number($value)
173  
174 Returns a de-tained C<$value> if it is an integer. A leading + is OK.
175 Any other input returns C<undef>.
176  
177 =cut
178  
179 184 ▶sub validate_number {
Subroutine validate_number: called CC=3 SCAR=11.0
Pod documented
180 184 my ($value) = @_;
181
182 # References aren't allowed here
183 ## no critic (ProhibitExplicitReturnUndef)
184 184 ▶ return undef if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 9 175 184
185 ##use critic
186
187 # Numbers must be integers, positive or negative (or zero).
188 175 ▶ if ($value =~ /^$RE{num}{int}{-keep}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^$Config::Apple::Profile::Payload::Types::Validation::RE{"num"}{"int"}{"-keep"}$/) 155 18 173
189 155 return $1;
190 }
191
192 # If we're here, the matching failed, so return undef
193 ## no critic (ProhibitExplicitReturnUndef)
194 18 return undef;
195 ##use critic
196 }
197  
198  
199 =head2 validate_real
200  
201 my $number = validate_real($value)
202  
203 Returns a de-tainted C<$value> if it is an integer or a floating-point number.
204 A loading C<+> is OK. An exponent, positive or negative, is also OK.
205 Any other input returns C<undef>.
206  
207 =cut
208  
209 49 ▶sub validate_real {
Subroutine validate_real: called CC=3 SCAR=11.0
Pod documented
210 49 my ($value) = @_;
211
212 # References aren't allowed here
213 ## no critic (ProhibitExplicitReturnUndef)
214 49 ▶ return undef if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 6 43 49
215 ##use critic
216
217 # Numbers must be floating-point, positive or negative (or zero).
218 43 ▶ if ($value =~ /^$RE{num}{real}{-keep}$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^$Config::Apple::Profile::Payload::Types::Validation::RE{"num"}{"real"}{"-keep"}$/i) 36 6 42
219 36 return $1;
220 }
221
222 # If we're here, the matching failed, so return undef
223 ## no critic (ProhibitExplicitReturnUndef)
224 6 return undef;
225 ##use critic
226 }
227  
228  
229 =head2 validate_boolean
230  
231 my $boolean = validate_boolean($value);
232  
233 If C<$value> can be evaluated as true or false, returns a C<1> or a C<0>,
234 respectively. Will return C<undef> if a reference is passed.
235  
236 =cut
237  
238 0 ▶sub validate_boolean {
Subroutine validate_boolean: not called CC=4 SCAR=30.0
Pod documented
239 0 my ($value) = @_;
240
241 # References aren't allowed here
242 ## no critic (ProhibitExplicitReturnUndef)
243 0 ▶ return undef if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 0 0
244 ##use critic
245
246 # A simple evaluation!
247 0 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 0 0 0
248 0 return 1;
249 }
250 0 ▶ if (!$value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $value) 0 0 0
251 0 return 0;
252 }
253 }
254  
255  
256 =head2 validate_date
257  
258 my $date_object = validate_date($value);
259  
260 If C<$value> is already a finite C<DateTime> object, it is returned immediately.
261 If C<$value> is a string, and can be parsed by L<DateTime::Format::Flexible>,
262 the resulting C<DateTime> object will be returned.
263  
264 Unparseable strings, infinite C<DateTime> objects, and any other input will
265 return C<undef>.
266  
267 =cut
268  
269 15 ▶sub validate_date {
Subroutine validate_date: called CC=5 SCAR=16.1
Pod documented
270 15 my ($value) = @_;
271
272 # If we have a blessed ref, which is a DateTime object, and it represents
273 # a finite time, then we're good!
274 15 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 4 11 15
275 ## no critic (ProhibitExplicitReturnUndef)
276 4 ▶ return undef unless blessed($value);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) 0 4 4
277 4 ▶ return undef unless $value->isa('DateTime');
Branch: 1 branch
BranchTrueFalseTotal
unless $value->isa("DateTime") 1 3 4
278 3 ▶ return undef unless $value->is_finite;
Branch: 1 branch
BranchTrueFalseTotal
unless $value->is_finite 2 1 3
279 ##use critic
280
281 1 return $value;
282 }
283
284 # At this point, we have a scalar, so let's see if it can be parsed
285 11 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
286 11 $value = DateTime::Format::Flexible->parse_datetime($value);
287 }
288 # If the parse fails, it dies, so return undef
289 3 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
290 3 $value = undef;
291 11 };
292
293 # Return either our object, or undef
294 11 return $value;
295 }
296  
297  
298 =head2 validate_data
299  
300 # With a file handle
301 my $handle = validate_data($handle);
302  
303 # With binary data
304 my $handle = validate_data($bytes);
305  
306 If passed an already-open file handle, or any object that represents a file
307 (such as an C<IO::> object), the handle (without the object tie) will be
308 returned.
309  
310 If passed a scalar, it will be checked to make sure it is not empty, and that
311 is not a utf8 string. The contents of the string will be placed into an
312 anonymous in-memory file, and the filehandle will be returned.
313  
314 =cut
315  
316 16 ▶sub validate_data {
Subroutine validate_data: called CC=7 SCAR=19.5
Pod documented
317 16 my ($value) = @_;
318
319 # How we act here depends if we have a string or a filehandle
320 # Let's first check if we were given an open filehandle
321 16 ▶ if (openhandle($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&openhandle($value)) 12 4 16
322 # First, get just the plain filehandle
323 12 my $value = openhandle($value);
324
325 # Check if the file is open for reading
326 # I would like to use the solution from
327 # <http://stackoverflow.com/questions/672214>, but that doesn't work on
328 # all filehandles, unfortunately.
329 # We'll have to do it the hard way.
330
331 #my $modes = fcntl($value, F_GETFL, 0);
332 #my $mask = 2**O_RDONLY + 2**O_RDWR;
333 #unless (($modes & $mask) > 0) {
334 # die "Filehandle is not open for reading.";
335 #}
336 12 my $ignore;
337 my $count ;
338 12 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
339 12 $count = read $value, $ignore, 1;
340 12 };
341 12 ▶ unless (defined $count) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $count) 2 10 12
342 2 die "Unable to read from filehandle. Is it open for reading?";
343 }
344
345 # If we can't seek, we're probably dealing with something bad
346 10 ▶ unless (seek $value, -1, SEEK_CUR) {
Branch: 1 branch
BranchTrueFalseTotal
unless (seek $value, -1, 1) 0 10 10
347 0 die "Unable to seek with filehandle. Is it pointing to a file?";
348 }
349
350 10 return $value;
351 }
352
353 # If we don't have an open handle, then make sure it's not an object
354 4 ▶ unless (ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 3 1 4
355 # We have a string. Let's make sure it's binary, and non-empty.
356 3 ▶ if ( (!utf8::is_utf8($value))
Branch: 1 branch
BranchTrueFalseTotal
if (not utf8::is_utf8($value) and length $value > 0) 2 1 3
Condition66%2/3
not utf8::is_utf8($value) and length $value > 0
!ll&&!rl&&r
102
MC/DC50%1/2
not utf8::is_utf8($value) and length $value > 0
not utf8::is_utf8($value) length $value > 0
Truth table66%2/3
not(A) and B
A= not utf8::is_utf8($value)B= length $value > 0
ABresult
0X0
100
111
357 && (length($value) > 0)
358 ) {
359 # Pull the string into an anonymous in-memory file, and return that.
360 2 open(my $file, '+>', undef);
361 2 binmode $file;
362 2 print $file $value;
363 2 seek $file, 0, 0;
364 2 return $file;
365 }
366 }
367
368 # If we're here, then we are dealing with something unknown to us.
369 ## no critic (ProhibitExplicitReturnUndef)
370 2 return undef;
371 ##use critic
372 }
373  
374  
375 =head2 validate_identifier
376  
377 my $valid_identifier = validate_identifier($value);
378  
379 Returns a de-tained C<$value> if it is a single-line string that matches the
380 format of a domain name (without spaces). Otherwise, returns C<undef>.
381  
382 =cut
383  
384 31 ▶sub validate_identifier {
Subroutine validate_identifier: called CC=5 SCAR=16.1
Pod documented
385 31 my ($value) = @_;
386
387 # References aren't allowed here
388 ## no critic (ProhibitExplicitReturnUndef)
389 31 ▶ return undef if ref($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 6 25 31
390 ##use critic
391
392 # Empty strings aren't allowed, either.
393 25 ▶ if ($value =~ m/^(.+)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(.+)$/s) 22 2 24
394 22 my $matched_string = $1;
395 # Identifiers are one-line strings
396 22 ▶ if ( ($matched_string !~ m/\n/s)
Branch: 1 branch
BranchTrueFalseTotal
if (not $matched_string =~ /\n/s and $matched_string =~ /^$Config::Apple::Profile::Payload::Types::Validation::RE{"net"}{"domain"}{"-nospace"}$/) 17 5 22
Condition100%3/3
not $matched_string =~ /\n/s and $matched_string =~ /^$Config::Apple::Profile::Payload::Types::Validation::RE{"net"}{"domain"}{"-nospace"}$/
!ll&&!rl&&r
1417
MC/DC100%2/2
not $matched_string =~ /\n/s and $matched_string =~ /^$Config::Apple::Profile::Payload::Types::Validation::RE{"net"}{"domain"}{"-nospace"}$/
not $matched_string =~ /\n/s $matched_string =~ /^$Config::Apple::Profile::Payload::Types::Validation::RE{"net"}{"domain"}{"-nospace"}$/
Truth table100%3/3
not(A) and B
A= not $matched_string =~ /\n/sB= $matched_string =~ /^$Config::Apple::Profile::Payload::Types::Validation::RE{"net"}{"domain"}{"-nospace"}$/
ABresult
0X0
100
111
397 && ($matched_string =~ m/^$RE{net}{domain}{-nospace}$/)
398 ) {
399 17 return $matched_string;
400 }
401 }
402
403 # If we're here, the matching failed, so return undef
404 ## no critic (ProhibitExplicitReturnUndef)
405 7 return undef;
406 ##use critic
407 }
408  
409 =head2 validate_uuid
410  
411 my $guid = validate_uuid($value);
412  
413 If C<$value> is a C<Data::GUID> object, it is returned immediately.
414 If C<$value> is a C<Data::UUID> object, an equivalent C<Data::GUID> object is
415 returned. Objects of other types return C<undef>.
416  
417 If C<$value> is a string that can be parsed as a GUID, an equivalent
418 C<Data::GUID> object is returned. Otherwise, C<undef> is returned.
419  
420 =cut
421  
422 322 ▶sub validate_uuid {
Subroutine validate_uuid: called CC=4 SCAR=14.0
Pod documented
423 322 my ($value) = @_;
424
425 322 my $class = ref($value);
426 322 my $uuid;
427
428 # We accept Data::UUID objects
429 322 ▶ if ($class eq 'Data::UUID') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "Data::UUID") 0 322 322
430 0 $uuid = Data::GUID::from_data_uuid($value);
431 0 return $uuid;
432 }
433
434 # We accept Data::GUID objects
435 322 ▶ if ($class eq 'Data::GUID') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq "Data::GUID") 101 221 322
436 101 return $value;
437 }
438
439 # We don't accept other kinds of objects
440 221 ▶ if ($class ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($class ne "") 3 218 221
441 ## no critic (ProhibitExplicitReturnUndef)
442 3 return undef;
443 ## use critic
444 }
445
446 # Have Data::GUID try to parse the input
447 # If from_any_string doesn't die, then it wored OK
448 218 eval {
449 218 $uuid = Data::GUID->from_any_string($value);
450 };
451  
452 # If parsing went OK, then we have our object! Otherwise, we have undef.
453 218 return $uuid;
454 }
455  
456  
457 =head2 validate_class
458  
459 my $object = validate_class($value)
460  
461 If C<$value> is an object, and is also an instance of
462 C<Config::Apple::Profile::Payload::Common> (or something that is a subclass),
463 then C<$value> is returned. Otherwise, C<undef> is returned.
464  
465 =cut
466  
467 0 ▶sub validate_class {
Subroutine validate_class: not called CC=3 SCAR=24.8
Pod documented
468 0 my ($object) = @_;
469
470 # We don't accept non-references
471 0 ▶ if (!blessed($object)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($object)) 0 0 0
472 ## no critic (ProhibitExplicitReturnUndef)
473 0 return undef;
474 ## use critic
475 }
476
477 0 ▶ if ($object->isa('Config::Apple::Profile::Payload::Common')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->isa("Config::Apple::Profile::Payload::Common")) 0 0 0
478 0 return $object;
479 }
480
481 # If we're here, then we have an object of the wrong class
482 ## no critic (ProhibitExplicitReturnUndef)
483 0 return undef;
484 ## use critic
485 }
486  
487 =head1 ACKNOWLEDGEMENTS
488  
489 Refer to L<Config::Apple::Profile> for acknowledgements.
490  
491 =head1 AUTHOR
492  
493 A. Karl Kornel, C<< <karl at kornel.us> >>
494  
495 =head1 COPYRIGHT AND LICENSE
496  
497 Copyright © 2014 A. Karl Kornel.
498  
499 This program is free software; you can redistribute it and/or modify it
500 under the terms of either: the GNU General Public License as published
501 by the Free Software Foundation; or the Artistic License.
502  
503 See L<http://dev.perl.org/licenses/> for more information.
504  
505 =cut
506  
507 16 1;
« Profile/Payload/WiFi.pm ↑ Summary Profile/Payload/Tie/Root.pm »