blib/lib/Net/APNs/Extended/Base.pm

filter: Statementstmt 34.4% 51 / 148 Branchbran 14.0% 9 / 64 Conditioncond 44.0% 11 / 25 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 57.6% 15 / 26 Podpod 33.3% 1 / 3 totaltotal 33.1% 93 / 281 CC 42 SCAR 30.7
CC
42
Cov
30%
disconnect
58.5
_do_select
42.0
_connect
30.0
CRAP 21.6
↑ Summary blib/lib/Net/APNs/Extended.pm »
1 package Net::APNs::Extended::Base;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use parent 'Class::Accessor::Lite';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Socket qw(PF_INET SOCK_STREAM MSG_DONTWAIT inet_aton pack_sockaddr_in);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Net::SSLeay ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use Errno qw(EAGAIN EWOULDBLOCK EINTR);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 13 __PACKAGE__->mk_accessors(qw[
18 host_production
19 host_sandbox
20 is_sandbox
21 port
22 password
23 cert_file
24 cert
25 cert_type
26 key_file
27 key
28 key_type
29 read_timeout
30 write_timeout
31 json
32 ]);
33  
34 13 my %default = (
35 cert_type => Net::SSLeay::FILETYPE_PEM(),
36 key_type => Net::SSLeay::FILETYPE_PEM(),
37 read_timeout => 3,
38 write_timeout => undef,
39 );
40  
41 sub new {
42 14 ▶ my ($class, %args) = @_;
Subroutine new: called CC=8 SCAR=20.8
Pod documented
43 croak "`cert_file` or `cert` must be specify"
44 14 ▶ unless exists $args{cert_file} or exists $args{cert};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"cert_file"} or exists $args{"cert"} 1 13 14
Condition100%3/3
exists $args{"cert_file"} or exists $args{"cert"}
l!l&&r!l&&!r
581
MC/DC100%2/2
exists $args{"cert_file"} or exists $args{"cert"}
exists $args{"cert_file"} exists $args{"cert"}
Truth table100%3/3
A or B
A= exists $args{"cert_file"}B= exists $args{"cert"}
ABresult
1X1
011
000
45 croak "specifying both `cert_file` and `cert` is not allowed"
46 13 ▶ if exists $args{cert_file} and exists $args{cert};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"cert_file"} and exists $args{"cert"} 1 12 13
Condition100%3/3
exists $args{"cert_file"} and exists $args{"cert"}
!ll&&!rl&&r
841
MC/DC100%2/2
exists $args{"cert_file"} and exists $args{"cert"}
exists $args{"cert_file"} exists $args{"cert"}
Truth table100%3/3
A and B
A= exists $args{"cert_file"}B= exists $args{"cert"}
ABresult
0X0
100
111
47 croak "specifying both `key_file` and `key` is not allowed"
48 12 ▶ if exists $args{key_file} and exists $args{key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"key_file"} and exists $args{"key"} 1 11 12
Condition100%3/3
exists $args{"key_file"} and exists $args{"key"}
!ll&&!rl&&r
831
MC/DC100%2/2
exists $args{"key_file"} and exists $args{"key"}
exists $args{"key_file"} exists $args{"key"}
Truth table100%3/3
A and B
A= exists $args{"key_file"}B= exists $args{"key"}
ABresult
0X0
100
111
49  
50 11 Net::SSLeay::load_error_strings();
51 11 Net::SSLeay::SSLeay_add_ssl_algorithms();
52 11 Net::SSLeay::randomize();
53  
54 11 ▶ $args{json} ||= JSON::XS->new->utf8;
Condition33%1/3
$args{"json"} ||= "JSON::XS"->new->utf8
l!l&&r!l&&!r
0110
MC/DC0%0/2
$args{"json"} ||= "JSON::XS"->new->utf8
$args{"json"} "JSON::XS"->new->utf8
Truth table33%1/3
A ||= B
A= $args{"json"}B= "JSON::XS"->new->utf8
ABresult
1X1
011
000
55 11 bless { %default, %args }, $class;
56 }
57  
58 sub hostname {
59 2 ▶ my $self = shift;
Subroutine hostname: called CC=2 SCAR=6.9
Pod undocumented
60 2 ▶ $self->is_sandbox ? $self->host_sandbox : $self->host_production;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_sandbox ? : 1 1 2
61 }
62  
63 sub _connect {
64 0 ▶ my $self = shift;
Subroutine _connect: not called CC=5 SCAR=34.0
65 0 ▶ my $connection = $self->{_connection} || [];
Condition0%0/2
$self->{'_connection'} || []
l!l
00
MC/DC0%0/1
$self->{'_connection'} || []
$self->{'_connection'}
Truth table0%0/2
A
A= $self->{'_connection'}
Aresult
11
00
66 0 my ($sock, $ctx, $ssl) = @$connection;
67 0 ▶ return $connection if $sock && $ctx && $ssl;
Branch: 1 branch
BranchTrueFalseTotal
if $sock and $ctx and $ssl 0 0 0
Condition0%0/6
$sock and $ctx and $ssl
!ll&&!rl&&r
000
$sock and $ctx
!ll&&!rl&&r
000
MC/DC0%0/3
$sock and $ctx and $ssl
$sock $ctx $ssl
Truth table0%0/4
A and B and C
A= $sockB= $ctxC= $ssl
ABCresult
0XX0
10X0
1100
1111
68  
69 0 $self->disconnect;
70  
71 0 $sock = $self->_create_socket;
72 0 $ctx = $self->_create_ctx;
73 0 $ssl = $self->_create_ssl($sock, $ctx);
74  
75 0 $self->{_connection} = [$sock, $ctx, $ssl];
76 }
77  
78 sub _create_socket {
79 0 ▶ my $self = shift;
Subroutine _create_socket: not called CC=5 SCAR=34.0
80 0 ▶ socket(my $sock, PF_INET, SOCK_STREAM, 0) or die "can't create socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
socket my $sock, 2, 1, 0 or die "can't create socket: $!" 0 0 0
81 0 my $sock_addr = do {
82 0 ▶ my $iaddr = inet_aton($self->hostname)
Branch: 1 branch
BranchTrueFalseTotal
my $iaddr = inet_aton($self->hostname) or die sprintf("can't create iaddr from %s", $self->hostname) 0 0 0
83 or die sprintf "can't create iaddr from %s", $self->hostname;
84 0 ▶ pack_sockaddr_in $self->port, $iaddr or die "can't create sock_addr: $!";
Branch: 1 branch
BranchTrueFalseTotal
pack_sockaddr_in($self->port, $iaddr) or die "can't create sock_addr: $!" 0 0 0
85 };
86 0 ▶ CORE::connect($sock, $sock_addr) or die "can't connect socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
connect $sock, $sock_addr or die "can't connect socket: $!" 0 0 0
87 0 my $old_out = select($sock); $| = 1; select($old_out); # autoflush
88  
89 0 return $sock;
90 }
91  
92 sub _create_ctx {
93 0 ▶ my $self = shift;
Subroutine _create_ctx: not called CC=3 SCAR=24.8
94 0 ▶ my $ctx = Net::SSLeay::CTX_tlsv1_new() or _die_if_ssl_error("can't create SSL_CTX: $!");
Branch: 1 branch
BranchTrueFalseTotal
my $ctx = &Net::SSLeay::CTX_tlsv1_new() or _die_if_ssl_error("can't create SSL_CTX: $!") 0 0 0
95 0 Net::SSLeay::CTX_set_options($ctx, Net::SSLeay::OP_ALL());
96 0 _die_if_ssl_error("ctx options: $!");
97  
98 0 my $pw = $self->password;
99 0 ▶ Net::SSLeay::CTX_set_default_passwd_cb($ctx, ref $pw ? $pw : sub { $pw });
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $pw ? : 0 0 0
100  
101 0 $self->_set_certificate($ctx);
102  
103 0 return $ctx;
104 }
105  
106 sub _create_ssl {
107 0 ▶ my ($self, $sock, $ctx) = @_;
Subroutine _create_ssl: not called CC=2 SCAR=17.9
108 0 my $ssl = Net::SSLeay::new($ctx);
109 0 Net::SSLeay::set_fd($ssl, fileno $sock);
110 0 ▶ Net::SSLeay::connect($ssl) or _die_if_ssl_error("failed ssl connect: $!");
Branch: 1 branch
BranchTrueFalseTotal
&Net::SSLeay::connect($ssl) or _die_if_ssl_error("failed ssl connect: $!") 0 0 0
111  
112 0 return $ssl;
113 }
114  
115 sub _set_certificate {
116 0 ▶ my ($self, $ctx) = @_;
Subroutine _set_certificate: not called CC=5 SCAR=34.0
117 0 my ($cert_guard, $key_guard);
118 0 my $cert_file = $self->cert_file;
119 0 ▶ ($cert_guard, $cert_file) = _tmpfile($self->cert) unless defined $cert_file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cert_file 0 0 0
120 0 Net::SSLeay::CTX_use_certificate_file($ctx, $cert_file, $self->cert_type);
121 0 _die_if_ssl_error("certificate: $!");
122  
123 0 my $key_file;
124 0 ▶ if (exists $self->{key_file} or exists $self->{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'key_file'} or exists $self->{'key'}) { } 0 0 0
Condition0%0/3
exists $self->{"key_file"} or exists $self->{"key"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $self->{"key_file"} or exists $self->{"key"}
exists $self->{"key_file"} exists $self->{"key"}
Truth table0%0/3
A or B
A= exists $self->{"key_file"}B= exists $self->{"key"}
ABresult
1X1
011
000
125 0 $key_file = $self->key_file;
126 0 ▶ ($key_guard, $key_file) = _tmpfile($self->key) unless defined $key_file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key_file 0 0 0
127 }
128 else {
129 0 $key_file = $cert_file;
130 }
131 0 Net::SSLeay::CTX_use_RSAPrivateKey_file($ctx, $key_file, $self->key_type);
132 0 _die_if_ssl_error("private key: $!");
133 }
134  
135 sub disconnect {
136 11 ▶ my $self = shift;
Subroutine disconnect: called CC=9 SCAR=40.7
Pod undocumented
137 11 ▶ my $connection = $self->{_connection} || [];
Condition50%1/2
$self->{'_connection'} || []
l!l
011
MC/DC0%0/1
$self->{'_connection'} || []
$self->{'_connection'}
Truth table50%1/2
A
A= $self->{'_connection'}
Aresult
11
00
138 11 ▶ return 1 unless @$connection;
Branch: 1 branch
BranchTrueFalseTotal
unless @$connection 11 0 11
139  
140 0 my ($sock, $ctx, $ssl) = @$connection;
141 0 ▶ if ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) 0 0 0
142 0 ▶ unless (defined CORE::shutdown($sock, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined shutdown($sock, 1)) 0 0 0
143 0 die "can't shutdown socket: $!";
144 }
145 }
146 0 ▶ if ($ssl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssl) 0 0 0
147 0 Net::SSLeay::free($ssl);
148 0 _die_if_ssl_error("failed ssl free: $!");
149 }
150 0 ▶ if ($ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx) 0 0 0
151 0 Net::SSLeay::CTX_free($ctx);
152 0 _die_if_ssl_error("failed ctx free: $!");
153 }
154 0 ▶ if ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) 0 0 0
155 0 ▶ close $sock or die "can't close socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $sock or die "can't close socket: $!" 0 0 0
156 }
157  
158 0 delete $self->{_connection};
159  
160 0 return 1;
161 }
162  
163 sub _send {
164 0 ▶ my $self = shift;
Subroutine _send: not called CC=3 SCAR=24.8
165 0 my $data = \$_[0];
166 0 my ($sock, $ctx, $ssl) = @{$self->_connect};
167  
168 0 ▶ return unless $self->_do_select($sock, 'write', $self->write_timeout);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_do_select($sock, "write", $self->write_timeout) 0 0 0
169  
170 0 ▶ Net::SSLeay::ssl_write_all($ssl, $data) or _die_if_ssl_error("ssl_write_all error: $!");
Branch: 1 branch
BranchTrueFalseTotal
Net::SSLeay::ssl_write_all($ssl, $data) or _die_if_ssl_error("ssl_write_all error: $!") 0 0 0
171 0 return 1;
172 }
173  
174 sub _read {
175 0 ▶ my $self = shift;
Subroutine _read: not called CC=3 SCAR=24.8
176 0 my ($sock, $ctx, $ssl) = @{$self->_connect};
177  
178 0 ▶ return unless $self->_do_select($sock, 'read', $self->read_timeout);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_do_select($sock, "read", $self->read_timeout) 0 0 0
179  
180 0 ▶ my $data = Net::SSLeay::ssl_read_all($ssl) or _die_if_ssl_error("ssl_read_all error: $!");
Branch: 1 branch
BranchTrueFalseTotal
my $data = Net::SSLeay::ssl_read_all($ssl) or _die_if_ssl_error("ssl_read_all error: $!") 0 0 0
181 0 return $data;
182 }
183  
184 sub _do_select {
185 0 ▶ my ($self, $sock, $act, $timeout) = @_;
Subroutine _do_select: not called CC=6 SCAR=37.4
186  
187 0 my $begin_time = Time::HiRes::time();
188  
189 0 vec(my $bits = '', fileno($sock), 1) = 1;
190 0 while (1) {
191 0 my $nfound;
192 0 ▶ if ($act eq 'read') {
Branch: 1 branch
BranchTrueFalseTotal
if ($act eq 'read') { } 0 0 0
193 0 $nfound = select my $rout = $bits, undef, undef, $timeout;
194 }
195 else {
196 0 $nfound = select undef, my $wout = $bits, undef, $timeout;
197 }
198 0 ▶ return unless $nfound; # timeout
Branch: 1 branch
BranchTrueFalseTotal
unless $nfound 0 0 0
199  
200 # returned error
201 0 ▶ if ($nfound == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound == -1) 0 0 0
202 0 ▶ if ($! == EINTR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 4) { } 0 0 0
203 # can retry
204 0 ▶ $timeout -= (Time::HiRes::time() - $begin_time) if defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout 0 0 0
205 0 next;
206 }
207 else {
208 # other error
209 0 $self->disconnect;
210 0 return;
211 }
212 }
213  
214 0 last;
215 }
216  
217 0 return 1;
218 }
219  
220 sub DESTROY {
221 11 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
222 11 $self->disconnect;
223 }
224  
225 sub _tmpfile {
226 0 ▶ my $fh = File::Temp->new(
Subroutine _tmpfile: not called CC=1 SCAR=6.9
227 TEMPLATE => "napnseXXXXXXXXXXX",
228 TMPDIR => 1,
229 EXLOCK => 0,
230 );
231 0 syswrite $fh, $_[0];
232 0 close $fh;
233  
234 0 return $fh, $fh->filename;
235 }
236  
237 sub _die_if_ssl_error {
238 0 ▶ my ($msg) = @_;
Subroutine _die_if_ssl_error: not called CC=2 SCAR=17.9
239 0 my $err = Net::SSLeay::print_errs("SSL error: $msg");
240 0 ▶ croak $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
241 }
242  
243 13 1;
244 __END__
↑ Summary blib/lib/Net/APNs/Extended.pm »