Profile/Payload/Types/Serialize.pm

filter: Statementstmt 78.4% 40 / 51 Branchbran 63.6% 14 / 22 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 75.2% 70 / 93 CC 16 SCAR 30.5
CC
16
Cov
73%
serialize
31.1
BEGIN
1.0
BEGIN
1.0
CRAP 21.0
« Profile/Payload/Common.pm ↑ Summary Profile/Payload/WiFi/EAPClientConfiguration.pm »
1 # This is the code for Config::Apple::Profile::Payload::Types::Serialize.
2 # For Copyright, please see the bottom of the file.
3  
4 package Config::Apple::Profile::Payload::Types::Serialize;
5  
6 16 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 16 our $VERSION = '0.87.1';
11  
12 16 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use Exporter::Easy (
Subroutine BEGIN: called CC=1 SCAR=0.0
15 16 OK => [qw(
16 serialize
17 )],
18 TAGS => [
19 'all' => [qw(
20 serialize
21 )],
22 ],
23 16 );
24 16 ▶use Mac::PropertyList;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 16 ▶use Config::Apple::Profile::Payload::Types qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27  
28 =encoding utf8
29  
30 =head1 NAME
31  
32 Config::Apple::Profile::Payload::Types::Serialize - Convert common payload
33 types to plist form.
34  
35  
36 =head1 DESCRIPTION
37  
38 This module contains code that is used to convert common payload types into
39 plist form.
40  
41  
42 =head1 FUNCTIONS
43  
44 =head2 serialize
45  
46 my $plist_fragment = serialize($type, $value, [$subtype]);
47  
48 Given C<$value>, returns a C<Mac::PropertyList> object containing the contents
49 of C<$value>. C<$type> must be one of the types listed in
50 L<Config::Apple::Profile::Payload::Types>, and is used to identify which type
51 of plist item to create (string, number, array, etc.).
52  
53 If C<$type> is C<$ProfileArray> or C<$ProfileDict>, then C<$subtype> must be
54 defined. C<serialize> will recurse into the structure, serialize it, and then
55 put everything into the appropriate plist array or dictionary, which will be
56 returned. C<$subtype> is used to tell C<serialize> what type of data is being
57 used within the array or dictionary.
58  
59 If C<$type> is C<$ProfileClass>, then C<< $value->plist >> will be called,
60 and the result will be returned.
61  
62 An exception will be thrown if C<$type> or C<$subtype> are not recognized.
63  
64 =cut
65  
66 302 ▶sub serialize {
Subroutine serialize: called CC=16 SCAR=34.4
Pod documented
67 302 my ($type, $value, $subtype) = @_;
68
69 # Strings need to be encoded as UTF-8 before export
70 302 ▶ if ( ($type == $ProfileString)
Branch: 10 branches
BranchTrueFalseTotal
if ($type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileString or $type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileIdentifier) { } 31 271 302
elsif ($type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileNumber) { } 73 198 271
elsif ($type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileReal) { } 36 162 198
elsif ($type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileData or $type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileNSDataBlob) { } 0 162 162
elsif ($type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileBool) { } 0 162 162
elsif ($type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileDate) { } 0 162 162
elsif ($type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileUUID) { } 50 112 162
elsif ($type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileArray) { } 100 12 112
elsif ($type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileDict) { } 12 0 12
elsif ($type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileClass) { } 0 0 0
Condition100%3/3
$type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileString or $type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileIdentifier
l!l&&r!l&&!r
1714271
MC/DC100%2/2
$type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileString or $type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileIdentifier
$type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileString $type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileIdentifier
Truth table100%3/3
A or B
A= $type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileStringB= $type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileIdentifier
ABresult
1X1
011
000
71 || ($type == $ProfileIdentifier)
72 ) {
73 31 $value = Mac::PropertyList::string->new(
74 Encode::encode('UTF-8', $value)
75 );
76 }
77
78 # Numbers are easy
79 elsif ($type == $ProfileNumber) {
80 73 $value = Mac::PropertyList::integer->new($value);
81 }
82
83 # Reals are similarly easy, but use an uppercase E
84 elsif ($type == $ProfileReal) {
85 36 $value = Mac::PropertyList::real->new(uc($value));
86 }
87
88 # All data is Base64-encoded for us by Mac::PropertyList
89 ▶ elsif ( ($type == $ProfileData)
Condition33%1/3
$type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileData or $type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileNSDataBlob
l!l&&r!l&&!r
00162
MC/DC0%0/2
$type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileData or $type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileNSDataBlob
$type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileData $type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileNSDataBlob
Truth table33%1/3
A or B
A= $type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileDataB= $type == $Config::Apple::Profile::Payload::Types::Serialize::ProfileNSDataBlob
ABresult
1X1
011
000
90 || ($type == $ProfileNSDataBlob)
91 ) {
92 # Slurp the file, and then hand it over
93 0 local $/ = undef;
94 0 $value = <$value>;
95 0 $value = Mac::PropertyList::data->new($value);
96 }
97
98 # There are separate objects for true/false booleans
99 elsif ($type == $ProfileBool) {
100 0 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 0 0 0
101 0 $value = Mac::PropertyList::true->new;
102 }
103 else {
104 0 $value = Mac::PropertyList::false->new;
105 }
106 }
107
108 # Date
109 elsif ($type == $ProfileDate) {
110 # Set the time zone to UTC, make a string, and plist that
111 0 $value->set_time_zone('UTC');
112 0 my $string = $value->ymd . 'T' . $value->hms . 'Z';
113 0 $value = Mac::PropertyList::date->new($string);
114 }
115
116 # UUIDs are converted to strings, then processed as such
117 elsif ($type == $ProfileUUID) {
118 50 $value = Mac::PropertyList::string->new(
119 Encode::encode('UTF-8', $value->as_string())
120 );
121 }
122
123 # For arrays, make a Perl array of fragments, then plist that
124 elsif ($type == $ProfileArray) {
125 100 my @array;
126
127 # Go through each array item, serialize it, and add it to our array
128 100 foreach my $item (@$value) {
129 63 push @array, serialize($subtype, $item, undef);
130 }
131
132 100 $value = Mac::PropertyList::array->new(\@array);
133 }
134
135 # For dictionaries, make a Perl hash of fragments, then plist that
136 elsif ($type == $ProfileDict) {
137 12 my %hash;
138
139 # Go through each hash key, serialize it, and add it to our array
140 12 foreach my $key (keys %$value) {
141 31 $hash{$key} = serialize($subtype, $value->{$key}, undef);
142 }
143
144 12 $value = Mac::PropertyList::dict->new(\%hash);
145 }
146
147 # For classes, let the object plist itself
148 elsif ($type == $ProfileClass) {
149 0 $value = $value->plist;
150 }
151
152 # We've checked all the types we know about
153 else {
154 0 die "Unknown type $type";
155 }
156
157 302 return $value;
158 }
159  
160  
161  
162  
163 =head1 ACKNOWLEDGEMENTS
164  
165 Refer to the L<Config::Apple::Profile> for acknowledgements.
166  
167 =head1 AUTHOR
168  
169 A. Karl Kornel, C<< <karl at kornel.us> >>
170  
171 =head1 COPYRIGHT AND LICENSE
172  
173 Copyright © 2014 A. Karl Kornel.
174  
175 This program is free software; you can redistribute it and/or modify it
176 under the terms of either: the GNU General Public License as published
177 by the Free Software Foundation; or the Artistic License.
178  
179 See L<http://dev.perl.org/licenses/> for more information.
180  
181 =cut
182  
183 16 1;
« Profile/Payload/Common.pm ↑ Summary Profile/Payload/WiFi/EAPClientConfiguration.pm »