File: 10-fileno.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 (118 lines) | stat: -rw-r--r-- 3,792 bytes parent folder | download | duplicates (4)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!perl -T
use strict;
use File::Spec;
use Test::More;
use Net::Pcap;
use lib 't';
use Utils;

plan skip_all => "no network device available" unless find_network_device();
plan tests => 21;

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

my($dev,$pcap,$filehandle,$fileno,$err) = ('','','','','');

# Testing error messages
SKIP: {
    skip "Test::Exception not available", 6 unless $has_test_exception;

    # file() errors
    throws_ok(sub {
        Net::Pcap::file()
    }, '/^Usage: Net::Pcap::file\(p\)/', 
       "calling file() with no argument");

    throws_ok(sub {
        Net::Pcap::file(0)
    }, '/^p is not of type pcap_tPtr/', 
       "calling file() with incorrect argument type");

    # fileno() errors
    throws_ok(sub {
        Net::Pcap::fileno()
    }, '/^Usage: Net::Pcap::fileno\(p\)/', 
       "calling fileno() with no argument");

    throws_ok(sub {
        Net::Pcap::fileno(0)
    }, '/^p is not of type pcap_tPtr/', 
       "calling fileno() with incorrect argument type");

    skip "pcap_get_selectable_fd() is not available", 2 unless is_available('pcap_get_selectable_fd');
    # get_selectable_fd() errors
    throws_ok(sub {
        Net::Pcap::get_selectable_fd()
    }, '/^Usage: Net::Pcap::get_selectable_fd\(p\)/', 
       "calling get_selectable_fd() with no argument");

    throws_ok(sub {
        Net::Pcap::get_selectable_fd(0)
    }, '/^p is not of type pcap_tPtr/', 
       "calling get_selectable_fd() with incorrect argument type");
}

SKIP: {
    skip "must be run as root", 7 unless is_allowed_to_use_pcap();

    # Find a device and open it
    $dev = find_network_device();
    $pcap = Net::Pcap::open_live($dev, 1024, 1, 100, \$err);
    isa_ok( $pcap, 'pcap_tPtr', "\$pcap" );

    # Testing file()
    $filehandle = 0;
    eval { $filehandle = Net::Pcap::file($pcap) };
    is( $@, '', "file() on a live connection" );
    is( $filehandle, undef, " - returned filehandle should be undef" );

    # Testing fileno()
    $fileno = undef;
    eval { $fileno = Net::Pcap::fileno($pcap) };
    is( $@, '', "fileno() on a live connection" );
    like( $fileno, '/^\d+$/', " - fileno must be an integer: $fileno" );

    skip "pcap_get_selectable_fd() is not available", 2 unless is_available('pcap_get_selectable_fd');
    # Testing get_selectable_fd()
    $fileno = undef;
    eval { $fileno = Net::Pcap::get_selectable_fd($pcap) };
    is( $@, '', "get_selectable_fd() on a live connection" );
    like( $fileno, '/^\d+$/', " - fileno must be an integer: $fileno" );

    Net::Pcap::close($pcap);
}

# Open a sample dump
$pcap = Net::Pcap::open_offline(File::Spec->catfile(qw(t samples ping-ietf-20pk-be.dmp)), \$err);
isa_ok( $pcap, 'pcap_tPtr', "\$pcap" );

# Testing file()
TODO: {
    todo_skip "file() on a dump file currently causes a segmentation fault", 3;
    eval { $filehandle = Net::Pcap::file($pcap) };
    is( $@, '', "file() on a dump file" );
    ok( defined $filehandle, " - returned filehandle must be defined" );
    isa_ok( $filehandle, 'GLOB', " - \$filehandle" );
}

# Testing fileno()
eval { $fileno = Net::Pcap::fileno($pcap) };
is( $@, '', "fileno() on a dump file" );
# fileno() is documented to return -1 when called on save file, but seems 
# to always return an actual file number. 
TODO: {
    local $TODO = " => result should be -1";
    like( $fileno, '/^(?:\d+|-1)$/', " - fileno must be an integer: $fileno" );
}

# Testing get_selectable_fd()
SKIP: {
    skip "pcap_get_selectable_fd() is not available", 2 unless is_available('pcap_get_selectable_fd');
    $fileno = undef;
    eval { $fileno = Net::Pcap::get_selectable_fd($pcap) };
    is( $@, '', "get_selectable_fd() on a dump file" );
    like( $fileno, '/^\d+$/', " - fileno must be an integer: $fileno" );
}

Net::Pcap::close($pcap);