19-breakloop.t

来自「SinFP是一种新的识别对方计算机操作系统类型的工具」· T 代码 · 共 62 行

T
62
字号
#!/usr/bin/perl -Tuse strict;use Test::More;use Net::Pcap;use lib 't';use Utils;plan skip_all => "pcap_breakloop() is not available" unless is_available('pcap_breakloop');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 => 5;eval "use Test::Exception"; my $has_test_exception = !$@;my $total = 10;  # number of packets to processmy($dev,$pcap,$dumper,$dump_file,$err) = ('','','','');# Find a device and open it$dev = find_network_device();$pcap = Net::Pcap::open_live($dev, 1024, 1, 0, \$err);# Testing error messagesSKIP: {    skip "Test::Exception not available", 2 unless $has_test_exception;    # breakloop() errors    throws_ok(sub {        Net::Pcap::breakloop()    }, '/^Usage: Net::Pcap::breakloop\(p\)/',        "calling breakloop() with no argument");    throws_ok(sub {        Net::Pcap::breakloop(0)    }, '/^p is not of type pcap_tPtr/',        "calling breakloop() with incorrect argument type");}# Testing stats()my $user_text = "Net::Pcap test suite";my $count = 0;sub process_packet {    my($user_data, $header, $packet) = @_;    my %stats = ();    if(++$count == $total/2) {        eval { Net::Pcap::breakloop($pcap) };        is( $@, '', "breakloop()" );    }}my $r = Net::Pcap::loop($pcap, $total, \&process_packet, $user_text);ok( ($r == -2 or $r == $count), "checking loop() return value" );is( $count, $total/2, "half the packets processed" );# Note: I'm not sure why $count is always $total/2 even when $r == -2# Maybe I just don't understand what the docmentation says. # Or maybe I shouldn't write tests at 02:10 %-)Net::Pcap::close($pcap);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?