/
test_models.py
180 lines (157 loc) · 6.31 KB
/
test_models.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2009 Adriano Monteiro Marques.
#
# Author: Bartosz SKOWRON <getxsick at gmail dot com>
#
# This library is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import Queue
import threading
import umit.umpa
import umit.umpa.sniffing
from umit.umpa.protocols import IP, TCP, UDP
from umit.umpa.extensions import models
from umit.umpa.utils.exceptions import UMPAException
from tests.utils import SendPacket
import py.test
class SniffThread(threading.Thread):
def __init__(self, filter, device, queue):
super(SniffThread, self).__init__()
self._filter = filter
self._device = device
self._queue = queue
class RevPortsTCPThread(SniffThread):
def run(self):
pkt = umit.umpa.sniffing.sniff(2, device=self._device,
filter=self._filter)
try:
assert pkt[0].ip.src == "127.0.0.1"
assert pkt[0].ip.dst == "127.0.0.1"
assert pkt[0].tcp.srcport == 80
assert pkt[0].tcp.dstport == 0
assert pkt[1].ip.src == "127.0.0.1"
assert pkt[1].ip.dst == "127.0.0.1"
assert pkt[1].tcp.srcport == 0
assert pkt[1].tcp.dstport == 80
except Exception, e:
self._queue.put(e)
class RevPortsUDPThread(SniffThread):
def run(self):
pkt = umit.umpa.sniffing.sniff(2, device=self._device,
filter=self._filter)
try:
assert pkt[0].ip.src == "127.0.0.1"
assert pkt[0].ip.dst == "127.0.0.1"
assert pkt[0].udp.srcport == 80
assert pkt[0].udp.dstport == 0
assert pkt[1].ip.src == "127.0.0.1"
assert pkt[1].ip.dst == "127.0.0.1"
assert pkt[1].udp.srcport == 0
assert pkt[1].udp.dstport == 80
except Exception, e:
self._queue.put(e)
class RevHostsThread(SniffThread):
def run(self):
pkt = umit.umpa.sniffing.sniff(2, device=self._device, filter=self._filter)
try:
assert pkt[0].ip.src == "67.205.14.183"
assert pkt[0].ip.dst == "127.0.0.1"
assert pkt[1].ip.src == "127.0.0.1"
assert pkt[1].ip.dst == "67.205.14.183"
except Exception, e:
self._queue.put(e)
class ForwardThread(SniffThread):
def run(self):
pkt = umit.umpa.sniffing.sniff(2, device=self._device, filter=self._filter)
try:
assert pkt[0].ip.src == "127.0.0.1"
assert pkt[0].ip.dst == "67.205.14.183"
assert pkt[1].ip.src == "127.0.0.1"
assert pkt[1].ip.dst == "127.0.0.1"
except Exception, e:
self._queue.put(e)
class TestModels(object):
pass
class TestReact(TestModels):
def test_revports_tcp(self):
# use queue to communicate between threads
# py.test doesn't catch assertions from threads by itself
queue = Queue.Queue()
th = SendPacket(umit.umpa.Packet(IP(), TCP(srcport=80, dstport=0)))
th.start()
th2 = RevPortsTCPThread("host 127.0.0.1 and port 80", "lo", queue)
th2.start()
models.react(1, filter="host 127.0.0.1 and port 80", device="lo",
revports=True)
th.join()
th2.join()
if not queue.empty():
err = queue.get()
raise AssertionError(err)
queue.join()
def test_revports_udp(self):
# use queue to communicate between threads
# py.test doesn't catch assertions from threads by itself
queue = Queue.Queue()
th = SendPacket(umit.umpa.Packet(IP(), UDP(srcport=80, dstport=0)))
th.start()
th2 = RevPortsUDPThread("host 127.0.0.1 and port 80", "lo", queue)
th2.start()
models.react(1, filter="host 127.0.0.1 and port 80", device="lo",
revports=True)
th.join()
th2.join()
if not queue.empty():
err = queue.get()
raise AssertionError(err)
queue.join()
def test_revhosts(self):
# use queue to communicate between threads
# py.test doesn't catch assertions from threads by itself
queue = Queue.Queue()
th = SendPacket(umit.umpa.Packet(IP(src="67.205.14.183", dst="127.0.0.1")))
th.start()
th2 = RevHostsThread("host 67.205.14.183", "any", queue)
th2.start()
models.react(1, filter="host 67.205.14.183", device="any", revhosts=True)
th.join()
th2.join()
if not queue.empty():
err = queue.get()
raise AssertionError(err)
queue.join()
def test_forward(self):
# use queue to communicate between threads
# py.test doesn't catch assertions from threads by itself
queue = Queue.Queue()
th = SendPacket(umit.umpa.Packet(IP(src="127.0.0.1", dst="67.205.14.183"), TCP(srcport=888)))
th.start()
th2 = ForwardThread("host 127.0.0.1 and port 888", "any", queue)
th2.start()
models.react(1, filter="host 67.205.14.183 and port 888", device="any",
forward="127.0.0.1")
th.join()
th2.join()
if not queue.empty():
err = queue.get()
raise AssertionError(err)
queue.join()
def test_wrongargs(self):
py.test.raises(UMPAException, "models.react(1, foo=True)")
py.test.raises(UMPAException,
"models.react(1, foo=True, revports=True)")
py.test.raises(UMPAException,
"models.react(1, foo=False, revhosts=True)")
py.test.raises(UMPAException, "models.react(1, foo=True, bar=False)")