1#!perl -T
2use strict;
3use Test::More;
4use Net::Pcap;
5use lib 't';
6use Utils;
7
8my @sizes = (128, 512, 1024, 2048, 4096, 8192, int(10000*rand), int(10000*rand), int(10000*rand), int(10000*rand));  # snapshot sizes
9
10plan skip_all => "must be run as root" unless is_allowed_to_use_pcap();
11plan skip_all => "no network device available" unless find_network_device();
12plan tests => @sizes * 2 + 2;
13
14my $has_test_exception = eval "use Test::Exception; 1";
15
16my($dev,$pcap,$snapshot,$err) = ('','','','');
17
18# Testing error messages
19SKIP: {
20    skip "Test::Exception not available", 2 unless $has_test_exception;
21
22    # snapshot() errors
23    throws_ok(sub {
24        Net::Pcap::snapshot()
25    }, '/^Usage: Net::Pcap::snapshot\(p\)/',
26       "calling snapshot() with no argument");
27
28    throws_ok(sub {
29        Net::Pcap::snapshot(0)
30    }, '/^p is not of type pcap_tPtr/',
31       "calling snapshot() with incorrect argument type");
32}
33
34# Find a device
35$dev = find_network_device();
36
37for my $size (@sizes) {
38    # Open the device
39    $pcap = Net::Pcap::open_live($dev, $size, 1, 100, \$err);
40
41    # Testing snapshot()
42    $snapshot = 0;
43    eval { $snapshot = Net::Pcap::snapshot($pcap) };
44    is( $@, '', "snapshot()" );
45    is( $snapshot, $size, " - snapshot has the expected size" );
46    Net::Pcap::close($pcap);
47}
48
49