def test_filter_frames_higher_classes(self): # Filter on each protocol with self.assertRaises(InputParameterError): filtered, ignored = Frame.filter_frames(self.frames, InetPacketValue) with self.assertRaises(InputParameterError): filtered, ignored = Frame.filter_frames(self.frames, PacketValue) with self.assertRaises(InputParameterError): filtered, ignored = Frame.filter_frames(self.frames, Value)
def test_filter_frames(self): # Get all the protocols protocols = Dissector.get_implemented_protocols() # Filter on each protocol for protocol in protocols: # Filter on each protocol filtered, ignored = Frame.filter_frames(self.frames, protocol) # Get the id of frames with this protocol ids = self.frames_with_protocol(protocol) # Check the two datas received self.assertEqual(type(filtered), list) for f in filtered: self.assertEqual(type(f), Frame) self.assertEqual(type(ignored), list) for i in ignored: self.assertEqual(type(i), Frame) self.assertEqual(len(filtered) + len(ignored), len(self.frames)) # Check the length of filtered self.assertEqual(len(filtered), len(ids)) # Check that each element goes together for frame in filtered: dictionnary = frame.dict() self.assertIn(dictionnary['id'], ids)
def preprocess( cls, capture: Capture, expected_frames_pattern: list_of(Value) ) -> (list_of(Conversation), list_of(Frame)): """ Preprocess and filter the frames of the capture into test case related conversations. :param Capture: The capture which will be filtered/preprocessed :return: """ # Get informations from the test case protocol = CoapPrivacyTestCase.get_protocol() nodes = CoapPrivacyTestCase.get_nodes_identification_templates() conversations = [] ignored = [] if not nodes or len(nodes) < 2: raise ValueError( 'Expected at leaset two nodes declaration from the test case') if not protocol: raise ValueError( 'Expected a protocol under test declaration from the test case' ) # Get protocol related frames frames, ignored = Frame.filter_frames(capture.frames, protocol) # For privacy we dont need a preprocess for splitting the frames into conversations, we just analize it all c = Conversation(nodes) for f in frames: c.append(f) conversations.append(c) return conversations, ignored
def extract_all_coap_conversations( cls, capture: Capture) -> (list_of(Conversation), list_of(Frame)): protocol = cls.get_protocol() nodes = cls.get_nodes_identification_templates() conversations = [] ignored = [] # TODO what happens if no protocol declared on the test case? if not nodes or len(nodes) < 2: raise ValueError( 'Expected at leaset two nodes declaration from the test case') if not protocol: raise ValueError( 'Expected a protocol under test declaration from the test case' ) # Get the frames related with the protocol under test frames, ignored = Frame.filter_frames(capture.frames, protocol) # Map a token to the corresponding conversations. tkn_to_conv = OrderedDict() # Map a MID of a frame to it's containing token. # It's allow us to know to which conversation ACK frames belongs to # even though ACK do not contains token. mid_to_tkn = OrderedDict() # A set of CMID of messages for which we already saw an ACK. # This set allows use to detect duplicated ACK. acknowledged_CMID = set() for frame in frames: CMID = frame[CoAP]["mid"] CTOK = frame[CoAP]["tok"] if frame[CoAP]["type"] == 2 or frame[CoAP]["type"] == 3: if CMID in acknowledged_CMID: # A duplicated ACK or RST ignored.append(frame) continue else: acknowledged_CMID.add(CMID) if CMID not in mid_to_tkn: # An orphan ACK or RST ignored.append(frame) else: tkn_to_conv[mid_to_tkn[CMID]].append(frame) else: if CTOK in tkn_to_conv: tkn_to_conv[CTOK].append(frame) else: # First time we encounter the token "CTOK". conv = Conversation(nodes) conv.append(frame) tkn_to_conv[CTOK] = conv mid_to_tkn[CMID] = CTOK conversations = [tkn_to_conv[k] for k in tkn_to_conv.keys()] return conversations, ignored
def test_filter_frames_list_of_non_frame(self): # Get all the protocols protocols = Dissector.get_implemented_protocols() # Filter on each protocol for protocol in protocols: # Filter on none protocol with self.assertRaises(TypeError): filtered, ignored = Frame.filter_frames(protocols, protocol)
def test_filter_frames_only_single_frame(self): # Get all the protocols protocols = Dissector.get_implemented_protocols() # Filter on each protocol for protocol in protocols: # Filter on none protocol with self.assertRaises(InputParameterError): filtered, ignored = Frame.filter_frames( self.frames[0], protocol)
def test_filter_frames_list_of_frame_with_a_non_frame(self): # Get all the protocols protocols = Dissector.get_implemented_protocols() # Insert a non frame object self.frames.insert(1, protocols[1]) # Filter on each protocol for protocol in protocols: # Filter on none protocol with self.assertRaises(TypeError): filtered, ignored = Frame.filter_frames(self.frames, protocol)
def preprocess( cls, capture: Capture, expected_frames_pattern:list_of(Value) ) -> (list_of(Conversation), list_of(Frame)): """ Preprocess and filter the frames of the capture into test case related conversations. :param Capture: The capture which will be filtered/preprocessed :return: """ # TODO assert is subclass of TesCase? # Get informations from the test case protocol = SixlowpanTestCase.get_protocol() nodes = TestCase.get_nodes_identification_templates() conversations = [] ignored = [] # TODO what happens if no protocol declared on the test case? if not nodes or len(nodes) < 2: raise ValueError( 'Expected at leaset two nodes declaration from the test case' ) if not protocol: raise ValueError( 'Expected a protocol under test declaration from the test case' ) # If there is no stimuli at all if not expected_frames_pattern or len(expected_frames_pattern) == 0: raise NoStimuliFoundForTestcase( 'Expected stimuli declaration from the test case' ) # Get the frames filtered on the protocol frames, ignored = Frame.filter_frames(capture.frames, protocol) # Get a counter of the current stimuli sti_count = 0 current_conversation = None nb_stimulis = len(expected_frames_pattern) for frame in frames: # If the frame matches a stimuli if expected_frames_pattern[sti_count].match(frame[protocol]): # If it's the first stimuli if sti_count == 0: # If there is already a conversation pending, save it if current_conversation: self._conversations.append(current_conversation) # Get the nodes as a list of nodes # TODO already done at begining. why isnde the iteeration? # nodes = testcase.get_nodes_identification_templates() # And create the new one current_conversation = Conversation(nodes) # If intermediate stimulis, just increment the counter sti_count = (sti_count + 1) % nb_stimulis # If there is a current_conversation, put the frame into it if current_conversation: current_conversation.append(frame) # If no conversation pending else: ignored.append(frame) # At the end, if there is a current conversation pending, close it if current_conversation: # If not all stimulis were consumed if sti_count != 0: raise FilterError( 'Not all stimulis were consumed, %d left and next one should have been %s' % ( nb_stimulis - sti_count, stimulis[sti_count] ) ) # Close the current conversation by adding it to list conversations.append(current_conversation) return conversations, ignored
def test_filter_frames_not_a_protocol(self): # Filter on each protocol with self.assertRaises(InputParameterError): filtered, ignored = Frame.filter_frames(self.frames, Frame)
def test_filter_frames_none(self): # Filter on none protocol with self.assertRaises(InputParameterError): filtered, ignored = Frame.filter_frames(self.frames, None)