# test.pl # # Before `make install' is performed this script should be runnable with # `make test'. After `make install' it should work as `perl test.pl' # # $Id: test.pl,v 1.18 1999/11/30 10:11:55 fanf Exp $ # This file is # Copyright (C) 1999 Tony Finch # # It is part of adns, which is # Copyright (C) 1997-1999 Ian Jackson # Copyright (C) 1999 Tony Finch # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foundation, # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # We start with some black magic to print on failure. # in 1..N below, N is the number of tests BEGIN { $| = 1; print "1..40\n"; } END { print "not ok 1\n" unless $loaded; } use Net::adns; $loaded = 1; print "ok 1\n"; # End of black magic. # The test code below prints "ok 13" (correspondingly "not ok 13") # depending on the success of the 13th test. my $count = 1; sub check { my $ok = shift; ++$count; print "not " unless $ok; print "ok $count\n"; } # test a few of the enum constants check adns_if_noenv == 1; check adns_qf_search == 1; check adns_r_none == 0; check adns_s_ok == 0; # test create and destroy { my $adns = Net::adns::init(0, \*STDERR, "nameserver 127.0.0.1"); check "Net::adns" eq ref $adns; } { my $adns = new Net::adns (diagfile => \*STDOUT); check "Net::adns" eq ref $adns; } # make an answer & examine its innards { my $adns = new Net::adns; my $answer = $adns->synchronous("localhost", adns_r_a, adns_qf_owner); check "Net::adns::answer::a" eq ref $answer; check adns_s_ok == $answer->status; check not defined $answer->cname; check "localhost" eq $answer->owner; check adns_r_a == $answer->type; check "A" eq $answer->type; check adns_r_a == ($answer->type)[0]; check "A" eq ($answer->type)[1]; check "" eq ($answer->type)[2]; check 1 == $answer->nrrs; # these test the Net::adns::answer::a accessor functions check "127.0.0.1" eq join ".", unpack "CCCC", $answer->addr; check "127.0.0.1" eq $answer->txt; } # test a representative accessor function for array handling { my $adns = new Net::adns; my $answer = $adns->synchronous("news.demon.co.uk.", adns_r_a); check grep /nnrp-\d+\.news\.demon\.net/i, gethostbyaddr $answer->addr, 2; check not defined $answer->addr(-1); check not defined $answer->addr($answer->nrrs); my @addr = $answer->addr; check @addr == $answer->nrrs; my @addr = $answer->addr(0); check @addr == 1; } # verify memory management { my $deletions = 0; sub Net::adns::test::TIESCALAR { my $class = shift; return bless [], $class; } sub Net::adns::test::DESTROY { ++$deletions; } { my $adns = new Net::adns; { my $test; tie $test, 'Net::adns::test'; my $query = $adns->submit("localhost", adns_r_a, 0, $test); # $test should not be destroyed now } check $deletions == 0; # $test should get destroyed now } check $deletions == 1; { my $query; { my $adns = new Net::adns; my $test; tie $test, 'Net::adns::test'; $query = $adns->submit("localhost", adns_r_a, 0, $test); # $test should not be destroyed now } check $deletions == 1; # $test should get destroyed now } check $deletions == 2; } # test some conversions to text { my $adns = new Net::adns; my $answer = $adns->synchronous(".", adns_r_ns_raw); check grep /a\.root-servers\.net/i, $answer->txt; check adns_r_ns_raw == $answer->type; check "NS" eq $answer->type; check adns_r_ns_raw == ($answer->type)[0]; check "NS" eq ($answer->type)[1]; check "raw" eq ($answer->type)[2]; my $answer = $adns->synchronous(".", adns_r_soa); check $answer->txt =~ / hostmaster\@internic\.NET /i; check adns_r_soa == $answer->type; check "SOA" eq $answer->type; check adns_r_soa == ($answer->type)[0]; check "SOA" eq ($answer->type)[1]; check "822" eq ($answer->type)[2]; } # txt records { my $adns = new Net::adns (configtext => "nameserver 212.240.58.2"); my $answer = $adns->synchronous("shirt.www.demon.net.", adns_r_txt); my $i = 0; my @txt; while (@txt = $answer->strings($i++)) { print map "$_\n", @txt; print "\n"; } print map "$_\n", $answer->txt; print "\n"; }