13
13
import subprocess
14
14
import sys
15
15
import unittest
16
+ import hashlib
16
17
17
18
try :
18
19
import pylint .epylint
@@ -402,6 +403,22 @@ def _check_output_community_id_pcap(self, args):
402
403
raise
403
404
self .skipTest ("This test requires dpkt" )
404
405
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
+
405
422
def test_communityid_pcap (self ):
406
423
# This only works if we have dpkt
407
424
out = self ._check_output_community_id_pcap ([self ._testfilepath ('tcp.pcap' )])
@@ -412,6 +429,10 @@ def test_communityid_pcap_json(self):
412
429
out = self ._check_output_community_id_pcap (['--json' , self ._testfilepath ('tcp.pcap' )])
413
430
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 ' )
414
431
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
+
415
436
def test_communityid_tcpdump (self ):
416
437
# This uses subprocess.check_output(..., input=...) which was added in 3.4:
417
438
if sys .version_info [0 ] < 3 or sys .version_info [1 ] < 4 :
@@ -424,6 +445,5 @@ def test_communityid_tcpdump(self):
424
445
first_line = out .decode ('ascii' ).split ('\n ' )[0 ].strip ()
425
446
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' )
426
447
427
-
428
448
if __name__ == '__main__' :
429
449
unittest .main ()
0 commit comments