| 1 | package Test::BDD::Infrastructure::DNS; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | our $VERSION = '1.005'; # VERSION | |||||||||||||||||||
| 7 | # ABSTRACT: cucumber step definitions for DNS based checks | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | ▶ | use Test::More; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use Test::BDD::Cucumber::StepFile qw( Given When Then ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 0 | ▶ | sub S { Test::BDD::Cucumber::StepFile::S } | ||||||||||||||||||
|
Subroutine
S: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 1 | ▶ | use Test::BDD::Infrastructure::Utils qw( | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | convert_unit convert_cmp_operator $CMP_OPERATOR_RE convert_interval | ||||||||||||||||||||
| 16 | 1 | lookup_config ); | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 1 | ▶ | use Net::DNS::Resolver; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 0 | ▶ | Given qr/the DNS resolver of the system is used/, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 22 | 0 | S->{'dns'} = Net::DNS::Resolver->new; | |||||||||||||||||||
| 23 | 1 | }; | |||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 0 | ▶ | Given qr/the DNS resolver as configured in (\S+) is used/, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 26 | 0 | S->{'dns'} = Net::DNS::Resolver->new( | |||||||||||||||||||
| 27 | config_file => $1, | ||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||
| 29 | 1 | }; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 0 | ▶ | Given qr/the DNS resolver nameserver(?:s are| is) (.*)$/, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 32 | 0 | S->{'dns'}->nameservers(split(/\s*,\s*/, $1)); | |||||||||||||||||||
| 33 | 1 | }; | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | ▶ | Given qr/the DNS resolver searchlist is (.*)$/, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 36 | 0 | S->{'dns'}->searchlist(split(/\s*,\s*/, $1)); | |||||||||||||||||||
| 37 | 1 | }; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 0 | ▶ | Given qr/the DNS resolver recursion flag is (enabled|disabled)$/, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 40 | 0 | ▶ | S->{'dns'}->recurse( $1 eq 'disabled' ? 0 : 1 ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 1 | }; | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 0 | ▶ | Given qr/the DNS resolver (dnssec|adflag|cdflag) flag is (enabled|disabled)$/, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 44 | 0 | ▶ | S->{'dns'}->$1( $2 eq 'enabled' ? 1 : 0 ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | 1 | }; | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 0 | ▶ | When qr/a DNS query for (\S+)(?: of type (\S+))? is sent$/, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 48 | 0 | my $name = $1; | |||||||||||||||||||
| 49 | 0 | my @types; | |||||||||||||||||||
| 50 | 0 | ▶ | if( defined $2 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 0 | @types = split(/\s*,\s*/, $2); | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | 0 | diag('current DNS resolver state is: '.S->{'dns'}->string); | |||||||||||||||||||
| 54 | 0 | my $packet = S->{'dns'}->send( $name, @types ); | |||||||||||||||||||
| 55 | 0 | ok( defined $packet, 'must recieve an answer to DNS query'); | |||||||||||||||||||
| 56 | 0 | isa_ok( $packet, 'Net::DNS::Packet'); | |||||||||||||||||||
| 57 | 0 | ▶ | if( defined $packet ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 0 | diag('recieved DNS packet: '.$packet->string); | |||||||||||||||||||
| 59 | 0 | S->{'dns-packet'} = $packet; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | 1 | }; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 0 | ▶ | Then qr/the DNS answer must contain $CMP_OPERATOR_RE (\d+) records?$/, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 64 | 0 | my $op = convert_cmp_operator( $1 ); | |||||||||||||||||||
| 65 | 0 | my $count = $2; | |||||||||||||||||||
| 66 | 0 | cmp_ok( scalar(S->{'dns-packet'}->answer), $op, $count, "the DNS answer must contain $op $count records"); | |||||||||||||||||||
| 67 | 1 | }; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 0 | ▶ | Then qr/the DNS header (qr|aa|tc|rd|ra|z|ad|cd) flag must be (not set|set)$/, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 70 | 0 | my $flag = $1; | |||||||||||||||||||
| 71 | 0 | ▶ | my $value = $2 eq 'set' ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 72 | 0 | cmp_ok( S->{'dns-packet'}->header->$flag, '==', $value, "the dns header flag $flag must be $value"); | |||||||||||||||||||
| 73 | 1 | }; | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 0 | ▶ | Then qr/the DNS (answer|pre|prerequisite|authority|update|additional) (?:section )?must contain a RR (\S+)(?: of type (\S+))?/, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 76 | 0 | my $section = $1; | |||||||||||||||||||
| 77 | 0 | my $name = $2; | |||||||||||||||||||
| 78 | 0 | my $type = $3; | |||||||||||||||||||
| 79 | 0 | my $rr; | |||||||||||||||||||
| 80 | 0 | ▶ | if( defined $type ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 81 | 0 | ▶ | ($rr) = grep { $_->name eq $name && $_->type eq 'type' } S->{'dns-packet'}->$section; | ||||||||||||||||||
|
Condition0%0/3
$_->name eq $name && $_->type eq 'type'
MC/DC0%0/2
$_->name eq $name && $_->type eq 'type'
$_->name eq $name
$_->type eq 'type'
Truth table0%0/3
A && B
A= $_->name eq $nameB= $_->type eq 'type'
| |||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||
| 83 | 0 | ($rr) = grep { $_->name eq $name } S->{'dns-packet'}->$section; | |||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | 0 | ok( defined $rr, 'packet section must contain a matching RR'); | |||||||||||||||||||
| 86 | 0 | isa_ok( $rr, 'Net::DNS::RR'); | |||||||||||||||||||
| 87 | 0 | S->{'dns-rr'} = $rr; | |||||||||||||||||||
| 88 | 1 | }; | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | ▶ | Then qr/the DNS record must be of (type|class) (\S+)$/, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 91 | 0 | my $field = $1; | |||||||||||||||||||
| 92 | 0 | my $value = $2; | |||||||||||||||||||
| 93 | 0 | cmp_ok( S->{'dns-rr'}->$field, 'eq', $value, "DNS record must be of $field $value"); | |||||||||||||||||||
| 94 | 1 | }; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 0 | ▶ | Then qr/the DNS record TTL must be $CMP_OPERATOR_RE (\d+)$/, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 97 | 0 | my $op = convert_cmp_operator( $1 ); | |||||||||||||||||||
| 98 | 0 | my $count = $2; | |||||||||||||||||||
| 99 | 0 | cmp_ok(S->{'dns-rr'}->ttl, $op, $count, "ttl must be $op $count"); | |||||||||||||||||||
| 100 | 1 | }; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 0 | ▶ | Then qr/the DNS record (address|cname|preference|exchange|nsdname|ptrdname|txtdata) must be (.*)$/, sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 103 | 0 | my $field = $1; | |||||||||||||||||||
| 104 | 0 | my $value = $2; | |||||||||||||||||||
| 105 | 0 | cmp_ok(S->{'dns-rr'}->$field, 'eq', $value, "DNS records $field must be $value"); | |||||||||||||||||||
| 106 | 1 | }; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 1 | 1; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||