def test_coap_core_shall_ignore_duplicated_NON_request(self): self.test_resource.resource_handler = self.responder self.rsp = message.Message(NON, TEST_MID, CONTENT, TEST_PAYLOAD, TEST_TOKEN) req = message.Message(NON, TEST_MID, GET, TEST_PAYLOAD, TEST_TOKEN) req.opt.uri_path = ("test", ) raw = req.encode() remote = (TEST_ADDRESS, TEST_PORT) # Receive first request. self.transport._receive(raw, remote, (TEST_LOCAL_ADDRESS, TEST_LOCAL_PORT)) # Verify that resource handler was called and response sent. self.assertEqual(self.test_resource.call_counter, 1) self.assertEqual(self.transport.output_count, 1) self.assertTupleEqual(self.transport.tester_remote, remote) self.assertEqual(self.transport.tester_data, self.rsp.encode()) # Receive duplicated request. self.transport._receive(raw, remote, (TEST_LOCAL_ADDRESS, TEST_LOCAL_PORT)) # Verify that resource handler was not called and nothing was transmitted. self.assertEqual(self.test_resource.call_counter, 1) self.assertEqual(self.transport.output_count, 1)
def test_coap_core_shall_return_error_when_non_response_message_is_sent_as_response( self): self.rsp = message.Message(ACK, TEST_MID, GET, "", TEST_TOKEN) # Prepare fake request to trigger response sending. req = message.Message(CON, TEST_MID, GET, "", TEST_TOKEN) req.opt.uri_path = ("test", ) raw = req.encode() # Check that error is raised on incorrect response type self.assertRaises(ValueError, self.transport._receive, raw, (TEST_ADDRESS, TEST_PORT), (TEST_LOCAL_ADDRESS, TEST_LOCAL_PORT))
def test_coap_core_shall_queue_CON_response_on_retransmission_list(self): self.rsp = message.Message(CON, TEST_MID + 1, CONTENT, "", TEST_TOKEN) # Prepare fake request to trigger response sending. req = message.Message(NON, TEST_MID, GET, "", TEST_TOKEN) req.opt.uri_path = ("test", ) raw = req.encode() # Simulate fake request reception self.transport._receive(raw, (TEST_ADDRESS, TEST_PORT), (TEST_LOCAL_ADDRESS, TEST_LOCAL_PORT)) # Validate that response was handled properly self.assertInRetransmissionList(self.rsp)
def test_coap_core_shall_not_queue_request_on_pending_response_list_if_callback_is_not_registered( self): req = message.Message(CON, TEST_MID, GET, TEST_PAYLOAD, TEST_TOKEN) req.remote = (TEST_ADDRESS, TEST_PORT) self.protocol.request(req) self.assertNotInOutgoingRequestList(TEST_TOKEN, req.remote)
def test_coap_core_shall_queue_CON_request_on_retransmission_list(self): req = message.Message(CON, TEST_MID, GET, TEST_PAYLOAD, TEST_TOKEN) req.remote = (TEST_ADDRESS, TEST_PORT) self.protocol.request(req) self.assertMessageInTransport(req, req.remote, 1) self.assertInRetransmissionList(req)
def check_that_duplicated_request_is_automatically_responded(self): req = message.Message(CON, TEST_MID, GET, TEST_PAYLOAD, TEST_TOKEN) req.opt.uri_path = ("test", ) raw = req.encode() remote = (TEST_ADDRESS, TEST_PORT) # Receive first request. self.transport._receive(raw, remote, (TEST_LOCAL_ADDRESS, TEST_LOCAL_PORT)) # Verify that resource handler was called and response sent. self.assertEqual(self.test_resource.call_counter, 1) self.assertEqual(self.transport.output_count, 1) self.assertTupleEqual(self.transport.tester_remote, remote) self.assertEqual(self.transport.tester_data, self.rsp.encode()) # Receive duplicated request. self.transport._receive(raw, remote, (TEST_LOCAL_ADDRESS, TEST_LOCAL_PORT)) # Verify that resource handler was not called but reponse was resent. self.assertEqual(self.test_resource.call_counter, 1) self.assertEqual(self.transport.output_count, 2) self.assertTupleEqual(self.transport.tester_remote, remote) self.assertEqual(self.transport.tester_data, self.rsp.encode())
def test_coap_core_shall_resend_ACK_on_duplicated_CON_request(self): self.test_resource.resource_handler = self.responder self.rsp = message.Message(ACK, TEST_MID, CONTENT, TEST_PAYLOAD, TEST_TOKEN) self.check_that_duplicated_request_is_automatically_responded()
def test_coap_core_shall_store_received_NON_request_on_deduplication_list( self): req = message.Message(NON, TEST_MID, GET, TEST_PAYLOAD, TEST_TOKEN) raw = req.encode() remote = (TEST_ADDRESS, TEST_PORT) self.transport._receive(raw, remote, (TEST_LOCAL_ADDRESS, TEST_LOCAL_PORT)) self.assertInDeduplicationList(TEST_MID, remote)
def test_coap_core_shall_queue_ACK_and_RST_response_on_responded_list( self): self.rsp = message.Message(ACK, TEST_MID, CONTENT, "", TEST_TOKEN) # Prepare fake request to trigger response sending. req = message.Message(NON, TEST_MID, GET, "", TEST_TOKEN) req.opt.uri_path = ("test", ) raw = req.encode() remote = (TEST_ADDRESS, TEST_PORT) # Simulate fake request reception self.transport._receive(raw, remote, (TEST_LOCAL_ADDRESS, TEST_LOCAL_PORT)) # Validate that request was added to the deduplication list, and response was registered. self.assertInDeduplicationList(TEST_MID, remote, self.rsp)
def test_encode(self): msg1 = message.Message(mtype=constants.CON, mid=0, token='') binary1 = chr(64) + chr(0) + chr(0) + chr(0) self.assertEqual(msg1.encode(), binary1, "wrong encode operation for empty CON message") msg2 = message.Message(mtype=constants.ACK, mid=0xBC90, code=constants.CONTENT, payload="temp = 22.5 C", token='q') msg2.opt.etag = "abcd" binary2 = chr(97) + chr(69) + chr(188) + chr(144) + chr(113) + chr( 68) + "abcd" + chr(255) + "temp = 22.5 C" self.assertEqual( msg2.encode(), binary2, "wrong encode operation for ACK message with payload, and Etag option" ) msg3 = message.Message() self.assertRaises(TypeError, msg3.encode)
def test_encode(self): msg1 = message.Message(mtype=constants.CON, mid=0, token=b'') binary1 = bytes([(64)] + [(0)] + [(0)] + [(0)]) self.assertEqual(msg1.encode(), binary1, "wrong encode operation for empty CON message") msg2 = message.Message(mtype=constants.ACK, mid=0xBC90, code=constants.CONTENT, payload=b"temp = 22.5 C", token=b'q') msg2.opt.etag = b"abcd" binary2 = bytes([(97)] + [(69)] + [(188)] + [(144)] + [(113)] + [(68)]) + b"abcd" + bytes([(255)]) + b"temp = 22.5 C" self.assertEqual( msg2.encode(), binary2, "wrong encode operation for ACK message with payload, and Etag option" ) msg3 = message.Message() self.assertRaises(TypeError, msg3.encode)
def test_client_server_communication(self): request = message.Message(mtype=CON, code=GET) request.opt.uri_path = ("text", ) request.remote = (ip_address(u"127.0.0.1"), SERVER_PORT) request.timeout = ACK_TIMEOUT request.payload = "" req = self.client_protocol.request(request, self._handle_text_response) counter = 0 while not self.stopWaiting: counter += 1 time.sleep(0.01) self.assertLess(counter, 500, "Timeout while waiting for callback from Coap") self.assertTrue(self.responseReceived) self.assertEqual(self.responsePayload, PAYLOAD)
def test_coap_core_shall_ignore_duplicated_CON_if_no_response_was_sent_to_the_original_message( self): # No resopnse is sent. req = message.Message(CON, TEST_MID, GET, TEST_PAYLOAD, TEST_TOKEN) req.opt.uri_path = ("test", ) raw = req.encode() remote = (TEST_ADDRESS, TEST_PORT) # Receive first request. self.transport._receive(raw, remote, (TEST_LOCAL_ADDRESS, TEST_LOCAL_PORT)) # Verify that resource handler was called.. self.assertEqual(self.test_resource.call_counter, 1) # Receive duplicated request. self.transport._receive(raw, remote, (TEST_LOCAL_ADDRESS, TEST_LOCAL_PORT)) # Verify that resource handler was not called and no response sent. self.assertEqual(self.test_resource.call_counter, 1) self.assertEqual(self.transport.output_count, 0)
def test_coap_core_shall_resend_ACK_on_duplicated_CON_response(self): # Send initial request. remote = (TEST_ADDRESS, TEST_PORT) raw_empty_ack = message.Message(ACK, TEST_MID + 1, EMPTY).encode() self.send_initial_request(remote) self.assertEqual(self.transport.output_count, 1) # Simulate receiving empty ACK and verify that no callback was called. self.receive_empty_ack_response(remote) self.assertIsNone(self.responseResult) # Receive separete CON response amd verify that callback was called and empty ACK was automatically sent. self.receive_con_response(remote) self.assertEqual(self.transport.output_count, 2) self.assertEqual(self.transport.tester_data, raw_empty_ack) self.assertEqual(self.callbackCounter, 1) self.assertEqual(self.responseResult, RESULT_SUCCESS) # Receive separate response duplicate, verify that empty ACK was forwarded and no callback was called. self.receive_con_response(remote) self.assertEqual(self.transport.output_count, 3) self.assertEqual(self.transport.tester_data, raw_empty_ack) self.assertEqual(self.callbackCounter, 1)
def render_GET(self, request): response = message.Message(code=CONTENT, payload=self.text) return response
def receive_con_response(self, remote): rsp = message.Message(CON, TEST_MID + 1, CONTENT, TEST_PAYLOAD, TEST_TOKEN) raw = rsp.encode() self.transport._receive(raw, remote, (TEST_LOCAL_ADDRESS, TEST_LOCAL_PORT))
def receive_rst_response(self, remote): rsp = message.Message(RST, TEST_MID, EMPTY, "", "") raw = rsp.encode() self.transport._receive(raw, remote, (TEST_LOCAL_ADDRESS, TEST_LOCAL_PORT))
def send_initial_request(self, remote, timeout=None): self.req = message.Message(CON, TEST_MID, GET, "", TEST_TOKEN) self.req.remote = remote if timeout != None: self.req.timeout = timeout self.protocol.request(self.req, self.callback)
def receive_empty_ack_response(self, remote): rsp = message.Message(ACK, TEST_MID, EMPTY) raw = rsp.encode() self.transport._receive(raw, remote, (TEST_LOCAL_ADDRESS, TEST_LOCAL_PORT))
def test_coap_core_shall_return_error_when_non_request_message_is_sent_as_request( self): req = message.Message(CON, TEST_MID, CHANGED, TEST_PAYLOAD, TEST_TOKEN) req.remote = (TEST_ADDRESS, TEST_PORT) self.assertRaises(ValueError, self.protocol.request, (req))