MToken.pm

filter: Statementstmt 14.2% 126 / 887 Branchbran 0.0% 0 / 422 Conditioncond 0.0% 0 / 319 MC/DCmcdc 0.0% 0 / 176 Subroutinesub 46.1% 36 / 78 Podpod 100% 12 / 12 totaltotal 9.1% 174 / 1894 CC 349 SCAR 63.5
CC
349
Cov
8%
__ANON__
1640.0
__ANON__
1560.0
__ANON__
870.0
CRAP 575.3
↑ Summary MToken/Client.pm »
1 package MToken; # $Id: MToken.pm 116 2021-10-12 15:17:49Z minus $
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =encoding utf-8
6  
7 =head1 NAME
8  
9 MToken - Tokens processing system (Security)
10  
11 =head1 VERSION
12  
13 Version 1.04
14  
15 =head1 SYNOPSIS
16  
17 use MToken;
18  
19 =head1 DESCRIPTION
20  
21 Tokens processing system (Security)
22  
23 =head2 client
24  
25 my $client = $mt->client;
26  
27 Returns the Mojo client (user agent) instance
28  
29 =head2 execmd
30  
31 my %exest = $self->execmd("command", "arg1", "arg2", "argn");
32  
33 Performs execute system commands and returns hash:
34  
35 =over 8
36  
37 =item command
38  
39 The command line
40  
41 =item status
42  
43 The status of operation. 1 - no errors; 0 - error
44  
45 =item exitval
46  
47 The exitval value
48  
49 =item error
50  
51 The error message
52  
53 =item output
54  
55 The data from program
56  
57 =back
58  
59 =head2 get_fingerprint
60  
61 Returns the fingerprint from local config or ask it
62  
63 =head2 get_gpgbin
64  
65 Returns the GNUPG path from local config
66  
67 =head2 get_manifest
68  
69 Returns manifest of current token
70  
71 =head2 get_name
72  
73 Returns name of current token
74  
75 =head2 get_opensslbin
76  
77 Returns the OpenSSL path from local config
78  
79 =head2 get_server_url
80  
81 Returns SERVER_URL from local config
82  
83 =head2 lconfig
84  
85 my $lconfig = $mt->lconfig;
86  
87 Returns local config instance
88  
89 =head2 raise
90  
91 return $mt->raise("Red message");
92  
93 Sends message to STDERR and returns 0
94  
95 =head2 store
96  
97 my $store = $mt->store;
98  
99 Returns the Store instance (Database)
100  
101 =head1 HISTORY
102  
103 See C<Changes> file
104  
105 =head1 DEPENDENCIES
106  
107 C<openssl>, C<gnupg>
108  
109 =head1 TO DO
110  
111 See C<TODO> file
112  
113 =head1 BUGS
114  
115 * none noted
116  
117 =head1 SEE ALSO
118  
119 L<Mojolicious>, L<CTK>
120  
121 =head1 AUTHOR
122  
123 Serż Minus (Sergey Lepenkov) L<http://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
124  
125 =head1 COPYRIGHT
126  
127 Copyright (C) 1998-2021 D&D Corporation. All Rights Reserved
128  
129 =head1 LICENSE
130  
131 This program is free software; you can redistribute it and/or
132 modify it under the same terms as Perl itself.
133  
134 See C<LICENSE> file and L<https://dev.perl.org/licenses/>
135  
136 =cut
137  
138 1 ▶use vars qw/ $VERSION /;
Subroutine BEGIN: called CC=1 SCAR=0.0
139 1 $VERSION = "1.04";
140  
141 1 ▶use feature qw/say/;
Subroutine BEGIN: called CC=1 SCAR=0.0
142 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 1 ▶use Encode; # Encode::_utf8_on();
Subroutine BEGIN: called CC=1 SCAR=0.0
144 1 ▶use Encode::Locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
145  
146 1 ▶use Archive::Tar;
Subroutine BEGIN: called CC=1 SCAR=0.0
147 1 ▶use Cwd qw/getcwd/;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 1 ▶use Digest::MD5 qw/md5_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 1 ▶use ExtUtils::Manifest qw/maniread/;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
151 1 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
152 1 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
153 1 ▶use File::stat qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
154 1 ▶use List::Util qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
155 1 ▶use POSIX qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 1 ▶use Text::SimpleTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
157 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
158  
159 1 ▶use Mojo::File qw/path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
160 1 ▶use Mojo::Util qw/tablify steady_time/;
Subroutine BEGIN: called CC=1 SCAR=0.0
161 1 ▶use Mojo::Date qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
162 1 ▶use Mojo::Server::Prefork;
Subroutine BEGIN: called CC=1 SCAR=0.0
163  
164 1 ▶use CTK::Skel;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 1 ▶use CTK::Util qw/preparedir which dtf tz_diff isTrueFlag rundir sharedir sharedstatedir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
166 1 ▶use CTK::UtilXS qw/wipe/;
Subroutine BEGIN: called CC=1 SCAR=0.0
167 1 ▶use CTK::TFVals qw/ :ALL /;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 1 ▶use CTK::ConfGenUtil;
Subroutine BEGIN: called CC=1 SCAR=0.0
169  
170 1 ▶use MToken::Const;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 1 ▶use MToken::Util qw/explain sha1sum red green yellow cyan blue magenta yep nope skip wow filesize/;
Subroutine BEGIN: called CC=1 SCAR=0.0
172 1 ▶use MToken::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 1 ▶use MToken::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
174 1 ▶use MToken::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
175 1 ▶use MToken::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
176  
177 1 ▶use base qw/ CTK::App /;
Subroutine BEGIN: called CC=1 SCAR=0.0
178  
179 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
180 1 ERROR_NO_TOKEN => "No token selected. Please use --datadir option or change the current directory to Your token device",
181 1 };
182  
183 __PACKAGE__->register_handler(
184 handler => "test",
185 description => "MToken testing (internal use only)",
186 0 ▶ code => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
187 ### CODE:
188 0 my ($self, $meta, @arguments) = @_;
189  
190 #say tablify [['foo', 'bar'], ['yadaffffgff', 'yada'], ['baz', 'yada']];
191  
192 #my $fingerprint = $self->get_fingerprint;
193 #say explain(\%exest);
194 #my @strings = split("\n", $exest{output});
195 #say explain(\@strings);
196  
197 0 return 1;
198 1 });
199  
200 __PACKAGE__->register_handler(
201 handler => "version",
202 description => sprintf("%s Version", PROJECTNAME),
203 0 ▶ code => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
204 ### CODE:
205 0 my ($self, $meta, @arguments) = @_;
206 0 printf("%s/%s\n", PROJECTNAME, $self->VERSION);
207 0 return 1;
208 1 });
209  
210 __PACKAGE__->register_handler(
211 handler => "status",
212 description => "Get status information",
213 0 ▶ code => sub {
Subroutine __ANON__: not called CC=26 SCAR=65.5
214 ### CODE:
215 0 my ($self, $meta, @arguments) = @_;
216  
217 0 printf("Version : %s\n", $self->VERSION);
218 0 printf("Data dir : %s\n", $self->datadir);
219 0 printf("Temp dir : %s\n", $self->tempdir);
220 0 ▶ printf("Global config : %s\n", $self->conf("loadstatus") ? $self->configfile : yellow("not loaded"));
Branch: 1 branch
BranchTrueFalseTotal
$self->conf('loadstatus') ? : 0 0 0
221 0 ▶ $self->debug(explain($self->config)) if $self->conf("loadstatus") && $self->verbosemode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf("loadstatus") and $self->verbosemode 0 0 0
Condition0%0/3
$self->conf("loadstatus") and $self->verbosemode
!ll&&!rl&&r
000
MC/DC0%0/2
$self->conf("loadstatus") and $self->verbosemode
$self->conf("loadstatus") $self->verbosemode
Truth table0%0/3
A and B
A= $self->conf("loadstatus")B= $self->verbosemode
ABresult
0X0
100
111
222 0 ▶ printf("Local config : %s\n", $self->lconfig->is_loaded ? green("loaded") : red("not loaded"));
Branch: 1 branch
BranchTrueFalseTotal
$self->lconfig->is_loaded ? : 0 0 0
223 0 ▶ $self->debug(explain($self->lconfig)) if $self->lconfig->is_loaded && $self->verbosemode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->lconfig->is_loaded and $self->verbosemode 0 0 0
Condition0%0/3
$self->lconfig->is_loaded and $self->verbosemode
!ll&&!rl&&r
000
MC/DC0%0/2
$self->lconfig->is_loaded and $self->verbosemode
$self->lconfig->is_loaded $self->verbosemode
Truth table0%0/3
A and B
A= $self->lconfig->is_loadedB= $self->verbosemode
ABresult
0X0
100
111
224  
225 # Return if no token selected
226 0 ▶ return 1 unless $self->lconfig->is_loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->lconfig->is_loaded 0 0 0
227  
228 # Database
229  
230 0 my $store = $self->store;
231 0 printf("DB DSN : %s\n", $store->dsn);
232 0 ▶ printf("DB status : %s\n", $store->status ? green("ok") : red($store->error || sprintf("Store (%s): Unknown error", $store->dsn)));
Branch: 1 branch
BranchTrueFalseTotal
$store->status ? : 0 0 0
Condition0%0/3
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
l!l&&r!l&&!r
000
MC/DC0%0/2
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
$store->error sprintf('Store (%s): Unknown error', $store->dsn)
Truth table0%0/3
A || B
A= $store->errorB= sprintf('Store (%s): Unknown error', $store->dsn)
ABresult
1X1
011
000
233 0 ▶ if ($store->file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store->file) 0 0 0
234 0 ▶ my $s = filesize($store->file) || 0;
Condition0%0/2
filesize($store->file) || 0
l!l
00
MC/DC0%0/1
filesize($store->file) || 0
filesize($store->file)
Truth table0%0/2
A
A= filesize($store->file)
Aresult
11
00
235 0 ▶ printf("DB size : %s\n", $store->status ? sprintf("%s (%d bytes)", _fbytes($s), $s) : yellow("unknown"));
Branch: 1 branch
BranchTrueFalseTotal
$store->status ? : 0 0 0
236 0 ▶ printf("DB modified : %s\n", $store->status ? _fdate(File::stat::stat($store->file)->mtime || 0) : yellow("unknown"));
Branch: 1 branch
BranchTrueFalseTotal
$store->status ? : 0 0 0
Condition0%0/2
&File::stat::stat(scalar $store->file)->mtime || 0
l!l
00
MC/DC0%0/1
&File::stat::stat(scalar $store->file)->mtime || 0
&File::stat::stat(scalar $store->file)->mtime
Truth table0%0/2
A
A= &File::stat::stat(scalar $store->file)->mtime
Aresult
11
00
237 }
238 0 ▶ printf("Stored files : %s\n", $store->status ? $store->count || 0 : yellow("unknown"));
Branch: 1 branch
BranchTrueFalseTotal
$store->status ? : 0 0 0
Condition0%0/2
$store->count or 0
l!l
00
MC/DC0%0/1
$store->count or 0
$store->count
Truth table0%0/2
A
A= $store->count
Aresult
11
00
239  
240 # Server
241 0 my $client = $self->client;
242 0 $client->check(); # Check
243 0 ▶ printf("Server URL : %s\n", $client->url ? $client->url->to_string : yellow("unknown"));
Branch: 1 branch
BranchTrueFalseTotal
$client->url ? : 0 0 0
244 0 ▶ printf("Server status : %s\n", $client->status ? green("ok") : red($client->error));
Branch: 1 branch
BranchTrueFalseTotal
$client->status ? : 0 0 0
245 0 $self->debug($client->trace);
246  
247 # Get info from server
248 0 ▶ if ($client->status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client->status) 0 0 0
249 0 ▶ if ($client->info($self->get_name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client->info($self->get_name)) { } 0 0 0
250 0 my $files = array($client->res->json("/files"));
251  
252 # Init table
253 0 my $tbl = Text::SimpleTable->new(
254 [24, 'TARBALL FILE'],
255 [10, 'FILE SIZE'],
256 [25, 'MAKE TIME'],
257 );
258 0 my $i = 0;
259 0 my $tz = tz_diff();
260 # Table caption
261 0 foreach my $row (@$files) {
262 0 $i++;
263 $tbl->row(
264 $row->{filename} || "noname",
265 _fbytes($row->{size} || 0),
266 0 ▶ dtf(DATETIME_FORMAT . " " . $tz, $row->{mtime} || 0),
Condition0%0/6
$row->{'filename'} || 'noname'
l!l
00
$row->{'size'} || 0
l!l
00
$row->{'mtime'} || 0
l!l
00
MC/DC0%0/1
$row->{'filename'} || 'noname'
$row->{'filename'}
MC/DC0%0/1
$row->{'size'} || 0
$row->{'size'}
MC/DC0%0/1
$row->{'mtime'} || 0
$row->{'mtime'}
Truth table0%0/2
A
A= $row->{'filename'}
Aresult
11
00
Truth table0%0/2
A
A= $row->{'size'}
Aresult
11
00
Truth table0%0/2
A
A= $row->{'mtime'}
Aresult
11
00
267 );
268 }
269 # Show table
270 0 ▶ if ($i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) { } 0 0 0
271 0 print $tbl->draw();
272 0 say cyan("total %d file(s)", $i);
273 } else {
274 0 say yellow("No data found on server");
275 }
276 } else {
277 0 say red($client->error);
278 0 $self->debug($client->trace);
279 }
280 }
281  
282 0 return 1;
283 1 });
284  
285 __PACKAGE__->register_handler(
286 handler => "init",
287 description => "Initialize token",
288 0 ▶ code => sub {
Subroutine __ANON__: not called CC=39 SCAR=73.5
289 ### CODE:
290 0 my ($self, $meta, @arguments) = @_;
291 0 my $tkn = shift(@arguments);
292 0 ▶ my $dir = $self->datadir || getcwd(); # Destination directory
Condition0%0/3
$self->datadir || getcwd()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->datadir || getcwd()
$self->datadir getcwd()
Truth table0%0/3
A || B
A= $self->datadirB= getcwd()
ABresult
1X1
011
000
293  
294 # Prepare DataDir if specified
295 0 ▶ if ($self->option("datadir")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->option("datadir")) 0 0 0
296 0 ▶ unless (preparedir($dir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (preparedir($dir)) 0 0 0
297 0 $self->error(sprintf("Can't prepare directory %s", $dir));
298 0 return 0;
299 }
300 }
301  
302 # Project name
303 0 ▶ $tkn ||= $self->cli_prompt('Token name:', $self->prefix);
Condition0%0/3
$tkn ||= $self->cli_prompt("Token name:", $self->prefix)
l!l&&r!l&&!r
000
MC/DC0%0/2
$tkn ||= $self->cli_prompt("Token name:", $self->prefix)
$tkn $self->cli_prompt("Token name:", $self->prefix)
Truth table0%0/3
A ||= B
A= $tknB= $self->cli_prompt("Token name:", $self->prefix)
ABresult
1X1
011
000
304 0 $tkn = lc($tkn);
305 0 $tkn =~ s/\s+//g;
306 0 $tkn =~ s/[^a-z0-9]//g;
307 0 ▶ $tkn ||= $self->prefix;
Condition0%0/3
$tkn ||= $self->prefix
l!l&&r!l&&!r
000
MC/DC0%0/2
$tkn ||= $self->prefix
$tkn $self->prefix
Truth table0%0/3
A ||= B
A= $tknB= $self->prefix
ABresult
1X1
011
000
308 0 ▶ if ($tkn =~ /^\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tkn =~ /^\d/) 0 0 0
309 0 $self->error("The token name must not begin with numbers. Choose another name consisting mainly of letters of the Latin alphabet");
310 0 return 0;
311 }
312  
313 0 printf("Initializing token \"%s\"...\n", $tkn);
314  
315 # Initialize local configuration for device
316 0 $self->{lconfig} = MToken::Config->new(
317 file => File::Spec->catfile($dir, DIR_PRIVATE, DEVICE_CONF_FILE),
318 name => $tkn,
319 );
320 #say(explain($self->lconfig));
321 0 my %before = $self->lconfig->getall;
322  
323  
324 # Ask OpenSSL
325 0 ▶ my $opensslbin = $self->cli_prompt('OpenSSL program:', $self->lconfig->get("opensslbin") ||
Condition0%0/8
$self->lconfig->get('opensslbin') || $self->conf('opensslbin') || which('openssl') || 'openssl'
l!l
00
$self->lconfig->get("opensslbin") or $self->conf("opensslbin") or which("openssl")
l!l&&r!l&&!r
000
$self->lconfig->get("opensslbin") or $self->conf("opensslbin")
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->lconfig->get('opensslbin') || $self->conf('opensslbin') || which('openssl') || 'openssl'
$self->lconfig->get("opensslbin") $self->conf("opensslbin") which("openssl")
Truth table0%0/4
A or B or C
A= $self->lconfig->get("opensslbin")B= $self->conf("opensslbin")C= which("openssl")
ABCresult
1XX1
01X1
0011
0000
326 $self->conf("opensslbin") || which(OPENSSLBIN) || OPENSSLBIN);
327 0 ▶ unless ($opensslbin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opensslbin) { } 0 0 0
328 0 return $self->raise("Program openssl not found. Please install it and try again later");
329 } else {
330 0 my $cmd = [$opensslbin, "version"];
331 0 my $err = "";
332 0 my $out = CTK::Util::execute( $cmd, undef, \$err );
333 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
334 0 say cyan("#", join(" ", @$cmd));
335 0 say STDERR red($err);
336 }
337 0 ▶ return $self->raise("Program openssl not found. Please install it and try again later") unless $out;
Branch: 1 branch
BranchTrueFalseTotal
unless $out 0 0 0
338 0 ▶ unless ($out =~ /^OpenSSL\s+[1-9]\.[0-9]/m) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out =~ /^OpenSSL\s+[1-9]\.[0-9]/m) 0 0 0
339 0 say STDERR yellow("OpenSSL Version is not correctly. May be some problems");
340 0 ▶ say cyan($out) if $self->verbosemode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbosemode 0 0 0
341 }
342 }
343 0 $self->lconfig->set(opensslbin => $opensslbin);
344  
345 # Ask GnuPG
346 0 ▶ my $gpgbin = $self->cli_prompt('GnuPG (gpg) program:', $self->lconfig->get("ogpgbin") ||
Condition0%0/8
$self->lconfig->get('ogpgbin') || $self->conf('gpgbin') || which('gpg') || 'gpg'
l!l
00
$self->lconfig->get("ogpgbin") or $self->conf("gpgbin") or which("gpg")
l!l&&r!l&&!r
000
$self->lconfig->get("ogpgbin") or $self->conf("gpgbin")
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->lconfig->get('ogpgbin') || $self->conf('gpgbin') || which('gpg') || 'gpg'
$self->lconfig->get("ogpgbin") $self->conf("gpgbin") which("gpg")
Truth table0%0/4
A or B or C
A= $self->lconfig->get("ogpgbin")B= $self->conf("gpgbin")C= which("gpg")
ABCresult
1XX1
01X1
0011
0000
347 $self->conf("gpgbin") || which(GPGBIN) || GPGBIN);
348 0 ▶ unless ($gpgbin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gpgbin) { } 0 0 0
349 0 return $self->raise("Program GnuPG (gpg) not found. Please install it and try again later");
350 } else {
351 0 my $cmd = [$gpgbin, "--version"];
352 0 my $err = "";
353 0 my $out = CTK::Util::execute( $cmd, undef, \$err );
354 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
355 0 say cyan("#", join(" ", @$cmd));
356 0 say STDERR red($err);
357 }
358 0 ▶ return $self->raise("Program GnuPG (gpg) not found. Please install it and try again later") unless $out;
Branch: 1 branch
BranchTrueFalseTotal
unless $out 0 0 0
359 0 ▶ unless ($out =~ /^gpg\s+\(GnuPG\)\s+[2-9]\.[0-9]/m) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out =~ /^gpg\s+\(GnuPG\)\s+[2-9]\.[0-9]/m) 0 0 0
360 0 say STDERR yellow("GnuPG Version is not correctly. May be some problems");
361 0 ▶ say cyan($out) if $self->verbosemode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbosemode 0 0 0
362 }
363 }
364 0 $self->lconfig->set(gpgbin => $gpgbin);
365  
366 # Ask fingerprint
367 0 my $fingerprint = $self->get_fingerprint;
368 0 ▶ $self->lconfig->set(fingerprint => $fingerprint) if $fingerprint;
Branch: 1 branch
BranchTrueFalseTotal
if $fingerprint 0 0 0
369  
370 # Server URL (server_url)
371 0 my $default_url = _get_default_url($tkn);
372 0 ▶ my $server_url = $self->cli_prompt('Server URL:', MToken::Util::hide_pasword($self->lconfig->get("server_url")
Condition0%0/6
$self->lconfig->get('server_url') || $self->conf('server_url') || $default_url
l!l&&r!l&&!r
000
$self->lconfig->get("server_url") or $self->conf("server_url")
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->lconfig->get('server_url') || $self->conf('server_url') || $default_url
$self->lconfig->get("server_url") $self->conf("server_url") $default_url
Truth table0%0/4
A or B || C
A= $self->lconfig->get("server_url")B= $self->conf("server_url")C= $default_url
ABCresult
1XX1
01X1
0011
0000
373 || $self->conf("server_url") || $default_url, 1));
374 0 my $uri = URI->new( $server_url );
375 0 my $url = $uri->canonical->as_string;
376  
377 # Server credentials
378 0 ▶ if ($self->cli_prompt('Ask the credentials interactively (Recommended, t. It\'s safer)?:','yes') =~ /^\s*y/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cli_prompt(q[Ask the credentials interactively (Recommended, t. It's safer)?:], 'yes') =~ /^\s*y/i) { } 0 0 0
379 0 $uri->userinfo(undef);
380 } else {
381 0 my ($server_user, $server_password) = MToken::Util::parse_credentials($uri);
382 0 ▶ unless ($server_user) { # User
Branch: 1 branch
BranchTrueFalseTotal
unless ($server_user) 0 0 0
383 0 ▶ $server_user = $self->cli_prompt('Server user:', "anonymous") // "";
Condition0%0/2
$self->cli_prompt("Server user:", "anonymous") // ""
l!l
00
MC/DC0%0/1
$self->cli_prompt("Server user:", "anonymous") // ""
$self->cli_prompt("Server user:", "anonymous")
Truth table0%0/2
A
A= $self->cli_prompt("Server user:", "anonymous")
Aresult
11
00
384 0 $server_user =~ s/%/%25/g;
385 0 $server_user =~ s/:/%3A/g;
386 }
387 0 ▶ unless ($server_password) { # Password
Branch: 1 branch
BranchTrueFalseTotal
unless ($server_password) 0 0 0
388 0 system("stty -echo") unless IS_MSWIN;
389 0 ▶ $server_password = $self->cli_prompt('Server password:', "none") // "";
Condition0%0/2
$self->cli_prompt("Server password:", "none") // ""
l!l
00
MC/DC0%0/1
$self->cli_prompt("Server password:", "none") // ""
$self->cli_prompt("Server password:", "none")
Truth table0%0/2
A
A= $self->cli_prompt("Server password:", "none")
Aresult
11
00
390 0 $server_password =~ s/%/%25/g;
391 0 system("stty echo") unless IS_MSWIN;
392 0 print STDERR "\n"; # because we disabled echo
393 0 ▶ $server_password = "" if $server_password eq "none";
Branch: 1 branch
BranchTrueFalseTotal
if $server_password eq "none" 0 0 0
394 }
395 0 $uri->userinfo(sprintf("%s:%s", $server_user, $server_password));
396 0 $url = $uri->canonical->as_string;
397 0 wow("Full server URL: %s", MToken::Util::hide_pasword($url));
398 }
399 0 $self->lconfig->set(server_url => $url);
400  
401 # Hash Diff and Save
402 0 my %after = $self->lconfig->getall;
403 0 ▶ if (_hashmd5(%before) eq _hashmd5(%after)) {
Branch: 2 branches
BranchTrueFalseTotal
if (_hashmd5(%before) eq _hashmd5(%after)) { } 0 0 0
elsif ($self->cli_prompt('Are you sure you want to save all changes to local configuration file?:', 'yes') =~ /^\s*y/i) { } 0 0 0
404 0 skip("Nothing changed in current configuration data");
405 } elsif ($self->cli_prompt('Are you sure you want to save all changes to local configuration file?:','yes') =~ /^\s*y/i) {
406 0 ▶ if ($self->lconfig->save) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lconfig->save) { } 0 0 0
407 0 yep("File \"%s\" successfully saved", $self->lconfig->{local_config_file});
408 } else {
409 0 return $self->raise("Can't save file \"%s\"", $self->lconfig->{local_config_file});
410 }
411 }
412  
413 # Skeleton
414 0 my $skel = CTK::Skel->new (
415 -name => $tkn,
416 -root => $dir,
417 -skels => {
418 device => 'MToken::DeviceSkel',
419 },
420 -debug => $self->debugmode,
421 );
422 #say("Skel object: ", explain($skel));
423  
424 # Ask
425 0 ▶ return skip("Aborted") unless $self->cli_prompt("Are you sure you want to build token $tkn to \"$dir\"?:",'no') =~ /^\s*y/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cli_prompt(qq[Are you sure you want to build token $tkn to "$dir"?:], "no") =~ /^\s*y/i 0 0 0
426  
427 # Build
428 0 my %vars = (
429 PACKAGE => __PACKAGE__,
430 VERSION => $self->VERSION, MTOKEN_VERSION => $self->VERSION,
431 TOKEN => $tkn, TOKEN_NAME => $tkn,
432 SERVER_URL => $url,
433 );
434 0 ▶ return $self->raise("Can't build the token to \"%s\" directory", $dir)
Branch: 1 branch
BranchTrueFalseTotal
unless $skel->build("device", $dir, {%vars}) 0 0 0
435 unless $skel->build("device", $dir, {%vars});
436  
437 # Database (store)
438 0 my $store = $self->store(do_init => 1);
439 0 ▶ return $self->raise($store->error ? $store->error : sprintf("Store (%s): Unknown error", $store->dsn))
Branch: 2 branches
BranchTrueFalseTotal
unless $store->status 0 0 0
$store->error ? : 0 0 0
440 unless $store->status;
441 #say(explain($store));
442  
443 # Ok
444 0 return yep("Done");
445 1 });
446  
447 __PACKAGE__->register_handler(
448 handler => "add",
449 description => "Add file(s) to token",
450 0 ▶ code => sub {
Subroutine __ANON__: not called CC=24 SCAR=64.0
451 ### CODE:
452 0 my ($self, $meta, @arguments) = @_;
453 0 ▶ unless ($self->lconfig->is_loaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->lconfig->is_loaded) 0 0 0
454 0 $self->error(ERROR_NO_TOKEN);
455 0 return 0;
456 }
457  
458 # Database
459 0 my $store = $self->store;
460 0 ▶ unless ($store->status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->status) 0 0 0
461 0 ▶ $self->error($store->error || sprintf("Store (%s): Unknown error", $store->dsn));
Condition0%0/3
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
l!l&&r!l&&!r
000
MC/DC0%0/2
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
$store->error sprintf('Store (%s): Unknown error', $store->dsn)
Truth table0%0/3
A || B
A= $store->errorB= sprintf('Store (%s): Unknown error', $store->dsn)
ABresult
1X1
011
000
462 0 return 0;
463 }
464  
465 # Input files
466 0 my @in_files = uniq(_expand_wildcards(@arguments));
467 0 ▶ unless (scalar(@in_files)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @in_files) 0 0 0
468 0 $self->error("No input file(s) specified");
469 0 return 0;
470 }
471  
472 # Get Fingerprint
473 0 my $fingerprint = $self->get_fingerprint;
474 0 ▶ unless ($fingerprint) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fingerprint) 0 0 0
475 0 $self->error("No fingerprint specified");
476 0 return 0;
477 }
478  
479 # Processing every file
480 0 foreach my $in_file (@in_files) {
481 0 my $in_file_path = path($in_file);
482 0 $in_file = $in_file_path->to_abs->to_string;
483  
484 # Check input file first
485 0 ▶ unless ($in_file && -f $in_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_file and -f $in_file) 0 0 0
Condition0%0/3
$in_file and -f $in_file
!ll&&!rl&&r
000
MC/DC0%0/2
$in_file and -f $in_file
$in_file -f $in_file
Truth table0%0/3
A and B
A= $in_fileB= -f $in_file
ABresult
0X0
100
111
486 0 skip("Can't load file %s", $in_file);
487 0 next;
488 }
489  
490 # Get file info
491 0 my $fname = $in_file_path->basename();
492 0 my $size = filesize($in_file_path->to_string);
493 0 my $mtime = File::stat::stat($in_file_path->to_string)->mtime;
494 0 my $sha1 = sha1sum($in_file);
495 #say explain([$fname, $size, $mtime, $sha1]);
496  
497 # Get info from DB
498 0 my %db_info = $store->get($fname);
499 0 ▶ unless ($store->status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->status) 0 0 0
500 0 $self->raise($store->error);
501 0 next;
502 }
503 0 ▶ if ($db_info{id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($db_info{"id"}) 0 0 0
504 0 ▶ unless ($self->option("force") || $self->cli_prompt('The file '.$in_file.' already exists in token. Are you sure you want to update it file?:','yes') =~ /^\s*y/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->option("force") or $self->cli_prompt('The file ' . $in_file . ' already exists in token. Are you sure you want to update it file?:', "yes") =~ /^\s*y/i) 0 0 0
Condition0%0/3
$self->option("force") or $self->cli_prompt('The file ' . $in_file . ' already exists in token. Are you sure you want to update it file?:', "yes") =~ /^\s*y/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->option("force") or $self->cli_prompt('The file ' . $in_file . ' already exists in token. Are you sure you want to update it file?:', "yes") =~ /^\s*y/i
$self->option("force") $self->cli_prompt('The file ' . $in_file . ' already exists in token. Are you sure you want to update it file?:', "yes") =~ /^\s*y/i
Truth table0%0/3
A or B
A= $self->option("force")B= $self->cli_prompt('The file ' . $in_file . ' already exists in token. Are you sure you want to update it file?:', "yes") =~ /^\s*y/i
ABresult
1X1
011
000
505 0 skip("Skip file %s", $in_file);
506 0 next;
507 }
508 }
509  
510 # Ask subject
511 my $subject = $self->option("force")
512 ? $db_info{subject}
513 0 ▶ : decode(locale => $self->cli_prompt('Subject (commas, slash or backslash is as line delimiter):', encode(locale => $db_info{subject} || "")));
Branch: 1 branch
BranchTrueFalseTotal
$self->option('force') ? : 0 0 0
Condition0%0/2
$db_info{'subject'} || ''
l!l
00
MC/DC0%0/1
$db_info{'subject'} || ''
$db_info{'subject'}
Truth table0%0/2
A
A= $db_info{'subject'}
Aresult
11
00
514  
515 # Ask tags
516 my $tags = $self->option("force")
517 ? $db_info{tags}
518 0 ▶ : decode(locale => $self->cli_prompt('Tags (commas or spaces are tag delimiter):', encode(locale => $db_info{tags} || "")));
Branch: 1 branch
BranchTrueFalseTotal
$self->option('force') ? : 0 0 0
Condition0%0/2
$db_info{'tags'} || ''
l!l
00
MC/DC0%0/1
$db_info{'tags'} || ''
$db_info{'tags'}
Truth table0%0/2
A
A= $db_info{'tags'}
Aresult
11
00
519  
520  
521 # New filename
522 0 my $out_file = File::Spec->catfile($self->tempdir, sprintf("%s.gpg", $fname));
523 #say $out_file;
524  
525 # Encrypt file to tempdir
526 0 my %exest = $self->execmd($self->get_gpgbin, "--encrypt", "--armor", "--quiet", "--recipient", $fingerprint, "--output", $out_file, $in_file);
527 0 ▶ unless ($exest{status} && -f $out_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exest{"status"} and -f $out_file) 0 0 0
Condition0%0/3
$exest{"status"} and -f $out_file
!ll&&!rl&&r
000
MC/DC0%0/2
$exest{"status"} and -f $out_file
$exest{"status"} -f $out_file
Truth table0%0/3
A and B
A= $exest{"status"}B= -f $out_file
ABresult
0X0
100
111
528 0 $self->raise("Can't encrypt file %s", $in_file);
529 0 next;
530 }
531  
532 # Get path object
533 0 my $out_file_path = path($out_file);
534  
535 # Add/Set new record
536 0 my @sarg = (
537 file => $fname,
538 size => $size,
539 mtime => $mtime,
540 checksum => $sha1,
541 tags => $tags,
542 subject => $subject,
543 content => $out_file_path->slurp,
544 );
545 0 ▶ my $sts = $db_info{id} ? $store->set(id => $db_info{id}, @sarg) : $store->add(@sarg);
Branch: 1 branch
BranchTrueFalseTotal
$db_info{'id'} ? : 0 0 0
546 0 ▶ unless ($sts) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sts) 0 0 0
547 0 $out_file_path->remove;
548 0 $self->raise($store->error);
549 0 next;
550 }
551  
552 # Remove output file
553 0 $out_file_path->remove;
554  
555 # Remove source file (if set the remove option)
556 0 ▶ if ($self->option("remove")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->option("remove")) 0 0 0
557 0 ▶ if ($self->option("force") || $self->cli_prompt('Are you sure you want to remove file '.$in_file.'?:','no') =~ /^\s*y/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->option("force") or $self->cli_prompt('Are you sure you want to remove file ' . $in_file . '?:', "no") =~ /^\s*y/i) 0 0 0
Condition0%0/3
$self->option("force") or $self->cli_prompt('Are you sure you want to remove file ' . $in_file . '?:', "no") =~ /^\s*y/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->option("force") or $self->cli_prompt('Are you sure you want to remove file ' . $in_file . '?:', "no") =~ /^\s*y/i
$self->option("force") $self->cli_prompt('Are you sure you want to remove file ' . $in_file . '?:', "no") =~ /^\s*y/i
Truth table0%0/3
A or B
A= $self->option("force")B= $self->cli_prompt('Are you sure you want to remove file ' . $in_file . '?:', "no") =~ /^\s*y/i
ABresult
1X1
011
000
558 0 wipe($in_file);
559 0 $in_file_path->remove;
560 }
561 }
562  
563 # Ok
564 0 yep("File %s successfully added", $in_file);
565 }
566  
567 # Ok
568 0 return yep("Done");
569  
570 0 return 1;
571 1 });
572  
573 __PACKAGE__->register_handler(
574 handler => "list",
575 description => "Files list on token",
576 0 ▶ code => sub {
Subroutine __ANON__: not called CC=16 SCAR=56.1
577 ### CODE:
578 0 my ($self, $meta, @arguments) = @_;
579 0 ▶ unless ($self->lconfig->is_loaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->lconfig->is_loaded) 0 0 0
580 0 $self->error(ERROR_NO_TOKEN);
581 0 return 0;
582 }
583  
584 # Database
585 0 my $store = $self->store;
586 0 ▶ unless ($store->status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->status) 0 0 0
587 0 ▶ $self->error($store->error || sprintf("Store (%s): Unknown error", $store->dsn));
Condition0%0/3
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
l!l&&r!l&&!r
000
MC/DC0%0/2
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
$store->error sprintf('Store (%s): Unknown error', $store->dsn)
Truth table0%0/3
A || B
A= $store->errorB= sprintf('Store (%s): Unknown error', $store->dsn)
ABresult
1X1
011
000
588 0 return 0;
589 }
590  
591 # Get info from DB
592 0 ▶ my $page = $self->option("page") || 1;
Condition0%0/2
$self->option('page') || 1
l!l
00
MC/DC0%0/1
$self->option('page') || 1
$self->option('page')
Truth table0%0/2
A
A= $self->option('page')
Aresult
11
00
593 0 ▶ my $cnt = $store->count || 0;
Condition0%0/2
$store->count || 0
l!l
00
MC/DC0%0/1
$store->count || 0
$store->count
Truth table0%0/2
A
A= $store->count
Aresult
11
00
594 0 my $of = ($cnt - $cnt % RECORDS_PER_PAGE)/RECORDS_PER_PAGE + 1;
595 0 ▶ $page = $of if $page > $of;
Branch: 1 branch
BranchTrueFalseTotal
if $page > $of 0 0 0
596 0 say cyan("File list of \"%s\"", $self->get_name);
597  
598 0 my @table = $store->getall(($page - 1) * RECORDS_PER_PAGE, RECORDS_PER_PAGE); # offset, row_count
599 0 ▶ unless ($store->status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->status) 0 0 0
600 0 $self->error($store->error);
601 0 return 0;
602 }
603  
604 # Init table
605 0 my $tbl_hdrs = [(
606 [SCREENWIDTH() - 54, 'FILE/SUBJECT'],
607 [21, 'TAGS'],
608 [10, 'SIZE, B'],
609 [10, 'MTIME'],
610 )];
611 0 my $tbl = Text::SimpleTable->new(@$tbl_hdrs);
612  
613 # Show table
614 0 my $i = 0;
615 0 my $c = scalar(@table);
616 0 foreach my $row (@table) {
617 0 $i++;
618 #$tbl->row("Test.txt\nTest document", "foo, bar, baz", 1024, "2020-12-12\n12:12:12");
619 0 ▶ $tbl->row(
Condition0%0/12
$row->[1] || 'noname'
l!l
00
$row->[6] || ''
l!l
00
$row->[5] || '-'
l!l
00
$row->[2] || 0
l!l
00
$row->[3] || 0
l!l
00
$row->[3] || 0
l!l
00
MC/DC0%0/1
$row->[1] || 'noname'
$row->[1]
MC/DC0%0/1
$row->[6] || ''
$row->[6]
MC/DC0%0/1
$row->[5] || '-'
$row->[5]
MC/DC0%0/1
$row->[2] || 0
$row->[2]
MC/DC0%0/1
$row->[3] || 0
$row->[3]
MC/DC0%0/1
$row->[3] || 0
$row->[3]
Truth table0%0/2
A
A= $row->[1]
Aresult
11
00
Truth table0%0/2
A
A= $row->[6]
Aresult
11
00
Truth table0%0/2
A
A= $row->[5]
Aresult
11
00
Truth table0%0/2
A
A= $row->[2]
Aresult
11
00
Truth table0%0/2
A
A= $row->[3]
Aresult
11
00
Truth table0%0/2
A
A= $row->[3]
Aresult
11
00
620 sprintf("%s\n %s%s",
621 $row->[1] || "noname",
622 encode(locale => $row->[6] || ''),
623 "", #($c > $i ? "\n" : ""),
624 ),
625 encode(locale => $row->[5] || '-'),
626 _fbytes($row->[2] || 0),
627 sprintf("%s\n %s",
628 dtf(DATE_FORMAT, $row->[3] || 0),
629 dtf(TIME_FORMAT, $row->[3] || 0),
630 ),
631 );
632 #$tbl->hr if $c > $i;
633 }
634  
635 # Show table
636 0 print $tbl->draw();
637 0 ▶ say cyan("total %d file(s); page %d of %d", $store->count || 0, $page, $of);
Condition0%0/2
$store->count || 0
l!l
00
MC/DC0%0/1
$store->count || 0
$store->count
Truth table0%0/2
A
A= $store->count
Aresult
11
00
638  
639 0 return 1;
640 1 });
641  
642 __PACKAGE__->register_handler(
643 handler => "info",
644 description => "Get file/database information",
645 0 ▶ code => sub {
Subroutine __ANON__: not called CC=25 SCAR=64.8
646 ### CODE:
647 0 my ($self, $meta, @arguments) = @_;
648 0 ▶ unless ($self->lconfig->is_loaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->lconfig->is_loaded) 0 0 0
649 0 $self->error(ERROR_NO_TOKEN);
650 0 return 0;
651 }
652  
653 # Database
654 0 my $store = $self->store;
655 0 ▶ unless ($store->status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->status) 0 0 0
656 0 ▶ $self->error($store->error || sprintf("Store (%s): Unknown error", $store->dsn));
Condition0%0/3
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
l!l&&r!l&&!r
000
MC/DC0%0/2
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
$store->error sprintf('Store (%s): Unknown error', $store->dsn)
Truth table0%0/3
A || B
A= $store->errorB= sprintf('Store (%s): Unknown error', $store->dsn)
ABresult
1X1
011
000
657 0 return 0;
658 }
659  
660 # Input file
661 0 my $filename = shift @arguments;
662 0 ▶ if ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename) { } 0 0 0
663 0 my %data = $store->get($filename);
664 0 ▶ unless ($store->status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->status) 0 0 0
665 0 ▶ $self->error($store->error || sprintf("Store (%s): Unknown error", $store->dsn));
Condition0%0/3
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
l!l&&r!l&&!r
000
MC/DC0%0/2
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
$store->error sprintf('Store (%s): Unknown error', $store->dsn)
Truth table0%0/3
A || B
A= $store->errorB= sprintf('Store (%s): Unknown error', $store->dsn)
ABresult
1X1
011
000
666 0 return 0;
667 }
668  
669 # Show table
670 say tablify([
671 ['Filename :', $filename],
672 ['Id :', $data{id} || 0],
673 ['Size :', sprintf("%s (%d bytes)", _fbytes($data{size} || 0), $data{size} || 0)],
674 ['MTime :', _fdate($data{mtime} || 0)],
675 ['Checksum :', $data{checksum} || ""],
676 0 ▶ ['Tags :', encode(locale => $data{tags} || "")],
Condition0%0/12
$data{'id'} || 0
l!l
00
$data{'size'} || 0
l!l
00
$data{'size'} || 0
l!l
00
$data{'mtime'} || 0
l!l
00
$data{'checksum'} || ''
l!l
00
$data{'tags'} || ''
l!l
00
MC/DC0%0/1
$data{'id'} || 0
$data{'id'}
MC/DC0%0/1
$data{'size'} || 0
$data{'size'}
MC/DC0%0/1
$data{'size'} || 0
$data{'size'}
MC/DC0%0/1
$data{'mtime'} || 0
$data{'mtime'}
MC/DC0%0/1
$data{'checksum'} || ''
$data{'checksum'}
MC/DC0%0/1
$data{'tags'} || ''
$data{'tags'}
Truth table0%0/2
A
A= $data{'id'}
Aresult
11
00
Truth table0%0/2
A
A= $data{'size'}
Aresult
11
00
Truth table0%0/2
A
A= $data{'size'}
Aresult
11
00
Truth table0%0/2
A
A= $data{'mtime'}
Aresult
11
00
Truth table0%0/2
A
A= $data{'checksum'}
Aresult
11
00
Truth table0%0/2
A
A= $data{'tags'}
Aresult
11
00
677 ]);
678 0 ▶ say cyan(encode(locale => $data{subject} || "none")), "\n";
Condition0%0/2
$data{'subject'} || 'none'
l!l
00
MC/DC0%0/1
$data{'subject'} || 'none'
$data{'subject'}
Truth table0%0/2
A
A= $data{'subject'}
Aresult
11
00
679 0 ▶ say $data{content} || "" if $self->verbosemode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbosemode 0 0 0
Condition0%0/2
$data{'content'} || ''
l!l
00
MC/DC0%0/1
$data{'content'} || ''
$data{'content'}
Truth table0%0/2
A
A= $data{'content'}
Aresult
11
00
680 } else {
681 0 ▶ my $count = $store->count || 0;
Condition0%0/2
$store->count || 0
l!l
00
MC/DC0%0/1
$store->count || 0
$store->count
Truth table0%0/2
A
A= $store->count
Aresult
11
00
682 0 ▶ unless ($store->status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->status) 0 0 0
683 0 ▶ $self->error($store->error || sprintf("Store (%s): Unknown error", $store->dsn));
Condition0%0/3
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
l!l&&r!l&&!r
000
MC/DC0%0/2
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
$store->error sprintf('Store (%s): Unknown error', $store->dsn)
Truth table0%0/3
A || B
A= $store->errorB= sprintf('Store (%s): Unknown error', $store->dsn)
ABresult
1X1
011
000
684 0 return 0;
685 }
686 0 my $dbfile = $store->{file};
687 0 ▶ my $dbfile_size = ($dbfile && -e $dbfile) ? filesize($dbfile) || 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dbfile && -e $dbfile ? : 0 0 0
Condition0%0/5
$dbfile and -e $dbfile
!ll&&!rl&&r
000
filesize($dbfile) or 0
l!l
00
MC/DC0%0/2
$dbfile and -e $dbfile
$dbfile -e $dbfile
MC/DC0%0/1
filesize($dbfile) or 0
filesize($dbfile)
Truth table0%0/3
A and B
A= $dbfileB= -e $dbfile
ABresult
0X0
100
111
Truth table0%0/2
A
A= filesize($dbfile)
Aresult
11
00
688 0 ▶ say tablify([
Branch: 1 branch
BranchTrueFalseTotal
$dbfile ? : 0 0 0
Condition0%0/4
$store->dsn || ''
l!l
00
$count || 0
l!l
00
MC/DC0%0/1
$store->dsn || ''
$store->dsn
MC/DC0%0/1
$count || 0
$count
Truth table0%0/2
A
A= $store->dsn
Aresult
11
00
Truth table0%0/2
A
A= $count
Aresult
11
00
689 ['DSN :', $store->dsn || ""],
690 ['Files in DB :', $count || 0],
691 ($dbfile ? (
692 ['DB File :', $dbfile],
693 ['DB File size :', sprintf("%s (%d bytes)", _fbytes($dbfile_size), $dbfile_size)],
694 ) : ()),
695 ]);
696 }
697  
698 0 return 1;
699 1 });
700  
701 __PACKAGE__->register_handler(
702 handler => "get",
703 description => "Get (extract) file from token to disk",
704 0 ▶ code => sub {
Subroutine __ANON__: not called CC=22 SCAR=62.3
705 ### CODE:
706 0 my ($self, $meta, @arguments) = @_;
707 0 ▶ unless ($self->lconfig->is_loaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->lconfig->is_loaded) 0 0 0
708 0 $self->error(ERROR_NO_TOKEN);
709 0 return 0;
710 }
711  
712 # Database
713 0 my $store = $self->store;
714 0 ▶ unless ($store->status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->status) 0 0 0
715 0 ▶ $self->error($store->error || sprintf("Store (%s): Unknown error", $store->dsn));
Condition0%0/3
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
l!l&&r!l&&!r
000
MC/DC0%0/2
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
$store->error sprintf('Store (%s): Unknown error', $store->dsn)
Truth table0%0/3
A || B
A= $store->errorB= sprintf('Store (%s): Unknown error', $store->dsn)
ABresult
1X1
011
000
716 0 return 0;
717 }
718  
719 # Input file
720 0 my $filename = shift @arguments;
721 0 ▶ unless ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 0 0
722 0 $self->error("No input file specified");
723 0 return 0;
724 }
725  
726 # Get data from database
727 0 my %data = $store->get($filename);
728 0 ▶ unless ($store->status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->status) 0 0 0
729 0 ▶ $self->error($store->error || sprintf("Store (%s): Unknown error", $store->dsn));
Condition0%0/3
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
l!l&&r!l&&!r
000
MC/DC0%0/2
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
$store->error sprintf('Store (%s): Unknown error', $store->dsn)
Truth table0%0/3
A || B
A= $store->errorB= sprintf('Store (%s): Unknown error', $store->dsn)
ABresult
1X1
011
000
730 0 return 0;
731 }
732  
733 # Get file names
734 0 my $enc_file_path = path($self->tempdir, sprintf("%s.gpg", $filename));
735 0 ▶ my $dec_file_path = path($self->option("output") || File::Spec->catfile(getcwd(), $filename));
Condition0%0/3
$self->option('output') || 'File::Spec'->catfile(getcwd(), $filename)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->option('output') || 'File::Spec'->catfile(getcwd(), $filename)
$self->option('output') 'File::Spec'->catfile(getcwd(), $filename)
Truth table0%0/3
A || B
A= $self->option('output')B= 'File::Spec'->catfile(getcwd(), $filename)
ABresult
1X1
011
000
736 #say explain({enc_file_path => $enc_file_path->to_string, dec_file_path => $dec_file_path->to_string});
737  
738 # Write file content on disk (spurt, spew; see also Module::Build::Base::_spew)
739 0 ▶ $enc_file_path->spurt($data{content} || "");
Condition0%0/2
$data{'content'} || ''
l!l
00
MC/DC0%0/1
$data{'content'} || ''
$data{'content'}
Truth table0%0/2
A
A= $data{'content'}
Aresult
11
00
740 0 ▶ unless (filesize($enc_file_path->to_string)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (filesize($enc_file_path->to_string)) 0 0 0
741 0 $self->error(sprintf("Can't load empty file %s", $enc_file_path->to_string));
742 0 return 0;
743 }
744  
745 # Decrypt file to tempdir
746 # gpg -d -q -o $bname $1
747 0 my $out_file = $dec_file_path->to_string;
748 0 my %exest = $self->execmd($self->get_gpgbin, "--decrypt", "--quiet", "--output", $out_file, $enc_file_path->to_string);
749 0 ▶ unless ($exest{status} && -e $out_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exest{"status"} and -e $out_file) 0 0 0
Condition0%0/3
$exest{"status"} and -e $out_file
!ll&&!rl&&r
000
MC/DC0%0/2
$exest{"status"} and -e $out_file
$exest{"status"} -e $out_file
Truth table0%0/3
A and B
A= $exest{"status"}B= -e $out_file
ABresult
0X0
100
111
750 0 $self->error(sprintf("Can't decrypt file %s", $enc_file_path->to_string));
751 0 my $newfile = $enc_file_path->copy_to(sprintf("%s.gpg", $out_file));
752 0 ▶ say magenta("The encrypted file has been stored to %s", $newfile->to_string) if filesize($newfile->to_string);
Branch: 1 branch
BranchTrueFalseTotal
if filesize($newfile->to_string) 0 0 0
753 0 return 0;
754 }
755  
756 # Check size
757 0 ▶ my $nsize = filesize($dec_file_path->to_string) || 0;
Condition0%0/2
filesize($dec_file_path->to_string) || 0
l!l
00
MC/DC0%0/1
filesize($dec_file_path->to_string) || 0
filesize($dec_file_path->to_string)
Truth table0%0/2
A
A= filesize($dec_file_path->to_string)
Aresult
11
00
758 0 ▶ unless ($nsize == ($data{size} || 0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nsize == ($data{"size"} || 0)) 0 0 0
Condition0%0/2
$data{'size'} || 0
l!l
00
MC/DC0%0/1
$data{'size'} || 0
$data{'size'}
Truth table0%0/2
A
A= $data{'size'}
Aresult
11
00
759 0 ▶ $self->error(sprintf("File size mismatch (%s). Expected %d, got %d", $out_file, $nsize, $data{size} || 0));
Condition0%0/2
$data{'size'} || 0
l!l
00
MC/DC0%0/1
$data{'size'} || 0
$data{'size'}
Truth table0%0/2
A
A= $data{'size'}
Aresult
11
00
760 0 return 0;
761 }
762  
763 # Check sha1
764 0 my $sha1 = sha1sum($out_file);
765 0 ▶ unless ($sha1 eq ($data{checksum} || "~")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sha1 eq ($data{"checksum"} || "~")) 0 0 0
Condition0%0/2
$data{'checksum'} || '~'
l!l
00
MC/DC0%0/1
$data{'checksum'} || '~'
$data{'checksum'}
Truth table0%0/2
A
A= $data{'checksum'}
Aresult
11
00
766 0 $self->error(sprintf("File checksum mismatch (%s)", $out_file));
767 0 return 0;
768 }
769  
770 # Change utime
771 0 ▶ if ($data{mtime}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{"mtime"}) 0 0 0
772 0 ▶ utime(time(), $data{mtime}, $out_file) || skip("Couldn't touch %s: %s", $out_file, $!);
Branch: 1 branch
BranchTrueFalseTotal
utime time, $data{"mtime"}, $out_file or skip("Couldn't touch %s: %s", $out_file, $!) 0 0 0
773 }
774  
775 # Remove temp file
776 0 $enc_file_path->remove;
777  
778 # Ok
779 0 yep("File %s successfully extracted", $out_file);
780 0 ▶ say cyan(encode(locale => $data{subject} || "none")), "\n";
Condition0%0/2
$data{'subject'} || 'none'
l!l
00
MC/DC0%0/1
$data{'subject'} || 'none'
$data{'subject'}
Truth table0%0/2
A
A= $data{'subject'}
Aresult
11
00
781  
782 0 return 1;
783 1 });
784  
785 __PACKAGE__->register_handler(
786 handler => "show",
787 description => "Extract and print file from token to STDOUT",
788 0 ▶ code => sub {
Subroutine __ANON__: not called CC=23 SCAR=63.1
789 ### CODE:
790 0 my ($self, $meta, @arguments) = @_;
791 0 ▶ unless ($self->lconfig->is_loaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->lconfig->is_loaded) 0 0 0
792 0 $self->error(ERROR_NO_TOKEN);
793 0 return 0;
794 }
795  
796 # Database
797 0 my $store = $self->store;
798 0 ▶ unless ($store->status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->status) 0 0 0
799 0 ▶ $self->error($store->error || sprintf("Store (%s): Unknown error", $store->dsn));
Condition0%0/3
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
l!l&&r!l&&!r
000
MC/DC0%0/2
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
$store->error sprintf('Store (%s): Unknown error', $store->dsn)
Truth table0%0/3
A || B
A= $store->errorB= sprintf('Store (%s): Unknown error', $store->dsn)
ABresult
1X1
011
000
800 0 return 0;
801 }
802  
803 # Input file
804 0 my $filename = shift @arguments;
805 0 ▶ unless ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 0 0
806 0 $self->error("No input file specified");
807 0 return 0;
808 }
809  
810 # Get data from database
811 0 my %data = $store->get($filename);
812 0 ▶ unless ($store->status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->status) 0 0 0
813 0 ▶ $self->error($store->error || sprintf("Store (%s): Unknown error", $store->dsn));
Condition0%0/3
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
l!l&&r!l&&!r
000
MC/DC0%0/2
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
$store->error sprintf('Store (%s): Unknown error', $store->dsn)
Truth table0%0/3
A || B
A= $store->errorB= sprintf('Store (%s): Unknown error', $store->dsn)
ABresult
1X1
011
000
814 0 return 0;
815 }
816  
817 # Show raw file
818 0 ▶ if ($self->option("raw")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->option("raw")) 0 0 0
819 0 ▶ say $data{content} || "";
Condition0%0/2
$data{'content'} || ''
l!l
00
MC/DC0%0/1
$data{'content'} || ''
$data{'content'}
Truth table0%0/2
A
A= $data{'content'}
Aresult
11
00
820 0 return 1;
821 }
822  
823 # Get file names
824 0 my $enc_file_path = path($self->tempdir, sprintf("%s.gpg", $filename));
825 0 my $dec_file_path = path($self->tempdir, $filename);
826  
827 # Write file content on disk (spurt, spew; see also Module::Build::Base::_spew)
828 0 my $in_file = $enc_file_path->to_string;
829 0 ▶ $enc_file_path->spurt($data{content} || "");
Condition0%0/2
$data{'content'} || ''
l!l
00
MC/DC0%0/1
$data{'content'} || ''
$data{'content'}
Truth table0%0/2
A
A= $data{'content'}
Aresult
11
00
830 0 ▶ unless (filesize($enc_file_path->to_string)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (filesize($enc_file_path->to_string)) 0 0 0
831 0 $self->error(sprintf("Can't load empty file %s", $in_file));
832 0 return 0;
833 }
834  
835 # Decrypt file to tempdir
836 # gpg -d -q -o $bname $1
837 0 my $out_file = $dec_file_path->to_string;
838 0 my %exest = $self->execmd($self->get_gpgbin, "--decrypt", "--quiet", "--output", $out_file, $in_file);
839 0 ▶ unless ($exest{status} && -e $out_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exest{"status"} and -e $out_file) 0 0 0
Condition0%0/3
$exest{"status"} and -e $out_file
!ll&&!rl&&r
000
MC/DC0%0/2
$exest{"status"} and -e $out_file
$exest{"status"} -e $out_file
Truth table0%0/3
A and B
A= $exest{"status"}B= -e $out_file
ABresult
0X0
100
111
840 0 $self->error(sprintf("Can't decrypt file %s", $in_file));
841 0 ▶ say $data{content} || "";
Condition0%0/2
$data{'content'} || ''
l!l
00
MC/DC0%0/1
$data{'content'} || ''
$data{'content'}
Truth table0%0/2
A
A= $data{'content'}
Aresult
11
00
842 0 return 0;
843 }
844  
845 # Check size
846 0 ▶ my $nsize = filesize($dec_file_path->to_string) || 0;
Condition0%0/2
filesize($dec_file_path->to_string) || 0
l!l
00
MC/DC0%0/1
filesize($dec_file_path->to_string) || 0
filesize($dec_file_path->to_string)
Truth table0%0/2
A
A= filesize($dec_file_path->to_string)
Aresult
11
00
847 0 ▶ unless ($nsize && $nsize == ($data{size} || 0)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nsize and $nsize == ($data{"size"} || 0)) 0 0 0
Condition0%0/5
$nsize and $nsize == ($data{"size"} || 0)
!ll&&!rl&&r
000
$data{'size'} || 0
l!l
00
MC/DC0%0/2
$nsize and $nsize == ($data{"size"} || 0)
$nsize $nsize == ($data{"size"} || 0)
MC/DC0%0/1
$data{'size'} || 0
$data{'size'}
Truth table0%0/3
A and B
A= $nsizeB= $nsize == ($data{"size"} || 0)
ABresult
0X0
100
111
Truth table0%0/2
A
A= $data{'size'}
Aresult
11
00
848 0 ▶ $self->error(sprintf("File size mismatch (%s). Expected %d, got %d", $out_file, $nsize, $data{size} || 0));
Condition0%0/2
$data{'size'} || 0
l!l
00
MC/DC0%0/1
$data{'size'} || 0
$data{'size'}
Truth table0%0/2
A
A= $data{'size'}
Aresult
11
00
849 0 return 0;
850 }
851  
852 # Check sha1
853 0 my $sha1 = sha1sum($out_file);
854 0 ▶ unless ($sha1 eq ($data{checksum} || "~")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sha1 eq ($data{"checksum"} || "~")) 0 0 0
Condition0%0/2
$data{'checksum'} || '~'
l!l
00
MC/DC0%0/1
$data{'checksum'} || '~'
$data{'checksum'}
Truth table0%0/2
A
A= $data{'checksum'}
Aresult
11
00
855 0 $self->error(sprintf("File checksum mismatch (%s)", $out_file));
856 0 return 0;
857 }
858  
859 # Check text or binary file (-T)
860 0 ▶ if (-B $out_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-B $out_file) { } 0 0 0
861 0 say STDERR yellow("File %s is binary!\nPlease use the \"get\" command for extract it as file", $out_file);
862 0 ▶ say STDERR cyan(encode(locale => $data{subject} || "none")), "\n";
Condition0%0/2
$data{'subject'} || 'none'
l!l
00
MC/DC0%0/1
$data{'subject'} || 'none'
$data{'subject'}
Truth table0%0/2
A
A= $data{'subject'}
Aresult
11
00
863 } else {
864 0 say $dec_file_path->slurp;
865 }
866  
867 # Remove temp files
868 0 $enc_file_path->remove;
869 0 wipe($out_file);
870 0 $dec_file_path->remove;
871  
872 0 return 1;
873 1 });
874  
875 __PACKAGE__->register_handler(
876 handler => "del",
877 description => "Delete file from token",
878 0 ▶ code => sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
879 ### CODE:
880 0 my ($self, $meta, @arguments) = @_;
881 0 ▶ unless ($self->lconfig->is_loaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->lconfig->is_loaded) 0 0 0
882 0 $self->error(ERROR_NO_TOKEN);
883 0 return 0;
884 }
885  
886 # Database
887 0 my $store = $self->store;
888 0 ▶ unless ($store->status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->status) 0 0 0
889 0 ▶ $self->error($store->error || sprintf("Store (%s): Unknown error", $store->dsn));
Condition0%0/3
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
l!l&&r!l&&!r
000
MC/DC0%0/2
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
$store->error sprintf('Store (%s): Unknown error', $store->dsn)
Truth table0%0/3
A || B
A= $store->errorB= sprintf('Store (%s): Unknown error', $store->dsn)
ABresult
1X1
011
000
890 0 return 0;
891 }
892  
893 # Input file
894 0 my $filename = shift @arguments;
895 0 ▶ unless ($filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 0 0
896 0 $self->error("No input file specified");
897 0 return 0;
898 }
899  
900 # Get data from database
901 0 my %data = $store->get($filename);
902 0 ▶ unless ($store->status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store->status) 0 0 0
903 0 ▶ $self->error($store->error || sprintf("Store (%s): Unknown error", $store->dsn));
Condition0%0/3
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
l!l&&r!l&&!r
000
MC/DC0%0/2
$store->error || sprintf('Store (%s): Unknown error', $store->dsn)
$store->error sprintf('Store (%s): Unknown error', $store->dsn)
Truth table0%0/3
A || B
A= $store->errorB= sprintf('Store (%s): Unknown error', $store->dsn)
ABresult
1X1
011
000
904 0 return 0;
905 }
906 0 ▶ unless ($data{id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data{"id"}) 0 0 0
907 0 $self->error("File not found");
908 0 return 0;
909 }
910  
911 # Delete file
912 0 ▶ if ($self->option("force") || $self->cli_prompt('Are you sure you want to remove file '.$filename .'?:','no') =~ /^\s*y/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->option('force') or $self->cli_prompt('Are you sure you want to remove file ' . $filename . '?:', 'no') =~ /^\s*y/i) { } 0 0 0
Condition0%0/3
$self->option("force") or $self->cli_prompt('Are you sure you want to remove file ' . $filename . '?:', "no") =~ /^\s*y/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->option("force") or $self->cli_prompt('Are you sure you want to remove file ' . $filename . '?:', "no") =~ /^\s*y/i
$self->option("force") $self->cli_prompt('Are you sure you want to remove file ' . $filename . '?:', "no") =~ /^\s*y/i
Truth table0%0/3
A or B
A= $self->option("force")B= $self->cli_prompt('Are you sure you want to remove file ' . $filename . '?:', "no") =~ /^\s*y/i
ABresult
1X1
011
000
913 0 ▶ $store->del($filename) or do {
Branch: 1 branch
BranchTrueFalseTotal
$store->del($filename) or do { $self->error($store->error); return 0 } 0 0 0
914 0 $self->error($store->error);
915 0 return 0;
916 };
917 } else {
918 0 return skip("Aborted. Skip file %s", $filename);
919 }
920  
921 # Ok
922 0 return yep("File %s successfully deleted", $filename);
923 1 });
924  
925 __PACKAGE__->register_handler(
926 handler => "genkey",
927 description => "Generate",
928 0 ▶ code => sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
929 ### CODE:
930 0 my ($self, $meta, @arguments) = @_;
931  
932 # Output file
933 0 ▶ my $filename = shift(@arguments) || $self->option("output");
Condition0%0/3
shift @arguments || $self->option('output')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift @arguments || $self->option('output')
shift @arguments $self->option('output')
Truth table0%0/3
A || B
A= shift @argumentsB= $self->option('output')
ABresult
1X1
011
000
934 0 ▶ my $path = $filename
Branch: 2 branches
BranchTrueFalseTotal
$filename ? : 0 0 0
$self->lconfig->is_loaded ? : 0 0 0
935 ? path($filename)
936 : $self->lconfig->is_loaded
937 ? path($self->datadir, DIR_PRIVATE, RND_KEY_FILE)
938 : path(getcwd(), RND_KEY_FILE);
939 0 my $file_out = $path->to_string;
940 0 ▶ if (-e $file_out) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file_out) 0 0 0
941 0 ▶ unless ($self->option("force") ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->option("force") or $self->cli_prompt('File ' . $file_out . ' already exists. Are you sure you want to replace this file?:', "no") =~ /^\s*y/i) 0 0 0
Condition0%0/3
$self->option("force") or $self->cli_prompt('File ' . $file_out . ' already exists. Are you sure you want to replace this file?:', "no") =~ /^\s*y/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->option("force") or $self->cli_prompt('File ' . $file_out . ' already exists. Are you sure you want to replace this file?:', "no") =~ /^\s*y/i
$self->option("force") $self->cli_prompt('File ' . $file_out . ' already exists. Are you sure you want to replace this file?:', "no") =~ /^\s*y/i
Truth table0%0/3
A or B
A= $self->option("force")B= $self->cli_prompt('File ' . $file_out . ' already exists. Are you sure you want to replace this file?:', "no") =~ /^\s*y/i
ABresult
1X1
011
000
942 $self->cli_prompt('File '.$file_out.' already exists. Are you sure you want to replace this file?:','no') =~ /^\s*y/i) {
943 0 return skip("Aborted. Skip file %s", $file_out);
944 }
945 }
946  
947 # Get size
948 0 ▶ my $size = $self->option("size") ||
Condition0%0/3
$self->option('size') || int(rand 2016) + 32
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->option('size') || int(rand 2016) + 32
$self->option('size') int(rand 2016) + 32
Truth table0%0/3
A || B
A= $self->option('size')B= int(rand 2016) + 32
ABresult
1X1
011
000
949 int(rand(MToken::Const::KEYMAXSIZE - MToken::Const::KEYMINSIZE)) + MToken::Const::KEYMINSIZE;
950  
951 0 my %exest = $self->execmd($self->get_opensslbin, "rand", "-out", $file_out, $size);
952 0 ▶ unless ($exest{status} && -e $file_out) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exest{"status"} and -e $file_out) 0 0 0
Condition0%0/3
$exest{"status"} and -e $file_out
!ll&&!rl&&r
000
MC/DC0%0/2
$exest{"status"} and -e $file_out
$exest{"status"} -e $file_out
Truth table0%0/3
A and B
A= $exest{"status"}B= -e $file_out
ABresult
0X0
100
111
953 0 $self->error(sprintf("Can't generate rand key file %s", $file_out));
954 0 return 0;
955 }
956 0 ▶ say cyan($exest{output}) if $exest{output};
Branch: 1 branch
BranchTrueFalseTotal
if $exest{"output"} 0 0 0
957  
958 # Ok
959 0 return yep("File %s successfully generated", $file_out);
960 1 });
961  
962 __PACKAGE__->register_handler(
963 handler => "server",
964 description => "MToken HTTP server",
965 0 ▶ code => sub {
Subroutine __ANON__: not called CC=40 SCAR=74.0
966 ### CODE:
967 0 my ($self, $meta, @arguments) = @_;
968  
969 # Dash k
970 0 ▶ my $dash_k = shift(@arguments) || "status";
Condition0%0/2
shift @arguments || 'status'
l!l
00
MC/DC0%0/1
shift @arguments || 'status'
shift @arguments
Truth table0%0/2
A
A= shift @arguments
Aresult
11
00
971 0 ▶ unless (grep {$_ eq $dash_k} qw/start status stop restart reload/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $dash_k;} "start", "status", "stop", "restart", "reload") 0 0 0
972 0 $self->error("Incorrect LSB command! Please use start, status, stop, restart or reload");
973 0 return 0;
974 }
975  
976 # Get permisions by names
977 0 my $uid = $>; # Effect. UID
978 0 my $gid = $); # Effect. GID
979 0 if (IS_ROOT) {
980 $uid = getpwnam(USERNAME) || do {
981 $self->error("getpwnam failed - $!");
982 return 0;
983 };
984 $gid = getgrnam(GROUPNAME) || do {
985 $self->error("getgrnam failed - $!\n");
986 return 0;
987 };
988 }
989  
990 # Prepare DataDir if not specified
991 0 ▶ unless ($self->option("datadir")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->option("datadir")) 0 0 0
992 0 if (IS_ROOT) { # /var/lib/mtoken
993 $self->datadir(File::Spec->catdir(sharedstatedir(), PROJECTNAMEL));
994 } else { #~/.local/share/mtoken/www
995 0 $self->datadir(File::Spec->catdir(File::HomeDir->my_data(), PROJECTNAMEL, "www"));
996 }
997 0 ▶ unless (-e $self->datadir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->datadir) 0 0 0
998 0 ▶ unless (preparedir($self->datadir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (preparedir($self->datadir)) 0 0 0
999 0 $self->error(sprintf("Can't prepare directory %s", $self->datadir));
1000 0 return 0;
1001 }
1002 }
1003 # Set permisions (GID and UID) for work directory
1004 0 chown($uid, $gid, $self->datadir) if IS_ROOT && File::stat::stat($self->datadir)->uid != $uid;
1005 }
1006  
1007 # Prepare tempdir
1008 0 $self->tempdir(File::Spec->catdir(File::Spec->tmpdir(), PROJECTNAMEL));
1009 0 ▶ unless (preparedir( $self->tempdir, 0777 )) {
Branch: 1 branch
BranchTrueFalseTotal
unless (preparedir($self->tempdir, 511)) 0 0 0
1010 0 $self->error(sprintf("Can't prepare temp directory: %s", $self->tempdir));
1011 0 return 0;
1012 }
1013 0 chown($uid, $gid, $self->tempdir) if IS_ROOT && File::stat::stat($self->tempdir)->uid != $uid;
1014 0 $self->debug(sprintf("Temp dir: %s", $self->tempdir));
1015  
1016 # Prepare log directory
1017 0 if (IS_ROOT) {
1018 my $logdir = $self->logdir;
1019 unless (preparedir( $logdir, 0777 )) {
1020 $self->error(sprintf("Can't prepare log directory: %s", $logdir));
1021 return 0;
1022 }
1023 # Set permisions (GID and UID) for log directory
1024 chown($uid, $gid, $logdir) if File::stat::stat($logdir)->uid != $uid;
1025 $self->debug(sprintf("Log dir: %s", $self->logdir));
1026 } else {
1027 0 $self->logfile(File::Spec->catfile($self->tempdir(), sprintf("%s.log", PROJECTNAMEL)));
1028 0 $self->debug(sprintf("Log file: %s", $self->logfile));
1029 }
1030  
1031 # Prepare pid directory and file
1032 0 my $piddir = IS_ROOT ? File::Spec->catdir( rundir(), PROJECTNAMEL) : $self->tempdir();
1033 0 my $pidfile = File::Spec->catfile($piddir, sprintf("%s.pid", PROJECTNAMEL));
1034 0 ▶ unless (preparedir($piddir)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (preparedir($piddir)) 0 0 0
1035 0 $self->error(sprintf("Can't prepare pid directory: %s", $piddir));
1036 0 return 0;
1037 }
1038 # Set permisions (GID and UID) for pid directory
1039 0 chown($uid, $gid, $piddir) if IS_ROOT && File::stat::stat($piddir)->uid != $uid;
1040 0 $self->debug(sprintf("Pid file: %s", $pidfile));
1041  
1042 # Hypnotoad variables
1043 0 my $upgrade = 0;
1044 0 my $reload = 0;
1045 0 my $upgrade_timeout = UPGRADE_TIMEOUT;
1046  
1047 # Mojolicious Application
1048 0 my $app = MToken::Server->new(ctk => $self);
1049 0 ▶ $app->attr(ctk => sub { $self }); # has ctk => sub { CTKx->instance->ctk };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1050 0 my $prefork = Mojo::Server::Prefork->new( app => $app ); # app => $self
1051 0 $prefork->pid_file($pidfile);
1052  
1053 # Hypnotoad Pre-fork settings
1054 0 ▶ $prefork->max_clients(tv2int(value($self->conf("clients")))) if defined $self->conf("clients");
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->conf("clients") 0 0 0
1055 0 ▶ $prefork->max_requests(tv2int(value($self->conf("requests")))) if defined $self->conf("requests");
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->conf("requests") 0 0 0
1056 0 ▶ $prefork->accepts(tv2int(value($self->conf("accepts")))) if defined $self->conf("accepts");
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->conf("accepts") 0 0 0
1057 0 ▶ $prefork->spare(tv2int(value($self->conf("spare")))) if defined $self->conf("spare");
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->conf("spare") 0 0 0
1058 0 ▶ $prefork->workers(tv2int(value($self->conf("workers")))) if defined $self->conf("workers");
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->conf("workers") 0 0 0
1059  
1060 # Make Listen
1061 0 my $cfg_listen = value($self->conf("listen"));
1062 0 my $tls_on = isTrueFlag(value($self->conf("tls")));
1063 0 ▶ my $listen = $tls_on ? "https://" : "http://";
Branch: 1 branch
BranchTrueFalseTotal
$tls_on ? : 0 0 0
1064 0 ▶ if ($cfg_listen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg_listen) { } 0 0 0
1065 0 $listen .= $cfg_listen;
1066 } else {
1067 0 ▶ $listen .= sprintf("%s:%d",
Condition0%0/4
value($self->conf('listenaddr')) || '*'
l!l
00
&tv2int16(scalar value($self->conf('listenport'))) || 8642
l!l
00
MC/DC0%0/1
value($self->conf('listenaddr')) || '*'
value($self->conf('listenaddr'))
MC/DC0%0/1
&tv2int16(scalar value($self->conf('listenport'))) || 8642
&tv2int16(scalar value($self->conf('listenport')))
Truth table0%0/2
A
A= value($self->conf('listenaddr'))
Aresult
11
00
Truth table0%0/2
A
A= &tv2int16(scalar value($self->conf('listenport')))
Aresult
11
00
1068 value($self->conf("listenaddr")) || SERVER_LISTEN_ADDR,
1069 tv2int16(value($self->conf("listenport"))) || SERVER_LISTEN_PORT,
1070 );
1071 }
1072 0 ▶ my $_resolve_cf = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1073 0 my $f = shift;
1074 0 ▶ return $f if File::Spec->file_name_is_absolute($f);
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($f) 0 0 0
1075 0 return File::Spec->catfile($self->root, $f);
1076 0 };
1077 0 ▶ if ($tls_on) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tls_on) 0 0 0
1078 0 my @p = ();
1079 0 foreach my $k (qw/ciphers version/) {
1080 0 ▶ my $v = value($self->conf("tls_$k")) // '';
Condition0%0/2
value($self->conf("tls_$k")) // ""
l!l
00
MC/DC0%0/1
value($self->conf("tls_$k")) // ""
value($self->conf("tls_$k"))
Truth table0%0/2
A
A= value($self->conf("tls_$k"))
Aresult
11
00
1081 0 ▶ next unless length $v;
Branch: 1 branch
BranchTrueFalseTotal
unless length $v 0 0 0
1082 0 push @p, sprintf("%s=%s", $k, $v);
1083 }
1084 0 foreach my $k (qw/ca cert key/) {
1085 0 ▶ my $v = value($self->conf("tls_$k")) // '';
Condition0%0/2
value($self->conf("tls_$k")) // ""
l!l
00
MC/DC0%0/1
value($self->conf("tls_$k")) // ""
value($self->conf("tls_$k"))
Truth table0%0/2
A
A= value($self->conf("tls_$k"))
Aresult
11
00
1086 0 ▶ next unless length $v;
Branch: 1 branch
BranchTrueFalseTotal
unless length $v 0 0 0
1087 0 push @p, sprintf("%s=%s", $k, $_resolve_cf->($v));
1088 }
1089 0 ▶ push @p, sprintf("%s=%s", "verify", value($self->conf("tls_verify")) || '0x00')
Branch: 1 branch
BranchTrueFalseTotal
if value($self->conf("tls_verify")) 0 0 0
Condition0%0/2
value($self->conf('tls_verify')) || '0x00'
l!l
00
MC/DC0%0/1
value($self->conf('tls_verify')) || '0x00'
value($self->conf('tls_verify'))
Truth table0%0/2
A
A= value($self->conf('tls_verify'))
Aresult
11
00
1090 if value($self->conf("tls_verify"));
1091 0 $listen .= sprintf("?%s", join('&', @p));
1092 }
1093 0 $prefork->listen([$listen]);
1094  
1095 # Working with Dash k
1096 0 ▶ if ($dash_k eq 'start') {
Branch: 4 branches
BranchTrueFalseTotal
if ($dash_k eq 'start') { } 0 0 0
elsif ($dash_k eq 'stop') { } 0 0 0
elsif ($dash_k eq 'restart') { } 0 0 0
elsif ($dash_k eq 'reload') { } 0 0 0
1097 0 ▶ if (my $pid = $prefork->check_pid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = $prefork->check_pid) 0 0 0
1098 0 say "Already running $pid";
1099 0 return 1;
1100 }
1101 } elsif ($dash_k eq 'stop') {
1102 0 ▶ if (my $pid = $prefork->check_pid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = $prefork->check_pid) { } 0 0 0
1103 0 kill 'QUIT', $pid;
1104 0 say "Stopping $pid";
1105 } else {
1106 0 say "Not running";
1107 }
1108 0 return 1;
1109 } elsif ($dash_k eq 'restart') {
1110 0 ▶ if (my $pid = $prefork->check_pid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = $prefork->check_pid) 0 0 0
1111 0 ▶ $upgrade ||= steady_time;
Condition0%0/3
$upgrade ||= &steady_time()
l!l&&r!l&&!r
000
MC/DC0%0/2
$upgrade ||= &steady_time()
$upgrade &steady_time()
Truth table0%0/3
A ||= B
A= $upgradeB= &steady_time()
ABresult
1X1
011
000
1112 0 kill 'QUIT', $pid;
1113 0 my $up = $upgrade_timeout;
1114 0 ▶ while (kill 0, $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill 0, $pid) 0 0 0
1115 0 $up--;
1116 0 sleep 1;
1117 }
1118 0 ▶ die("Can't stop $pid") if $up <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $up <= 0 0 0 0
1119 0 say "Stopping $pid";
1120 0 $upgrade = 0;
1121 }
1122 } elsif ($dash_k eq 'reload') {
1123 0 my $pid = $prefork->check_pid();
1124 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
1125 # Start hot deployment
1126 0 kill 'USR2', $pid;
1127 0 say "Reloading $pid";
1128 0 return 1;
1129 }
1130 0 say "Not running";
1131 } else { # status
1132 0 ▶ if (my $pid = $prefork->check_pid()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = $prefork->check_pid) { } 0 0 0
1133 0 say "Running $pid";
1134 } else {
1135 0 say "Not running";
1136 }
1137 0 return 1;
1138 }
1139  
1140 #
1141 # LSB start
1142 #
1143  
1144 # This is a production server
1145 0 ▶ $ENV{MOJO_MODE} ||= 'production';
Condition0%0/2
$ENV{"MOJO_MODE"} ||= "production"
l!l
00
MC/DC0%0/1
$ENV{"MOJO_MODE"} ||= "production"
$ENV{"MOJO_MODE"}
Truth table0%0/2
A
A= $ENV{"MOJO_MODE"}
Aresult
11
00
1146  
1147 # Listen USR2 (reload)
1148 0 ▶ $SIG{USR2} = sub { $upgrade ||= steady_time };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
$upgrade ||= &steady_time()
l!l&&r!l&&!r
000
MC/DC0%0/2
$upgrade ||= &steady_time()
$upgrade &steady_time()
Truth table0%0/3
A ||= B
A= $upgradeB= &steady_time()
ABresult
1X1
011
000
1149  
1150 # Set hooks
1151 #$prefork->on(spawn => sub () { # Spawn (start worker)
1152 # my $self = shift; # Prefork object
1153 # my $pid = shift;
1154 # #say "Spawn (start) $pid";
1155 # $self->app->log->debug("Spawn (start) $pid");
1156 #});
1157 0 ▶ $prefork->on(wait => sub { # Manage (every 1 sec)
Subroutine __ANON__: not called CC=5 SCAR=34.0
1158 0 my $self = shift; # Prefork object
1159  
1160 # Upgrade
1161 0 ▶ if ($upgrade) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upgrade) 0 0 0
1162 #$self->app->log->debug(">>> " . $self->healthy() || '?');
1163 0 ▶ unless ($reload) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reload) 0 0 0
1164 0 $reload = 1; # Off next reloading
1165 0 ▶ if ($self->app->reload()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->app->reload) 0 0 0
1166 0 $reload = 0;
1167 0 $upgrade = 0;
1168 0 return;
1169 }
1170 }
1171  
1172 # Timeout
1173 0 ▶ if (($upgrade + $upgrade_timeout) <= steady_time()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upgrade + $upgrade_timeout <= &steady_time()) 0 0 0
1174 0 kill 'KILL', $$;
1175 0 $upgrade = 0;
1176 }
1177 }
1178 0 });
1179 #$prefork->on(reap => sub { # Cleanup (Emitted when a child process exited)
1180 # my $self = shift; # Prefork object
1181 # my $pid = shift;
1182 # #say "Reap (cleanup) $pid";
1183 # $self->app->log->debug("Reap (cleanup) $pid");
1184 #});
1185 0 ▶ $prefork->on(finish => sub { # Finish
Subroutine __ANON__: not called CC=2 SCAR=17.9
1186 0 my $self = shift; # Prefork object
1187 0 my $graceful = shift;
1188 0 ▶ $self->app->log->debug($graceful ? 'Graceful server shutdown' : 'Server shutdown');
Branch: 1 branch
BranchTrueFalseTotal
$graceful ? : 0 0 0
1189 0 });
1190  
1191 # Set GID and UID
1192 0 if (IS_ROOT) {
1193 if (defined($gid)) {
1194 POSIX::setgid($gid) || do {
1195 $self->error("setgid $gid failed - $!");
1196 return 0;
1197 };
1198 $) = "$gid $gid"; # this calls setgroups
1199 if (!($( eq "$gid $gid" && $) eq "$gid $gid")) { # just to be sure
1200 $self->error("detected strange gid");
1201 return 0;
1202 }
1203 }
1204 if (defined($uid)) {
1205 POSIX::setuid($uid) || do {
1206 $self->error("setuid $uid failed - $!");
1207 return 0;
1208 };
1209 if (!($< == $uid && $> == $uid)) { # just to be sure
1210 $self->error("detected strange uid");
1211 return 0;
1212 }
1213 }
1214 }
1215  
1216  
1217 # Daemonize
1218 0 ▶ $prefork->daemonize() unless $self->debugmode();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->debugmode 0 0 0
1219  
1220 # Running
1221 0 say "Running";
1222 0 $prefork->run();
1223  
1224 #my $fingerprint = $self->get_fingerprint;
1225 #say explain(\%exest);
1226 #my @strings = split("\n", $exest{output});
1227 #say explain(\@strings);
1228  
1229 0 return 1;
1230 1 });
1231  
1232 __PACKAGE__->register_handler(
1233 handler => "commit",
1234 description => "Send tarball to server (backup)",
1235 0 ▶ code => sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
1236 ### CODE:
1237 0 my ($self, $meta, @arguments) = @_;
1238 0 ▶ unless ($self->lconfig->is_loaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->lconfig->is_loaded) 0 0 0
1239 0 $self->error(ERROR_NO_TOKEN);
1240 0 return 0;
1241 }
1242  
1243 # Check client
1244 0 ▶ unless ($self->client->check) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->client->check) 0 0 0
1245 0 $self->error($self->client->error);
1246 0 $self->debug($self->client->trace);
1247 0 return 0;
1248 }
1249  
1250 # Get Fingerprint
1251 0 my $fingerprint = $self->get_fingerprint;
1252 0 ▶ unless ($fingerprint) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fingerprint) 0 0 0
1253 0 $self->error("No fingerprint specified");
1254 0 return 0;
1255 }
1256  
1257 # Get Manifest
1258 0 my $manifest = $self->get_manifest; # file=>full_path
1259  
1260 # Get file for tarball making
1261 0 ▶ my $tmp_dir = $self->debugmode ? File::Spec->catdir(File::Spec->tmpdir(), "mtoken") : $self->tempdir;
Branch: 1 branch
BranchTrueFalseTotal
$self->debugmode ? : 0 0 0
1262 0 my $tarball_name = dtf(TARBALL_FORMAT, time());
1263 0 ▶ my $tarball_arch_name = sprintf("%s.tgz", $tarball_name =~ m/(.+?)\.tkn/ ? $1 : $tarball_name);
Branch: 1 branch
BranchTrueFalseTotal
$tarball_name =~ /(.+?)\.tkn/ ? : 0 0 0
1264 0 my $tarball_path = path($tmp_dir, $tarball_name);
1265 0 my $tarball_arch_path = path($tmp_dir, $tarball_arch_name);
1266  
1267 # make_tarball
1268 0 my $curdir = path(getcwd())->to_abs->to_string;
1269 0 my $newdir = path($self->datadir)->to_abs->to_string;
1270 0 chdir $newdir;
1271 0 my $tar = Archive::Tar->new;
1272 0 $tar->add_files(keys(%$manifest));
1273 0 for my $f ($tar->get_files) {
1274 0 $f->mode($f->mode & ~022); # chmod go-w
1275 }
1276 0 $tar->write($tarball_arch_path->to_string, 1);
1277 0 chdir $curdir;
1278  
1279 # Encrypt file to tempdir
1280 0 my %exest = $self->execmd($self->get_gpgbin, "--encrypt", "--quiet", "--recipient", $fingerprint, "--output",
1281 $tarball_path->to_string, $tarball_arch_path->to_string);
1282 0 ▶ unless ($exest{status} && -f $tarball_path->to_string) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exest{"status"} and -f $tarball_path->to_string) 0 0 0
Condition0%0/3
$exest{"status"} and -f $tarball_path->to_string
!ll&&!rl&&r
000
MC/DC0%0/2
$exest{"status"} and -f $tarball_path->to_string
$exest{"status"} -f $tarball_path->to_string
Truth table0%0/3
A and B
A= $exest{"status"}B= -f $tarball_path->to_string
ABresult
0X0
100
111
1283 0 $self->error(sprintf("Can't encrypt file %s: %s", $tarball_arch_path->to_string, $exest{error}));
1284 0 return 0;
1285 }
1286 0 $tarball_arch_path->remove;
1287  
1288 # Upload (PUT method)
1289 0 my $status = $self->client->upload($self->get_name, $tarball_path->to_string); # "C20211009T090718.tkn"
1290 #say magenta($tarball_path->to_string);
1291 #say explain($self->client->req->content);
1292 #$self->debug($self->client->trace);
1293 #$self->debug($self->client->res->body);
1294 0 ▶ if ($status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) { } 0 0 0
1295 0 $tarball_path->remove;
1296 } else {
1297 0 $self->error($self->client->error);
1298 0 $self->debug($self->client->trace);
1299 0 return 0;
1300 }
1301  
1302 # Ok
1303 0 return yep("Done");
1304 1 });
1305  
1306 __PACKAGE__->register_handler(
1307 handler => "update",
1308 description => "Get tarball from server (restore)",
1309 0 ▶ code => sub {
Subroutine __ANON__: not called CC=29 SCAR=67.7
1310 ### CODE:
1311 0 my ($self, $meta, @arguments) = @_;
1312 0 ▶ unless ($self->lconfig->is_loaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->lconfig->is_loaded) 0 0 0
1313 0 $self->error(ERROR_NO_TOKEN);
1314 0 return 0;
1315 }
1316  
1317 # Check client & get filelist
1318 0 ▶ unless ($self->client->info($self->get_name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->client->info($self->get_name)) 0 0 0
1319 0 $self->error($self->client->error);
1320 0 $self->debug($self->client->trace);
1321 0 return 0;
1322 }
1323  
1324 # Get file for tarball
1325 0 my $tarball_name = shift @arguments;
1326 0 ▶ if ($tarball_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tarball_name) { } 0 0 0
1327 0 ▶ unless ($tarball_name =~ TARBALL_PATTERN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tarball_name =~ /^C[0-9]{8}T[0-9]{6}\.tkn$/) 0 0 0
1328 0 $self->error("Incorrect tarball name");
1329 0 return 0;
1330 }
1331 } else {
1332 0 my $files = array($self->client->res->json("/files"));
1333 0 my @tmp = sort {$a->{mtime} <=> $b->{mtime}} @$files;
1334 0 $tarball_name = value(pop(@tmp), "filename");
1335 0 ▶ unless ($tarball_name && $tarball_name =~ TARBALL_PATTERN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tarball_name and $tarball_name =~ /^C[0-9]{8}T[0-9]{6}\.tkn$/) 0 0 0
Condition0%0/3
$tarball_name and $tarball_name =~ /^C[0-9]{8}T[0-9]{6}\.tkn$/
!ll&&!rl&&r
000
MC/DC0%0/2
$tarball_name and $tarball_name =~ /^C[0-9]{8}T[0-9]{6}\.tkn$/
$tarball_name $tarball_name =~ /^C[0-9]{8}T[0-9]{6}\.tkn$/
Truth table0%0/3
A and B
A= $tarball_nameB= $tarball_name =~ /^C[0-9]{8}T[0-9]{6}\.tkn$/
ABresult
0X0
100
111
1336 0 $self->error("Tarball not found");
1337 0 return 0;
1338 }
1339 }
1340  
1341 # Get paths
1342 0 ▶ my $tmp_dir = $self->debugmode ? File::Spec->catdir(File::Spec->tmpdir(), "mtoken") : $self->tempdir;
Branch: 1 branch
BranchTrueFalseTotal
$self->debugmode ? : 0 0 0
1343 0 ▶ my $tarball_pfx = $tarball_name =~ m/(.+?)\.tkn/ ? $1 : $tarball_name;
Branch: 1 branch
BranchTrueFalseTotal
$tarball_name =~ /(.+?)\.tkn/ ? : 0 0 0
1344 0 my $tarball_path = path($tmp_dir, $tarball_name);
1345 0 my $archive_path = path($tmp_dir, sprintf("%s.tgz", $tarball_pfx));
1346 0 my $tarball_dir = path($tmp_dir, $tarball_pfx)->make_path;
1347  
1348 # Download file
1349 0 ▶ unless ($self->client->download($self->get_name => $tarball_path->to_string)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->client->download($self->get_name, $tarball_path->to_string)) 0 0 0
1350 0 $self->error($self->client->error);
1351 0 $self->debug($self->client->trace);
1352 0 return 0;
1353 }
1354  
1355 # Get Last_Modified from headers
1356 0 my $lm = $self->client->res->headers->last_modified;
1357 0 ▶ my $last_modified = $lm ? Mojo::Date->new($lm)->epoch : 0;
Branch: 1 branch
BranchTrueFalseTotal
$lm ? : 0 0 0
1358  
1359 # Check mtime
1360 0 ▶ if ($self->store->status) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->store->status) 0 0 0
1361 0 ▶ my $db_mtime = $self->store->file ? File::stat::stat($self->store->file)->mtime || 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->store->file ? : 0 0 0
Condition0%0/2
&File::stat::stat(scalar $self->store->file)->mtime or 0
l!l
00
MC/DC0%0/1
&File::stat::stat(scalar $self->store->file)->mtime or 0
&File::stat::stat(scalar $self->store->file)->mtime
Truth table0%0/2
A
A= &File::stat::stat(scalar $self->store->file)->mtime
Aresult
11
00
1362 0 ▶ if ($last_modified && $db_mtime && $db_mtime > $last_modified) { # Conflict
Branch: 1 branch
BranchTrueFalseTotal
if ($last_modified and $db_mtime and $db_mtime > $last_modified) 0 0 0
Condition0%0/6
$last_modified and $db_mtime and $db_mtime > $last_modified
!ll&&!rl&&r
000
$last_modified and $db_mtime
!ll&&!rl&&r
000
MC/DC0%0/3
$last_modified and $db_mtime and $db_mtime > $last_modified
$last_modified $db_mtime $db_mtime > $last_modified
Truth table0%0/4
A and B and C
A= $last_modifiedB= $db_mtimeC= $db_mtime > $last_modified
ABCresult
0XX0
10X0
1100
1111
1363 0 say yellow("%s: conflict detected", $tarball_name);
1364 0 say yellow(" Tarball created: %s", _fdate($last_modified));
1365 0 say yellow(" Token modified: %s", _fdate($db_mtime));
1366 0 say yellow("The current token was changed later than the one in the repository.");
1367 0 ▶ unless ($self->option("force") ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->option("force") or $self->cli_prompt("Are you sure you want to revert to an earlier state of the token?:", "no") =~ /^\s*y/i) 0 0 0
Condition0%0/3
$self->option("force") or $self->cli_prompt("Are you sure you want to revert to an earlier state of the token?:", "no") =~ /^\s*y/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->option("force") or $self->cli_prompt("Are you sure you want to revert to an earlier state of the token?:", "no") =~ /^\s*y/i
$self->option("force") $self->cli_prompt("Are you sure you want to revert to an earlier state of the token?:", "no") =~ /^\s*y/i
Truth table0%0/3
A or B
A= $self->option("force")B= $self->cli_prompt("Are you sure you want to revert to an earlier state of the token?:", "no") =~ /^\s*y/i
ABresult
1X1
011
000
1368 $self->cli_prompt('Are you sure you want to revert to an earlier state of the token?:','no') =~ /^\s*y/i) {
1369 0 return skip("Aborted");
1370 }
1371 }
1372 }
1373  
1374 # Decrypt file
1375 0 ▶ unless (-e $archive_path->to_string) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $archive_path->to_string) 0 0 0
1376 0 my %exest = $self->execmd($self->get_gpgbin, "--decrypt", "--quiet", "--output", $archive_path->to_string, $tarball_path->to_string);
1377 0 ▶ unless ($exest{status} && -e $archive_path->to_string) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exest{"status"} and -e $archive_path->to_string) 0 0 0
Condition0%0/3
$exest{"status"} and -e $archive_path->to_string
!ll&&!rl&&r
000
MC/DC0%0/2
$exest{"status"} and -e $archive_path->to_string
$exest{"status"} -e $archive_path->to_string
Truth table0%0/3
A and B
A= $exest{"status"}B= -e $archive_path->to_string
ABresult
0X0
100
111
1378 0 $self->error(sprintf("Can't decrypt file %s: %s", $tarball_path->to_string, $exest{error}));
1379 0 return 0;
1380 }
1381 0 $tarball_path->remove;
1382 }
1383  
1384 # Store to selected file or directory
1385 0 ▶ if ($self->option("output") || $self->option("outdir")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->option("output") or $self->option("outdir")) 0 0 0
Condition0%0/3
$self->option("output") or $self->option("outdir")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->option("output") or $self->option("outdir")
$self->option("output") $self->option("outdir")
Truth table0%0/3
A or B
A= $self->option("output")B= $self->option("outdir")
ABresult
1X1
011
000
1386 0 my $file_out = $self->option("output");
1387 0 ▶ $file_out = File::Spec->catfile($self->option("outdir"), sprintf("%s.tgz", $tarball_pfx))
Branch: 1 branch
BranchTrueFalseTotal
if not $file_out and -d $self->option("outdir") 0 0 0
Condition0%0/3
not $file_out and -d $self->option("outdir")
!ll&&!rl&&r
000
MC/DC0%0/2
not $file_out and -d $self->option("outdir")
not $file_out -d $self->option("outdir")
Truth table0%0/3
not(A) and B
A= not $file_outB= -d $self->option("outdir")
ABresult
0X0
100
111
1388 if !$file_out && -d $self->option("outdir");
1389 0 ▶ return skip("Incorrect output file %s. File already exists", $tarball_name) if -e $file_out;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file_out 0 0 0
1390 0 $archive_path->move_to($file_out);
1391 0 ▶ return nope("Can't download %s tarbal", $tarball_name) unless -f $file_out;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file_out 0 0 0
1392 0 return yep("Tarbal %s successfully downloaded as archive to %s", $tarball_name, $file_out);
1393 }
1394  
1395 # Extract files from archive
1396 0 my $tar = Archive::Tar->new;
1397 0 $tar->read($archive_path->to_abs->to_string);
1398 0 $tar->setcwd($archive_path->to_string);
1399 0 foreach my $file ($tar->list_files()) {
1400 0 $tar->extract_file($file, path($tarball_dir->to_string, $file)->to_string);
1401 }
1402 #say explain(\@files);
1403  
1404 # Install files
1405 find({
1406 no_chdir => 1,
1407 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1408 0 ▶ return if -d;
Branch: 1 branch
BranchTrueFalseTotal
if -d $_ 0 0 0
1409 0 my $src = path($_);
1410 0 my $dst = path($_)->to_rel($tarball_dir->to_string);
1411 #say blue("%s -> %s", $src->to_string, $dst->to_string);
1412 0 $src->move_to(File::Spec->catfile($self->datadir, $dst->to_string));
1413 0 }}, $tarball_dir->to_string);
1414  
1415 # Ok
1416 0 return yep("Done");
1417 1 });
1418  
1419 __PACKAGE__->register_handler(
1420 handler => "revoke",
1421 description => "Revoke tarball from server (delete)",
1422 0 ▶ code => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
1423 ### CODE:
1424 0 my ($self, $meta, @arguments) = @_;
1425 0 ▶ unless ($self->lconfig->is_loaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->lconfig->is_loaded) 0 0 0
1426 0 $self->error(ERROR_NO_TOKEN);
1427 0 return 0;
1428 }
1429  
1430 # Check client & get filelist
1431 0 ▶ unless ($self->client->info($self->get_name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->client->info($self->get_name)) 0 0 0
1432 0 $self->error($self->client->error);
1433 0 $self->debug($self->client->trace);
1434 0 return 0;
1435 }
1436  
1437 # Get file for tarball
1438 0 my $tarball_name = shift @arguments;
1439 0 ▶ if ($tarball_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tarball_name) { } 0 0 0
1440 0 ▶ unless ($tarball_name =~ TARBALL_PATTERN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tarball_name =~ /^C[0-9]{8}T[0-9]{6}\.tkn$/) 0 0 0
1441 0 $self->error("Incorrect tarball name");
1442 0 return 0;
1443 }
1444 } else {
1445 0 my $files = array($self->client->res->json("/files"));
1446 0 my @tmp = sort {$a->{mtime} <=> $b->{mtime}} @$files;
1447 0 $tarball_name = value(pop(@tmp), "filename");
1448 0 ▶ unless ($tarball_name && $tarball_name =~ TARBALL_PATTERN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tarball_name and $tarball_name =~ /^C[0-9]{8}T[0-9]{6}\.tkn$/) 0 0 0
Condition0%0/3
$tarball_name and $tarball_name =~ /^C[0-9]{8}T[0-9]{6}\.tkn$/
!ll&&!rl&&r
000
MC/DC0%0/2
$tarball_name and $tarball_name =~ /^C[0-9]{8}T[0-9]{6}\.tkn$/
$tarball_name $tarball_name =~ /^C[0-9]{8}T[0-9]{6}\.tkn$/
Truth table0%0/3
A and B
A= $tarball_nameB= $tarball_name =~ /^C[0-9]{8}T[0-9]{6}\.tkn$/
ABresult
0X0
100
111
1449 0 $self->error("Tarball not found");
1450 0 return 0;
1451 }
1452 }
1453  
1454 # Delete
1455 0 ▶ unless ($self->client->remove($self->get_name, $tarball_name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->client->remove($self->get_name, $tarball_name)) 0 0 0
1456 0 $self->error($self->client->error);
1457 0 $self->debug($self->client->trace);
1458 0 return 0;
1459 }
1460  
1461 # Ok
1462 0 return yep("Done");
1463 1 });
1464  
1465 __PACKAGE__->register_handler(
1466 handler => "clean",
1467 description => "Clean temporary files",
1468 0 ▶ code => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1469 ### CODE:
1470 0 my ($self, $meta, @arguments) = @_;
1471 0 ▶ unless ($self->lconfig->is_loaded) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->lconfig->is_loaded) 0 0 0
1472 0 $self->error(ERROR_NO_TOKEN);
1473 0 return 0;
1474 }
1475  
1476 # Temp directory
1477 0 my $tmp_dir = File::Spec->catdir(File::Spec->tmpdir(), "mtoken");
1478 find({
1479 no_chdir => 1,
1480 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1481 0 ▶ return if -d;
Branch: 1 branch
BranchTrueFalseTotal
if -d $_ 0 0 0
1482 0 ▶ return if /\.pid$/;
Branch: 1 branch
BranchTrueFalseTotal
if /\.pid$/ 0 0 0
1483 0 my $file = path($_);
1484  
1485 # Remove
1486 0 $file->remove;
1487 0 ▶ say magenta("Remove file %s", $file->to_string) if $self->verbosemode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbosemode 0 0 0
1488 0 }}, $tmp_dir);
1489  
1490 # Private directory
1491 0 my $priv_dir = File::Spec->catfile($self->datadir, DIR_PRIVATE);
1492 find({
1493 no_chdir => 1,
1494 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1495 0 ▶ return if -d;
Branch: 1 branch
BranchTrueFalseTotal
if -d $_ 0 0 0
1496 0 ▶ return unless /\.tmp$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /\.tmp$/ 0 0 0
1497 0 my $file = path($_);
1498  
1499 # Remove
1500 0 $file->remove;
1501 0 ▶ say magenta("Remove file %s", $file->to_string) if $self->verbosemode;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbosemode 0 0 0
1502 0 }}, $priv_dir);
1503  
1504 # Ok
1505 0 return yep("Done");
1506 1 });
1507  
1508 0 ▶sub again {
Subroutine again: not called CC=6 SCAR=37.4
Pod documented
1509 0 my $self = shift;
1510 0 $self->SUPER::again(); # CTK::App again first!!
1511  
1512 # Device & Local configuration
1513 0 $self->{lconfig} = MToken::Config->new(file => File::Spec->catfile($self->datadir, DIR_PRIVATE, DEVICE_CONF_FILE));
1514  
1515 # Store conf
1516 0 ▶ my $store_conf = $self->{lconfig}->get("store") || $self->config("store") || {};
Condition0%0/5
$self->{'lconfig'}->get('store') || $self->config('store') || {}
l!l
00
$self->{"lconfig"}->get("store") or $self->config("store")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'lconfig'}->get('store') || $self->config('store') || {}
$self->{"lconfig"}->get("store") $self->config("store")
Truth table0%0/3
A or B
A= $self->{"lconfig"}->get("store")B= $self->config("store")
ABresult
1X1
011
000
1517 0 ▶ $store_conf = {} unless is_hash($store_conf);
Branch: 1 branch
BranchTrueFalseTotal
unless is_hash($store_conf) 0 0 0
1518 0 $self->{store_conf} = {%$store_conf};
1519 0 $self->{store} = undef;
1520 #$self->debug(_explain($store));
1521  
1522 # Client instance
1523 0 ▶ $self->{client} = MToken::Client->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->lconfig->is_loaded ? : 0 0 0
$self->option('insecure') ? : 0 0 0
1524 url => $self->lconfig->is_loaded ? $self->get_server_url : undef,
1525 insecure => $self->option("insecure"),
1526 max_redirects => $self->conf("maxredirects"),
1527 connect_timeout => $self->conf("connecttimeout"),
1528 inactivity_timeout => $self->conf("inactivitytimeout"),
1529 request_timeout => $self->conf("requesttimeout"),
1530 pwcache => File::Spec->catfile($self->datadir, DIR_PRIVATE, PWCACHE_FILE),
1531 $self->option("insecure") ? (pwcache_ttl => 0) : (),
1532 );
1533  
1534 0 return $self; # CTK requires!
1535 }
1536 0 ▶sub raise {
Subroutine raise: not called CC=1 SCAR=6.9
Pod documented
1537 0 my $self = shift;
1538 0 say STDERR red(@_);
1539 0 return 0;
1540 }
1541 0 ▶sub store {
Subroutine store: not called CC=6 SCAR=37.4
Pod documented
1542 0 my $self = shift;
1543 0 my %store_args = (@_);
1544 0 ▶ if (is_void(\%store_args)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_void(\%store_args)) 0 0 0
1545 0 ▶ return $self->{store} if defined $self->{store}; # Already initialized
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"store"} 0 0 0
1546 0 my $sconf = $self->{store_conf};
1547 0 %store_args = %$sconf;
1548 0 ▶ $store_args{do_init} = 1 if $self->lconfig->is_loaded;
Branch: 1 branch
BranchTrueFalseTotal
if $self->lconfig->is_loaded 0 0 0
1549 }
1550  
1551 # Leazy initializing
1552 $store_args{file} = File::Spec->catfile($self->datadir, DIR_PRIVATE, DB_FILE)
1553 0 ▶ unless ($store_args{file} || $store_args{dsn});
Branch: 1 branch
BranchTrueFalseTotal
unless $store_args{"file"} or $store_args{"dsn"} 0 0 0
Condition0%0/3
$store_args{"file"} or $store_args{"dsn"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$store_args{"file"} or $store_args{"dsn"}
$store_args{"file"} $store_args{"dsn"}
Truth table0%0/3
A or B
A= $store_args{"file"}B= $store_args{"dsn"}
ABresult
1X1
011
000
1554 0 $self->{store} = MToken::Store->new(%store_args);
1555  
1556 0 return $self->{store};
1557 }
1558 0 ▶sub lconfig {
Subroutine lconfig: not called CC=1 SCAR=6.9
Pod documented
1559 0 my $self = shift;
1560 return $self->{lconfig}
1561 0 }
1562 0 ▶sub client {
Subroutine client: not called CC=1 SCAR=6.9
Pod documented
1563 0 my $self = shift;
1564 return $self->{client}
1565 0 }
1566 0 ▶sub execmd {
Subroutine execmd: not called CC=7 SCAR=40.3
Pod documented
1567 0 my $self = shift;
1568 0 my @cmd = (@_);
1569 0 my $scmd = join(" ", @cmd);
1570 0 my $error;
1571  
1572 # Run command
1573 0 my $exe_err = '';
1574 0 my $exe_out = CTK::Util::execute([@cmd], undef, \$exe_err);
1575 0 my $stt = $? >> 8;
1576 0 ▶ my $exe_stt = $stt ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$stt ? : 0 0 0
1577 0 ▶ chomp($exe_out) if defined($exe_out) && length($exe_out);
Branch: 1 branch
BranchTrueFalseTotal
if defined $exe_out and length $exe_out 0 0 0
Condition0%0/3
defined $exe_out and length $exe_out
!ll&&!rl&&r
000
MC/DC0%0/2
defined $exe_out and length $exe_out
defined $exe_out length $exe_out
Truth table0%0/3
A and B
A= defined $exe_outB= length $exe_out
ABresult
0X0
100
111
1578 0 ▶ if (!$exe_stt && $exe_err) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $exe_stt and $exe_err) { } 0 0 0
elsif ($stt) { } 0 0 0
Condition0%0/3
not $exe_stt and $exe_err
!ll&&!rl&&r
000
MC/DC0%0/2
not $exe_stt and $exe_err
not $exe_stt $exe_err
Truth table0%0/3
not(A) and B
A= not $exe_sttB= $exe_err
ABresult
0X0
100
111
1579 0 chomp($exe_err);
1580 0 say cyan("#", $scmd);
1581 0 $error = $exe_err;
1582 0 say STDERR red($error);
1583 } elsif ($stt) {
1584 0 say cyan("#", $scmd);
1585 0 $error = sprintf("Exitval=%d", $stt);
1586 0 say STDERR red($error);
1587 }
1588  
1589 return (
1590 0 command => $scmd,
1591 status => $exe_stt,
1592 exitval => $stt,
1593 error => $error,
1594 output => $exe_out,
1595 );
1596 }
1597 0 ▶sub get_name {
Subroutine get_name: not called CC=1 SCAR=6.9
Pod documented
1598 0 my $self = shift;
1599 0 $self->lconfig->{name};
1600 }
1601 0 ▶sub get_opensslbin {
Subroutine get_opensslbin: not called CC=4 SCAR=30.0
Pod documented
1602 0 my $self = shift;
1603 0 ▶ return $self->lconfig->get("opensslbin") || $self->conf("opensslbin") || which(OPENSSLBIN) || OPENSSLBIN;
Condition0%0/8
$self->lconfig->get('opensslbin') || $self->conf('opensslbin') || which('openssl') || 'openssl'
l!l
00
$self->lconfig->get("opensslbin") or $self->conf("opensslbin") or which("openssl")
l!l&&r!l&&!r
000
$self->lconfig->get("opensslbin") or $self->conf("opensslbin")
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->lconfig->get('opensslbin') || $self->conf('opensslbin') || which('openssl') || 'openssl'
$self->lconfig->get("opensslbin") $self->conf("opensslbin") which("openssl")
Truth table0%0/4
A or B or C
A= $self->lconfig->get("opensslbin")B= $self->conf("opensslbin")C= which("openssl")
ABCresult
1XX1
01X1
0011
0000
1604 }
1605 0 ▶sub get_gpgbin {
Subroutine get_gpgbin: not called CC=4 SCAR=30.0
Pod documented
1606 0 my $self = shift;
1607 0 ▶ return $self->lconfig->get("gpgbin") || $self->conf("gpgbin") || which(GPGBIN) || GPGBIN;
Condition0%0/8
$self->lconfig->get('gpgbin') || $self->conf('gpgbin') || which('gpg') || 'gpg'
l!l
00
$self->lconfig->get("gpgbin") or $self->conf("gpgbin") or which("gpg")
l!l&&r!l&&!r
000
$self->lconfig->get("gpgbin") or $self->conf("gpgbin")
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->lconfig->get('gpgbin') || $self->conf('gpgbin') || which('gpg') || 'gpg'
$self->lconfig->get("gpgbin") $self->conf("gpgbin") which("gpg")
Truth table0%0/4
A or B or C
A= $self->lconfig->get("gpgbin")B= $self->conf("gpgbin")C= which("gpg")
ABCresult
1XX1
01X1
0011
0000
1608 }
1609 0 ▶sub get_server_url {
Subroutine get_server_url: not called CC=3 SCAR=24.8
Pod documented
1610 0 my $self = shift;
1611 0 ▶ return $self->lconfig->get("server_url") || $self->conf("server_url") || SERVER_URL;
Condition0%0/5
$self->lconfig->get('server_url') || $self->conf('server_url') || 'https://localhost:8642/mtoken'
l!l
00
$self->lconfig->get("server_url") or $self->conf("server_url")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->lconfig->get('server_url') || $self->conf('server_url') || 'https://localhost:8642/mtoken'
$self->lconfig->get("server_url") $self->conf("server_url")
Truth table0%0/3
A or B
A= $self->lconfig->get("server_url")B= $self->conf("server_url")
ABresult
1X1
011
000
1612 }
1613 0 ▶sub get_fingerprint {
Subroutine get_fingerprint: not called CC=21 SCAR=61.4
Pod documented
1614 0 my $self = shift;
1615 0 ▶ my $fingerprint_cfg = $self->lconfig->get("fingerprint") || $self->conf("fingerprint") || "";
Condition0%0/5
$self->lconfig->get('fingerprint') || $self->conf('fingerprint') || ''
l!l
00
$self->lconfig->get("fingerprint") or $self->conf("fingerprint")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->lconfig->get('fingerprint') || $self->conf('fingerprint') || ''
$self->lconfig->get("fingerprint") $self->conf("fingerprint")
Truth table0%0/3
A or B
A= $self->lconfig->get("fingerprint")B= $self->conf("fingerprint")
ABresult
1X1
011
000
1616 0 my $fingerprint = "";
1617 0 my %exest = ();
1618  
1619 # Get public keys info
1620 0 ▶ unless ($self->option("force")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->option("force")) 0 0 0
1621 0 %exest = $self->execmd($self->get_gpgbin, "--list-keys");
1622 0 ▶ if ($exest{status}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exest{"status"}) 0 0 0
1623 0 ▶ say blue($exest{output} || "no keys found");
Condition0%0/2
$exest{'output'} || 'no keys found'
l!l
00
MC/DC0%0/1
$exest{'output'} || 'no keys found'
$exest{'output'}
Truth table0%0/2
A
A= $exest{'output'}
Aresult
11
00
1624 }
1625 }
1626  
1627 # Get public keys fingerprints
1628 0 %exest = $self->execmd($self->get_gpgbin, "--list-keys", "--with-colons");
1629 0 ▶ if ($exest{status} && $exest{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exest{'status'} and $exest{'output'}) { } 0 0 0
Condition0%0/3
$exest{"status"} and $exest{"output"}
!ll&&!rl&&r
000
MC/DC0%0/2
$exest{"status"} and $exest{"output"}
$exest{"status"} $exest{"output"}
Truth table0%0/3
A and B
A= $exest{"status"}B= $exest{"output"}
ABresult
0X0
100
111
1630 0 ▶ my @fingerprints = map {$_ = uc($1) if /\:([0-9a-f]{16,40})\:/i } grep { /fpr/ } split("\n", $exest{output});
Branch: 1 branch
BranchTrueFalseTotal
if /\:([0-9a-f]{16,40})\:/i 0 0 0
1631 0 ▶ my $fingerprint_default = $fingerprint_cfg || $fingerprints[0] || 'none';
Condition0%0/5
$fingerprint_cfg || $fingerprints[0] || 'none'
l!l
00
$fingerprint_cfg or $fingerprints[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$fingerprint_cfg || $fingerprints[0] || 'none'
$fingerprint_cfg $fingerprints[0]
Truth table0%0/3
A or B
A= $fingerprint_cfgB= $fingerprints[0]
ABresult
1X1
011
000
1632 0 while (1) {
1633 0 ▶ if ($self->option("force")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->option("force")) 0 0 0
1634 0 $fingerprint = $fingerprint_default;
1635 0 ▶ $fingerprint = "" if $fingerprint =~ /^\s*n/i;
Branch: 1 branch
BranchTrueFalseTotal
if $fingerprint =~ /^\s*n/i 0 0 0
1636 0 last;
1637 }
1638 0 $fingerprint = uc($self->cli_prompt('Please provide the fingerprint of recipient:', $fingerprint_default));
1639 0 ▶ unless (grep {$_ eq $fingerprint} @fingerprints) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $fingerprint;} @fingerprints) 0 0 0
1640 0 ▶ if ($fingerprint =~ /^\s*n/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fingerprint =~ /^\s*n/i) 0 0 0
1641 0 $fingerprint = "";
1642 0 last;
1643 }
1644 0 say yellow("Fingerprint not found! Type \"n\" to skip");
1645 0 next;
1646 }
1647 0 last;
1648 }
1649 } else {
1650 0 ▶ $fingerprint = $self->option("force")
Branch: 1 branch
BranchTrueFalseTotal
$self->option('force') ? : 0 0 0
Condition0%0/4
$fingerprint_cfg or "none"
l!l
00
$fingerprint_cfg || 'none'
l!l
00
MC/DC0%0/1
$fingerprint_cfg or "none"
$fingerprint_cfg
MC/DC0%0/1
$fingerprint_cfg || 'none'
$fingerprint_cfg
Truth table0%0/2
A
A= $fingerprint_cfg
Aresult
11
00
Truth table0%0/2
A
A= $fingerprint_cfg
Aresult
11
00
1651 ? $fingerprint_cfg || "none"
1652 : uc($self->cli_prompt('Please provide the fingerprint of recipient:', $fingerprint_cfg || "none"));
1653 0 ▶ if ($fingerprint =~ /^\s*n/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fingerprint =~ /^\s*n/i) { } 0 0 0
elsif (not _fingerprint_check($fingerprint)) { } 0 0 0
1654 0 $fingerprint = "";
1655 } elsif (!_fingerprint_check($fingerprint)) {
1656 0 say yellow("Fingerprint is incorrect!");
1657 0 $fingerprint = "";
1658 }
1659 }
1660 0 ▶ say cyan("Fingerprint: %s", $fingerprint) if $fingerprint;
Branch: 1 branch
BranchTrueFalseTotal
if $fingerprint 0 0 0
1661  
1662 0 return $fingerprint;
1663 }
1664 0 ▶sub get_manifest {
Subroutine get_manifest: not called CC=4 SCAR=30.0
Pod documented
1665 0 my $self = shift;
1666 0 my $manifile = File::Spec->catfile($self->datadir, DIR_PRIVATE, DEVICE_MANIFEST_FILE);
1667 0 ▶ return {} unless -e $manifile;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $manifile 0 0 0
1668 0 my $manifest = maniread($manifile);
1669 0 my $dir = path($self->datadir)->to_abs->to_string;
1670 0 ▶ while (my ($k, $v) = each %$manifest) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$manifest) 0 0 0
1671 0 $manifest->{$k} = path($dir, $k)->to_string;
1672 0 ▶ delete $manifest->{$k} unless -e $manifest->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $manifest->{$k} 0 0 0
1673 }
1674 0 return $manifest;
1675 }
1676  
1677  
1678 0 ▶sub _get_default_url {
Subroutine _get_default_url: not called CC=2 SCAR=17.9
1679 0 ▶ my $name = shift || PROJECTNAMEL;
Condition0%0/2
shift() || 'mtoken'
l!l
00
MC/DC0%0/1
shift() || 'mtoken'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1680 0 my $uri = URI->new( DEFAULT_URL );
1681 0 $uri->scheme('https');
1682 0 $uri->host(HOSTNAME);
1683 0 $uri->port(SERVER_LISTEN_PORT);
1684 #$uri->path(join("/", "mtoken", $name)); # Disabled!
1685 0 return $uri->canonical->as_string;
1686 }
1687 0 ▶sub _hashmd5 {
Subroutine _hashmd5: not called CC=3 SCAR=24.8
1688 0 my %h = @_ ;
1689 0 my $s = "";
1690 0 foreach my $k (sort {$a cmp $b} (keys(%h))) { $s .= uv2null($h{$k}) }
1691 0 ▶ return "" unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 0 0
1692 0 return md5_hex($s);
1693 }
1694 0 ▶sub _expand_wildcards {
Subroutine _expand_wildcards: not called CC=2 SCAR=17.9
1695 0 my @files = (@_);
1696 # Original in package ExtUtils::Command
1697 0 ▶ @files = map(/[*?]/o ? glob($_) : $_, @files);
Branch: 1 branch
BranchTrueFalseTotal
/[*?]/o ? : 0 0 0
1698 0 return (@files);
1699 }
1700 0 ▶sub _fingerprint_check {
Subroutine _fingerprint_check: not called CC=5 SCAR=34.0
1701 0 ▶ my $fpr = shift || '';
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1702 0 my $l = length($fpr);
1703 0 ▶ return 0 unless $l == 40 or $l == 16; # Fingerprint or KeyID
Branch: 1 branch
BranchTrueFalseTotal
unless $l == 40 or $l == 16 0 0 0
Condition0%0/3
$l == 40 or $l == 16
l!l&&r!l&&!r
000
MC/DC0%0/2
$l == 40 or $l == 16
$l == 40 $l == 16
Truth table0%0/3
A or B
A= $l == 40B= $l == 16
ABresult
1X1
011
000
1704 0 ▶ return 1 if $fpr =~ /^[0-9a-f]+$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $fpr =~ /^[0-9a-f]+$/i 0 0 0
1705 0 return 0;
1706 }
1707 0 ▶sub _fbytes {
Subroutine _fbytes: not called CC=4 SCAR=30.0
1708 0 my $n = int(shift);
1709 0 ▶ if ($n >= 1024 ** 3) {
Branch: 3 branches
BranchTrueFalseTotal
if ($n >= 1073741824.0) { } 0 0 0
elsif ($n >= 1048576.0) { } 0 0 0
elsif ($n >= 1024) { } 0 0 0
1710 0 return sprintf "%.3g GB", $n / (1024 ** 3);
1711 } elsif ($n >= 1024 ** 2) {
1712 0 return sprintf "%.3g MB", $n / (1024.0 * 1024);
1713 } elsif ($n >= 1024) {
1714 0 return sprintf "%.3g KB", $n / 1024.0;
1715 } else {
1716 0 return "$n B";
1717 }
1718 }
1719 0 ▶sub _fdate {
Subroutine _fdate: not called CC=5 SCAR=34.0
1720 0 ▶ my $d = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1721 0 ▶ my $g = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1722 0 ▶ return "unknown" unless $d;
Branch: 1 branch
BranchTrueFalseTotal
unless $d 0 0 0
1723 0 ▶ return dtf(DATETIME_GMT_FORMAT, $d, 1) if $g;
Branch: 1 branch
BranchTrueFalseTotal
if $g 0 0 0
1724 0 return dtf(DATETIME_FORMAT . " " . tz_diff(), $d);
1725 }
1726  
1727 1 1;
1728  
1729 __END__
↑ Summary MToken/Client.pm »