File: __init__.py

package info (click to toggle)
python-pure-pcapy3 1.0.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 104 kB
  • sloc: python: 434; makefile: 8; sh: 4
file content (316 lines) | stat: -rwxr-xr-x 8,788 bytes parent folder | download
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import unittest, tempfile, struct
import pure_pcapy

class OpenerTest(unittest.TestCase):
	def test_open_nonexistant(self):
		self.assertRaises(pure_pcapy.PcapError, pure_pcapy.open_offline, 'hufioewhauiefhshcuidhuighureiahufiesa')
	def test_open_directory(self):
		self.assertRaises(pure_pcapy.PcapError, pure_pcapy.open_offline, './')
	
	def test_open_good_file(self):
		import os
		input_filename = tempfile.mktemp()
		f = open(input_filename, "wb")
		f.write(struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1))
		f.close()
		reader = pure_pcapy.open_offline(input_filename)
		self.assertTrue(isinstance(reader, pure_pcapy.Reader))
		os.unlink(input_filename)

class FixupTest(unittest.TestCase):
	def test_fixup_short(self):
		self.assertEqual(0x3412, pure_pcapy.fixup_swapped_short(0x1234))
	def test_fixup_long(self):
		self.assertEqual(0x78563412, pure_pcapy.fixup_swapped_long(0x12345678))

def create_pcap_file(parts):
	input = tempfile.TemporaryFile()
	for part in parts:
		input.write(part)
	input.seek(0)
	return input

class ReaderHeaderTest(unittest.TestCase):
	def test_open_short(self):
		input = tempfile.TemporaryFile()
		try:
			pure_pcapy.Reader(input)
			self.fail("exception not thrown")
		except pure_pcapy.PcapError as e:
			self.assertEqual("truncated dump file; tried to read 24 file header bytes, only got 0", e.args[0])
	
	def test_open_header_bad(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0x11111111, 2, 4, 0, 0, 65535, 1)
			])

		try:
			pure_pcapy.Reader(input)
			self.fail("exception not thrown")
		except pure_pcapy.PcapError as e:
			self.assertEqual("bad dump file format", e.args[0])
	
	def test_open_header_bigendian(self):
		input = create_pcap_file([
			struct.pack(">IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1)
			])

		reader = pure_pcapy.Reader(input)
		self.assertEqual(2, reader.version_major)
		self.assertEqual(4, reader.version_minor)
	
	def test_open_header_littleendian(self):
		input = create_pcap_file([
			struct.pack("<IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1)
			])

		reader = pure_pcapy.Reader(input)
		self.assertEqual(2, reader.version_major)
		self.assertEqual(4, reader.version_minor)
	
	def test_datalink(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1234)
			])

		reader = pure_pcapy.Reader(input)
		self.assertEqual(1234, reader.datalink())

	def test_getnonblock(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1234)
			])

		reader = pure_pcapy.Reader(input)
		self.assertEqual(0, reader.getnonblock())

class ReaderPacketTest(unittest.TestCase):
	def test_read_empty(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			])

		reader = pure_pcapy.Reader(input)
		res = reader.next()
		self.assertEqual((None, ''), res)
	
	def test_read_half_header(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			struct.pack("II", 10, 20),
			])

		reader = pure_pcapy.Reader(input)
		try:
			res = reader.next()
			self.fail("exception not thrown")
		except pure_pcapy.PcapError as e:
			self.assertEqual("truncated dump file; tried to read 16 header bytes, only got 8", e.args[0])
	
	def test_read_half_data(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			struct.pack("IIII", 10, 20, 4, 4),
			b"aa"
			])

		reader = pure_pcapy.Reader(input)
		try:
			res = reader.next()
			self.fail("exception not thrown")
		except pure_pcapy.PcapError as e:
			self.assertEqual("truncated dump file; tried to read 4 captured bytes, only got 2", e.args[0])

	def test_read_packet(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			struct.pack("IIII", 10, 20, 30, 40),
			b"x" * 30
			])

		reader = pure_pcapy.Reader(input)
		res = reader.next()
		self.assertEqual(tuple, type(res))
		self.assertEqual(2, len(res))
		self.assertTrue(isinstance(res[0], pure_pcapy.Pkthdr))
		self.assertEqual(bytes, type(res[1]))
		self.assertEqual(b"x"*30, res[1])

	def test_read_2_packets(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			struct.pack("IIII", 10, 20, 30, 40),
			b"a" * 30,
			struct.pack("IIII", 11, 20, 30, 40),
			b"b" * 30,
			])

		reader = pure_pcapy.Reader(input)
		res = reader.next()
		self.assertEqual(b"a"*30, res[1])
		res = reader.next()
		self.assertEqual(b"b"*30, res[1])

