1#!perl -T 2use strict; 3use Test::More; 4use Net::Pcap; 5use lib 't'; 6use Utils; 7 8plan tests => 29; 9 10my $has_test_exception = eval "use Test::Exception; 1"; 11 12my($dev,$net,$mask,$pcap,$filter,$res,$err) = ('','',0,'','','',''); 13 14# Find a device 15$dev = find_network_device(); 16$res = Net::Pcap::lookupnet($dev, \$net, \$mask, \$err); 17 18SKIP: { 19 skip "pcap_compile_nopcap() is not available", 7 20 unless is_available('pcap_compile_nopcap'); 21 22 # Testing error messages 23 SKIP: { 24 skip "Test::Exception not available", 2 unless $has_test_exception; 25 26 # compile_nopcap() 27 throws_ok(sub { 28 Net::Pcap::compile_nopcap() 29 }, '/^Usage: Net::Pcap::compile_nopcap\(snaplen, linktype, fp, str, optimize, mask\)/', 30 "calling compile_nopcap() with no argument"); 31 32 throws_ok(sub { 33 Net::Pcap::compile_nopcap(0, 0, 0, 0, 0, 0) 34 }, '/^arg3 not a reference/', 35 "calling compile_nopcap() with incorrect argument type for arg2"); 36 } 37 38 # Testing compile_nopcap() 39 eval { $res = Net::Pcap::compile_nopcap(1024, DLT_EN10MB, \$filter, "tcp", 0, $mask) }; 40 is( $@, '', "compile_nopcap()" ); 41 is( $res, 0, " - result must be null: $res" ); 42 ok( defined $filter, " - \$filter is defined" ); 43 isa_ok( $filter, 'SCALAR', " - \$filter" ); 44 isa_ok( $filter, 'pcap_bpf_program_tPtr', " - \$filter" ); 45} 46 47 48SKIP: { 49 skip "must be run as root", 22 unless is_allowed_to_use_pcap(); 50 skip "no network device available", 22 unless find_network_device(); 51 52 # Open the device 53 $pcap = Net::Pcap::open_live($dev, 1024, 1, 100, \$err); 54 55 # Testing error messages 56 SKIP: { 57 skip "Test::Exception not available", 10 unless $has_test_exception; 58 59 # compile() errors 60 throws_ok(sub { 61 Net::Pcap::compile() 62 }, '/^Usage: Net::Pcap::compile\(p, fp, str, optimize, mask\)/', 63 "calling compile() with no argument"); 64 65 throws_ok(sub { 66 Net::Pcap::compile(0, 0, 0, 0, 0) 67 }, '/^p is not of type pcap_tPtr/', 68 "calling compile() with incorrect argument type for arg1"); 69 70 throws_ok(sub { 71 Net::Pcap::compile($pcap, 0, 0, 0, 0) 72 }, '/^arg2 not a reference/', 73 "calling compile() with incorrect argument type for arg2"); 74 75 # geterr() errors 76 throws_ok(sub { 77 Net::Pcap::geterr() 78 }, '/^Usage: Net::Pcap::geterr\(p\)/', 79 "calling compile() with no argument"); 80 81 throws_ok(sub { 82 Net::Pcap::geterr(0) 83 }, '/^p is not of type pcap_tPtr/', 84 "calling geterr() with incorrect argument type for arg1"); 85 86 # setfilter() errors 87 throws_ok(sub { 88 Net::Pcap::setfilter() 89 }, '/^Usage: Net::Pcap::setfilter\(p, fp\)/', 90 "calling setfilter() with no argument"); 91 92 throws_ok(sub { 93 Net::Pcap::setfilter(0, 0) 94 }, '/^p is not of type pcap_tPtr/', 95 "calling setfilter() with incorrect argument type for arg1"); 96 97 throws_ok(sub { 98 Net::Pcap::setfilter($pcap, 0) 99 }, '/^fp is not of type pcap_bpf_program_tPtr/', 100 "calling setfilter() with incorrect argument type for arg2"); 101 102 # freecode() errors 103 throws_ok(sub { 104 Net::Pcap::freecode() 105 }, '/^Usage: Net::Pcap::freecode\(fp\)/', 106 "calling freecode() with no argument"); 107 108 throws_ok(sub { 109 Net::Pcap::freecode(0) 110 }, '/^fp is not of type pcap_bpf_program_tPtr/', 111 "calling freecode() with incorrect argument type for arg1"); 112 113 } 114 115 # Testing compile() 116 eval { $res = Net::Pcap::compile($pcap, \$filter, "tcp", 0, $mask) }; 117 is( $@, '', "compile()" ); 118 is( $res, 0, " - result must be null: $res" ); 119 ok( defined $filter, " - \$filter is defined" ); 120 isa_ok( $filter, 'SCALAR', " - \$filter" ); 121 isa_ok( $filter, 'pcap_bpf_program_tPtr', " - \$filter" ); 122 123 # Testing geterr() 124 eval { $err = Net::Pcap::geterr($pcap) }; 125 is( $@, '', "geterr()" ); 126 if($res == 0) { 127 is( $err, '', " - \$err should be null" ) 128 } else { 129 isnt( $err, '', " - \$err should not be null" ) 130 } 131 132 # Testing setfilter() 133 eval { $res = Net::Pcap::setfilter($pcap, $filter) }; 134 is( $@, '', "setfilter()" ); 135 is( $res, 0, " - result should be null: $res" ); 136 137 # Testing freecode() 138 eval { Net::Pcap::freecode($filter) }; 139 is( $@, '', "freecode()" ); 140 141 # Testing geterr() 142 eval { $err = Net::Pcap::geterr($pcap) }; 143 is( $@, '', "geterr()" ); 144 if($res == 0) { 145 is( $err, '', " - \$err should be null" ) 146 } else { 147 isnt( $err, '', " - \$err should not be null" ) 148 } 149 150 Net::Pcap::close($pcap); 151} 152