| 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 |
|
|---|
| 6 | use Test::More;
|
|---|
| 7 | use Data::Dumper;
|
|---|
| 8 |
|
|---|
| 9 | use lib 't';
|
|---|
| 10 |
|
|---|
| 11 | use DNSTest;
|
|---|
| 12 | my $dtest = DNSTest::new;
|
|---|
| 13 |
|
|---|
| 14 | my $code,$msg;
|
|---|
| 15 | my $rectype = 5;
|
|---|
| 16 | my $newname;
|
|---|
| 17 | my $newval;
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | ## Domain tests
|
|---|
| 21 | subtest 'Domain tests' => sub {
|
|---|
| 22 |
|
|---|
| 23 | subtest 'CNAME add - new name' => sub {
|
|---|
| 24 | $newname = 'newname.example.com';
|
|---|
| 25 | $newval = 'fredshosting.example.net';
|
|---|
| 26 | ($code, $msg) = $dnsdb->addRec('n', 'n', 1, \$newname, \$rectype, \$newval, 900);
|
|---|
| 27 | ok( $code eq 'OK', "addRec() claimed succeess" );
|
|---|
| 28 | if ($code eq 'OK') {
|
|---|
| 29 | # crosscheck in the DB
|
|---|
| 30 | ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE domain_id = 1 AND host = '$newname'");
|
|---|
| 31 | ok( $rcount == 1, " ... [$rcount] yep, hostname only occurs once" );
|
|---|
| 32 | } else {
|
|---|
| 33 | print "not ok: $msg";
|
|---|
| 34 | }
|
|---|
| 35 | };
|
|---|
| 36 |
|
|---|
| 37 | subtest 'CNAME add - existing/colliding non-CNAME' => sub {
|
|---|
| 38 | $newname = 'mx1.example.com';
|
|---|
| 39 | ($code, $msg) = $dnsdb->addRec('n', 'n', 1, \$newname, \$rectype, \$newval, 900);
|
|---|
| 40 | ok( $code eq 'FAIL', "addRec() claimed failure" );
|
|---|
| 41 | if ($code eq 'FAIL') {
|
|---|
| 42 | ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE domain_id = 1 AND host = '$newname' AND type <> 5");
|
|---|
| 43 | ok( $rcount == 2, " ... [$rcount] record(s) with $newname already exist" );
|
|---|
| 44 | like( $msg, qr/One or more non-CNAME records/, " ... returned matching error" );
|
|---|
| 45 | }
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | subtest 'CNAME add - existing/colliding CNAME' => sub {
|
|---|
| 49 | $newname = 'www.example.com';
|
|---|
| 50 | ($code, $msg) = $dnsdb->addRec('n', 'n', 1, \$newname, \$rectype, \$newval, 900);
|
|---|
| 51 | ok( $code eq 'FAIL', "addRec() claimed failure" );
|
|---|
| 52 | if ($code eq 'FAIL') {
|
|---|
| 53 | ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE domain_id = 1 AND host = '$newname' AND type = 5");
|
|---|
| 54 | ok( $rcount == 1, " ... [$rcount] CNAME already exists" );
|
|---|
| 55 | like( $msg, qr/already a CNAME present/, " ... returned matching error" );
|
|---|
| 56 | }
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | subtest 'CNAME update - non-CNAME to CNAME, non-colliding' => sub {
|
|---|
| 60 | $newname = 'smtp.example.com';
|
|---|
| 61 | $newval = 'example.com';
|
|---|
| 62 | ($code, $msg) = $dnsdb->updateRec('n', 'n', 39, 1, \$newname, \$rectype, \$newval, 900);
|
|---|
| 63 | ok( $code eq 'OK', "updateRec() claimed success" );
|
|---|
| 64 | if ($code eq 'OK') {
|
|---|
| 65 | ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE domain_id = 1 AND host = '$newname'");
|
|---|
| 66 | ok( $rcount == 1, " ... [$rcount] yep, hostname only occurs once" );
|
|---|
| 67 | } else {
|
|---|
| 68 | print "not ok: $msg";
|
|---|
| 69 | }
|
|---|
| 70 | };
|
|---|
| 71 |
|
|---|
| 72 | subtest 'CNAME update - non-CNAME to CNAME, colliding' => sub {
|
|---|
| 73 | $newname = 'mx1.example.com';
|
|---|
| 74 | ($code, $msg) = $dnsdb->updateRec('n', 'n', 39, 1, \$newname, \$rectype, \$newval, 900);
|
|---|
| 75 | ok( $code eq 'FAIL', "updateRec() claimed failure" );
|
|---|
| 76 | if ($code eq 'FAIL') {
|
|---|
| 77 | ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE domain_id = 1 AND host = '$newname' AND type <> 5");
|
|---|
| 78 | ok( $rcount == 2, " ... [$rcount] record(s) with $newname already exist" );
|
|---|
| 79 | like( $msg, qr/One or more non-CNAME records/, " ... returned matching error" );
|
|---|
| 80 | }
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| 83 | subtest 'CNAME update - name to non-colliding name' => sub {
|
|---|
| 84 | $newname = 'imap.example.com';
|
|---|
| 85 | ($code, $msg) = $dnsdb->updateRec('n', 'n', 37, 1, \$newname, \$rectype, \$newval, 900);
|
|---|
| 86 | ok( $code eq 'OK', "updateRec() claimed success" );
|
|---|
| 87 | if ($code eq 'OK') {
|
|---|
| 88 | ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE domain_id = 1 AND host = '$newname'");
|
|---|
| 89 | ok( $rcount == 1, " ... [$rcount] yep, hostname only occurs once" );
|
|---|
| 90 | } else {
|
|---|
| 91 | print "not ok: $msg";
|
|---|
| 92 | }
|
|---|
| 93 | };
|
|---|
| 94 |
|
|---|
| 95 | subtest 'CNAME update - name to colliding name' => sub {
|
|---|
| 96 | $newname = 'mx1.example.com';
|
|---|
| 97 | ($code, $msg) = $dnsdb->updateRec('n', 'n', 41, 1, \$newname, \$rectype, \$newval, 900);
|
|---|
| 98 | ok( $code eq 'FAIL', "updateRec() claimed failure" );
|
|---|
| 99 | if ($code eq 'FAIL') {
|
|---|
| 100 | ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE domain_id = 1 AND host = '$newname' AND type <> 5");
|
|---|
| 101 | ok( $rcount == 2, " ... [$rcount] record(s) with $newname already exist" );
|
|---|
| 102 | like( $msg, qr/One or more non-CNAME records/, " ... returned matching error" );
|
|---|
| 103 | }
|
|---|
| 104 | };
|
|---|
| 105 |
|
|---|
| 106 | }; # domain tests
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | ## Reverse zone tests
|
|---|
| 110 | subtest 'Reverse zone tests' => sub {
|
|---|
| 111 |
|
|---|
| 112 | subtest 'CNAME add - new reverse name' => sub {
|
|---|
| 113 | $newval = '192.168.2.12';
|
|---|
| 114 | $newname = '12.8-29.2.168.192.in-addr.arpa';
|
|---|
| 115 | ($code, $msg) = $dnsdb->addRec('n', 'y', 1, \$newname, \$rectype, \$newval, 900);
|
|---|
| 116 | ok( $code eq 'OK', "addRec() claimed succeess" );
|
|---|
| 117 | if ($code eq 'OK') {
|
|---|
| 118 | ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE rdns_id = 1 AND val = '$newval'");
|
|---|
| 119 | ok( $rcount == 1, " ... [$rcount] yep, IP only occurs once" );
|
|---|
| 120 | } else {
|
|---|
| 121 | print "not ok: $msg\n";
|
|---|
| 122 | }
|
|---|
| 123 | };
|
|---|
| 124 |
|
|---|
| 125 | subtest 'CNAME add - existing/colliding non-CNAME' => sub {
|
|---|
| 126 | $newval = '192.168.2.14';
|
|---|
| 127 | $newname = '14.8-29.2.168.192.in-addr.arpa';
|
|---|
| 128 | ($code, $msg) = $dnsdb->addRec('n', 'y', 1, \$newname, \$rectype, \$newval, 900);
|
|---|
| 129 | ok( $code eq 'FAIL', "addRec() claimed failure" );
|
|---|
| 130 | if ($code eq 'FAIL') {
|
|---|
| 131 | ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE rdns_id = 1 AND val = '$newval' AND type <> 5");
|
|---|
| 132 | ok( $rcount == 2, " ... [$rcount] record(s) with $newname already exist" );
|
|---|
| 133 | like( $msg, qr/One or more non-CNAME records/, " ... returned matching error" );
|
|---|
| 134 | }
|
|---|
| 135 | };
|
|---|
| 136 |
|
|---|
| 137 | subtest 'CNAME add - existing/colliding CNAME' => sub {
|
|---|
| 138 | $newval = '192.168.2.13';
|
|---|
| 139 | $newname = '13.8-29.2.168.192.in-addr.arpa';
|
|---|
| 140 | ($code, $msg) = $dnsdb->addRec('n', 'y', 1, \$newname, \$rectype, \$newval, 900);
|
|---|
| 141 | ok( $code eq 'FAIL', "addRec() claimed failure" );
|
|---|
| 142 | if ($code eq 'FAIL') {
|
|---|
| 143 | ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE rdns_id = 1 AND val = '$newval' AND type = 5");
|
|---|
| 144 | ok( $rcount == 1, " ... [$rcount] CNAME already exists" );
|
|---|
| 145 | like( $msg, qr/already a CNAME present/, " ... returned matching error" );
|
|---|
| 146 | }
|
|---|
| 147 | };
|
|---|
| 148 |
|
|---|
| 149 | subtest 'CNAME update - non-CNAME to CNAME, non-colliding' => sub {
|
|---|
| 150 | $newval = '192.168.2.15';
|
|---|
| 151 | $newname = '15-29.arpa.example.net';
|
|---|
| 152 | ($code, $msg) = $dnsdb->updateRec('n', 'y', 43, 1, \$newname, \$rectype, \$newval, 900);
|
|---|
| 153 | ok( $code eq 'OK', "updateRec() claimed success" );
|
|---|
| 154 | if ($code eq 'OK') {
|
|---|
| 155 | ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE rdns_id = 1 AND val = '$newval'");
|
|---|
| 156 | ok( $rcount == 1, " ... [$rcount] yep, IP only occurs once" );
|
|---|
| 157 | } else {
|
|---|
| 158 | print "not ok: $msg\n";
|
|---|
| 159 | }
|
|---|
| 160 | };
|
|---|
| 161 |
|
|---|
| 162 | subtest 'CNAME update - non-CNAME to CNAME, colliding' => sub {
|
|---|
| 163 | $newval = '192.168.2.14';
|
|---|
| 164 | $newname = 'arpa14.rev.example.net';
|
|---|
| 165 | ($code, $msg) = $dnsdb->updateRec('n', 'y', 42, 1, \$newname, \$rectype, \$newval, 900);
|
|---|
| 166 | ok( $code eq 'FAIL', "updateRec() claimed failure updating revzone record type to CNAME" );
|
|---|
| 167 | if ($code eq 'FAIL') {
|
|---|
| 168 | ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE rdns_id = 1 AND val = '$newval' AND type <> 5");
|
|---|
| 169 | ok( $rcount == 2, " ... [$rcount] record(s) with $newval already exist" );
|
|---|
| 170 | like( $msg, qr/One or more non-CNAME records/, " ... returned matching error" );
|
|---|
| 171 | }
|
|---|
| 172 | };
|
|---|
| 173 |
|
|---|
| 174 | subtest 'CNAME update - name to non-colliding name' => sub {
|
|---|
| 175 | $newval = '192.168.2.11';
|
|---|
| 176 | ($code, $msg) = $dnsdb->updateRec('n', 'y', 34, 1, \$newname, \$rectype, \$newval, 900);
|
|---|
| 177 | ok( $code eq 'OK', "updateRec() claimed success updating revzone CNAME \"hostname\" (non-colliding)" );
|
|---|
| 178 | if ($code eq 'OK') {
|
|---|
| 179 | ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE rdns_id = 1 AND val = '$newval'");
|
|---|
| 180 | ok( $rcount == 1, " ... [$rcount] yep, IP only occurs once" );
|
|---|
| 181 | } else {
|
|---|
| 182 | print "not ok: $msg";
|
|---|
| 183 | }
|
|---|
| 184 | };
|
|---|
| 185 |
|
|---|
| 186 | subtest 'CNAME update - name to colliding name' => sub {
|
|---|
| 187 | $newval = '192.168.2.17';
|
|---|
| 188 | ($code, $msg) = $dnsdb->updateRec('n', 'y', 46, 1, \$newname, \$rectype, \$newval, 900);
|
|---|
| 189 | ok( $code eq 'FAIL', "updateRec() claimed failure" );
|
|---|
| 190 | if ($code eq 'FAIL') {
|
|---|
| 191 | ($rcount) = $dbh->selectrow_array("SELECT count(*) FROM records WHERE rdns_id = 1 AND val = '$newval' AND type <> 5");
|
|---|
| 192 | ok( $rcount == 1, " ... [$rcount] record(s) with $newval already exist" );
|
|---|
| 193 | like( $msg, qr/One or more non-CNAME records/, " ... returned matching error" );
|
|---|
| 194 | }
|
|---|
| 195 | };
|
|---|
| 196 |
|
|---|
| 197 | }; # reverse zone tests
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 | done_testing();
|
|---|