blib/lib/Net/SPOCP/Protocol.pm

filter: Statementstmt 27.3% 52 / 190 Branchbran 4.5% 3 / 66 Conditioncond 13.0% 3 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 23.6% 13 / 55 Podpod 0.0% 0 / 12 totaltotal 19.7% 71 / 360 CC 42 SCAR 35.1
CC
42
Cov
21%
read
182.0
auth
56.0
send
20.0
CRAP 33.5
↑ Summary blib/lib/Net/SPOCP/SExpr.pm »
1 package Net::SPOCP::Protocol;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 @Net::SPOCP::Protocol::ISA = qw(Net::SPOCP);
8  
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Authen::SASL;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub init
16 1 ▶ {
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
17 1 $_[0]->connect();
18 }
19  
20 sub connect
21 1 ▶ {
Subroutine connect: called CC=5 SCAR=18.6
Pod undocumented
22 1 my $self = shift;
23  
24 1 ▶ $self->disconnect() if ref $self->{_sock};
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"_sock"} 0 1 1
25 $self->{_sock} = IO::Socket::INET->new(PeerAddr=>$self->{server},
26 Proto=>'tcp',
27 1 ▶ Timeout=>$self->{timeout} || 300);
Condition50%1/2
$self->{'timeout'} || 300
l!l
01
MC/DC0%0/1
$self->{'timeout'} || 300
$self->{'timeout'}
Truth table50%1/2
A
A= $self->{'timeout'}
Aresult
11
00
28  
29 croak "Net::SPOCP::connect failed: $!\n"
30 1 ▶ unless $self->{_sock} && $self->{_sock}->connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_sock"} and $self->{"_sock"}->connected 1 0 1
Condition33%1/3
$self->{"_sock"} and $self->{"_sock"}->connected
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"_sock"} and $self->{"_sock"}->connected
$self->{"_sock"} $self->{"_sock"}->connected
Truth table33%1/3
A and B
A= $self->{"_sock"}B= $self->{"_sock"}->connected
ABresult
0X0
100
111
31 }
32  
33 sub DESTROY
34 1 ▶ {
Subroutine DESTROY: called CC=3 SCAR=13.1
35 1 my $self = shift;
36 1 ▶ $self->disconnect() if $self->{_sock} && $self->{_sock}->connected;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_sock"} and $self->{"_sock"}->connected 0 1 1
Condition33%1/3
$self->{"_sock"} and $self->{"_sock"}->connected
!ll&&!rl&&r
100
MC/DC0%0/2
$self->{"_sock"} and $self->{"_sock"}->connected
$self->{"_sock"} $self->{"_sock"}->connected
Truth table33%1/3
A and B
A= $self->{"_sock"}B= $self->{"_sock"}->connected
ABresult
0X0
100
111
37 }
38  
39 sub disconnect
40 0 ▶ {
Subroutine disconnect: not called CC=3 SCAR=24.8
Pod undocumented
41 0 my $self = shift;
42 eval
43 0 {
44 0 $self->logout();
45 0 ▶ $self->{_sock}->close(SSL_no_shutdown=>1) if $self->{_tls};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_tls"} 0 0 0
46 0 $self->{_sock}->shutdown(2);
47 };
48 0 ▶ if ($@) { carp "Net::SPOCP::disconnect: $@\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
49 0 $self->{_sock} = undef;
50 }
51  
52 sub starttls
53 0 ▶ {
Subroutine starttls: not called CC=3 SCAR=24.8
Pod undocumented
54 0 my $self = shift;
55 0 my $res = $self->send(Net::SPOCP::Request::Starttls->new())->recv;
56 0 ▶ if($res->code() == 205)
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 205) 0 0 0
57 {
58 $self->{_sock} = IO::Socket::SSL->start_SSL($self->{_sock},
59 SSL_verify_mode => 0x01,
60 0 SSL_ca_file => $self->{ssl_ca_file});
61 }
62 0 ▶ if($res->code() != 205)
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code != 205) 0 0 0
63 {
64 0 croak("Net::SPOCP: Failed starting tls, probably forbidden by server.")
65 }
66 0 $res;
67 }
68  
69 sub query
70 0 ▶ {
Subroutine query: not called CC=2 SCAR=17.9
Pod undocumented
71 0 my $self = shift;
72  
73 0 my $rule = $_[0];
74 0 ▶ unless (UNIVERSAL::isa('Net::SPOCP::SExpr',$_[0]))
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa("Net::SPOCP::SExpr", $_[0])) 0 0 0
75 {
76 0 $rule = Net::SPOCP::SExpr->new($_[0]);
77 }
78  
79 0 $self->send(Net::SPOCP::Request::Query->new(rule=>$rule,path=>'/'))->recv();
80 }
81  
82 sub capa
83 0 ▶ {
Subroutine capa: not called CC=1 SCAR=6.9
Pod undocumented
84 0 my $self = shift;
85 0 $self->send(Net::SPOCP::Request::Capa->new())->recv();
86 }
87  
88 sub auth
89 0 ▶ {
Subroutine auth: not called CC=7 SCAR=40.3
Pod undocumented
90 0 my $self = shift;
91 0 my $mech = shift;
92 0 my $callbacks = shift;
93 0 my $res;
94  
95 0 $mech =~ m/(\w+):(\w+)/;
96  
97 0 ▶ $callbacks = "" unless $callbacks;
Branch: 1 branch
BranchTrueFalseTotal
unless $callbacks 0 0 0
98  
99 0 my $sasl = Authen::SASL->new(
100 mechanism => "$2",
101 callback => "$callbacks",
102 );
103  
104 0 $self->{server} =~ m/([\w\d\.-]+):(\d+)/;
105 0 my $server = $1;
106  
107 0 my $conn = $sasl->client_new("spocp", "$server");
108 0 ▶ die($conn->code()) if $conn->code() < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $conn->code < 0 0 0 0
109  
110 {
111 0 my $data = encode_base64($conn->client_start(), '');
112  
113 0 $res = $self->send(Net::SPOCP::Request::Auth->new(
114 mech => $mech,
115 data => $data))->recv();
116 }
117  
118 0 ▶ while($res->code == 301)
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 301) 0 0 0
119 {
120 0 my $dec_data = decode_base64($res->[0]->data);
121 0 my $raw_data = $conn->client_step($dec_data);
122 0 ▶ my $data = encode_base64($raw_data, '') if $raw_data;
Branch: 1 branch
BranchTrueFalseTotal
if $raw_data 0 0 0
123 0 ▶ $data = "" unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
124 0 $res = $self->send(Net::SPOCP::Request::Auth->new(
125 data => $data))->recv();
126 }
127 0 ▶ if($res->code == 200)
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 200) { } 0 0 0
128 {
129 0 $self->{sasl} = $conn;
130 }
131 else
132 {
133 0 croak("Net::SPOCP: Sasl auth failed.")
134 }
135 0 $res;
136 }
137  
138 sub logout
139 0 ▶ {
Subroutine logout: not called CC=1 SCAR=6.9
Pod undocumented
140 0 my $self = shift;
141 0 my $res = $self->send(Net::SPOCP::Request::Logout->new())->recv();
142 0 $self->{sasl} = undef;
143 0 $self->{rest_buf} = undef;
144 0 $res;
145 }
146  
147 sub noop
148 0 ▶ {
Subroutine noop: not called CC=1 SCAR=6.9
Pod undocumented
149 0 my $self = shift;
150 0 $self->send(Net::SPOCP::Request::Noop->new())->recv();
151 }
152  
153 sub send
154 0 ▶ {
Subroutine send: not called CC=4 SCAR=30.0
Pod undocumented
155 0 my $self = shift;
156 0 my $msg = shift;
157 0 my $tosend;
158  
159 carp "Net::SPOCP::send disconnected\n" unless
160 0 ▶ $self->{_sock} && $self->{_sock}->connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_sock"} and $self->{"_sock"}->connected 0 0 0
Condition0%0/3
$self->{"_sock"} and $self->{"_sock"}->connected
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_sock"} and $self->{"_sock"}->connected
$self->{"_sock"} $self->{"_sock"}->connected
Truth table0%0/3
A and B
A= $self->{"_sock"}B= $self->{"_sock"}->connected
ABresult
0X0
100
111
161  
162 0 ▶ if($self->{sasl})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'sasl'}) { } 0 0 0
163 {
164 0 $tosend = $self->{sasl}->encode($msg->toString());
165 }
166 else
167 {
168 0 $tosend = $msg->toString();
169 }
170 0 $self->{_sock}->print($tosend);
171 0 $self;
172 }
173  
174  
175 sub read
176 0 ▶ {
Subroutine read: not called CC=13 SCAR=52.0
Pod undocumented
177 0 my $self = shift;
178  
179 carp "Net::SPOCP::send disconnected\n" unless
180 0 ▶ $self->{_sock} && $self->{_sock}->connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_sock"} and $self->{"_sock"}->connected 0 0 0
Condition0%0/3
$self->{"_sock"} and $self->{"_sock"}->connected
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"_sock"} and $self->{"_sock"}->connected
$self->{"_sock"} $self->{"_sock"}->connected
Truth table0%0/3
A and B
A= $self->{"_sock"}B= $self->{"_sock"}->connected
ABresult
0X0
100
111
181  
182 0 my $buf = '';
183  
184 0 ▶ if(!$self->{rest_buf})
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'rest_buf'}) { } 0 0 0
185 {
186 0 my $nread = 0;
187 0 my $tbuf = '';
188 0 my $maxread = 1024;
189 0 ▶ while($nread = sysread($self->{_sock}, $tbuf, $maxread))
Branch: 1 branch
BranchTrueFalseTotal
if ($nread = sysread($self->{"_sock"}, $tbuf, $maxread)) 0 0 0
190 {
191 0 ▶ last if $nread == 0; # EOF
Branch: 1 branch
BranchTrueFalseTotal
if $nread == 0 0 0 0
192 0 $buf .= $tbuf;
193 0 ▶ last if ($maxread - $nread) != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $maxread - $nread != 0 0 0 0
194 }
195 0 ▶ croak "Net::SPOCP::recv read error: $!\n" unless defined $nread;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nread 0 0 0
196  
197 0 ▶ if($self->{sasl})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sasl"}) 0 0 0
198 {
199 0 $buf = $self->{sasl}->decode($buf);
200 }
201 }
202 else
203 {
204 0 $buf = $self->{rest_buf};
205 }
206  
207 0 $buf =~ m/^(\d+):/;
208 0 ▶ my $len = $1 if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 0 0 0
209 0 ▶ carp("couldn't get len in buf at Net::SPOCP::recv read") unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 0 0
210 0 $buf =~ m/^(\d+):(.{$len})(.*)$/;
211 0 ▶ $buf = $2 if $2;
Branch: 1 branch
BranchTrueFalseTotal
if $2 0 0 0
212 0 ▶ carp("couldn't get buf in of $len at Net::SPOCP::recv read") unless $buf;
Branch: 1 branch
BranchTrueFalseTotal
unless $buf 0 0 0
213 # there is a second message after the first one. we store this in
214 # $self->{rest_buf} and take it out on the next read.
215 0 $self->{rest_buf} = $3;
216 0 $buf;
217 }
218  
219 sub recv
220 0 ▶ {
Subroutine recv: not called CC=3 SCAR=24.8
Pod undocumented
221 0 my $self = shift;
222  
223 0 my $res = Net::SPOCP::Response->new();
224 0 my $r;
225 do
226 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$r->code == 201 || $r->code == 301 and do { do { $r = "Net::SPOCP::Reply"->parse($self->read); $res->add_reply($r) } } 0 0 0
Condition0%0/3
$r->code == 201 or $r->code == 301
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->code == 201 or $r->code == 301
$r->code == 201 $r->code == 301
Truth table0%0/3
A or B
A= $r->code == 201B= $r->code == 301
ABresult
1X1
011
000
227 0 $r = Net::SPOCP::Reply->parse($self->read());
228 0 $res->add_reply($r);
229 } while ($r->code == 201 || $r->code == 301);
230  
231 0 $res;
232 }
233  
234 package Net::SPOCP::Client;
235 1 @Net::SPOCP::Client::ISA = qw(Net::SPOCP::Protocol);
236  
237 package Net::SPOCP::Request;
238 1 @Net::SPOCP::Request::ISA = qw(Net::SPOCP);
239  
240 sub toString
241 0 ▶ {
Subroutine toString: not called CC=1 SCAR=6.9
242 0 $_[0]->l_encode($_[0]->l_encode($_[0]->type).$_[0]->encode());
243 }
244  
245 0 ▶sub init { }
Subroutine init: not called CC=1 SCAR=0.0
246  
247 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
248 0 die "Implementation error calling type: ".join(',',caller())."\n";
249 }
250  
251 sub encode
252 0 ▶ {
Subroutine encode: not called CC=1 SCAR=6.9
253 0 die $_[0]->type . " not implemented yet"
254 }
255  
256 package Net::SPOCP::Request::Query;
257 1 @Net::SPOCP::Request::Query::ISA = qw(Net::SPOCP::Request);
258  
259 0 ▶sub type { 'QUERY' }
Subroutine type: not called CC=1 SCAR=6.9
260  
261 sub encode
262 0 ▶ {
Subroutine encode: not called CC=1 SCAR=6.9
263 0 $_[0]->l_encode($_[0]->{path}).$_[0]->l_encode($_[0]->{rule}->toString()).$_[0]->l_encode($_[0]->{data});
264 }
265  
266 package Net::SPOCP::Request::List;
267 1 @Net::SPOCP::Request::List::ISA = qw(Net::SPOCP::Request);
268  
269 0 ▶sub type { 'LIST' }
Subroutine type: not called CC=1 SCAR=6.9
270  
271 package Net::SPOCP::Request::BSearch;
272 1 @Net::SPOCP::Request::BSearch::ISA = qw(Net::SPOCP::Request);
273  
274 0 ▶sub type { 'BSEARCH' }
Subroutine type: not called CC=1 SCAR=6.9
275  
276 package Net::SPOCP::Request::Add;
277 1 @Net::SPOCP::Request::Add::ISA = qw(Net::SPOCP::Request);
278  
279 0 ▶sub type { 'ADD' }
Subroutine type: not called CC=1 SCAR=6.9
280  
281 package Net::SPOCP::Request::Capa;
282 1 @Net::SPOCP::Request::Capa::ISA = qw(Net::SPOCP::Request);
283  
284 0 ▶sub type { 'CAPA' }
Subroutine type: not called CC=1 SCAR=6.9
285  
286 sub encode
287 0 ▶ {
Subroutine encode: not called CC=1 SCAR=6.9
288 0 return("")
289 }
290  
291 package Net::SPOCP::Request::Auth;
292 1 @Net::SPOCP::Request::Auth::ISA = qw(Net::SPOCP::Request);
293  
294 0 ▶sub type { 'AUTH' }
Subroutine type: not called CC=1 SCAR=6.9
295  
296 sub encode
297 0 ▶ {
Subroutine encode: not called CC=2 SCAR=17.9
298 0 my $mech = "";
299 0 ▶ $mech = $_[0]->l_encode($_[0]->{mech}) if $_[0]->{mech};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"mech"} 0 0 0
300 0 $mech.$_[0]->l_encode($_[0]->{data});
301 }
302  
303 package Net::SPOCP::Request::Logout;
304 1 @Net::SPOCP::Request::Logout::ISA = qw(Net::SPOCP::Request);
305  
306 0 ▶sub type { 'LOGOUT' }
Subroutine type: not called CC=1 SCAR=6.9
307  
308 sub encode
309 0 ▶ {
Subroutine encode: not called CC=1 SCAR=6.9
310 0 return("");
311 }
312  
313 package Net::SPOCP::Request::Noop;
314 1 @Net::SPOCP::Request::Noop::ISA = qw(Net::SPOCP::Request);
315  
316 0 ▶sub type { 'NOOP' }
Subroutine type: not called CC=1 SCAR=6.9
317  
318 sub encode
319 0 ▶ {
Subroutine encode: not called CC=1 SCAR=6.9
320 0 return("");
321 }
322  
323 package Net::SPOCP::Request::Starttls;
324 1 @Net::SPOCP::Request::Starttls::ISA = qw(Net::SPOCP::Request);
325  
326 0 ▶sub type { 'STARTTLS' }
Subroutine type: not called CC=1 SCAR=6.9
327  
328 sub encode
329 0 ▶ {
Subroutine encode: not called CC=1 SCAR=6.9
330 0 return("");
331 }
332  
333 package Net::SPOCP::Response;
334 1 @Net::SPOCP::Response::ISA = qw(Net::SPOCP);
335  
336 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
337  
338 sub new
339 0 ▶ {
Subroutine new: not called CC=1 SCAR=6.9
340 0 my $class = shift;
341  
342 0 bless \@_,$class;
343 }
344  
345 sub add_reply
346 0 ▶ {
Subroutine add_reply: not called CC=1 SCAR=6.9
347 0 push(@{$_[0]},$_[1]);
348 }
349  
350 sub replies
351 0 ▶ {
Subroutine replies: not called CC=1 SCAR=6.9
352 0 @{$_[0]};
353 }
354  
355 sub reply
356 0 ▶ {
Subroutine reply: not called CC=1 SCAR=6.9
357 0 $_[0]->[$_[1]];
358 }
359  
360 sub is_error
361 0 ▶ {
Subroutine is_error: not called CC=4 SCAR=30.0
362 0 my $code = $_[0]->reply(0)->code;
363 # multi-part, ok, authdata, auth ok
364 0 ▶ $code != 201 && $code != 200 && $code != 301 && $code != 300
Branch: 1 branch
BranchTrueFalseTotal
$code != 201 and $code != 200 and $code != 301 and $code != 300 0 0 0
Condition0%0/6
$code != 201 and $code != 200 and $code != 301
!ll&&!rl&&r
000
$code != 201 and $code != 200
!ll&&!rl&&r
000
MC/DC0%0/3
$code != 201 and $code != 200 and $code != 301
$code != 201 $code != 200 $code != 301
Truth table0%0/4
A and B and C
A= $code != 201B= $code != 200C= $code != 301
ABCresult
0XX0
10X0
1100
1111
365 }
366  
367 sub error
368 0 ▶ {
Subroutine error: not called CC=1 SCAR=6.9
369 0 $_[0]->reply(0)->error;
370 }
371  
372 sub code
373 0 ▶ {
Subroutine code: not called CC=1 SCAR=6.9
374 0 $_[0]->reply(0)->code;
375 }
376  
377 package Net::SPOCP::Reply;
378 1 @Net::SPOCP::Reply::ISA = qw(Net::SPOCP);
379  
380 0 ▶sub init {}
Subroutine init: not called CC=1 SCAR=0.0
381  
382 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
383  
384 1 my %CODE = (
385 200 => 'Ok',
386 201 => 'Multiline',
387 202 => 'Denied',
388 203 => 'Bye',
389 204 => 'Transaction complete',
390 205 => 'Ready to start TLS',
391 300 => 'Authentication in progress',
392 301 => 'Authentication Data',
393 401 => 'Service not available',
394 402 => 'Information unavailable',
395 500 => 'Syntax error',
396 501 => 'Operations error',
397 502 => 'Not supported',
398 503 => 'Already in operation',
399 504 => 'Line too long',
400 505 => 'Unknown ID',
401 506 => 'Already exists',
402 507 => 'Line too long',
403 508 => 'Unknown command',
404 509 => 'Access denied',
405 510 => 'Argument error',
406 511 => 'Already active',
407 512 => 'Internal error',
408 513 => 'Input error',
409 514 => 'Timelimit exceeded',
410 515 => 'Sizelimit exceeded',
411 516 => 'Other'
412 );
413  
414 sub parse
415 0 ▶ {
Subroutine parse: not called CC=3 SCAR=24.8
416 0 my $self = shift;
417 0 my $str = shift;
418  
419 0 my $me = Net::SPOCP::Reply->new();
420  
421 0 ▶ carp "Net::SPOCP::Reply::parse format error: missing error code\n" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ s/^3:([0-9]{3})//o 0 0 0
422 $str =~ s/^3:([0-9]{3})//o;
423  
424 0 $me->{code} = $1;
425  
426 0 ▶ carp "Net::SPOCP::Reply::parse format error: format error\n" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ s/^([0-9]+):(.*)//o 0 0 0
427 $str =~ s/^([0-9]+):(.*)//o;
428  
429 0 $me->{length} = $1;
430 0 $me->{data} = $2;
431  
432 0 $me;
433 }
434  
435 sub code
436 0 ▶ {
Subroutine code: not called CC=1 SCAR=6.9
437 0 $_[0]->{code};
438 }
439  
440 sub length
441 0 ▶ {
Subroutine length: not called CC=1 SCAR=6.9
442 0 $_[0]->{length};
443 }
444  
445 sub data
446 0 ▶ {
Subroutine data: not called CC=1 SCAR=6.9
447 0 $_[0]->{data};
448 }
449  
450 sub error
451 0 ▶ {
Subroutine error: not called CC=2 SCAR=17.9
452 0 my $code = $_[0]->{code};
453  
454 0 ▶ return "Unknown error" unless exists $CODE{$code};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $CODE{$code} 0 0 0
455 0 $CODE{$code};
456 }
457  
458 package Net::SPOCP;
459  
460 1 1;
↑ Summary blib/lib/Net/SPOCP/SExpr.pm »