scripts/downloadSamlMetadata

filter: Statementstmt 76.4% 65 / 85 Branchbran 62.5% 20 / 32 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 75.0% 9 / 12 Podpod n/a totaltotal 73.3% 102 / 139 CC 18 SCAR 20.3
CC
18
Cov
73%
_check_arguments
20.0
_check_xml
9.2
downloadSamlMetadata
6.1
CRAP 7.6
« blib/lib/Lemonldap/NG/Portal/Auth/SSL.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/Captcha.pm »
1 #!/usr/bin/perl
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Getopt::Long qw/GetOptionsFromArray/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use XML::LibXML::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Get configuration from CLI opts
12 0 ▶sub get_config {
Subroutine get_config: not called CC=2 SCAR=17.9
13 0 my ($array) = @_;
14  
15 0 my %opts;
16  
17 # Get command line options
18 0 my $result =
19 GetOptionsFromArray( $array, \%opts, 'output|o=s', 'metadata|m=s',
20 'verbose|v', 'dry-run|n', 'help|h', );
21  
22 0 ▶ pod2usage(1) if $opts{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"help"} 0 0 0
23  
24 0 _check_arguments( \%opts );
25 0 return \%opts;
26 }
27  
28 1 our $verbose = 0;
29  
30 sub printlog {
31 if ($verbose) {
32 print STDERR @_, "\n";
33 }
34 }
35  
36 sub printfatal {
37 print STDERR @_, "\n";
38 }
39  
40 0 ▶sub _check_arguments {
Subroutine _check_arguments: not called CC=4 SCAR=30.0
41 0 my ($opts) = @_;
42  
43 0 my @missing;
44 0 ▶ push @missing, "metadata URL (-m)" if !$opts->{metadata};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"metadata"} 0 0 0
45 0 ▶ push @missing, "output path (-o)" if !$opts->{output};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"output"} 0 0 0
46  
47 0 ▶ pod2usage(
Branch: 1 branch
BranchTrueFalseTotal
if @missing 0 0 0
48 -message => ( "Missing " . join( ' and ', @missing ) ),
49 -exitval => 2
50 ) if @missing;
51 }
52  
53 6 ▶sub _check_xml {
Subroutine _check_xml: called CC=9 SCAR=22.2
54 6 my ($tmpfile) = @_;
55  
56 # Check XML syntax
57 6 my $reader = XML::LibXML::Reader->new( location => $tmpfile );
58 6 ▶ if ( !$reader ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reader) 0 6 6
59 0 unlink($tmpfile);
60 0 printfatal "Cannot read $tmpfile: $!";
61 0 return 0;
62 }
63  
64 6 my $valid = 0;
65 6 eval {
66 6 ▶ while ( $reader->read ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->read) 16 5 21
67 16 ▶ if ( $reader->depth == 0
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->depth == 0 and $reader->nodeType == 1) 6 10 16
Condition100%3/3
$reader->depth == 0 and $reader->nodeType == 1
!ll&&!rl&&r
556
MC/DC100%2/2
$reader->depth == 0 and $reader->nodeType == 1
$reader->depth == 0 $reader->nodeType == 1
Truth table100%3/3
A and B
A= $reader->depth == 0B= $reader->nodeType == 1
ABresult
0X0
100
111
68 and $reader->nodeType == XML_READER_TYPE_ELEMENT )
69 {
70 6 ▶ if ( $reader->localName eq "EntitiesDescriptor"
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->localName eq "EntitiesDescriptor" and $reader->namespaceURI eq "urn:oasis:names:tc:SAML:2.0:metadata") 4 2 6
Condition66%2/3
$reader->localName eq "EntitiesDescriptor" and $reader->namespaceURI eq "urn:oasis:names:tc:SAML:2.0:metadata"
!ll&&!rl&&r
204
MC/DC50%1/2
$reader->localName eq "EntitiesDescriptor" and $reader->namespaceURI eq "urn:oasis:names:tc:SAML:2.0:metadata"
$reader->localName eq "EntitiesDescriptor" $reader->namespaceURI eq "urn:oasis:names:tc:SAML:2.0:metadata"
Truth table66%2/3
A and B
A= $reader->localName eq "EntitiesDescriptor"B= $reader->namespaceURI eq "urn:oasis:names:tc:SAML:2.0:metadata"
ABresult
0X0
100
111
71 and $reader->namespaceURI eq
72 "urn:oasis:names:tc:SAML:2.0:metadata" )
73 {
74 4 $valid = 1;
75 }
76 }
77 }
78 };
79 6 ▶ if ( my $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 1 5 6
80 1 unlink($tmpfile);
81 1 printfatal "Cannot parse $tmpfile as XML: $@";
82 1 return 0;
83 }
84  
85 5 ▶ if ( $valid == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid == 1) { } 4 1 5
86 4 return 1;
87 }
88 else {
89 1 unlink($tmpfile);
90 1 printfatal "XML does not contain an EntitiesDescriptor";
91 1 return 0;
92 }
93 }
94  
95 7 ▶sub downloadSamlMetadata {
Subroutine downloadSamlMetadata: called CC=6 SCAR=18.1
96 7 my ($opts) = @_;
97  
98 # Get merged config
99 7 $verbose = $opts->{verbose};
100  
101 # Download metadata file
102 7 my $ua = LWP::UserAgent->new;
103 7 $ua->timeout(10);
104 7 ▶ $ua->show_progress(1) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $main::verbose 0 7 7
105 7 $ua->env_proxy;
106  
107 7 my $metadata_file = $opts->{metadata};
108 7 my $output = $opts->{output};
109  
110 7 printlog("Try to download metadata file at $metadata_file");
111 7 my $tmpfile = mktemp("$output.XXXXXXXX");
112 7 printlog("Using temporary file $tmpfile");
113  
114 7 my $response = $ua->get( $metadata_file, ':content_file' => $tmpfile );
115  
116 7 ▶ if ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 6 1 7
117 6 printlog("Metadata successfully downloaded");
118 }
119 else {
120 1 printlog("Removing $tmpfile");
121 1 unlink($tmpfile);
122 1 printfatal "Error downloading metadata ", $response->status_line;
123 1 return 1;
124 }
125  
126 6 ▶ return 1 unless _check_xml($tmpfile);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_xml($tmpfile) 2 4 6
127  
128 4 ▶ if ( $opts->{'dry-run'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'dry-run'}) { } 2 2 4
129 2 printlog("Removing $tmpfile");
130 2 unlink($tmpfile);
131 }
132 else {
133 # Move file
134 2 ▶ if ( !rename( $tmpfile, $output ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (rename $tmpfile, $output) 0 2 2
135 0 unlink($tmpfile);
136 0 printfatal "Could not atomically replace $output";
137 0 return 1;
138 }
139 }
140  
141 4 return 0;
142 }
143  
144 0 ▶sub main {
Subroutine main: not called CC=1 SCAR=6.9
145 0 my $config = get_config( \@ARGV );
146 0 exit( downloadSamlMetadata($config) );
147  
148 }
149  
150 # If run as a script
151 1 ▶unless (caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 1 1
152 0 main();
153 }
154  
155 __END__
« blib/lib/Lemonldap/NG/Portal/Auth/SSL.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/Captcha.pm »