source: branches/cname-collision/t/cname.t@ 952

Last change on this file since 952 was 952, checked in by Kris Deugau, 16 hours ago

/branches/cname-collision

Add add-duplicate-CNAME test that got missed somehow when shuffling patches
Wrap domain and reverse zone groups of tests in their own blocks
See #88, #72

File size: 3.8 KB
Line 
1# -*- Perl -*-
2# Tests for CNAME records
3# Note that not all possible cases are caught!
4# Template records mean not all published records are natively present in the DB
5
6use Test::More;
7use Data::Dumper;
8
9use lib 't';
10
11use DNSTest;
12my $dtest = DNSTest::new;
13
14my $code,$msg;
15my $rectype = 5;
16my $newname;
17my $newval;
18
19## Domain tests
20subtest 'Domain tests' => sub {
21
22subtest 'CNAME add - new name' => sub {
23 $newname = 'newname.example.com';
24 $newval = 'fredshosting.example.net';
25 ($code, $msg) = $dnsdb->addRec('n', 'n', 1, \$newname, \$rectype, \$newval, 900);
26 ok( $code eq 'OK', "addRec() claimed succeess" );
27 if ($code eq 'OK') {
28 # crosscheck in the DB
29 ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE domain_id = 1 AND host = '$newname'");
30 ok( $rcount == 1, " ... [$rcount] yep, hostname only occurs once" );
31 } else {
32 print "not ok: $msg";
33 }
34};
35
36subtest 'CNAME add - existing/colliding CNAME' => sub {
37 $newname = 'www.example.com';
38 ($code, $msg) = $dnsdb->addRec('n', 'n', 1, \$newname, \$rectype, \$newval, 900);
39 ok( $code eq 'FAIL', "addRec() claimed failure adding duplicate CNAME" );
40 if ($code eq 'FAIL') {
41 ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE domain_id = 1 AND host = '$newname' AND type = 5");
42 ok( $rcount == 1, " ... [$rcount] CNAME already exists" );
43 like( $msg, qr/already a CNAME present/, " ... returned matching error" );
44 }
45};
46
47subtest 'CNAME add - existing/colliding CNAME' => sub {
48 $newname = 'www.example.com';
49 ($code, $msg) = $dnsdb->addRec('n', 'n', 1, \$newname, \$rectype, \$newval, 900);
50 ok( $code eq 'FAIL', "addRec() claimed failure adding duplicate CNAME" );
51 if ($code eq 'FAIL') {
52 ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE domain_id = 1 AND host = '$newname' AND type = 5");
53 ok( $rcount == 1, " ... [$rcount] CNAME already exists" );
54 like( $msg, qr/already a CNAME present/, " ... returned matching error" );
55 }
56};
57
58}; # domain tests
59
60
61## Reverse zone
62subtest 'Reverse zone tests' => sub {
63
64subtest 'CNAME add - new reverse name' => sub {
65 $newval = '192.168.2.12';
66 $newname = '12.8-29.2.168.192.in-addr.arpa';
67 ($code, $msg) = $dnsdb->addRec('n', 'y', 1, \$newname, \$rectype, \$newval, 900);
68 ok( $code eq 'OK', "addRec() claimed succeess adding reverse zone CNAME" );
69 if ($code eq 'OK') {
70 ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE rdns_id = 1 AND val = '$newval'");
71 ok( $rcount == 1, " ... [$rcount] yep, IP only occurs once" );
72 } else {
73 print "not ok: $msg\n";
74 }
75};
76
77subtest 'CNAME add - existing/colliding non-CNAME' => sub {
78 $newval = '192.168.2.14';
79 $newname = '14.8-29.2.168.192.in-addr.arpa';
80 ($code, $msg) = $dnsdb->addRec('n', 'y', 1, \$newname, \$rectype, \$newval, 900);
81 ok( $code eq 'FAIL', "addRec() claimed failure adding colliding reverse zone CNAME" );
82 if ($code eq 'FAIL') {
83 ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE rdns_id = 1 AND val = '$newval' AND type <> 5");
84 ok( $rcount == 2, " ... [$rcount] record(s) with $newname already exist" );
85 like( $msg, qr/One or more non-CNAME records/, " ... returned matching error" );
86 }
87};
88
89subtest 'CNAME add - existing/colliding CNAME' => sub {
90 $newval = '192.168.2.13';
91 $newname = '13.8-29.2.168.192.in-addr.arpa';
92 ($code, $msg) = $dnsdb->addRec('n', 'y', 1, \$newname, \$rectype, \$newval, 900);
93 ok( $code eq 'FAIL', "addRec() claimed failure adding duplicate reverse zone CNAME" );
94 if ($code eq 'FAIL') {
95 ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE rdns_id = 1 AND val = '$newval' AND type = 5");
96 ok( $rcount == 1, " ... [$rcount] CNAME already exists" );
97 like( $msg, qr/already a CNAME present/, " ... returned matching error" );
98 }
99};
100
101}; # reverse zone tests
102
103done_testing();
Note: See TracBrowser for help on using the repository browser.