lib/Mogura/Scanner/Client.pm

filter: Statementstmt 98.0% 49 / 50 Branchbran 62.5% 20 / 32 Conditioncond 30.0% 9 / 30 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 2 totaltotal 63.9% 87 / 136 CC 29 SCAR 34.3
CC
29
Cov
70%
request
47.5
initialize
11.6
BEGIN
1.0
CRAP 31.0
« lib/Mogura/Scanner.pm ↑ Summary lib/Mogura/ResultStream.pm »
1 package Mogura::Scanner::Client;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mojo::Asset::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Fcntl qw(O_RDONLY O_NOFOLLOW);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use IO::Socket::SSL 2.009;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 has 'config';
10 2 ▶has ua => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
11 2 Mojo::UserAgent->new->insecure(0)
12 ->max_redirects(0)
13 ->connect_timeout(10)
14 ->inactivity_timeout(30)
15 ->max_response_size(262144);
16 2 };
17  
18 2 ▶sub initialize {
Subroutine initialize: called CC=8 SCAR=24.5
Pod undocumented
19 2 my ($self) = @_;
20 2 ▶ sysopen my $fh, $self->config->token_file, O_RDONLY | O_NOFOLLOW or die "Open token file: $!";
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $self->config->token_file, 131072 or die "Open token file: $!" 2 0 2
21 2 my @st = stat $fh;
22 2 ▶ die 'Token file must be private and owned by the scanner user'
Branch: 1 branch
BranchTrueFalseTotal
unless -f $fh and $st[4] == $> and not $st[2] & 63 and $st[7] <= 65 0 2 2
Condition33%3/9
-f $fh and $st[4] == $> and not $st[2] & 63 and $st[7] <= 65
!ll&&!rl&&r
002
-f $fh and $st[4] == $> and not $st[2] & 63
!ll&&!rl&&r
002
-f $fh and $st[4] == $>
!ll&&!rl&&r
002
MC/DC0%0/4
-f $fh and $st[4] == $> and not $st[2] & 63 and $st[7] <= 65
-f $fh $st[4] == $> not $st[2] & 63 $st[7] <= 65
Truth table20%1/5
A and B and not(C) and D
A= -f $fhB= $st[4] == $>C= not $st[2] & 63D= $st[7] <= 65
ABCDresult
0XXX0
10XX0
110X0
11100
11111
23 unless -f $fh && $st[4] == $> && !($st[2] & 0077) && $st[7] <= 65;
24 2 ▶ my $token = <$fh> // '';
Condition50%1/2
readline $fh // ""
l!l
20
MC/DC0%0/1
readline $fh // ""
readline $fh
Truth table50%1/2
A
A= readline $fh
Aresult
11
00
25 2 chomp $token;
26 2 ▶ die 'Invalid token file' unless $token =~ /\A[a-f0-9]{64}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $token =~ /\A[a-f0-9]{64}\z/u 0 2 2
27 2 $self->{token} = $token;
28 2 return $self;
29 }
30  
31 99 ▶sub request {
Subroutine request: called CC=22 SCAR=38.6
Pod undocumented
32 99 my ($self, $method, $route, $data, $file) = @_;
33 my $tx = $self->ua->build_tx(
34 $method => $self->config->server . '/api'
35 . $route => {Authorization => 'Bearer ' . $self->{token}},
36 99 ▶ (defined $data ? (json => $data) : ())
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 17 82 99
37 );
38 99 ▶ if (defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 2 97 99
39 2 ▶ open my $fh, '<:raw', $file or die "Open artifact: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die "Open artifact: $!" 2 0 2
40 2 $tx->req->headers->header(
41 'X-Content-SHA256' => Digest::SHA->new(256)->addfile($fh)->hexdigest);
42 2 $tx->req->headers->content_type('application/octet-stream');
43 2 $tx->req->content->asset(Mojo::Asset::File->new(path => "$file"));
44 }
45 99 ▶ my $res =
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 2 97 99
46 $self->ua->request_timeout(defined($file) ? $self->config->upload_timeout : 15)
47 ->start($tx)
48 ->result;
49 97 ▶ return {expired => 1} if $res->code == 410 && $route =~ m{/claim\z};
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 410 and $route =~ m[/claim\z]u 0 97 97
Condition33%1/3
$res->code == 410 and $route =~ m[/claim\z]u
!ll&&!rl&&r
9700
MC/DC0%0/2
$res->code == 410 and $route =~ m[/claim\z]u
$res->code == 410 $route =~ m[/claim\z]u
Truth table33%1/3
A and B
A= $res->code == 410B= $route =~ m[/claim\z]u
ABresult
0X0
100
111
50 97 ▶ die "Server $method $route: HTTP " . $res->code . "\n" unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 97 97
51 97 my $reply = $res->json;
52 97 ▶ die "Invalid server acknowledgement for $route" unless ref($reply) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $reply eq "HASH" 0 97 97
53 97 ▶ if ($route =~ m{\A/jobs/([^/]+)/([^/]+)/(.+)\z}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route =~ m[\A/jobs/([^/]+)/([^/]+)/(.+)\z]u) 8 89 97
54 8 my ($id, $run, $action) = ($1, $2, $3);
55 die 'Wrong execution acknowledgement'
56 8 ▶ unless ($reply->{id} // '') eq $id && ($reply->{run} // '') eq $run;
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply->{"id"} // "") eq $id and ($reply->{"run"} // "") eq $run 0 8 8
Condition42%3/7
($reply->{"id"} // "") eq $id and ($reply->{"run"} // "") eq $run
!ll&&!rl&&r
008
$reply->{"id"} // ""
l!l
80
$reply->{"run"} // ""
l!l
80
MC/DC0%0/2
($reply->{"id"} // "") eq $id and ($reply->{"run"} // "") eq $run
$reply->{"id"} $reply->{"run"}
Truth table33%1/3
A and B
A= $reply->{"id"}B= $reply->{"run"}
ABresult
0X0
100
111
57 8 ▶ if (defined $file) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $file) { } 0 8 8
elsif ($action ne 'claim') { } 2 6 8
58 die 'Wrong artifact receipt'
59 unless $action eq 'files/' . ($reply->{kind} // '')
60 0 ▶ && ($reply->{sha256} // '') eq $tx->req->headers->header('X-Content-SHA256');
Branch: 1 branch
BranchTrueFalseTotal
unless $action eq 'files/' . ($reply->{"kind"} // "") and ($reply->{"sha256"} // "") eq $tx->req->headers->header("X-Content-SHA256") 0 0 0
Condition0%0/7
$action eq 'files/' . ($reply->{"kind"} // "") and ($reply->{"sha256"} // "") eq $tx->req->headers->header("X-Content-SHA256")
!ll&&!rl&&r
000
$reply->{"kind"} // ""
l!l
00
$reply->{"sha256"} // ""
l!l
00
MC/DC0%0/2
$action eq 'files/' . ($reply->{"kind"} // "") and ($reply->{"sha256"} // "") eq $tx->req->headers->header("X-Content-SHA256")
$action eq 'files/' . ($reply->{"kind"} // "") $reply->{"sha256"}
MC/DC0%0/1
$reply->{"kind"} // ""
$reply->{"kind"}
Truth table0%0/3
A and B
A= $action eq 'files/' . ($reply->{"kind"} // "")B= $reply->{"sha256"}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $reply->{"kind"}
Aresult
11
00
61 }
62 elsif ($action ne 'claim') {
63 2 ▶ die 'Wrong state acknowledgement' unless ($reply->{action} // '') eq $action;
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply->{"action"} // "") eq $action 0 2 2
Condition50%1/2
$reply->{"action"} // ""
l!l
20
MC/DC0%0/1
$reply->{"action"} // ""
$reply->{"action"}
Truth table50%1/2
A
A= $reply->{"action"}
Aresult
11
00
64 }
65 }
66 97 return $reply;
67 }
68 2 1;
« lib/Mogura/Scanner.pm ↑ Summary lib/Mogura/ResultStream.pm »