Skip to content

Commit 7802136

Browse files
committed
add tests for community-id-pcapfilter
1 parent fef2443 commit 7802136

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

tests/communityid_test.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import subprocess
1414
import sys
1515
import unittest
16+
import hashlib
1617

1718
try:
1819
import pylint.epylint
@@ -402,6 +403,22 @@ def _check_output_community_id_pcap(self, args):
402403
raise
403404
self.skipTest("This test requires dpkt")
404405

406+
def _check_output_community_id_pcapfilter(self, args):
407+
try:
408+
args = [self._scriptpath('community-id-pcapfilter')] + args
409+
subprocess.run(args, env=self.env)
410+
outfileposition = args.index("--output") + 1
411+
outfilename = args[outfileposition]
412+
with open(outfilename, "rb") as f:
413+
bytes = f.read()
414+
checksum = hashlib.sha256(bytes).hexdigest()
415+
os.remove("output.pcap")
416+
return checksum
417+
except subprocess.CalledProcessError as err:
418+
if err.output.find(b'This needs the dpkt Python module') < 0:
419+
raise
420+
self.skipTest("This test requires dpkt")
421+
405422
def test_communityid_pcap(self):
406423
# This only works if we have dpkt
407424
out = self._check_output_community_id_pcap([self._testfilepath('tcp.pcap')])
@@ -412,6 +429,10 @@ def test_communityid_pcap_json(self):
412429
out = self._check_output_community_id_pcap(['--json', self._testfilepath('tcp.pcap')])
413430
self.assertEqual(out, b'[{"proto": 6, "saddr": "128.232.110.120", "daddr": "66.35.250.204", "sport": 34855, "dport": 80, "communityid": "1:LQU9qZlK+B5F3KDmev6m5PMibrg="}, {"proto": 6, "saddr": "66.35.250.204", "daddr": "128.232.110.120", "sport": 80, "dport": 34855, "communityid": "1:LQU9qZlK+B5F3KDmev6m5PMibrg="}]\n')
414431

432+
def test_communityid_pcapfilter(self):
433+
out = self._check_output_community_id_pcapfilter(['--filter', '1:p78FQ5Gn8XFgjlKgugj92+uTUDk=', '--output', 'output.pcap', self._testfilepath('tcp_multi.pcap')])
434+
self.assertEqual(out, 'f46ba2303318c400c257c08a2b70f412fc307694ede788baa96142b118b28a94')
435+
415436
def test_communityid_tcpdump(self):
416437
# This uses subprocess.check_output(..., input=...) which was added in 3.4:
417438
if sys.version_info[0] < 3 or sys.version_info[1] < 4:
@@ -424,6 +445,5 @@ def test_communityid_tcpdump(self):
424445
first_line = out.decode('ascii').split('\n')[0].strip()
425446
self.assertEqual(first_line, '1071580904.891921 IP 1:LQU9qZlK+B5F3KDmev6m5PMibrg= 128.232.110.120:34855 > 66.35.250.204.80: Flags [S], seq 3201037957, win 5840, options [mss 1460,sackOK,TS val 87269134 ecr 0,nop,wscale 0], length 0')
426447

427-
428448
if __name__ == '__main__':
429449
unittest.main()

tests/tcp_multi.pcap

9.56 KB
Binary file not shown.

0 commit comments

Comments
 (0)