1#!/usr/bin/perl 2 3use strict; 4use warnings; 5 6use Test::More; 7use Test::Exception; 8 9use Net::RNDC::Packet; 10 11## new / bad 12throws_ok { Net::RNDC::Packet->new(); } 13 qr/Missing required argument 'key'/, 14 "new() without 'key' fails"; 15 16throws_ok { Net::RNDC::Packet->new(key => 'aabc', data => 'bad'); } 17 qr/Argument 'data' must be a HASH/, 18 "new() with non-HASH 'data' fails"; 19 20throws_ok { Net::RNDC::Packet->new(key => 'aabc', version => 'cat'); } 21 qr/Argument 'version' must be a number/, 22 "new() with non-numeric 'version' fails"; 23 24throws_ok { Net::RNDC::Packet->new(key => 'aabc', nonce => 'dog'); } 25 qr/Argument 'nonce' must be a number/, 26 "new() with non-numeric 'nonce' fails"; 27 28## new / good 29my $p; 30 31ok($p = Net::RNDC::Packet->new(key => 'aabc'), "new(key => '..')"); 32isa_ok($p, 'Net::RNDC::Packet', "Got a packet"); 33is($p->{key}, 'aabc', "key is correct"); 34 35ok($p = Net::RNDC::Packet->new(key => 'aabc', nonce => 121), 36 "new(key => '..', nonce => '..'"); 37isa_ok($p, 'Net::RNDC::Packet', "Got a packet"); 38is($p->{data}{_ctrl}{_nonce}, 121, "nonce is correct"); 39 40## data 41$p = Net::RNDC::Packet->new(key => 'aabc', data => { cat => \"bird" }); 42ok($p, "Got a packet"); 43 44ok(!$p->data, "->data() failed with scalarref in data section"); 45like($p->error, 46 qr/'Unknown data type 'SCALAR' in _value_towire'/, 47 "Unknown data type 'SCALAR' error"); 48 49# Make sure a generated/parsed packet matches commands 50$p = Net::RNDC::Packet->new(key => 'aabc', data => { type => "status" }); 51ok($p, "Got a packet"); 52 53my $binary = $p->data; 54ok($binary, "Got binary representation of packet"); 55 56my $parser = Net::RNDC::Packet->new(key => 'aabc'); 57 58ok($parser->parse($binary), "Parsed binary representation of packet"); 59is($parser->{data}{_data}{type}, 'status', "Parsed packet has correct command"); 60 61# Too short 62my $res = $parser->parse('cat'); 63is($res, 0, 'Failed to parse bad string "cat"'); 64like($parser->error, qr/Expected 55 more bytes at least/, "Expected 55 bytes at least"); 65 66done_testing; 67