📄 12-next.t
字号:
#!/usr/bin/perl -Tuse strict;use Test::More;use Net::Pcap;use lib 't';use Utils;my $total = 3; # number of packets to processplan skip_all => "slowness and random failures... testing pcap_next() is a PITA";plan skip_all => "must be run as root" unless is_allowed_to_use_pcap();plan skip_all => "no network device available" unless find_network_device();plan tests => $total * 16 + 3;eval "use Test::Exception"; my $has_test_exception = !$@;my($dev,$pcap,$net,$mask,$filter,$data,$r,$err) = ('','','','','','','');my %header = ();# Find a device and open it$dev = find_network_device();Net::Pcap::lookupnet($dev, \$net, \$mask, \$err);$pcap = Net::Pcap::open_live($dev, 1024, 1, 0, \$err);# Testing error messagesSKIP: { skip "Test::Exception not available", 3 unless $has_test_exception; # next() errors throws_ok(sub { Net::Pcap::next() }, '/^Usage: Net::Pcap::next\(p, pkt_header\)/', "calling next() with no argument"); throws_ok(sub { Net::Pcap::next(0, 0) }, '/^p is not of type pcap_tPtr/', "calling next() with incorrect argument type for arg1"); throws_ok(sub { Net::Pcap::next($pcap, 0) }, '/^arg2 not a hash ref/', "calling next() with incorrect argument type for arg2");}# Compile and set a filterNet::Pcap::compile($pcap, \$filter, "ip", 0, $mask);Net::Pcap::setfilter($pcap, $filter);# Test next()my $count = 0;for (1..$total) { my($packet, %header); eval { $packet = Net::Pcap::next($pcap, \%header) }; is( $@, '', "next()" ); for my $field (qw(len caplen tv_sec tv_usec)) { ok( exists $header{$field}, " - field '$field' is present" ); ok( defined $header{$field}, " - field '$field' is defined" ); like( $header{$field}, '/^\d+$/', " - field '$field' is a number" ); } ok( $header{caplen} <= $header{len}, " - coherency check: packet length (caplen <= len)" ); ok( defined $packet, " - packet is defined" ); is( length $packet, $header{caplen}, " - packet has the advertised size" ); $count++;}is( $count, $total, "all packets processed" );Net::Pcap::close($pcap);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -