1# Before `make install' is performed this script should be runnable with 2# `make test'. After `make install' it should work as `perl test.pl' 3 4######################### We start with some black magic to print on failure. 5# Change 1..1 below to 1..last_test_to_print . 6# (It may become useful if the test is moved to ./t subdirectory.) 7 8BEGIN { $| = 1; print "1..15\n"; } 9END {print "not ok 1\n" unless $loaded;} 10 11use Net::DNS::Codes qw(:constants :header); 12use Net::DNS::ToolKit qw( 13 newhead 14 gethead 15 get16 16 get1char 17 parse_char 18 get_qdcount 19 get_ancount 20 get_arcount 21 get_nscount 22 put_qdcount 23 put_ancount 24 put_nscount 25 put_arcount 26); 27 28$loaded = 1; 29print "ok 1\n"; 30######################### End of black magic. 31 32# Insert your test code below (better if it prints "ok 13" 33# (correspondingly "not ok 13") depending on the success of chunk 13 34# of the test code): 35 36$test = 2; 37 38sub ok { 39 print "ok $test\n"; 40 ++$test; 41} 42 43## test 2 create basic header 44 45# 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 46# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 47# |QR| Opcode |AA|TC|RD|RA| Z|AD|CD| Rcode | 48# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 49# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 50 51## test 2 stuff buffer with a header 52my $buffer = ''; 53print "bad offset $_\nnot " 54 unless ($_ = newhead(\$buffer, 55 54321, # id 56 QR | BITS_IQUERY | RD, # flags 57 123, # question count 58 456, # answer count 59 789, # ns count 60 10112, # arcount 61 )); 62 63&ok; 64 65## test 3 retrieve header with gethead 66my($offset,$ID,$QR,$OPCODE,$AA,$TC,$RD,$RA,$Z,$AD,$CD,$RCODE, 67 $QDCOUNT,$ANCOUNT,$NSCOUNT,$ARCOUNT) = gethead(\$buffer); 68print "failed to retrieve header info\nnot " unless 69 $ID == 54321 && 70 $QR == 1 && 71 OpcodeTxt->{$OPCODE} eq 'IQUERY' && 72 !$TC && $RD && !$RA && !$Z && !$AD && !$CD && 73 RcodeTxt->{$RCODE} eq 'NOERROR' && 74 $QDCOUNT == 123 && 75 $ANCOUNT == 456 && 76 $NSCOUNT == 789 && 77 $ARCOUNT == 10112; 78&ok; 79 80## test 4 get qdcount 81$_ = get_qdcount(\$buffer); 82print "qdcount, got: $_, exp: 123\nnot " 83 unless $_ == 123; 84&ok; 85 86## test 5 get ancount 87$_ = get_ancount(\$buffer); 88print "ancount, got: $_, exp: 456\nnot " 89 unless $_ == 456; 90&ok; 91 92## test 6 get nscount 93$_ = get_nscount(\$buffer); 94print "nscount, got: $_, exp: 789\nnot " 95 unless $_ == 789; 96&ok; 97 98## test 7 get arcount 99$_ = get_arcount(\$buffer); 100print "arcount, got: $_, exp: 10112\nnot " 101 unless $_ == 10112; 102&ok; 103 104## test 8 put qdcount 105print "offset is: $_, exp: 6\nnot " 106 unless ($_ = put_qdcount(\$buffer,9999)) == 6; 107&ok; 108 109## test 9 check qd value 110print "qdcount, got: $_, exp: 9999\nnot " 111 unless (($_ = get_qdcount(\$buffer)) == 9999); 112&ok; 113 114## test 10 put ancount 115print "offset is: $_, exp: 8\nnot " 116 unless ($_ = put_ancount(\$buffer,8686)) == 8; 117&ok; 118 119## test 11 check an value 120print "ancount, got: $_, exp: 8686\nnot " 121 unless (($_ = get_ancount(\$buffer)) == 8686); 122&ok; 123 124## test 12 put nscount 125print "offset is: $_, exp: 10\nnot " 126 unless ($_ = put_nscount(\$buffer,7321)) == 10; 127&ok; 128 129## test 13 check ns value 130print "nscount, got: $_, exp: 7321\nnot " 131 unless (($_ = get_nscount(\$buffer)) == 7321); 132&ok; 133 134## test 14 put arcount 135print "offset is: $_, exp: 12\nnot " 136 unless ($_ = put_arcount(\$buffer,54321)) == 12; 137&ok; 138 139## test 15 check ar value 140print "arcount, got: $_, exp: 54321\nnot " 141 unless (($_ = get_arcount(\$buffer)) == 54321); 142&ok; 143 144#foreach (0..HFIXEDSZ -1) { 145# my $off = $_; 146# my $char = get1char(\$buffer,$off); 147# @x = parse_char($char); 148# print "$_\t: "; 149# foreach(@x) { 150# print "$_ "; 151# } 152# print "\n"; 153#} 154