def test_of_slicer_invalid_data1(self): """Test of_slicer with invalid data: oflen is zero""" data = b'\x04\x00\x00\x00' response = of_slicer(data) self.assertCountEqual(response[0], []) self.assertCountEqual(response[1], []) data = b'\x04\x00\x00\x05\x99\x04\x00\x00\x00' response = of_slicer(data) self.assertEqual(response[0][0], data[:5]) self.assertCountEqual(response[1], [])
def test_of_slicer_invalid_data2(self): """Test of_slicer with invalid data: non openflow""" data = b'\x00\x00\x00\x00' response = of_slicer(data) self.assertCountEqual(response[0], []) self.assertCountEqual(response[1], []) data = b'\x04\x00\x00\x00\x00\x00\x00\x00' response = of_slicer(data) self.assertCountEqual(response[0], []) self.assertCountEqual(response[1], [])
def test_of_slicer(self): """Test of_slicer.""" data = b'\x04\x00\x00\x10\x00\x00\x00\x3e' data += b'\x00\x01\x00\x08\x00\x00\x00\x10' response = of_slicer(data) self.assertEqual(data, response[0][0]) self.assertCountEqual(response[1], [])
def handle_raw_in(self, event): """Handle a RawEvent and generate a kytos/core.messages.in.* event. Args: event (KytosEvent): RawEvent with openflow message to be unpacked """ # If the switch is already known to the controller, update the # 'lastseen' attribute switch = event.source.switch if switch: switch.update_lastseen() connection = event.source data = connection.remaining_data + event.content['new_data'] packets, connection.remaining_data = of_slicer(data) if not packets: return unprocessed_packets = [] for packet in packets: if not connection.is_alive(): return log.debug('Connection %s: New Raw Openflow packet - %s', connection.id, packet.hex()) if connection.is_new(): try: message = GenericHello(packet=packet) self._negotiate(connection, message) except (UnpackException, NegotiationException) as e: if type(e) == UnpackException: log.debug('Connection %s: Invalid hello message', connection.id) else: log.debug('Connection %s: Negotiation Failed', connection.id) connection.protocol.state = 'hello_failed' connection.close() connection.state = ConnectionState.FAILED return connection.set_setup_state() continue try: message = connection.protocol.unpack(packet) except (UnpackException, AttributeError) as e: log.debug(e) if type(e) == AttributeError: debug_msg = 'connection closed before version negotiation' log.debug('Connection %s: %s' , connection.id, debug_msg) connection.close() return log.debug('Connection %s: IN OFP, version: %s, type: %s, xid: %s', connection.id, message.header.version, message.header.message_type, message.header.xid) if connection.is_during_setup(): if not (str(message.header.message_type) == 'Type.OFPT_FEATURES_REPLY' and connection.protocol.state == 'waiting_features_reply'): unprocessed_packets.append(packet) continue self.emit_message_in(connection, message) connection.remaining_data = b''.join(unprocessed_packets) + \ connection.remaining_data
def test_of_slicer2(self): """Test of_slicer with insufficient bytes.""" data = b'\x04\x00\x00' response = of_slicer(data) self.assertCountEqual(response[0], []) self.assertEqual(response[1], data)