root/branch/UMPA/tests/unit/test_sniffing/test_libpcap/test_pypcap.py @ 4849

Revision 4849, 3.6 kB (checked in by getxsick, 4 years ago)

add join() for threads for SendPacket?. tests are more reliably now

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4# Copyright (C) 2009 Adriano Monteiro Marques.
5#
6# Author: Bartosz SKOWRON <getxsick at gmail dot com>
7#
8# This library is free software; you can redistribute it and/or modify
9# it under the terms of the GNU Lesser General Public License as published
10# by the Free Software Foundation; either version 2.1 of the License, or
11# (at your option) any later version.
12#
13# This library is distributed in the hope that it will be useful, but
14# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16# License for more details.
17#
18# You should have received a copy of the GNU Lesser General Public License
19# along with this library; if not, write to the Free Software Foundation,
20# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22import pcap
23import py.test
24
25import umpa
26from umpa.protocols import IP, TCP
27from umpa.sniffing.libpcap import pypcap
28from umpa.utils.exceptions import UMPASniffingException
29from tests.utils import SendPacket
30
31class TestPypcap(object):
32    # tests are tivial...but it's a wrapper, right?
33    # TODO: test_dispatch()
34    def test_lookupdev(self):
35        try:
36            pcap.lookupdev()
37        except OSError:
38            py.test.raises(UMPASniffingException, pypcap.lookupdev)
39        else:
40            assert pypcap.lookupdev() == pcap.lookupdev()
41
42    def test_openlive(self):
43        try:
44            obj = pypcap.open_live()
45            assert obj.device == pcap.lookupdev()
46            obj = pypcap.open_live(device="any") # XXX can we use 'any'?
47            assert obj.device == "any"
48        except UMPASniffingException:
49            py.test.skip("no suitable devices for sniffing found. "
50                        "propably not sufficent priviliges.")
51
52    def test_findalldevs(self):
53        try:
54            assert pypcap.findalldevs() == pcap.findalldevs()
55        except UMPASniffingException:
56            py.test.skip("no suitable devices for sniffing found. "
57                        "propably not sufficent priviliges.")
58
59    def test_loop_and_filter(self):
60        # XXX it would rather looped than failing
61        def cbk(timestamp, pkt, *args):
62            assert args[0] == "foobar" # stupid isn't it? :)
63
64        try:
65            p = pypcap.open_live("any", to_ms=100)
66            p.setfilter("src host 1.2.3.4 and src port 99")
67            th = SendPacket(umpa.Packet(IP(source_address="1.2.3.4"),
68                                        TCP(source_port=99)))
69            th.start()
70            p.loop(1, cbk, "foobar")
71            th.join()
72
73            th = SendPacket(umpa.Packet(IP(source_address="1.2.3.4"),
74                                        TCP(source_port=99)), 5)
75            th.start()
76            p.loop(5, cbk, "foobar")
77            th.join()
78        except UMPASniffingException:
79            py.test.skip("no suitable devices for sniffing found. "
80                        "propably not sufficent priviliges.")
81
82    def test_next(self):
83        # can't test iterable of the object
84        # because pypcap doesn't raise StopIteration
85        amount = 5
86        th = SendPacket(umpa.Packet(IP(source_address="1.2.3.4"),
87                                    TCP(source_port=99)), amount)
88        th.start()
89        try:
90            p = pypcap.open_live("any", to_ms=100)
91            p.setfilter("src host 1.2.3.4 and src port 99")
92            for i in xrange(amount):
93                packet = p.next()
94        except UMPASniffingException:
95            py.test.skip("no suitable devices for sniffing found. "
96                        "propably not sufficent priviliges.")
97        finally:
98            th.join()
Note: See TracBrowser for help on using the browser.