GuacLite/Client/Guacd.pm

filter: Statementstmt 89.9% 125 / 139 Branchbran 61.7% 21 / 34 Conditioncond 54.5% 12 / 22 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 85.7% 30 / 35 Podpod 0.0% 0 / 8 totaltotal 76.0% 190 / 250 CC 25 SCAR 10.7
CC
25
Cov
81%
decode
6.1
close
6.0
__ANON__
6.0
CRAP 2.9
« GuacLite/Client/Web.pm ↑ Summary GuacLite/Plugin/Guacd.pm »
1 package GuacLite::Client::Guacd;
2  
3 2 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Mojo::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use constant DEBUG => $ENV{GUACLITE_GUACD_DEBUG};
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶has host => $ENV{GUACLITE_GUACD_HOST} || 'localhost';
Condition50%1/2
$ENV{'GUACLITE_GUACD_HOST'} || 'localhost'
l!l
02
MC/DC0%0/1
$ENV{'GUACLITE_GUACD_HOST'} || 'localhost'
$ENV{'GUACLITE_GUACD_HOST'}
Truth table50%1/2
A
A= $ENV{'GUACLITE_GUACD_HOST'}
Aresult
11
00
14 2 ▶has port => $ENV{GUACLITE_GUACD_PORT} || '4822';
Condition50%1/2
$ENV{'GUACLITE_GUACD_PORT'} || '4822'
l!l
02
MC/DC0%0/1
$ENV{'GUACLITE_GUACD_PORT'} || '4822'
$ENV{'GUACLITE_GUACD_PORT'}
Truth table50%1/2
A
A= $ENV{'GUACLITE_GUACD_PORT'}
Aresult
11
00
15  
16 # the following should probably all be required parameters, but for now, do this
17 2 has protocol => 'vnc';
18 1 ▶has connection_args => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
19  
20 2 has width => 1024;
21 2 has height => 768;
22 2 has dpi => 96;
23  
24 3 ▶has audio_mimetypes => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
25 3 ▶has image_mimetypes => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
26 3 ▶has video_mimetypes => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
27 2 has timezone => '';
28  
29 # supported version of guacamole protocol
30 2 my @v = (1,3,0);
31 2 my $v = do { local $" = '_'; "VERSION_@v" };
32  
33 4 ▶sub _check_version {
Subroutine _check_version: called CC=5 SCAR=16.6
34 4 my $version = shift;
35 4 ▶ return 0 unless
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ /^VERSION_(\d+)_(\d+)_(\d+)$/u 0 4 4
36 $version =~ /^VERSION_(\d+)_(\d+)_(\d+)$/;
37 4 ▶ return 0 unless $1 >= $v[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $1 >= $v[0] 0 4 4
38 4 ▶ return 0 unless $2 >= $v[1];
Branch: 1 branch
BranchTrueFalseTotal
unless $2 >= $v[1] 1 3 4
39 3 ▶ return 0 unless $3 >= $v[2];
Branch: 1 branch
BranchTrueFalseTotal
unless $3 >= $v[2] 0 3 3
40 3 return 1;
41 }
42  
43 0 ▶sub close {
Subroutine close: not called CC=2 SCAR=17.9
Pod undocumented
44 0 my $self = shift;
45 0 ▶ return unless my $s = $self->{stream};
Branch: 1 branch
BranchTrueFalseTotal
unless my $s = $self->{"stream"} 0 0 0
46 0 $s->close;
47 }
48  
49 7 ▶sub connect_p {
Subroutine connect_p: called CC=2 SCAR=7.6
Pod undocumented
50 7 my $self = shift;
51 7 ▶ my $connect = shift || {};
Condition50%1/2
shift() || {}
l!l
07
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
52 7 Scalar::Util::weaken($self);
53 7 ▶ return Mojo::Promise->new(sub {
Subroutine __ANON__: called CC=3 SCAR=12.3
54 7 my ($res, $rej) = @_;
55 7 ▶ $connect->{address} ||= $self->host;
Condition33%1/3
$connect->{"address"} ||= $self->host
l!l&&r!l&&!r
070
MC/DC0%0/2
$connect->{"address"} ||= $self->host
$connect->{"address"} $self->host
Truth table33%1/3
A ||= B
A= $connect->{"address"}B= $self->host
ABresult
1X1
011
000
56 7 ▶ $connect->{port} ||= $self->port;
Condition33%1/3
$connect->{"port"} ||= $self->port
l!l&&r!l&&!r
070
MC/DC0%0/2
$connect->{"port"} ||= $self->port
$connect->{"port"} $self->port
Truth table33%1/3
A ||= B
A= $connect->{"port"}B= $self->port
ABresult
1X1
011
000
57 7 ▶ Mojo::IOLoop->client($connect, sub {
Subroutine __ANON__: called CC=2 SCAR=7.7
58 7 my (undef, $err, $stream) = @_;
59 7 ▶ return $rej->("Connect error: $err") if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 7 7
60  
61 #TODO configurable timeout
62 7 $stream->timeout(0);
63 7 $self->{stream} = $stream;
64  
65 10 ▶ $stream->on(read => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
66 10 my (undef, $bytes) = @_;
67 10 print STDERR '<- ' . Mojo::Util::term_escape($bytes) . "\n" if DEBUG;
68 10 $self->{buffer} .= $bytes;
69 10 ▶ while($self->{buffer} =~ s/^([^;]+;)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"buffer"} =~ s/^([^;]+;)//u) 10 10 20
70 10 eval { $self->emit(instruction => $1) };
71 }
72 7 });
73  
74 0 ▶ $stream->on(error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 $self->emit(error => $_[1]);
76 7 });
77  
78 0 ▶ $stream->on(close => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
79 0 print STDERR "Connection to guacd closed\n" if DEBUG;
80 0 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
81 0 delete @{$self}{qw(buffer id stream)};
82 0 $self->emit('close');
83 7 });
84  
85 7 $res->();
86 7 });
87 7 });
88 }
89  
90 7 ▶sub handshake_p {
Subroutine handshake_p: called CC=2 SCAR=6.9
Pod undocumented
91 7 Scalar::Util::weaken(my $self = shift);
92  
93 return Mojo::Promise->reject('Not connected')
94 7 ▶ unless my $stream = $self->{stream};
Branch: 1 branch
BranchTrueFalseTotal
unless my $stream = $self->{"stream"} 0 7 7
95  
96 7 my $args;
97 return $self->_expect(args => [select => $self->protocol])
98 4 ▶ ->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
99 4 my $got = shift;
100 4 my $version = shift @$got;
101 4 ▶ return Mojo::Promise->reject("Version $version less than supported ($v)")
Branch: 1 branch
BranchTrueFalseTotal
unless _check_version($version) 1 3 4
102 unless _check_version($version);
103 3 $args = $got;
104 3 $self->write_p(encode([size => $self->width, $self->height, $self->dpi]));
105 })
106 3 ▶ ->then(sub{ $self->write_p(encode([audio => @{ $self->audio_mimetypes } ])) })
Subroutine __ANON__: called CC=1 SCAR=0.0
107 3 ▶ ->then(sub{ $self->write_p(encode([image => @{ $self->image_mimetypes } ])) })
Subroutine __ANON__: called CC=1 SCAR=0.0
108 3 ▶ ->then(sub{ $self->write_p(encode([video => @{ $self->video_mimetypes } ])) })
Subroutine __ANON__: called CC=1 SCAR=0.0
109 3 ▶ ->then(sub{
Subroutine __ANON__: called CC=2 SCAR=6.9
110 3 my @connect = (connect => $v);
111 3 my $proto = $self->connection_args;
112 3 ▶ push @connect, map { $proto->{$_} // '' } @$args;
Condition100%2/2
$proto->{$_} // ""
l!l
42
MC/DC100%1/1
$proto->{$_} // ""
$proto->{$_}
Truth table100%2/2
A
A= $proto->{$_}
Aresult
11
00
113 3 $self->_expect(ready => \@connect);
114 })
115 2 ▶ ->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
116 2 my $id = shift;
117 2 print STDERR "Session $id->[0] is ready" if DEBUG;
118 2 $self->{id} = $id->[0];
119 2 return $id->[0];
120 7 ▶ })->catch(sub { Mojo::Promise->reject("Handshake error: $_[0]") });
Subroutine __ANON__: called CC=1 SCAR=0.0
121 }
122  
123 1 ▶sub stream { shift->{stream} }
Subroutine stream: called CC=1 SCAR=0.0
Pod undocumented
124  
125 0 ▶sub write {
Subroutine write: not called CC=2 SCAR=17.9
Pod undocumented
126 0 my ($self, $bytes) = @_;
127 Carp::croak('Not connected')
128 0 ▶ unless my $s = $self->{stream};
Branch: 1 branch
BranchTrueFalseTotal
unless my $s = $self->{"stream"} 0 0 0
129 0 print STDERR '-> ' . Mojo::Util::term_escape($bytes) . "\n" if DEBUG;
130 0 $self->{stream}->write($bytes);
131 }
132  
133 22 ▶sub write_p {
Subroutine write_p: called CC=2 SCAR=7.0
Pod undocumented
134 22 my ($self, $bytes) = @_;
135 return Mojo::Promise->reject('Not connected')
136 22 ▶ unless my $s = $self->{stream};
Branch: 1 branch
BranchTrueFalseTotal
unless my $s = $self->{"stream"} 0 22 22
137  
138 22 my $p = Mojo::Promise->new;
139 22 print STDERR '-> ' . Mojo::Util::term_escape($bytes) . "\n" if DEBUG;
140 22 ▶ $self->{stream}->write($bytes, sub { $p->resolve });
Subroutine __ANON__: called CC=1 SCAR=0.0
141 22 return $p;
142 }
143  
144 10 ▶sub _expect {
Subroutine _expect: called CC=1 SCAR=0.0
145 10 my ($self, $command, $send) = @_;
146 10 my $p = Mojo::Promise->new;
147  
148 10 ▶ $self->once(instruction => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
149 10 my (undef, $raw) = @_;
150 10 my $instruction;
151 10 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $instruction = decode($raw); 1 } } or return $p->reject($@) 8 2 10
152 10 $instruction = decode($raw); 1;
153 } or return $p->reject($@);
154 8 my $got = shift @$instruction;
155 8 ▶ if ($got eq $command) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got eq $command) { } 6 2 8
156 6 $p->resolve($instruction);
157 } else {
158 2 $p->reject(qq[Unexpected command "$got" received, expected "$command"]);
159 }
160 10 });
161  
162 $self->write_p(encode($send))
163 0 ▶ ->catch(sub { $p->reject("Send failed: $_[0]") });
Subroutine __ANON__: not called CC=1 SCAR=1.2
164  
165 10 return $p;
166 }
167  
168 ## FUNCTIONS!
169  
170 23 ▶sub encode {
Subroutine encode: called CC=2 SCAR=7.0
Pod undocumented
171 23 my $words = shift;
172 23 ▶ return join(',', map { $_ //= ''; length . '.' . Mojo::Util::encode('UTF-8', $_) } @$words) . ";";
Condition50%1/2
$_ //= ""
l!l
490
MC/DC0%0/1
$_ //= ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
173 }
174  
175 10 ▶sub decode {
Subroutine decode: called CC=6 SCAR=18.1
Pod undocumented
176 10 my $line = Mojo::Util::decode('UTF-8', shift);
177 10 ▶ Carp::croak 'Instruction does not end with ;'
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ s/;$//u 0 10 10
178 unless $line =~ s/;$//;
179  
180 my @words =
181 map {
182 10 my ($l, $s) = split /\./, $_, 2;
183 22 ▶ Carp::croak 'Invalid instruction encoding'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l and defined $s and &Scalar::Util::looks_like_number($l) 1 21 22
Condition66%4/6
defined $l and defined $s and &Scalar::Util::looks_like_number($l)
!ll&&!rl&&r
1021
defined $l and defined $s
!ll&&!rl&&r
0121
MC/DC33%1/3
defined $l and defined $s and &Scalar::Util::looks_like_number($l)
defined $l defined $s &Scalar::Util::looks_like_number($l)
Truth table50%2/4
A and B and C
A= defined $lB= defined $sC= &Scalar::Util::looks_like_number($l)
ABCresult
0XX0
10X0
1100
1111
184 unless defined $l && defined $s && Scalar::Util::looks_like_number($l);
185 21 ▶ Carp::croak 'Word length mismatch'
Branch: 1 branch
BranchTrueFalseTotal
unless length $s == $l 1 20 21
186 unless length($s) == $l;
187 20 $s;
188 }
189 split ',', $line;
190  
191 8 return \@words;
192 }
193  
194  
195 2 1;
196  
« GuacLite/Client/Web.pm ↑ Summary GuacLite/Plugin/Guacd.pm »