MToken/Util.pm

filter: Statementstmt 45.3% 53 / 117 Branchbran 15.6% 5 / 32 Conditioncond 10.2% 4 / 39 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 46.8% 15 / 32 Podpod 100% 19 / 19 totaltotal 37.2% 96 / 258 SCAR 58.3
CC
32
Cov
33%
parse_credentials
42.0
hide_pasword
42.0
sha1sum
30.0
CRAP 340.3
« MToken/Config.pm ↑ Summary MToken.pm »
1 package MToken::Util; # $Id: Util.pm 69 2019-06-09 16:17:44Z minus $
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =encoding utf-8
6  
7 =head1 NAME
8  
9 MToken::Util - Exported utility functions
10  
11 =head1 VERSION
12  
13 Version 1.01
14  
15 =head1 SYNOPSIS
16  
17 use MToken::Util;
18  
19 =head1 DESCRIPTION
20  
21 Exported utility functions
22  
23 =over
24  
25 =item B<cleanServerName>
26  
27 my $servername = cleanServerName( "my.server.com" );
28  
29 Clening the specified ServerName value
30  
31 =item B<cleanFileName>
32  
33 my $filename = cleanServerName( "mtoken.12345678" );
34  
35 Clening the specified FileName value
36  
37 =item B<explain>
38  
39 print explain( $object );
40  
41 Returns Data::Dumper dump
42  
43 =item B<md5sum>
44  
45 my $md5 = md5sum( $file );
46  
47 See L<Digest::MD5>
48  
49 =item B<sha1sum>
50  
51 my $sha1 = sha1sum( $file );
52  
53 See L<Digest::SHA1>
54  
55 =item B<filesize>
56  
57 my $fsize = filesize( $file );
58  
59 Returns file size
60  
61 =item B<hide_pasword>
62  
63 print hide_pasword('http://user:password@example.com'); # 'http://user:*****@example.com'
64  
65 Returns specified URL but without password
66  
67 =item B<blue>, B<cyan>, B<green>, B<red>, B<yellow>
68  
69 print cyan("Format %s", "text");
70  
71 Returns colored string
72  
73 =item B<nope>, B<skip>, B<wow>, B<yep>
74  
75 my $status = nope("Format %s", "text");
76  
77 Prints status message and returns status.
78  
79 For nope returns - 0; for skip, wow, yep - 1
80  
81 =item B<parse_credentials>
82  
83 my ($user, $password) = parse_credentials( 'http://user:password@example.com' );
84 my ($user, $password) = parse_credentials( new URI('http://user:password@example.com') );
85  
86 Returns credentials pair by URL or URI object
87  
88 =item B<tcd_load>
89  
90 if (my $text = tcd_load("/my/file.tcd")) {
91 print $text; # Blah-Blah-Blah
92 } else {
93 or die("Oops");
94 }
95  
96 Load text data from TCD04 file
97  
98 =item B<tcd_save>
99  
100 tcd_save("/my/file.tcd", "Blah-Blah-Blah") or die("Oops");
101  
102 Save text data to TCD04 file
103  
104 =back
105  
106 =head1 HISTORY
107  
108 See C<Changes> file
109  
110 =head1 TO DO
111  
112 See C<TODO> file
113  
114 =head1 BUGS
115  
116 * none noted
117  
118 =head1 SEE ALSO
119  
120 L<Digest::MD5>, L<Digest::SHA1>
121  
122 =head1 AUTHOR
123  
124 Serż Minus (Sergey Lepenkov) L<http://www.serzik.com> E<lt>abalama@cpan.orgE<gt>
125  
126 =head1 COPYRIGHT
127  
128 Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved
129  
130 =head1 LICENSE
131  
132 This program is free software; you can redistribute it and/or
133 modify it under the same terms as Perl itself.
134  
135 See C<LICENSE> file and L<https://dev.perl.org/licenses/>
136  
137 =cut
138  
139 3 ▶use vars qw/ $VERSION @EXPORT_OK @EXPORT /;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 3 $VERSION = "1.01";
141  
142 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
143 3 ▶use CTK::Util qw/bload bsave/;
Subroutine BEGIN: called CC=1 SCAR=0.0
144 3 ▶use CTK::Crypt::TCD04;
Subroutine BEGIN: called CC=1 SCAR=0.0
145 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
146 3 ▶use URI::Escape qw/uri_unescape/;
Subroutine BEGIN: called CC=1 SCAR=0.0
147 3 ▶use Digest::MD5;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 3 ▶use Digest::SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 3 ▶use Data::Dumper; #$Data::Dumper::Deparse = 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
150 3 ▶use Term::ANSIColor qw/colored/;
Subroutine BEGIN: called CC=1 SCAR=0.0
151  
152 3 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
153 3 @EXPORT = qw(
154 yep nope skip wow
155 blue green red yellow cyan
156 );
157 3 @EXPORT_OK = (qw(
158 cleanServerName cleanFileName
159 filesize md5sum sha1sum
160 parse_credentials hide_pasword
161 tcd_save tcd_load
162 explain
163 ), @EXPORT);
164  
165 sub cleanServerName {
166 0 ▶ my $sn = shift // 'localhost';
Subroutine cleanServerName: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
shift() // "localhost"
l!l
00
MC/DC0%0/1
shift() // "localhost"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
167 0 $sn =~ s/[^a-z0-9_\-.]//ig;
168 0 return $sn;
169 }
170 sub cleanFileName {
171 0 ▶ my $f = shift // '';
Subroutine cleanFileName: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
172 0 $f =~ s/[^a-z0-9_\-.]//ig;
173 0 return $f;
174 }
175 sub sha1sum {
176 0 ▶ my $f = shift;
Subroutine sha1sum: not called CC=5 SCAR=34.0
Pod documented
177 0 my $sha1 = new Digest::SHA1;
178 0 my $sum = '';
179 0 ▶ return $sum unless -e $f;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $f 0 0 0
180 0 ▶ open( my $sha1_fh, '<', $f) or (carp("Can't open '$f': $!") && return $sum);
Branch: 1 branch
BranchTrueFalseTotal
open my $sha1_fh, "<", $f or carp("Can't open '${f}': $!") and return $sum 0 0 0
Condition0%0/5
open(my $sha1_fh, '<', $f) || carp("Can't open '${f}': $!") && (return $sum)
l!l&&r!l&&!r
000
carp("Can't open '${f}': $!") and return $sum
!ll
00
MC/DC0%0/2
open(my $sha1_fh, '<', $f) || carp("Can't open '${f}': $!") && (return $sum)
open(my $sha1_fh, '<', $f) carp("Can't open '${f}': $!")
Truth table0%0/3
A || B
A= open(my $sha1_fh, '<', $f)B= carp("Can't open '${f}': $!")
ABresult
1X1
011
000
181 0 ▶ if ($sha1_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sha1_fh) 0 0 0
182 0 binmode($sha1_fh);
183 0 $sha1->addfile($sha1_fh);
184 0 $sum = $sha1->hexdigest;
185 0 close($sha1_fh);
186 }
187 0 return $sum;
188 }
189 sub md5sum {
190 0 ▶ my $f = shift;
Subroutine md5sum: not called CC=5 SCAR=34.0
Pod documented
191 0 my $md5 = new Digest::MD5;
192 0 my $sum = '';
193 0 ▶ return $sum unless -e $f;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $f 0 0 0
194 0 ▶ open( my $md5_fh, '<', $f) or (carp("Can't open '$f': $!") && return $sum);
Branch: 1 branch
BranchTrueFalseTotal
open my $md5_fh, "<", $f or carp("Can't open '${f}': $!") and return $sum 0 0 0
Condition0%0/5
open(my $md5_fh, '<', $f) || carp("Can't open '${f}': $!") && (return $sum)
l!l&&r!l&&!r
000
carp("Can't open '${f}': $!") and return $sum
!ll
00
MC/DC0%0/2
open(my $md5_fh, '<', $f) || carp("Can't open '${f}': $!") && (return $sum)
open(my $md5_fh, '<', $f) carp("Can't open '${f}': $!")
Truth table0%0/3
A || B
A= open(my $md5_fh, '<', $f)B= carp("Can't open '${f}': $!")
ABresult
1X1
011
000
195 0 ▶ if ($md5_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($md5_fh) 0 0 0
196 0 binmode($md5_fh);
197 0 $md5->addfile($md5_fh);
198 0 $sum = $md5->hexdigest;
199 0 close($md5_fh);
200 }
201 0 return $sum;
202 }
203 sub filesize {
204 0 ▶ my $f = shift;
Subroutine filesize: not called CC=2 SCAR=17.9
Pod documented
205 0 my $filesize = 0;
206 0 ▶ $filesize = (stat $f)[7] if -e $f;
Branch: 1 branch
BranchTrueFalseTotal
if -e $f 0 0 0
207 0 return $filesize;
208 }
209 sub parse_credentials {
210 0 ▶ my $url = shift || return ();
Subroutine parse_credentials: not called CC=6 SCAR=37.4
Pod documented
Condition0%0/2
shift() || (return ())
l!l
00
MC/DC0%0/1
shift() || (return ())
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
211 0 ▶ my $uri = (ref($url) eq 'URI') ? $url : URI->new($url);
Branch: 1 branch
BranchTrueFalseTotal
ref $url eq 'URI' ? : 0 0 0
212 0 ▶ my $info = $uri->userinfo() // "";
Condition0%0/2
$uri->userinfo // ""
l!l
00
MC/DC0%0/1
$uri->userinfo // ""
$uri->userinfo
Truth table0%0/2
A
A= $uri->userinfo
Aresult
11
00
213 0 my $user = $info;
214 0 my $pass = $info;
215 0 $user =~ s/:.*//;
216 0 $pass =~ s/^[^:]*://;
217 0 ▶ return (uri_unescape($user // ''), uri_unescape($pass // ''));
Condition0%0/4
$user // ""
l!l
00
$pass // ""
l!l
00
MC/DC0%0/1
$user // ""
$user
MC/DC0%0/1
$pass // ""
$pass
Truth table0%0/2
A
A= $user
Aresult
11
00
Truth table0%0/2
A
A= $pass
Aresult
11
00
218 }
219 sub hide_pasword {
220 0 ▶ my $url = shift || return "";
Subroutine hide_pasword: not called CC=6 SCAR=37.4
Pod documented
Condition0%0/2
shift() || (return '')
l!l
00
MC/DC0%0/1
shift() || (return '')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
221 0 ▶ my $full = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
222 0 my $uri = new URI($url);
223 0 my ($u,$p) = parse_credentials($uri);
224 0 ▶ return $url unless defined($p) && length($p);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p and length $p 0 0 0
Condition0%0/3
defined $p and length $p
!ll&&!rl&&r
000
MC/DC0%0/2
defined $p and length $p
defined $p length $p
Truth table0%0/3
A and B
A= defined $pB= length $p
ABresult
0X0
100
111
225 0 ▶ $uri->userinfo($full ? undef : sprintf("%s:*****", $u));
Branch: 1 branch
BranchTrueFalseTotal
$full ? : 0 0 0
226 0 return $uri->canonical->as_string;
227 }
228 sub tcd_save {
229 1 ▶ my $fn = shift;
Subroutine tcd_save: called CC=5 SCAR=17.2
Pod documented
230 1 ▶ my $text = shift // '';
Condition50%1/2
shift() // ""
l!l
10
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
231 1 ▶ carp("No file specified") unless $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn 0 1 1
232 1 ▶ return unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 1 1
233 1 ▶ bsave($fn, CTK::Crypt::TCD04->new()->encrypt($text))
Branch: 1 branch
BranchTrueFalseTotal
bsave($fn, "CTK::Crypt::TCD04"->new->encrypt($text)) or carp(qq[Can't save file "$fn"]) 0 1 1
234 or carp("Can't save file \"$fn\"");
235 1 return 1;
236 }
237 sub tcd_load {
238 1 ▶ my $fn = shift;
Subroutine tcd_load: called CC=6 SCAR=22.0
Pod documented
239 1 ▶ carp("No file specified") unless $fn;
Branch: 1 branch
BranchTrueFalseTotal
unless $fn 0 1 1
240 1 ▶ return unless -f $fn and -r _ and -s _;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $fn and -r _ and -s _ 0 1 1
Condition33%2/6
-f $fn and -r _ and -s _
!ll&&!rl&&r
001
-f $fn and -r _
!ll&&!rl&&r
001
MC/DC0%0/3
-f $fn and -r _ and -s _
-f $fn -r _ -s _
Truth table25%1/4
A and B and C
A= -f $fnB= -r _C= -s _
ABCresult
0XX0
10X0
1100
1111
241 1 ▶ return CTK::Crypt::TCD04->new()->decrypt(bload($fn) // '');
Condition50%1/2
bload($fn) // ""
l!l
10
MC/DC0%0/1
bload($fn) // ""
bload($fn)
Truth table50%1/2
A
A= bload($fn)
Aresult
11
00
242 }
243 sub explain {
244 0 ▶ my $dumper = new Data::Dumper( [shift] );
Subroutine explain: not called CC=2 SCAR=17.9
Pod documented
245 0 $dumper->Indent(1)->Terse(1);
246 0 ▶ $dumper->Sortkeys(1) if $dumper->can("Sortkeys");
Branch: 1 branch
BranchTrueFalseTotal
if $dumper->can("Sortkeys") 0 0 0
247 0 return $dumper->Dump;
248 }
249  
250 ################
251 # Colored says
252 ################
253 sub yep {
254 0 ▶ print(green('[ OK ]'), ' ', sprintf(shift, @_), "\n");
Subroutine yep: not called CC=1 SCAR=6.9
Pod documented
255 0 return 1;
256 }
257 sub nope {
258 0 ▶ print(red('[ FAIL ]'), ' ', sprintf(shift, @_), "\n");
Subroutine nope: not called CC=1 SCAR=6.9
Pod documented
259 0 return 0;
260 }
261 sub skip {
262 0 ▶ print(yellow('[ SKIP ]'), ' ', sprintf(shift, @_), "\n");
Subroutine skip: not called CC=1 SCAR=6.9
Pod documented
263 0 return 1;
264 }
265 sub wow {
266 0 ▶ print(blue('[ INFO ]'), ' ', sprintf(shift, @_), "\n");
Subroutine wow: not called CC=1 SCAR=6.9
Pod documented
267 0 return 1;
268 }
269 # Colored helper functions
270 0 ▶sub green { colored(['bright_green'], sprintf(shift, @_)) }
Subroutine green: not called CC=1 SCAR=6.9
Pod documented
271 0 ▶sub red { colored(['bright_red'], sprintf(shift, @_)) }
Subroutine red: not called CC=1 SCAR=6.9
Pod documented
272 0 ▶sub yellow { colored(['bright_yellow'], sprintf(shift, @_)) }
Subroutine yellow: not called CC=1 SCAR=6.9
Pod documented
273 0 ▶sub cyan { colored(['bright_cyan'], sprintf(shift, @_)) }
Subroutine cyan: not called CC=1 SCAR=6.9
Pod documented
274 0 ▶sub blue { colored(['bright_blue'], sprintf(shift, @_)) }
Subroutine blue: not called CC=1 SCAR=6.9
Pod documented
275  
276 3 1;
« MToken/Config.pm ↑ Summary MToken.pm »