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 4     4   518924 use strict;
  4         21  
  4         84  
4 4     4   16 use warnings;
  4         8  
  4         71  
5              
6 4     4   13 use File::Spec ();
  4         8  
  4         58  
7              
8 4     4   14 use constant KEYRINGS_DIR => '.dbus-keyrings';
  4         7  
  4         1303  
9              
10             my $sha1_module;
11              
12             sub _sha1_module {
13 3   66 3   19 return $sha1_module ||= do {
14 2 50       5 if ( eval { require Digest::SHA1; 1 } ) {
  2 50       245  
  0         0  
15 0         0 'Digest::SHA1';
16             }
17 2         427 elsif ( eval { require Digest::SHA; 1 } ) {
  2         2305  
18 2         51 '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 844 my $cl_challenge = join(',', map { rand } 1 .. 4 );
  4         39  
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 10 return _sha1_module()->can('sha1_hex')->(@_);
37             }
38              
39             sub get_cookie {
40 5     5 0 7872 my ($homedir, $ck_ctx, $ck_id) = @_;
41              
42 5         60 my $path = File::Spec->catfile(
43             $homedir,
44             KEYRINGS_DIR(),
45             $ck_ctx,
46             );
47              
48 5 100       180 open my $rfh, '<', $path or die "open(< $path): $!";
49              
50 4         85 while ( my $line = <$rfh> ) {
51 6         16 chomp $line;
52              
53 6 100       34 next if 0 != index( $line, "$ck_id " );
54              
55 3         68 return substr( $line, 1 + index($line, q< >, 2 + length($ck_id)) );
56             }
57              
58 1 50       13 warn "readline: $!" if $!;
59              
60 1         45 die "Failed to find cookie “$ck_id” in “$path”!";
61             }
62              
63             1;