File Coverage

blib/lib/Protocol/DBus/Authn/Mechanism/DBUS_COOKIE_SHA1/Pieces.pm
Criterion Covered Total %
statement 31 34 91.1
branch 7 10 70.0
condition 2 3 66.6
subroutine 8 8 100.0
pod 0 3 0.0
total 48 58 82.7


line stmt bran cond sub pod time code
1             package Protocol::DBus::Authn::Mechanism::DBUS_COOKIE_SHA1::Pieces;
2              
3 5     5   698966 use strict;
  5         32  
  5         149  
4 5     5   54 use warnings;
  5         18  
  5         146  
5              
6 5     5   29 use File::Spec ();
  5         11  
  5         89  
7              
8 5     5   26 use constant KEYRINGS_DIR => '.dbus-keyrings';
  5         6  
  5         2106  
9              
10             my $sha1_module;
11              
12             sub _sha1_module {
13 3   66 3   23 return $sha1_module ||= do {
14 2 50       6 if ( eval { require Digest::SHA1; 1 } ) {
  2 50       337  
  0         0  
15 0         0 'Digest::SHA1';
16             }
17 2         504 elsif ( eval { require Digest::SHA; 1 } ) {
  2         3043  
18 2         69 'Digest::SHA';
19             }
20             else {
21 0         0 die "No SHA module available!";
22             }
23             };
24             }
25              
26             sub create_challenge {
27 1     1 0 957 my $cl_challenge = join(',', map { rand } 1 .. 4 );
  4         57  
28              
29             # Ensure that we use only hex characters for the challenge,
30             # or else the challenge might have a colon, space, or something else
31             # problematic.
32 1         5 return sha1_hex($cl_challenge);
33             }
34              
35             sub sha1_hex {
36 3     3 0 16 return _sha1_module()->can('sha1_hex')->(@_);
37             }
38              
39             sub get_cookie {
40 5     5 0 10500 my ($homedir, $ck_ctx, $ck_id) = @_;
41              
42 5         71 my $path = File::Spec->catfile(
43             $homedir,
44             KEYRINGS_DIR(),
45             $ck_ctx,
46             );
47              
48 5 100       221 open my $rfh, '<', $path or die "open(< $path): $!";
49              
50 4         92 while ( my $line = <$rfh> ) {
51 6         18 chomp $line;
52              
53 6 100       44 next if 0 != index( $line, "$ck_id " );
54              
55 3         76 return substr( $line, 1 + index($line, q< >, 2 + length($ck_id)) );
56             }
57              
58 1 50       13 warn "readline: $!" if $!;
59              
60 1         28 die "Failed to find cookie “$ck_id” in “$path”!";
61             }
62              
63             1;