-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_tests.py
291 lines (250 loc) · 11 KB
/
unit_tests.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from unittest import TestCase
from scapy.all import *
from scapy.layers.inet import IP, UDP, TCP, ICMP
from flow import FlowUDP, FlowTCP, FlowICMP, FlowSock
from fx import *
from genetic_engine import NetworkGenome, network_initializer, translate_nodes_and_nets, delete_node, network_mutator, \
node_mutator
from nets_manager import Translator
class TestFX(TestCase):
def test_random(self):
f = FX(1, 100, int, [[0.2, 42], [1.0, 9]])
counts = {}
for i in xrange(10000):
r = f.random()
if r in counts.keys():
counts[r] += 1
else:
counts[r] = 1
assert len(counts) == 2
assert 0.23 < float(counts[42]) / counts[9] < 0.27
def test_mutation(self):
f = FX(1, 100, int, [[0.2, 42], [1.0, 9]])
old_points = []
for p in f.points:
old_points.append(p[:])
f.mutation()
success = False
if len(f.points) != len(old_points):
success = True
else:
for i in xrange(len(f.points)):
if (f.points[i][0] != old_points[i][0]) or (f.points[i][1] != old_points[i][1]):
success = True
if not (0.0 <= f.points[i][0] <= 1.0):
raise ValueError("Некорректное изменение вероятности")
if not (f.v_from <= f.points[i][1] <= f.v_to):
raise ValueError("Некорректное изменение значения вероятности")
if f.points[-1][0] != 1.0:
raise ValueError("Последняя точка всегда 1.0")
assert success
def test_copy(self):
ftp = FTP([[0, 0.075]])
ftp2 = FTP([[1, 0.1]])
ftp.copy(ftp2)
assert ftp2.points[0][1] == 0.075
ftp2.points[0][1] = 3
assert ftp.points[0][1] == 0.075
assert ftp.v_delta == ftp2.v_delta
def test_clone(self):
ftp = FTP([[0.5, 0.1], [1.0, 0.05]])
ftp2 = ftp.clone()
assert isinstance(ftp2, FTP)
class TestTranslator(TestCase):
def test_ip_generate(self):
nets = [(24, 'l'), (8, 'r')]
nodes = [0, 1]
t = Translator(nets, nodes)
assert len(t.node2ip[0].split('.')) == 4
assert t.node2pos[1] == 'r'
assert t.ip2pos[t.node2ip[0]] == 'l'
pat = re.compile("\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}")
assert pat.match(t.node2ip[1])
class TestFlowSock(TestCase):
def test_copy(self):
ftp = FTP([[1.0, 0.1]])
flp = FLP([[1.0, 100]])
fttl = FTTL([[1.0, 1]])
ftf = FTF([[1.0, 100]])
fhf = FHF([[0.5, 1]])
f = FlowSock(9999, 42, 0, 1, ftp, flp, fttl, ftp, flp, fttl, ftf, fhf)
g = FlowSock(19, 20, 2, 3, ftp, flp, fttl, ftp, flp, fttl, ftf, fhf)
f.copy(g)
assert g.port1 == 9999
g.fhf.points = []
assert len(f.fhf.points) > 0
def test_clone(self):
ftp = FTP([[1.0, 0.1]])
flp = FLP([[1.0, 100]])
fttl = FTTL([[1.0, 1]])
ftf = FTF([[1.0, 100]])
fhf = FHF([[0.5, 1]])
f = FlowSock(9999, 42, 0, 1, ftp, flp, fttl, ftp, flp, fttl, ftf, fhf)
g = f.clone()
f.port1 = 12
assert g.port1 == 9999
g.fhf.points = []
assert len(f.fhf.points) > 0
class TestFlowUdp(TestCase):
def test_generate(self):
ftp = FTP([[1.0, 0.1]])
flp = FLP([[1.0, 100]])
fttl = FTTL([[1.0, 1]])
ftf = FTF([[1.0, 100]])
fhf = FHF([[0.5, 1]])
f = FlowUDP(9999, 42, 0, 1, ftp, flp, fttl, ftp, flp, fttl, ftf, fhf)
nets = [(8, 'l'), (16, 'r')]
nodes = [0, 1]
t = Translator(nets, nodes)
packs = f.generate(translator=t, t0=0)
assert len(packs) > 0
for p in packs:
assert isinstance(p, IP)
assert isinstance(p.payload, UDP)
class TestFlowTCP(TestCase):
def test_generate(self):
ftp = FTP([[1.0, 0.1]])
flp = FLP([[1.0, 100]])
fttl = FTTL([[1.0, 1]])
ftf = FTF([[1.0, 100]])
fhf = FHF([[0.5, 1]])
f = FlowTCP(9999, 42, 0, 1, ftp, flp, fttl, ftp, flp, fttl, ftf, fhf)
nets = [(8, 'l'), (16, 'r')]
nodes = [0, 1]
t = Translator(nets, nodes)
packs = f.generate(translator=t, t0=0)
assert len(packs) > 0
for p in packs:
assert isinstance(p, IP)
assert isinstance(p.payload, TCP)
class TestFlowICMP(TestCase):
def test_generate(self):
ftp = FTP([[1.0, 0.1]])
flp = FLP([[1.0, 100]])
fttl = FTTL([[1.0, 1]])
ftf = FTF([[1.0, 100]])
fhf = FHF([[0.5, 1]])
f = FlowICMP(0, 8, 0, 1, ftp, flp, fttl, ftp, flp, fttl, ftf, fhf)
nets = [(8, 'l'), (16, 'r')]
nodes = [0, 1]
t = Translator(nets, nodes)
packs = f.generate(translator=t, t0=0)
assert len(packs) > 0
for p in packs:
assert isinstance(p, IP)
assert isinstance(p.payload, ICMP)
def test_clone(self):
ftp = FTP([[1.0, 0.1]])
flp = FLP([[1.0, 100]])
fttl = FTTL([[1.0, 1]])
ftf = FTF([[1.0, 100]])
fhf = FHF([[0.5, 1]])
f = FlowICMP(0, 1, 0, 1, ftp, flp, fttl, ftp, flp, fttl, ftf, fhf)
g = f.clone()
f.type1 = 12
assert g.type1 == 0
g.fhf.points = []
assert len(f.fhf.points) > 0
class TestNetworkGenome(TestCase):
def test_clone(self):
fflow = FFlow([[0.1, 1], [0.3, 2], [0.5, 3], [1.0, 4]])
ftp = FTP([[0.1, 0.01], [0.2, 0.02], [0.8, 0.04], [1.0, 0.06]])
flp1 = FLP([[0.1, 110], [0.3, 220], [0.5, 330], [1.0, 440]])
flp2 = FLP([[0.1, 110], [0.3, 220], [0.7, 330], [1.0, 440]])
fttl = FTTL([[0.1, 0], [0.3, 5], [0.5, 15], [1.0, 25]])
ftf = FTF([[0.2, 10], [0.3, 20], [0.6, 30], [1.0, 40]])
fhf = FHF([[0.5, 1]])
f1 = FlowUDP(9995, 42, 0, 1, ftp, flp1, fttl, ftp, flp2, fttl, ftf, fhf)
f2 = FlowUDP(9999, 40, 0, 2, ftp, flp2, fttl, ftp, flp1, fttl, ftf, fhf)
f3 = FlowTCP(123, 456, 1, 2, ftp, flp1, fttl, ftp, flp2, fttl, ftf, fhf)
f4 = FlowTCP(8899, 9800, 2, 0, ftp, flp2, fttl, ftp, flp1, fttl, ftf, fhf)
flows = [f1, f2, f3, f4]
nets = [(8, 'l'), (16, 'r'), (8, 'r')]
nodes = [0, 1, 2, 0]
o1 = NetworkGenome(nets, nodes, flows, fflow, 42.0)
o2 = o1.clone()
assert len(o2.nets) == 3
o2.nets[1] = (16, 'l')
assert o1.nets[1][1] == 'r'
def test_network_initializer(self):
net = network_initializer(None)
assert isinstance(net, NetworkGenome)
def test_translate_nodes_and_nets(self):
ftp = FTP([[0.1, 0.010], [0.2, 0.020], [0.8, 0.040], [1.0, 0.060]])
flp1 = FLP([[0.1, 110], [0.3, 220], [0.5, 330], [1.0, 440]])
flp2 = FLP([[0.1, 110], [0.3, 220], [0.7, 330], [1.0, 440]])
fttl = FTTL([[0.1, 0], [0.3, 5], [0.5, 15], [1.0, 25]])
ftf = FTF([[0.2, 10], [0.3, 20], [0.6, 30], [1.0, 40]])
fhf = FHF([[0.5, 1]])
f1 = FlowUDP(9995, 42, 0, 1, ftp, flp1, fttl, ftp, flp2, fttl, ftf, fhf)
f2 = FlowUDP(9999, 40, 1, 0, ftp, flp2, fttl, ftp, flp1, fttl, ftf, fhf)
f3 = FlowTCP(123, 456, 0, 1, ftp, flp1, fttl, ftp, flp2, fttl, ftf, fhf)
f4 = FlowTCP(8899, 9800, 1, 0, ftp, flp2, fttl, ftp, flp1, fttl, ftf, fhf)
flows = [f1, f2, f3, f4]
nodes = [0, 1]
s_nets = [('a', 'l'), ('a', 'l')]
b_nets = [('b', 'r'), ('b', 'r')]
res_nets, res_nodes = translate_nodes_and_nets(flows, nodes, nodes, s_nets, b_nets,
lambda x: 's' if x < 2 else 'b')
assert len(res_nets) == 4
assert len(res_nodes) == 4
def test_delete_node(self):
fflow = FFlow([[0.1, 1], [0.3, 2], [0.5, 3], [1.0, 4]])
ftp = FTP([[0.1, 0.01], [0.2, 0.020], [0.8, 0.040], [1.0, 0.060]])
flp1 = FLP([[0.1, 110], [0.3, 220], [0.5, 330], [1.0, 440]])
flp2 = FLP([[0.1, 110], [0.3, 220], [0.7, 330], [1.0, 440]])
fttl = FTTL([[0.1, 0], [0.3, 5], [0.5, 15], [1.0, 25]])
ftf = FTF([[0.2, 10], [0.3, 20], [0.6, 30], [1.0, 40]])
fhf = FHF([[0.5, 1]])
f1 = FlowUDP(9995, 42, 0, 1, ftp, flp1, fttl, ftp, flp2, fttl, ftf, fhf)
f2 = FlowUDP(9999, 40, 0, 2, ftp, flp2, fttl, ftp, flp1, fttl, ftf, fhf)
f3 = FlowTCP(123, 456, 1, 2, ftp, flp1, fttl, ftp, flp2, fttl, ftf, fhf)
f4 = FlowTCP(8899, 9800, 2, 0, ftp, flp2, fttl, ftp, flp1, fttl, ftf, fhf)
flows = [f1, f2, f3, f4]
nets = [(8, 'l'), (16, 'r'), (8, 'r')]
nodes = [0, 1, 2, 0]
o1 = NetworkGenome(nets, nodes, flows, fflow, 42.0)
delete_node(o1, 2)
assert len(o1.nodes) == 3
# assert len(o1.nets) == 2
assert len(o1.flows) == 1
def test_network_mutator(self):
fflow = FFlow([[0.1, 1], [0.3, 2], [0.5, 3], [1.0, 4]])
ftp = FTP([[0.1, 0.010], [0.2, 0.020], [0.8, 0.040], [1.0, 0.060]])
flp1 = FLP([[0.1, 110], [0.3, 220], [0.5, 330], [1.0, 440]])
flp2 = FLP([[0.1, 110], [0.3, 220], [0.7, 330], [1.0, 440]])
fttl = FTTL([[0.1, 0], [0.3, 5], [0.5, 15], [1.0, 25]])
ftf = FTF([[0.2, 10], [0.3, 20], [0.6, 30], [1.0, 40]])
fhf = FHF([[0.5, 1]])
f1 = FlowUDP(9995, 42, 0, 1, ftp, flp1, fttl, ftp, flp2, fttl, ftf, fhf)
f2 = FlowUDP(9999, 40, 0, 2, ftp, flp2, fttl, ftp, flp1, fttl, ftf, fhf)
f3 = FlowTCP(123, 456, 1, 2, ftp, flp1, fttl, ftp, flp2, fttl, ftf, fhf)
f4 = FlowTCP(8899, 9800, 2, 0, ftp, flp2, fttl, ftp, flp1, fttl, ftf, fhf)
flows = [f1, f2, f3, f4]
nets = [(8, 'l'), (16, 'r'), (8, 'r')]
nodes = [0, 1, 2, 0]
o1 = NetworkGenome(nets, nodes, flows, fflow, 42.0)
old_nets = nets[:]
network_mutator(o1)
assert len(o1.nets) != 3 or any(old_nets[i] != o1.nets[i] for i in xrange(3))
def test_node_mutator(self):
fflow = FFlow([[0.1, 1], [0.3, 2], [0.5, 3], [1.0, 4]])
ftp = FTP([[0.1, 0.010], [0.2, 0.020], [0.8, 0.040], [1.0, 0.060]])
flp1 = FLP([[0.1, 110], [0.3, 220], [0.5, 330], [1.0, 440]])
flp2 = FLP([[0.1, 110], [0.3, 220], [0.7, 330], [1.0, 440]])
fttl = FTTL([[0.1, 0], [0.3, 5], [0.5, 15], [1.0, 25]])
ftf = FTF([[0.2, 10], [0.3, 20], [0.6, 30], [1.0, 40]])
fhf = FHF([[0.5, 1]])
f1 = FlowUDP(9995, 42, 0, 1, ftp, flp1, fttl, ftp, flp2, fttl, ftf, fhf)
f2 = FlowUDP(9999, 40, 0, 2, ftp, flp2, fttl, ftp, flp1, fttl, ftf, fhf)
f3 = FlowTCP(123, 456, 1, 2, ftp, flp1, fttl, ftp, flp2, fttl, ftf, fhf)
f4 = FlowTCP(8899, 9800, 2, 0, ftp, flp2, fttl, ftp, flp1, fttl, ftf, fhf)
flows = [f1, f2, f3, f4]
nets = [(8, 'l'), (16, 'r'), (8, 'r')]
nodes = [0, 1, 2, 0]
o1 = NetworkGenome(nets, nodes, flows, fflow, 42.0)
old_nodes = nodes[:]
node_mutator(o1)
assert len(o1.nodes) != 4 or any(old_nodes[i] != o1.nodes[i] for i in xrange(4))