def test_connect_response(self): """Test string representatoin of KNX/IP ConnectResponse.""" xknx = XKNX(loop=self.loop) connect_response = ConnectResponse(xknx) connect_response.communication_channel = 13 connect_response.request_type = ConnectRequestType.TUNNEL_CONNECTION connect_response.control_endpoint = HPAI(ip_addr='192.168.42.1', port=33941) connect_response.identifier = 42 self.assertEqual( str(connect_response), '<ConnectResponse communication_channel="13" status_code="ErrorCode.E_NO_ERROR" control_endpoint="<HPAI 192.168.42.1:33941 />" request_t' 'ype="ConnectRequestType.TUNNEL_CONNECTION" identifier="42" />')
def test_connect_response_connection_error_gira(self): """ Test parsing and streaming connection response KNX/IP packet with error e_no_more_connections. HPAI and CRD normal. This was received from Gira devices (2020). """ raw = bytes.fromhex( "06 10 02 06 00 14 C0 24 08 01 0A 01 00 29 0E 57 04 04 00 00" ) knxipframe = KNXIPFrame() knxipframe.from_knx(raw) assert isinstance(knxipframe.body, ConnectResponse) assert knxipframe.body.status_code == ErrorCode.E_NO_MORE_CONNECTIONS assert knxipframe.body.communication_channel == 192 connect_response = ConnectResponse( communication_channel=192, status_code=ErrorCode.E_NO_MORE_CONNECTIONS, request_type=ConnectRequestType.TUNNEL_CONNECTION, data_endpoint=HPAI(ip_addr="10.1.0.41", port=3671), identifier=0, ) knxipframe2 = KNXIPFrame.init_from_body(connect_response) assert knxipframe2.to_knx() == raw
def test_connect_response(self): """Test parsing and streaming connection response KNX/IP packet.""" raw = ( 0x06, 0x10, 0x02, 0x06, 0x00, 0x14, 0x01, 0x00, 0x08, 0x01, 0xC0, 0xA8, 0x2A, 0x0A, 0x0E, 0x57, 0x04, 0x04, 0x11, 0xFF, ) xknx = XKNX() knxipframe = KNXIPFrame(xknx) knxipframe.from_knx(raw) self.assertTrue(isinstance(knxipframe.body, ConnectResponse)) self.assertEqual(knxipframe.body.communication_channel, 1) self.assertEqual(knxipframe.body.status_code, ErrorCode.E_NO_ERROR) self.assertEqual(knxipframe.body.control_endpoint, HPAI(ip_addr="192.168.42.10", port=3671)) self.assertEqual(knxipframe.body.request_type, ConnectRequestType.TUNNEL_CONNECTION) self.assertEqual(knxipframe.body.identifier, 4607) connect_response = ConnectResponse( xknx, communication_channel=1, status_code=ErrorCode.E_NO_ERROR, request_type=ConnectRequestType.TUNNEL_CONNECTION, control_endpoint=HPAI(ip_addr="192.168.42.10", port=3671), identifier=4607, ) knxipframe2 = KNXIPFrame.init_from_body(connect_response) self.assertEqual(knxipframe2.to_knx(), list(raw))
def test_connect_response_connection_error_gira(self): """ Test parsing and streaming connection response KNX/IP packet with error e_no_more_connections. HPAI and CRD normal. This was received from Gira devices (2020). """ raw = ( 0x06, 0x10, 0x02, 0x06, 0x00, 0x14, 0xC0, 0x24, 0x08, 0x01, 0x0A, 0x01, 0x00, 0x29, 0x0E, 0x57, 0x04, 0x04, 0x00, 0x00, ) xknx = XKNX() knxipframe = KNXIPFrame(xknx) knxipframe.from_knx(raw) self.assertTrue(isinstance(knxipframe.body, ConnectResponse)) self.assertEqual(knxipframe.body.status_code, ErrorCode.E_NO_MORE_CONNECTIONS) self.assertEqual(knxipframe.body.communication_channel, 192) connect_response = ConnectResponse( xknx, communication_channel=192, status_code=ErrorCode.E_NO_MORE_CONNECTIONS, request_type=ConnectRequestType.TUNNEL_CONNECTION, control_endpoint=HPAI(ip_addr="10.1.0.41", port=3671), identifier=0, ) knxipframe2 = KNXIPFrame.init_from_body(connect_response) self.assertEqual(knxipframe2.to_knx(), list(raw))
def test_connect_response(self): """Test string representatoin of KNX/IP ConnectResponse.""" connect_response = ConnectResponse() connect_response.communication_channel = 13 connect_response.request_type = ConnectRequestType.TUNNEL_CONNECTION connect_response.data_endpoint = HPAI(ip_addr="192.168.42.1", port=33941) connect_response.identifier = 42 assert ( str(connect_response) == '<ConnectResponse communication_channel="13" status_code="ErrorCode.E_NO_ERROR" ' 'data_endpoint="192.168.42.1:33941/udp" ' 'request_type="ConnectRequestType.TUNNEL_CONNECTION" identifier="42" />' )
def test_connect_response(self): """Test parsing and streaming connection response KNX/IP packet.""" raw = bytes.fromhex( "06 10 02 06 00 14 01 00 08 01 C0 A8 2A 0A 0E 57 04 04 11 FF" ) knxipframe = KNXIPFrame() knxipframe.from_knx(raw) assert isinstance(knxipframe.body, ConnectResponse) assert knxipframe.body.communication_channel == 1 assert knxipframe.body.status_code == ErrorCode.E_NO_ERROR assert knxipframe.body.data_endpoint == HPAI(ip_addr="192.168.42.10", port=3671) assert knxipframe.body.request_type == ConnectRequestType.TUNNEL_CONNECTION assert knxipframe.body.identifier == 4607 connect_response = ConnectResponse( communication_channel=1, status_code=ErrorCode.E_NO_ERROR, request_type=ConnectRequestType.TUNNEL_CONNECTION, data_endpoint=HPAI(ip_addr="192.168.42.10", port=3671), identifier=4607, ) knxipframe2 = KNXIPFrame.init_from_body(connect_response) assert knxipframe2.to_knx() == raw
async def test_tunnel_connect_send_disconnect( self, time_travel, route_back, data_endpoint_addr, local_endpoint ): """Test initiating a tunnelling connection.""" local_addr = ("192.168.1.1", 12345) remote_addr = ("192.168.1.2", 3671) self.tunnel.route_back = route_back gateway_data_endpoint = ( HPAI(*data_endpoint_addr) if data_endpoint_addr else HPAI() ) self.tunnel.transport.connect = AsyncMock() self.tunnel.transport.getsockname = Mock(return_value=local_addr) self.tunnel.transport.send = Mock() self.tunnel.transport.stop = Mock() # Connect connect_request = ConnectRequest( control_endpoint=local_endpoint, data_endpoint=local_endpoint, ) connect_frame = KNXIPFrame.init_from_body(connect_request) connection_task = asyncio.create_task(self.tunnel.connect()) await time_travel(0) self.tunnel.transport.connect.assert_called_once() self.tunnel.transport.send.assert_called_once_with(connect_frame) connect_response_frame = KNXIPFrame.init_from_body( ConnectResponse( communication_channel=23, data_endpoint=gateway_data_endpoint, identifier=7, ) ) self.tunnel.transport.handle_knxipframe(connect_response_frame, remote_addr) await connection_task assert self.tunnel._data_endpoint_addr == data_endpoint_addr assert self.tunnel._src_address == IndividualAddress(7) # Send - use data endpoint self.tunnel.transport.send.reset_mock() test_telegram = Telegram(payload=GroupValueWrite(DPTArray((1,)))) test_telegram_frame = KNXIPFrame.init_from_body( TunnellingRequest( communication_channel_id=23, sequence_counter=0, cemi=CEMIFrame.init_from_telegram( test_telegram, code=CEMIMessageCode.L_DATA_REQ, src_addr=IndividualAddress(7), ), ) ) asyncio.create_task(self.tunnel.send_telegram(test_telegram)) await time_travel(0) self.tunnel.transport.send.assert_called_once_with( test_telegram_frame, addr=data_endpoint_addr ) # skip ack and confirmation # Disconnect self.tunnel.transport.send.reset_mock() disconnect_request = DisconnectRequest( communication_channel_id=23, control_endpoint=local_endpoint, ) disconnect_frame = KNXIPFrame.init_from_body(disconnect_request) disconnection_task = asyncio.create_task(self.tunnel.disconnect()) await time_travel(0) self.tunnel.transport.send.assert_called_once_with(disconnect_frame) disconnect_response_frame = KNXIPFrame.init_from_body( DisconnectResponse(communication_channel_id=23) ) self.tunnel.transport.handle_knxipframe(disconnect_response_frame, remote_addr) await disconnection_task assert self.tunnel._data_endpoint_addr is None self.tunnel.transport.stop.assert_called_once()