class ReaderLoopingTest(unittest.TestCase):
	def packet_counter(self, cnt):
		def callback(hdr, data):
			cnt[0]+=1
			self.assertEqual(cnt[0], hdr.getcaplen())
			self.assertEqual(cnt[0], len(data))
		return callback

	def test_dispatch_no_packets(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			])

		cnt = [0]
		reader = pure_pcapy.Reader(input)
		res = reader.dispatch(-1, self.packet_counter(cnt))
		self.assertEqual(0, cnt[0])
		self.assertEqual(0, res)
		res = reader.dispatch(0, self.packet_counter(cnt))
		self.assertEqual(0, cnt[0])
		self.assertEqual(0, res)
	
	def test_loop_no_packets(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			])

		cnt = [0]
		reader = pure_pcapy.Reader(input)
		res = reader.loop(0, self.packet_counter(cnt))
		self.assertEqual(0, cnt[0])
		self.assertEqual(None, res)
		res = reader.loop(0, self.packet_counter(cnt))
		self.assertEqual(0, cnt[0])
		self.assertEqual(None, res)
	
	def test_dispatch_1_packet_read_all(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			struct.pack("IIII", 10, 20, 1, 1),
			b"a",
			])

		cnt = [0]
		reader = pure_pcapy.Reader(input)
		res = reader.dispatch(-1, self.packet_counter(cnt))
		self.assertEqual(1, cnt[0])
		self.assertEqual(0, res)
	
	def test_loop_1_packet_read_all(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			struct.pack("IIII", 10, 20, 1, 1),
			b"a",
			])

		cnt = [0]
		reader = pure_pcapy.Reader(input)
		res = reader.loop(-1, self.packet_counter(cnt))
		self.assertEqual(1, cnt[0])
		self.assertEqual(None, res)
	
	def test_dispatch_1_packet_read_1(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			struct.pack("IIII", 10, 20, 1, 1),
			b"a",
			])

		cnt = [0]
		reader = pure_pcapy.Reader(input)
		res = reader.dispatch(1, self.packet_counter(cnt))
		self.assertEqual(1, cnt[0])
		self.assertEqual(1, res)
	
	def test_loop_1_packet_read_1(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			struct.pack("IIII", 10, 20, 1, 1),
			b"a",
			])

		cnt = [0]
		reader = pure_pcapy.Reader(input)
		res = reader.loop(1, self.packet_counter(cnt))
		self.assertEqual(1, cnt[0])
		self.assertEqual(None, res)
	
	def test_dispatch_2_packet_read_1(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			struct.pack("IIII", 10, 20, 1, 1),
			b"a",
			struct.pack("IIII", 10, 20, 2, 2),
			b"aa",
			])

		cnt = [0]
		reader = pure_pcapy.Reader(input)
		res = reader.dispatch(1, self.packet_counter(cnt))
		self.assertEqual(cnt[0], 1)
		self.assertEqual(res, 1)
	
	def test_loop_2_packet_read_1(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			struct.pack("IIII", 10, 20, 1, 1),
			b"a",
			struct.pack("IIII", 10, 20, 2, 2),
			b"aa",
			])

		cnt = [0]
		reader = pure_pcapy.Reader(input)
		res = reader.loop(1, self.packet_counter(cnt))
		self.assertEqual(cnt[0], 1)
		self.assertEqual(res, None)

class PkthdrTest(unittest.TestCase):
	def test_hdr_fields(self):
		input = create_pcap_file([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			struct.pack("IIII", 10, 20, 30, 40),
			b"x" * 30
			])
		
		reader = pure_pcapy.Reader(input)
		hdr, data = reader.next()
		self.assertEqual((10, 20), hdr.getts())
		self.assertEqual(30, hdr.getcaplen())
		self.assertEqual(40, hdr.getlen())

class DumperTest(unittest.TestCase):
	def test_replicate(self):
		import os

		original = b''.join([
			struct.pack("IHHIIII", 0xa1b2c3d4, 2, 4, 0, 0, 65535, 1),
			struct.pack("IIII", 10, 20, 30, 40),
			b"x" * 30])

		input = tempfile.TemporaryFile()
		input.write(original)
		input.seek(0)
		output_fd, output_filename = tempfile.mkstemp()

		reader = pure_pcapy.Reader(input)
		dumper = reader.dump_open(output_filename)
		dumper.dump(*reader.next())

		os.close(output_fd)
		verify = open(output_filename, "rb")
		replica = verify.read()
		os.unlink(output_filename)

		self.assertEqual(original, replica)


if __name__ == "__main__":
	unittest.main()