Brackup/Decrypt.pm

filter: Statementstmt 40.3% 21 / 52 Branchbran 8.3% 2 / 24 Conditioncond 7.6% 1 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 75.0% 6 / 8 Podpod 0.0% 0 / 4 totaltotal 27.7% 30 / 108 CC 18 SCAR 39.0
CC
18
Cov
27%
decrypt_file
110.0
write_to_file
20.0
decrypt_data
9.6
CRAP 49.5
« Brackup/Mount.pm ↑ Summary Brackup/Target/Filesystem.pm »
1 package Brackup::Decrypt;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Brackup::Util qw(slurp tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Decrypt a dataref into a dataref
9 45 ▶sub decrypt_data {
Subroutine decrypt_data: called CC=4 SCAR=22.6
Pod undocumented
10 45 my ($dataref, %opts) = @_;
11  
12 45 my $meta = $opts{meta};
13  
14 # do nothing if the data is not encrypted
15 45 ▶ return $dataref unless $meta && $meta->{"GPG-Recipient"};
Branch: 1 branch
BranchTrueFalseTotal
unless $meta and $meta->{"GPG-Recipient"} 45 0 45
Condition33%1/3
$meta and $meta->{"GPG-Recipient"}
!ll&&!rl&&r
0450
MC/DC0%0/2
$meta and $meta->{"GPG-Recipient"}
$meta $meta->{"GPG-Recipient"}
Truth table33%1/3
A and B
A= $metaB= $meta->{"GPG-Recipient"}
ABresult
0X0
100
111
16  
17 0 ▶ my $dataref_temp = ( (tempfile())[1] || die );
Condition0%0/2
(tempfile())[1] || die()
l!l
00
MC/DC0%0/1
(tempfile())[1] || die()
(tempfile())[1]
Truth table0%0/2
A
A= (tempfile())[1]
Aresult
11
00
18 0 write_to_file($dataref_temp, $dataref);
19  
20 0 my $decrypted_temp = decrypt_file($dataref_temp,%opts);
21 0 unlink($dataref_temp);
22  
23 0 my $data = slurp($decrypted_temp);
24 0 unlink($decrypted_temp);
25  
26 0 return \$data;
27 }
28  
29 0 ▶sub write_to_file {
Subroutine write_to_file: not called CC=4 SCAR=30.0
Pod undocumented
30 0 my ($file, $ref) = @_;
31 0 ▶ open (my $fh, '>', $file) or die "Failed to open $file for writing: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Failed to open $file for writing: $!\n" 0 0 0
32 0 print $fh $$ref;
33 0 ▶ close($fh) or die;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die 0 0 0
34 0 ▶ die "File is not of the correct size" unless -s $file == length $$ref;
Branch: 1 branch
BranchTrueFalseTotal
unless -s $file == length $$ref 0 0 0
35 0 return 1;
36 }
37  
38 9 ▶sub decrypt_file_if_needed {
Subroutine decrypt_file_if_needed: called CC=3 SCAR=15.7
Pod undocumented
39 9 my ($filename) = @_;
40  
41 9 my $meta = slurp($filename, decompress => 1);
42 9 ▶ if ($meta =~ /[\x00-\x08]/) { # silly is-binary heuristic
Branch: 1 branch
BranchTrueFalseTotal
if ($meta =~ /[\x00-\x08]/) 0 9 9
43 0 my $new_file = decrypt_file($filename,no_batch => 1);
44 0 ▶ if (defined $new_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_file) 0 0 0
45 0 warn "Decrypted ${filename} to ${new_file}.\n";
46 }
47 0 return $new_file;
48 }
49 9 return undef;
50 }
51  
52 # Decrypt a file into a new file
53 # Return the new file's name, or undef.
54  
55 13 our $warned_about_gpg_agent = 0;
56  
57 0 ▶sub decrypt_file {
Subroutine decrypt_file: not called CC=10 SCAR=47.0
Pod undocumented
58 0 my ($encrypted_file,%opts) = @_;
59  
60 0 my $no_batch = delete $opts{no_batch};
61 0 my $meta = delete $opts{meta};
62 0 ▶ croak("Unknown options: " . join(', ', keys %opts)) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
63  
64 # find which key we're using to decrypt it
65 0 ▶ if ($meta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta) 0 0 0
66 0 ▶ my $rcpt = $meta->{"GPG-Recipient"} or
Branch: 1 branch
BranchTrueFalseTotal
my $rcpt = $meta->{"GPG-Recipient"} or return undef 0 0 0
67 return undef;
68 }
69  
70 0 ▶ unless ($ENV{'GPG_AGENT_INFO'} ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"GPG_AGENT_INFO"} or @Brackup::GPG_ARGS or $Brackup::Decrypt::warned_about_gpg_agent++) 0 0 0
Condition0%0/6
$ENV{"GPG_AGENT_INFO"} or @Brackup::GPG_ARGS or $Brackup::Decrypt::warned_about_gpg_agent++
l!l&&r!l&&!r
000
$ENV{"GPG_AGENT_INFO"} or @Brackup::GPG_ARGS
l!l&&r!l&&!r
000
MC/DC0%0/3
$ENV{"GPG_AGENT_INFO"} or @Brackup::GPG_ARGS or $Brackup::Decrypt::warned_about_gpg_agent++
$ENV{"GPG_AGENT_INFO"} @Brackup::GPG_ARGS $Brackup::Decrypt::warned_about_gpg_agent++
Truth table0%0/4
A or B or C
A= $ENV{"GPG_AGENT_INFO"}B= @Brackup::GPG_ARGSC= $Brackup::Decrypt::warned_about_gpg_agent++
ABCresult
1XX1
01X1
0011
0000
71 @Brackup::GPG_ARGS ||
72 $warned_about_gpg_agent++)
73 {
74 0 my $err = q{
75 #
76 # WARNING: trying to restore encrypted files,
77 # but $ENV{'GPG_AGENT_INFO'} not present.
78 # Are you running gpg-agent?
79 #
80 };
81 0 $err =~ s/^\s+//gm;
82 0 warn $err;
83 }
84  
85 0 ▶ my $output_temp = ( (tempfile())[1] || die );
Condition0%0/2
(tempfile())[1] || die()
l!l
00
MC/DC0%0/1
(tempfile())[1] || die()
(tempfile())[1]
Truth table0%0/2
A
A= (tempfile())[1]
Aresult
11
00
86  
87 my @list = ("gpg", @Brackup::GPG_ARGS,
88 "--use-agent",
89 0 ▶ !$opts{no_batch} ? ("--batch") : (),
Branch: 1 branch
BranchTrueFalseTotal
!$opts{'no_batch'} ? : 0 0 0
90 "--trust-model=always",
91 "--output", $output_temp,
92 "--yes", "--quiet",
93 "--decrypt", $encrypted_file);
94 0 ▶ system(@list)
Branch: 1 branch
BranchTrueFalseTotal
system @list and die "Failed to decrypt with gpg: $!\n" 0 0 0
95 and die "Failed to decrypt with gpg: $!\n";
96  
97 0 return $output_temp;
98 }
99  
100 13 1;
« Brackup/Mount.pm ↑ Summary Brackup/Target/Filesystem.pm »