1#!perl -T 2use strict; 3use File::Spec; 4use Test::More; 5use Net::Pcap; 6use lib 't'; 7use Utils; 8 9plan skip_all => "pcap_setnonblock() and pcap_getnonblock() are not available" 10 unless is_available('pcap_setnonblock'); 11plan tests => 23; 12 13my $has_test_exception = eval "use Test::Exception; 1"; 14 15my($dev,$pcap,$r,$err) = ('','','',''); 16 17# Testing error messages 18SKIP: { 19 skip "Test::Exception not available", 4 unless $has_test_exception; 20 21 # setnonblock() errors 22 throws_ok(sub { 23 Net::Pcap::setnonblock() 24 }, '/^Usage: Net::Pcap::setnonblock\(p\, nb, err\)/', 25 "calling setnonblock() with no argument"); 26 27 throws_ok(sub { 28 Net::Pcap::setnonblock(0, 0, 0) 29 }, '/^p is not of type pcap_tPtr/', 30 "calling setnonblock() with incorrect argument type"); 31 32 # getnonblock() errors 33 throws_ok(sub { 34 Net::Pcap::getnonblock() 35 }, '/^Usage: Net::Pcap::getnonblock\(p\, err\)/', 36 "calling getnonblock() with no argument"); 37 38 throws_ok(sub { 39 Net::Pcap::getnonblock(0, 0) 40 }, '/^p is not of type pcap_tPtr/', 41 "calling getnonblock() with incorrect argument type"); 42} 43 44SKIP: { 45 skip "must be run as root", 13 unless is_allowed_to_use_pcap(); 46 skip "no network device available", 13 unless find_network_device(); 47 48 # Find a device and open it 49 $dev = find_network_device(); 50 $pcap = Net::Pcap::open_live($dev, 1024, 1, 100, \$err); 51 isa_ok( $pcap, 'pcap_tPtr', "\$pcap" ); 52 53 for my $state (0, 1) { 54 # Testing setnonblock() 55 eval { $r = Net::Pcap::setnonblock($pcap, $state, \$err) }; 56 is( $@, '', "setnonblock() state=$state" ); 57 is( $err, '', " - err must be null" ); 58 is( $r, 0, " - should return zero" ); 59 60 # Testing getnonblock() 61 eval { $r = Net::Pcap::getnonblock($pcap, \$err) }; 62 is( $@, '', "getnonblock()" ); 63 is( $err, '', " - err must be null" ); 64 is( $r, $state, " - state must be $state" ); 65 } 66 67 Net::Pcap::close($pcap); 68} 69 70# Open a sample dump 71$pcap = Net::Pcap::open_offline(File::Spec->catfile(qw(t samples ping-ietf-20pk-be.dmp)), \$err); 72isa_ok( $pcap, 'pcap_tPtr', "\$pcap" ); 73 74# Testing error messages 75SKIP: { 76 skip "Test::Exception not available", 2 unless $has_test_exception; 77 78 throws_ok(sub { 79 Net::Pcap::setnonblock($pcap, 0, 0) 80 }, '/^arg3 not a reference/', 81 "calling setnonblock() with incorrect argument type for arg3"); 82 83 throws_ok(sub { 84 Net::Pcap::getnonblock($pcap, 0) 85 }, '/^arg2 not a reference/', 86 "calling getnonblock() with incorrect argument type for arg2"); 87} 88 89# Testing getnonblock() 90eval { $r = Net::Pcap::getnonblock($pcap, \$err) }; 91is( $@, '', "getnonblock()" ); 92is( $err, '', " - err must be null" ); 93is( $r, 0, " - state must be 0 for savefile" ); 94 95Net::Pcap::close($pcap); 96