File: 24-offline_filter.t

package info (click to toggle)
libnet-pcap-perl 0.21-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: perl: 2,155; pascal: 830; ansic: 5; makefile: 3
file content (48 lines) | stat: -rw-r--r-- 1,259 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!perl -T
use strict;
use File::Spec;
use Test::More;
use Net::Pcap;
use lib 't';
use Utils;

my $has_test_exception = eval "use Test::Exception; 1";

plan tests => 7;

my ($r, $err, $icmpfilter, $tcpfilter);

my $path = File::Spec->catfile(qw(t samples ping-ietf-20pk-be.dmp));
my $pcap = pcap_open_offline($path, \$err);
ok $pcap, 'open testfile';

$r = eval { pcap_compile($pcap, \$icmpfilter, 'icmp', 1, 0xffffffff) };
is $r, 0, 'compile icmp filter';

$r = eval { pcap_compile($pcap, \$tcpfilter, 'tcp', 1, 0xffffffff) };
is $r, 0, 'compile tcp filter';

SKIP: {
    skip "Test::Exception not available", 1 unless $has_test_exception;

    # check offline_filter() errors
    throws_ok(sub {
        pcap_offline_filter($tcpfilter, undef, undef)
    }, '/^arg2 not a hash ref/',
       "calling offline_filter() with no argument");
}

my (%header, $packet);
my ($n, $icmp, $tcp) = (0, 0, 0);

while (pcap_next_ex($pcap, \%header, \$packet) == 1) {
    $n++;
    $icmp++ if pcap_offline_filter($icmpfilter, \%header, $packet);
    $tcp++  if pcap_offline_filter($tcpfilter,  \%header, $packet);
}

pcap_close($pcap);

is $n,    20, 'read all packets';
is $icmp, 20, 'found all icmp packets';
is $tcp,   0, 'test for tcp packets in an icmp-only testfile';