File Coverage

blib/lib/Zonemaster/Engine/Util.pm
Criterion Covered Total %
statement 75 77 97.4
branch 13 14 92.8
condition 8 8 100.0
subroutine 16 17 94.1
pod 7 7 100.0
total 119 123 96.7


line stmt bran cond sub pod time code
1             package Zonemaster::Engine::Util;
2              
3 26     26   44772 use version; our $VERSION = version->declare("v1.1.3");
  26         1319  
  26         160  
4              
5 26     26   2533 use 5.014002;
  26         90  
6              
7 26     26   344 use parent 'Exporter';
  26         245  
  26         192  
8              
9 26     26   1428 use strict;
  26         62  
  26         624  
10 26     26   124 use warnings;
  26         58  
  26         826  
11              
12 26     26   421 use Zonemaster::Engine;
  26         86  
  26         553  
13 26     26   135 use Zonemaster::Engine::DNSName;
  26         54  
  26         529  
14 26     26   7718 use Pod::Simple::SimpleTree;
  26         621261  
  26         22254  
15              
16             ## no critic (Modules::ProhibitAutomaticExportation)
17             our @EXPORT = qw[ ns info name pod_extract_for scramble_case ];
18             our @EXPORT_OK = qw[ ns info name pod_extract_for policy scramble_case ];
19             our %EXPORT_TAGS = ( all => \@EXPORT_OK );
20              
21             ## no critic (Subroutines::RequireArgUnpacking)
22             sub ns {
23 326738     326738 1 1389273 return Zonemaster::Engine->ns( @_ );
24             }
25              
26             sub info {
27 20383     20383 1 72600 my ( $tag, $argref ) = @_;
28              
29 20383         91961 return Zonemaster::Engine->logger->add( $tag, $argref );
30             }
31              
32             sub policy {
33 10     10 1 35 return Zonemaster::Engine->config->policy;
34             }
35              
36             sub name {
37 757553     757553 1 4489139 my ( $name ) = @_;
38              
39 757553         20189120 return Zonemaster::Engine::DNSName->new( $name );
40             }
41              
42             # Functions for extracting POD documentation from test modules
43              
44             sub _pod_process_tree {
45 20     20   34073 my ( $node, $flags ) = @_;
46 20         22 my ( $name, $ahash, @subnodes ) = @{$node};
  20         37  
47 20         22 my @res;
48              
49 20   100     34 $flags //= {};
50              
51 20         26 foreach my $node ( @subnodes ) {
52 57 100       81 if ( ref( $node ) ne 'ARRAY' ) {
53 17 100 100     35 $flags->{tests} = 1 if $name eq 'head1' and $node eq 'TESTS';
54 17 100 100     35 if ( $name eq 'item-text' and $flags->{tests} ) {
55 1         8 $node =~ s/\A(\w+).*\z/$1/x;
56 1         3 $flags->{item} = $node;
57 1         3 push @res, $node;
58             }
59             }
60             else {
61 40 100       60 if ( $flags->{item} ) {
62 21         31 push @res, _pod_extract_text( $node );
63             }
64             else {
65 19         31 push @res, _pod_process_tree( $node, $flags );
66             }
67             }
68             }
69              
70 20         48 return @res;
71             } ## end sub _pod_process_tree
72              
73             sub _pod_extract_text {
74 21     21   25 my ( $node ) = @_;
75 21         24 my ( $name, $ahash, @subnodes ) = @{$node};
  21         31  
76 21         30 my $res = q{};
77              
78 21         25 foreach my $node ( @subnodes ) {
79 21 100       31 if ( $name eq q{item-text} ) {
80 10         31 $node =~ s/\A(\w+).*\z/$1/x;
81             }
82              
83 21 50       36 if ( ref( $node ) eq q{ARRAY} ) {
84 0         0 $res .= _pod_extract_text( $node );
85             }
86             else {
87 21         31 $res .= $node;
88             }
89             }
90              
91 21         44 return $res;
92             } ## end sub _pod_extract_text
93              
94             sub pod_extract_for {
95 1     1 1 3 my ( $name ) = @_;
96              
97 1         16 my $parser = Pod::Simple::SimpleTree->new;
98 1         34 $parser->no_whining( 1 );
99              
100 1         15 my %desc = eval { _pod_process_tree( $parser->parse_file( $INC{"Zonemaster/Engine/Test/$name.pm"} )->root ) };
  1         9  
101              
102 1         28 return \%desc;
103             }
104              
105             # Function from CPAN package Text::Capitalize that causes
106             # issues when installing ZM.
107             #
108             sub scramble_case {
109 1     1 1 2 my $string = shift;
110 1         2 my ( @chars, $uppity, $newstring, $uppers, $downers );
111              
112 1         6 @chars = split //, $string;
113              
114 1         2 $uppers = 2;
115 1         2 $downers = 1;
116 1         3 foreach my $c ( @chars ) {
117 10         16 $uppity = int( rand( 1 + $downers / $uppers ) );
118              
119 10 100       18 if ( $uppity ) {
120 5         7 $c = uc( $c );
121 5         7 $uppers++;
122             }
123             else {
124 5         7 $c = lc( $c );
125 5         6 $downers++;
126             }
127             }
128 1         4 $newstring = join q{}, @chars;
129 1         5 return $newstring;
130             } # end sub scramble_case
131              
132             sub supports_ipv6 {
133 0     0 1   return;
134             }
135              
136             1;
137              
138             =head1 NAME
139              
140             Zonemaster::Engine::Util - utility functions for other Zonemaster modules
141              
142             =head1 SYNOPSIS
143              
144             use Zonemaster::Engine::Util;
145             info(TAG => { some => 'argument'});
146             my $ns = ns($name, $address);
147             my $name = name('whatever.example.org');
148              
149             =head1 EXPORTED FUNCTIONS
150              
151             =over
152              
153             =item info($tag, $href)
154              
155             Creates and returns a L<Zonemaster::Engine::Logger::Entry> object. The object
156             is also added to the global logger object's list of entries.
157              
158             =item ns($name, $address)
159              
160             Creates and returns a nameserver object with the given name and address.
161              
162             =item policy()
163              
164             Returns a reference to the global policy hash.
165              
166             =item name($string_name_or_zone)
167              
168             Creates and returns a L<Zonemaster::Engine::DNSName> object for the given argument.
169              
170             =item pod_extract_for($testname)
171              
172             Will attempt to extract the POD documentation for the test methods in
173             the test module for which the name is given. If it can, it returns a
174             reference to a hash where the keys are the test method names and the
175             values the documentation strings.
176              
177             This method blindly assumes that the structure of the POD is exactly
178             like that in the Example and Basic test modules. If it's not, the
179             results are undefined.
180              
181             =item scramble_case
182              
183             This routine provides a special effect: sCraMBliNg tHe CaSe
184              
185             =item supports_ipv6
186              
187             Check if ZOnemaster hosting server supports IPv6.
188              
189             =back