blib/lib/Net/Magallanes.pm

filter: Statementstmt 11.8% 22 / 186 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 50.0% 7 / 14 Podpod 71.4% 5 / 7 totaltotal 10.4% 34 / 324 CC 58 SCAR 50.7
CC
58
Cov
8%
answers
306.0
rcodes
240.0
nsids
110.0
CRAP 158.6
↑ Summary
1 package Net::Magallanes;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '0.04';
6  
7 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub new {
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
14 0 my $this = shift;
15 0 my %params = @_;
16  
17 0 my ($API_KEY, $IN_FILES);
18 0 my $API_BASE = 'https://atlas.ripe.net/api/v2';
19  
20 0 ▶ my $class = ref($this) || $this;
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
21  
22 0 ▶ $API_KEY = $params{'KEY'} if $params{'KEY'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"KEY"} 0 0 0
23 0 ▶ $IN_FILES = $params{'INFILES'} if $params{'INFILES'};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"INFILES"} 0 0 0
24  
25 # armar estructura con defaults sensibles
26 0 my $self = {};
27 0 bless $self, $class;
28  
29 # Si no hay KEY igual le damos, pero no podremos crear cosas, solo
30 # consultar.
31 0 $self->{'KEY'} = $API_KEY;
32 0 $self->{'ua'} = LWP::UserAgent->new(timeout => 10);
33 0 $self->{'ua'}->default_header('Content-Type' => 'application/json');
34 0 $self->{'ua'}->default_header('Accept' => 'application/json');
35 0 $self->{'URL'} = $API_BASE;
36  
37 0 $self->{'_CACHE_MSM'} = {};
38  
39 0 ▶ if ($IN_FILES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($IN_FILES) 0 0 0
40 0 my @files = split ',', $IN_FILES;
41 0 my $data;
42 0 foreach my $file (@files) {
43 0 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("Couldn't open file ${file}: $!") 0 0 0
44 or croak "Couldn't open file $file: $!";
45 0 local $/ = undef;
46 0 $data = <$fh>;
47 0 close $fh;
48 0 my $result = decode_json $data;
49 0 my $mi = $result->[0]->{msm_id};
50 0 $self->{'_CACHE_MSM'}->{$mi} = $result;
51 }
52 }
53  
54 # Qué puede venir:
55 # timeouts de https request
56 # versión de API
57 # defaults comunes a todo:
58 # - one_off (default true)
59  
60 0 return $self;
61 }
62  
63 0 ▶sub results {
Subroutine results: not called CC=5 SCAR=34.0
Pod undocumented
64 0 my $self = shift;
65 0 my $msm_id = shift;
66  
67 0 my $result;
68  
69 0 ▶ croak("You must provide the measurement identificator msm_id (only digits)")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msm_id and $msm_id =~ /^\d+$/ 0 0 0
Condition0%0/3
defined $msm_id and $msm_id =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $msm_id and $msm_id =~ /^\d+$/
defined $msm_id $msm_id =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $msm_idB= $msm_id =~ /^\d+$/
ABresult
0X0
100
111
70 unless defined $msm_id and $msm_id =~ /^\d+$/;
71  
72 return $self->{'_CACHE_MSM'}->{$msm_id}
73 0 ▶ if defined $self->{'_CACHE_MSM'}->{$msm_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_CACHE_MSM"}{$msm_id} 0 0 0
74  
75 0 my $res = $self->{'ua'}->get( $self->{'URL'} .
76 "/measurements/$msm_id/results/" .
77 '?format=json'
78 );
79  
80 0 $self->{'_JSON'} = $res->decoded_content;
81  
82 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
83 0 $result = decode_json $res->decoded_content;
84 }
85 else {
86 0 $result = 'ERROR: ' . $res->status_line;
87 }
88  
89 0 $self->{'_CACHE_MSM'}->{$msm_id} = $result;
90  
91 0 return $result;
92 }
93  
94 0 ▶sub json {
Subroutine json: not called CC=1 SCAR=6.9
Pod undocumented
95 0 my $self = shift;
96  
97 0 return $self->{'_JSON'};
98 }
99  
100 0 ▶sub answers {
Subroutine answers: not called CC=17 SCAR=57.2
Pod documented
101 0 my $self = shift;
102 0 my $msm_id = shift;
103 0 my $args = shift;
104  
105 0 my %args;
106 0 ▶ %args = %{$args} if defined $args;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args 0 0 0
107  
108 0 ▶ my $type = (defined($args{'TYPE'}) ? $args{'TYPE'} : 'A');
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'TYPE'} ? : 0 0 0
109 0 ▶ my $istime = (defined($args{'TIMESTAMP'}) ? $args{'TIMESTAMP'} : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'TIMESTAMP'} ? : 0 0 0
110  
111 0 my $result = results($self, $msm_id);
112  
113 0 my @sal;
114 0 foreach my $resdo (@{$result}) {
115 0 my $timestamp = $resdo->{'timestamp'};
116 0 ▶ if ($resdo->{'type'} eq 'dns') {
Branch: 1 branch
BranchTrueFalseTotal
if ($resdo->{"type"} eq "dns") 0 0 0
117 0 my $res_set = $resdo->{'resultset'};
118 0 ▶ if ($#{$res_set} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$res_set;} < 0) 0 0 0
119 0 push @{$res_set}, $resdo;
120 }
121 0 foreach my $dns (@$res_set) {
122 0 my $abuf = $dns->{'result'}->{'abuf'};
123 0 ▶ next unless $abuf;
Branch: 1 branch
BranchTrueFalseTotal
unless $abuf 0 0 0
124 0 my $dec_buff = decode_base64 $abuf;
125 0 ▶ if(defined $abuf && defined $dec_buff) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $abuf and defined $dec_buff) 0 0 0
Condition0%0/3
defined $abuf and defined $dec_buff
!ll&&!rl&&r
000
MC/DC0%0/2
defined $abuf and defined $dec_buff
defined $abuf defined $dec_buff
Truth table0%0/3
A and B
A= defined $abufB= defined $dec_buff
ABresult
0X0
100
111
126 0 my ($dns_pack)= new Net::DNS::Packet(\$dec_buff);
127 0 my @ans = $dns_pack->answer;
128 0 foreach my $ans (@ans) {
129 0 ▶ next unless $ans->type eq $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $ans->type eq $type 0 0 0
130 0 my $res_ip;
131 0 ▶ if ($type eq 'A') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'A') { } 0 0 0
elsif ($type eq 'AAAA') { } 0 0 0
132 0 $res_ip = $ans->address;
133 }
134 elsif ($type eq 'AAAA') {
135 0 $res_ip = $ans->address_short;
136 }
137 else {
138 0 $res_ip = $ans->string;
139 }
140 0 ▶ if ($res_ip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res_ip) 0 0 0
141 0 ▶ if ($istime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($istime) { } 0 0 0
142 0 my @val = ($timestamp, $res_ip);
143 0 push @sal, \@val;
144 }
145 else {
146 0 push @sal, $res_ip;
147 }
148 }
149 }
150 }
151 }
152 }
153 }
154 0 return @sal;
155 }
156  
157 0 ▶sub nsids {
Subroutine nsids: not called CC=10 SCAR=47.0
Pod documented
158 0 my $self = shift;
159 0 my $msm_id = shift;
160  
161 0 my $result = results($self, $msm_id);
162  
163 0 my @sal;
164 0 foreach my $resdo (@{$result}) {
165 0 ▶ if ($resdo->{'type'} eq 'dns') {
Branch: 1 branch
BranchTrueFalseTotal
if ($resdo->{"type"} eq "dns") 0 0 0
166 0 my $res_set = $resdo->{'resultset'};
167 0 ▶ if ($#{$res_set} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$res_set;} < 0) 0 0 0
168 0 push @{$res_set}, $resdo;
169 }
170 0 foreach my $dns (@$res_set) {
171 0 my $abuf = $dns->{'result'}->{'abuf'};
172 0 ▶ next unless $abuf;
Branch: 1 branch
BranchTrueFalseTotal
unless $abuf 0 0 0
173 0 my $dec_buff = decode_base64 $abuf;
174 0 ▶ if(defined $abuf && defined $dec_buff) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $abuf and defined $dec_buff) 0 0 0
Condition0%0/3
defined $abuf and defined $dec_buff
!ll&&!rl&&r
000
MC/DC0%0/2
defined $abuf and defined $dec_buff
defined $abuf defined $dec_buff
Truth table0%0/3
A and B
A= defined $abufB= defined $dec_buff
ABresult
0X0
100
111
175 0 my ($dns_pack)= new Net::DNS::Packet(\$dec_buff);
176 0 my @edns = $dns_pack->edns;
177 0 foreach my $edn (@edns) {
178 0 my $res_ip = $edn->option(3);
179 0 ▶ push @sal, ($res_ip ? $res_ip : 'NULL');
Branch: 1 branch
BranchTrueFalseTotal
$res_ip ? : 0 0 0
180 }
181 }
182 }
183 }
184 }
185 0 return @sal;
186 }
187  
188 0 ▶sub rcodes {
Subroutine rcodes: not called CC=15 SCAR=54.8
Pod documented
189 0 my $self = shift;
190 0 my $msm_id = shift;
191 0 my $args = shift;
192  
193 0 my %args;
194 0 ▶ %args = %{$args} if defined $args;
Branch: 1 branch
BranchTrueFalseTotal
if defined $args 0 0 0
195  
196 0 ▶ my $istime = (defined($args{'TIMESTAMP'}) ? $args{'TIMESTAMP'} : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'TIMESTAMP'} ? : 0 0 0
197 0 ▶ my $isprbi = (defined($args{'PROBEID'}) ? $args{'PROBEID'} : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'PROBEID'} ? : 0 0 0
198  
199 0 my $result = results($self, $msm_id);
200  
201 0 my @sal;
202 0 foreach my $resdo (@{$result}) {
203 0 my $timestamp = $resdo->{'timestamp'};
204 0 my $probe_id = $resdo->{'prb_id'};
205 0 ▶ if ($resdo->{'type'} eq 'dns') {
Branch: 1 branch
BranchTrueFalseTotal
if ($resdo->{"type"} eq "dns") 0 0 0
206 0 my $res_set = $resdo->{'resultset'};
207 0 ▶ if ($#{$res_set} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#{$res_set;} < 0) 0 0 0
208 0 push @{$res_set}, $resdo;
209 }
210 0 foreach my $dns (@$res_set) {
211 0 my $abuf = $dns->{'result'}->{'abuf'};
212 0 ▶ next unless $abuf;
Branch: 1 branch
BranchTrueFalseTotal
unless $abuf 0 0 0
213 0 my $dec_buff = decode_base64 $abuf;
214 0 ▶ if(defined $abuf && defined $dec_buff) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $abuf and defined $dec_buff) 0 0 0
Condition0%0/3
defined $abuf and defined $dec_buff
!ll&&!rl&&r
000
MC/DC0%0/2
defined $abuf and defined $dec_buff
defined $abuf defined $dec_buff
Truth table0%0/3
A and B
A= defined $abufB= defined $dec_buff
ABresult
0X0
100
111
215 0 my ($dns_pack)= new Net::DNS::Packet(\$dec_buff);
216 0 my $header = $dns_pack->header;
217 0 ▶ if ($istime or $isprbi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($istime or $isprbi) { } 0 0 0
Condition0%0/3
$istime or $isprbi
l!l&&r!l&&!r
000
MC/DC0%0/2
$istime or $isprbi
$istime $isprbi
Truth table0%0/3
A or B
A= $istimeB= $isprbi
ABresult
1X1
011
000
218 0 my @val;
219 0 ▶ push @val, $timestamp if $istime;
Branch: 1 branch
BranchTrueFalseTotal
if $istime 0 0 0
220 0 ▶ push @val, $probe_id if $isprbi;
Branch: 1 branch
BranchTrueFalseTotal
if $isprbi 0 0 0
221 0 push @val, $header->rcode;
222 0 push @sal, \@val;
223 }
224 else {
225 0 push @sal, $header->rcode;
226 }
227 }
228 }
229 }
230 }
231 0 return @sal;
232 }
233  
234 0 ▶sub dns {
Subroutine dns: not called CC=9 SCAR=45.0
Pod documented
235 0 my $self = shift;
236 0 my %params = @_;
237  
238 croak("You must provide at least the query name")
239 0 ▶ unless defined $params{'name'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"name"} 0 0 0
240 croak('You must provide an API key (KEY constructor param) to create measurements')
241 0 ▶ unless defined $self->{'KEY'} and $self->{'KEY'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"KEY"} and $self->{"KEY"} 0 0 0
Condition0%0/3
defined $self->{"KEY"} and $self->{"KEY"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"KEY"} and $self->{"KEY"}
defined $self->{"KEY"} $self->{"KEY"}
Truth table0%0/3
A and B
A= defined $self->{"KEY"}B= $self->{"KEY"}
ABresult
0X0
100
111
242  
243 0 ▶ my $qtype = defined($params{'type'}) ? $params{'type'} : 'AAAA';
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'type'} ? : 0 0 0
244 0 ▶ my $nprb = defined($params{'num_prb'}) ? $params{'num_prb'} : 5;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'num_prb'} ? : 0 0 0
245  
246 0 my %DEFS = (
247 description => 'DNS measurement to ',
248 type => 'dns',
249 query_class => 'IN',
250 timeout => 5000,
251 retry => 0,
252 af => 4,
253 use_macros => 'false',
254 use_probe_resolver => 'true',
255 resolve_on_probe => 'false',
256 set_nsid_bit => 'true',
257 protocol => 'UDP',
258 udp_payload_size => 1232,
259 skip_dns_check => 'false',
260 include_qbuf => 'false',
261 include_abuf => 'true',
262 prepend_probe_id => 'false',
263 set_rd_bit => 'false',
264 set_do_bit => 'true',
265 set_cd_bit => 'false',
266 # start_time
267 # stop_time
268 # interval
269 # target
270 );
271  
272 0 my %PROBES = (
273 type => 'area',
274 value => 'WW',
275 # tags_include => 'system-ipv4-works,system-can-resolve-a',
276 tags_include => 'system-ipv4-works',
277 );
278  
279 0 $PROBES{'requested'} = $nprb;
280  
281 0 $DEFS{'query_argument'} = $params{'name'};
282 0 $DEFS{'query_type'} = $qtype;
283 0 $DEFS{'description'} .= $params{'name'};
284  
285 0 my %ATLASCALL;
286 0 push @{$ATLASCALL{'definitions'}}, \%DEFS;
287 0 push @{$ATLASCALL{'probes'}}, \%PROBES;
288  
289 0 $ATLASCALL{'is_oneoff'} = 'true';
290  
291 0 my $json = encode_json \%ATLASCALL;
292  
293 my $res = $self->{'ua'}->post( $self->{'URL'} .
294 '/measurements/',
295 0 'Authorization' => 'Key ' . $self->{'KEY'},
296 Content => $json
297 );
298  
299 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
300 0 my $msmout = $res->decoded_content;
301 0 ▶ my $msm = $1 if $res->decoded_content =~ /{"measurements":\[(\d+)\]}/;
Branch: 1 branch
BranchTrueFalseTotal
if $res->decoded_content =~ /{"measurements":\[(\d+)\]}/ 0 0 0
302  
303 0 ▶ croak 'Bad measurement id, please check: ' . $res->decoded_content unless $msm;
Branch: 1 branch
BranchTrueFalseTotal
unless $msm 0 0 0
304  
305 0 return $msm;
306 }
307 else {
308 0 croak 'Could not create a measurement: ' . $res->status_line;
309 }
310 }
311  
312 1 1;
313  
314 __END__
↑ Summary