Netrc.pm

filter: Statementstmt 88.0% 88 / 100 Branchbran 75.0% 51 / 68 Conditioncond 35.7% 10 / 28 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 100% 11 / 11 Podpod 100% 5 / 5 totaltotal 73.6% 168 / 228 CC 46 SCAR 37.9
CC
46
Cov
76%
_readrc
69.6
lookup
10.5
login
2.0
CRAP 44.4
« POP3.pm ↑ Summary NNTP.pm »
1 # Net::Netrc.pm
2 #
3 # Copyright (C) 1995-1998 Graham Barr. All rights reserved.
4 # Copyright (C) 2013-2014, 2020 Steve Hay. All rights reserved.
5 # This module is free software; you can redistribute it and/or modify it under
6 # the same terms as Perl itself, i.e. under the terms of either the GNU General
7 # Public License or the Artistic License, as specified in the F<LICENCE> file.
8  
9 package Net::Netrc;
10  
11 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 our $VERSION = "3.16";
20  
21 2 our $TESTING;
22  
23 2 my %netrc = ();
24  
25 3 ▶sub _readrc {
Subroutine _readrc: called CC=34 SCAR=42.4
26 3 my($class, $host) = @_;
27 3 my ($home, $file);
28  
29 3 ▶ if ($^O eq "MacOS") {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MacOS') { } 0 3 3
30 0 ▶ $home = $ENV{HOME} || `pwd`;
Condition0%0/3
$ENV{'HOME'} || `pwd`
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'HOME'} || `pwd`
$ENV{'HOME'} `pwd`
Truth table0%0/3
A || B
A= $ENV{'HOME'}B= `pwd`
ABresult
1X1
011
000
31 0 chomp($home);
32 0 ▶ $file = ($home =~ /:$/ ? $home . "netrc" : $home . ":netrc");
Branch: 1 branch
BranchTrueFalseTotal
$home =~ /:$/ ? : 0 0 0
33 }
34 else {
35  
36 # Some OS's don't have "getpwuid", so we default to $ENV{HOME}
37 3 ▶ $home = eval { (getpwuid($>))[7] } || $ENV{HOME};
Condition33%1/3
eval { do { (getpwuid($>))[7] } } || $ENV{'HOME'}
l!l&&r!l&&!r
300
MC/DC0%0/2
eval { do { (getpwuid($>))[7] } } || $ENV{'HOME'}
eval { do { (getpwuid($>))[7] } } $ENV{'HOME'}
Truth table33%1/3
A || B
A= eval { do { (getpwuid($>))[7] } }B= $ENV{'HOME'}
ABresult
1X1
011
000
38 3 ▶ $home ||= $ENV{HOMEDRIVE} . ($ENV{HOMEPATH} || '') if defined $ENV{HOMEDRIVE};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"HOMEDRIVE"} 0 3 3
Condition0%0/5
$home ||= $ENV{"HOMEDRIVE"} . ($ENV{"HOMEPATH"} || "")
l!l&&r!l&&!r
000
$ENV{'HOMEPATH'} || ''
l!l
00
MC/DC0%0/2
$home ||= $ENV{"HOMEDRIVE"} . ($ENV{"HOMEPATH"} || "")
$home $ENV{"HOMEDRIVE"} . ($ENV{"HOMEPATH"} || "")
MC/DC0%0/1
$ENV{'HOMEPATH'} || ''
$ENV{'HOMEPATH'}
Truth table0%0/3
A ||= B
A= $homeB= $ENV{"HOMEDRIVE"} . ($ENV{"HOMEPATH"} || "")
ABresult
1X1
011
000
Truth table0%0/2
A
A= $ENV{'HOMEPATH'}
Aresult
11
00
39 3 ▶ if (-e $home . "/.netrc") {
Branch: 2 branches
BranchTrueFalseTotal
if (-e $home . '/.netrc') { } 0 3 3
elsif (-e $home . '/_netrc') { } 0 3 3
40 0 $file = $home . "/.netrc";
41 }
42 elsif (-e $home . "/_netrc") {
43 0 $file = $home . "/_netrc";
44 }
45 else {
46 3 ▶ return unless $TESTING;
Branch: 1 branch
BranchTrueFalseTotal
unless $Net::Netrc::TESTING 0 3 3
47 }
48 }
49  
50 3 my ($login, $pass, $acct) = (undef, undef, undef);
51 3 my $fh;
52 3 local $_;
53  
54 3 $netrc{default} = undef;
55  
56 # OS/2 and Win32 do not handle stat in a way compatible with this check :-(
57 3 ▶ unless ($^O eq 'os2'
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O eq "os2" or $^O eq "MSWin32" or $^O eq "MacOS" or $^O =~ /^cygwin/) 3 0 3
Condition33%3/9
$^O eq "os2" or $^O eq "MSWin32" or $^O eq "MacOS" or $^O =~ /^cygwin/
l!l&&r!l&&!r
003
$^O eq "os2" or $^O eq "MSWin32" or $^O eq "MacOS"
l!l&&r!l&&!r
003
$^O eq "os2" or $^O eq "MSWin32"
l!l&&r!l&&!r
003
MC/DC0%0/4
$^O eq "os2" or $^O eq "MSWin32" or $^O eq "MacOS" or $^O =~ /^cygwin/
$^O eq "os2" $^O eq "MSWin32" $^O eq "MacOS" $^O =~ /^cygwin/
Truth table20%1/5
A or B or C or D
A= $^O eq "os2"B= $^O eq "MSWin32"C= $^O eq "MacOS"D= $^O =~ /^cygwin/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
58 || $^O eq 'MSWin32'
59 || $^O eq 'MacOS'
60 || $^O =~ /^cygwin/)
61 {
62 3 my @stat = stat($file);
63  
64 3 ▶ if (@stat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stat) 3 0 3
65 3 ▶ if ($stat[2] & 077) { ## no critic (ValuesAndExpressions::ProhibitLeadingZeros)
Branch: 1 branch
BranchTrueFalseTotal
if ($stat[2] & 63) 1 2 3
66 1 carp "Bad permissions: $file";
67 1 return;
68 }
69 2 ▶ if ($stat[4] != $<) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat[4] != $<) 1 1 2
70 1 carp "Not owner: $file";
71 1 return;
72 }
73 }
74 }
75  
76 1 ▶ if ($fh = FileHandle->new($file, "r")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh = "FileHandle"->new($file, "r")) 1 0 1
77 1 my ($mach, $macdef, $tok, @tok) = (0, 0);
78  
79 1 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 10 1 11
80 10 ▶ undef $macdef if /\A\n\Z/;
Branch: 1 branch
BranchTrueFalseTotal
if /\A\n\Z/ 0 10 10
81  
82 10 ▶ if ($macdef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($macdef) 0 10 10
83 0 push(@$macdef, $_);
84 0 next;
85 }
86  
87 10 s/^\s*//;
88 10 chomp;
89  
90 10 ▶ while (length && s/^("((?:[^"]+|\\.)*)"|((?:[^\\\s]+|\\.)*))\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ and s/^("((?:[^"]+|\\.)*)"|((?:[^\\\s]+|\\.)*))\s*//) 29 10 39
Condition66%2/3
length $_ and s/^("((?:[^"]+|\\.)*)"|((?:[^\\\s]+|\\.)*))\s*//
!ll&&!rl&&r
10029
MC/DC50%1/2
length $_ and s/^("((?:[^"]+|\\.)*)"|((?:[^\\\s]+|\\.)*))\s*//
length $_ s/^("((?:[^"]+|\\.)*)"|((?:[^\\\s]+|\\.)*))\s*//
Truth table66%2/3
A and B
A= length $_B= s/^("((?:[^"]+|\\.)*)"|((?:[^\\\s]+|\\.)*))\s*//
ABresult
0X0
100
111
91 29 (my $tok = $+) =~ s/\\(.)/$1/g;
92 29 push(@tok, $tok);
93 }
94  
95 TOKEN:
96 10 ▶ while (@tok) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tok) 20 7 27
97 20 ▶ if ($tok[0] eq "default") {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok[0] eq "default") 2 18 20
98 2 shift(@tok);
99 2 $mach = bless {}, $class;
100 2 $netrc{default} = [$mach];
101  
102 2 next TOKEN;
103 }
104  
105 last TOKEN
106 18 ▶ unless @tok > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @tok > 1 3 15 18
107  
108 15 $tok = shift(@tok);
109  
110 15 ▶ if ($tok eq "machine") {
Branch: 3 branches
BranchTrueFalseTotal
if ($tok eq 'machine') { } 2 13 15
elsif ($tok =~ /^(login|password|account)$/) { } 10 3 13
elsif ($tok eq 'macdef') { } 1 2 3
111 2 my $host = shift @tok;
112 2 $mach = bless {machine => $host}, $class;
113  
114 $netrc{$host} = []
115 2 ▶ unless exists($netrc{$host});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $netrc{$host} 1 1 2
116 2 push(@{$netrc{$host}}, $mach);
117 }
118 elsif ($tok =~ /^(login|password|account)$/) {
119 10 ▶ next TOKEN unless $mach;
Branch: 1 branch
BranchTrueFalseTotal
unless $mach 1 9 10
120 9 my $value = shift @tok;
121  
122 # Following line added by rmerrell to remove '/' escape char in .netrc
123 9 $value =~ s/\/\\/\\/g;
124 9 $mach->{$1} = $value;
125 }
126 elsif ($tok eq "macdef") {
127 1 ▶ next TOKEN unless $mach;
Branch: 1 branch
BranchTrueFalseTotal
unless $mach 1 0 1
128 0 my $value = shift @tok;
129 $mach->{macdef} = {}
130 0 ▶ unless exists $mach->{macdef};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $mach->{"macdef"} 0 0 0
131 0 $macdef = $mach->{machdef}{$value} = [];
132 }
133 }
134 }
135 1 $fh->close();
136 }
137 }
138  
139  
140 5 ▶sub lookup {
Subroutine lookup: called CC=10 SCAR=23.5
Pod documented
141 5 my ($class, $mach, $login) = @_;
142  
143 $class->_readrc()
144 5 ▶ unless exists $netrc{default};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $netrc{"default"} 0 5 5
145  
146 5 ▶ $mach ||= 'default';
Condition100%2/2
$mach ||= "default"
l!l
41
MC/DC100%1/1
$mach ||= "default"
$mach
Truth table100%2/2
A
A= $mach
Aresult
11
00
147 5 ▶ undef $login
Branch: 1 branch
BranchTrueFalseTotal
if $mach eq "default" 2 3 5
148 if $mach eq 'default';
149  
150 5 ▶ if (exists $netrc{$mach}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $netrc{$mach}) 4 1 5
151 4 ▶ if (defined $login) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $login) 1 3 4
152 1 foreach my $m (@{$netrc{$mach}}) {
153 return $m
154 2 ▶ if (exists $m->{login} && $m->{login} eq $login);
Branch: 1 branch
BranchTrueFalseTotal
if exists $m->{"login"} and $m->{"login"} eq $login 1 1 2
Condition66%2/3
exists $m->{"login"} and $m->{"login"} eq $login
!ll&&!rl&&r
011
MC/DC50%1/2
exists $m->{"login"} and $m->{"login"} eq $login
exists $m->{"login"} $m->{"login"} eq $login
Truth table66%2/3
A and B
A= exists $m->{"login"}B= $m->{"login"} eq $login
ABresult
0X0
100
111
155 }
156 0 return;
157 }
158 3 return $netrc{$mach}->[0];
159 }
160  
161 return $netrc{default}->[0]
162 1 ▶ if defined $netrc{default};
Branch: 1 branch
BranchTrueFalseTotal
if defined $netrc{"default"} 1 0 1
163  
164 0 return;
165 }
166  
167  
168 4 ▶sub login {
Subroutine login: called CC=2 SCAR=6.9
Pod documented
169 4 my $me = shift;
170  
171 exists $me->{login}
172 ? $me->{login}
173 4 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $me->{'login'} ? : 3 1 4
174 }
175  
176  
177 4 ▶sub account {
Subroutine account: called CC=2 SCAR=6.9
Pod documented
178 4 my $me = shift;
179  
180 exists $me->{account}
181 ? $me->{account}
182 4 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $me->{'account'} ? : 3 1 4
183 }
184  
185  
186 4 ▶sub password {
Subroutine password: called CC=2 SCAR=6.9
Pod documented
187 4 my $me = shift;
188  
189 exists $me->{password}
190 ? $me->{password}
191 4 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $me->{'password'} ? : 3 1 4
192 }
193  
194  
195 2 ▶sub lpa {
Subroutine lpa: called CC=1 SCAR=0.0
Pod documented
196 2 my $me = shift;
197 2 ($me->login, $me->password, $me->account);
198 }
199  
200 2 1;
201  
202 __END__
« POP3.pm ↑ Summary NNTP.pm »