async def test_td_coap_core_23(self): client, server = await self.start_client_server() print("TD_COAP_CORE_CLIENT_23") path = "/storage/create1" # STEP 3 expected = Response() expected.type = defines.Type.ACK expected.code = defines.Code.CREATED expected.payload = None expected.source = self.server_address ret = await client.put(path, "New Resource", timeout=10, if_none_match=True) self.assertIsInstance(ret, Response) expected.token = ret.token expected.mid = ret.mid if ret != expected: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) # STEP 6 expected = Response() expected.type = defines.Type.ACK expected.code = defines.Code.PRECONDITION_FAILED expected.source = self.server_address expected.payload = None ret = await client.put(path, "Validate changed", timeout=10, if_none_match=True) self.assertIsInstance(ret, Response) expected.token = ret.token expected.mid = ret.mid if ret == expected: print("PASS") else: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) await self.stop_client_server(client, server)
async def test_td_coap_link_08(self): client, server = await self.start_client_server() print("TD_COAP_LINK_08") path = "/.well-known/core" req = Request() req.code = defines.Code.GET req.uri_path = path req.uri_query = "?href=/link*" req.type = defines.Type.CON req.mid = random.randint(1, 1000) req.destination = self.server_address req.token = utils.generate_random_hex(2) expected = Response() expected.type = defines.Type.ACK expected.mid = req.mid expected.code = defines.Code.CONTENT expected.payload = '</link1>;obs,</link2>;obs,</link3>;obs' expected.token = req.token expected.content_type = defines.ContentType.application_link_format expected.source = "127.0.0.1", 5683 transaction = await client.send_request(req) ret = await client.receive_response(transaction, 10) if ret == expected: print("PASS") else: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) print(ret.pretty_print()) self.assertEqual(ret, expected) self.stop_client_server(client, server)
async def test_td_coap_core_01(self): client, server = await self.start_client_server() print("TD_COAP_CORE_CLIENT_01") path = "/test" expected = Response() expected.type = defines.Type.ACK expected.code = defines.Code.CONTENT expected.payload = "Test" expected.content_type = defines.ContentType.TEXT_PLAIN expected.source = "127.0.0.1", 5683 ret = await client.get(path, timeout=10) self.assertIsInstance(ret, Response) expected.mid = ret.mid expected.token = ret.token if ret == expected: print("PASS") else: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) await self.stop_client_server(client, server)
async def test_td_coap_core_14(self): client, server = await self.start_client_server() print("TD_COAP_CORE_CLIENT_14") path = "/query" expected = Response() expected.type = defines.Type.ACK expected.code = defines.Code.CONTENT expected.payload = "Query12" expected.content_type = defines.ContentType.TEXT_PLAIN expected.source = self.server_address ret = await client.get(path, timeout=10, uri_query="first=1&second=2") self.assertIsInstance(ret, Response) expected.token = ret.token expected.mid = ret.mid if ret == expected: print("PASS") else: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) await self.stop_client_server(client, server)
async def test_td_coap_core_20(self): client, server = await self.start_client_server() print("TD_COAP_CORE_CLIENT_20") path = "/multi-format" expected = Response() expected.type = defines.Type.ACK expected.code = defines.Code.CONTENT expected.payload = "1" expected.content_type = defines.ContentType.TEXT_PLAIN expected.source = self.server_address ret = await client.get(path, timeout=10, accept=defines.ContentType.TEXT_PLAIN) self.assertIsInstance(ret, Response) expected.token = ret.token expected.mid = ret.mid if ret != expected: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) expected = Response() expected.type = defines.Type.ACK expected.code = defines.Code.CONTENT expected.payload = "<value>1</value>" expected.content_type = defines.ContentType.application_xml expected.source = self.server_address ret = await client.get(path, timeout=10, accept=defines.ContentType.application_xml) self.assertIsInstance(ret, Response) expected.token = ret.token expected.mid = ret.mid if ret == expected: print("PASS") else: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) await self.stop_client_server(client, server)
async def test_td_coap_obs_13(self): client, server = await self.start_client_server() print("TD_COAP_OBS_13") path = "/obs-large" token = utils.generate_random_hex(2) req = Request() req.code = defines.Code.GET req.uri_path = path req.type = defines.Type.CON req.mid = random.randint(1, 1000) req.destination = self.server_address req.token = token req.observe = 0 expected = Response() expected.type = defines.Type.ACK expected.mid = req.mid expected.code = defines.Code.CONTENT expected.token = token expected.observe = 2 expected.source = "127.0.0.1", 5683 expected.payload = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sollicitudin fermentum ornare. " \ "Cras accumsan tellus quis dui lacinia eleifend. Proin ultrices rutrum orci vitae luctus. " \ "Nullam malesuada pretium elit, at aliquam odio vehicula in. Etiam nec maximus elit. " \ "Etiam at erat ac ex ornare feugiat. Curabitur sed malesuada orci, id aliquet nunc. Phasellus " \ "nec leo luctus, blandit lorem sit amet, interdum metus. Duis efficitur volutpat magna, ac " \ "ultricies nibh aliquet sit amet. Etiam tempor egestas augue in hendrerit. Nunc eget augue " \ "ultricies, dignissim lacus et, vulputate dolor. Nulla eros odio, fringilla vel massa ut, " \ "facilisis cursus quam. Fusce faucibus lobortis congue. Fusce consectetur porta neque, id " \ "sollicitudin velit maximus eu. Sed pharetra leo quam, vel finibus turpis cursus ac. " \ "Aenean ac nisi massa. Cras commodo arcu nec ante tristique ullamcorper. Quisque eu hendrerit" \ " urna. Cras fringilla eros ut nunc maximus, non porta nisl mollis. Aliquam in rutrum massa." \ " Praesent tristique turpis dui, at ultri" expected.block2 = (0, 1, 1024) transaction = await client.send_request(req) ret = await client.receive_response(transaction, 10) if ret != expected: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) req = Request() req.code = defines.Code.GET req.uri_path = path req.type = defines.Type.CON req.mid = random.randint(1, 1000) req.destination = self.server_address req.token = token req.block2 = (1, 0, 1024) expected = Response() expected.type = defines.Type.ACK expected.mid = req.mid expected.code = defines.Code.CONTENT expected.payload = "cies lorem fermentum at. Vivamus sit amet ornare neque, " \ "a imperdiet nisl. Quisque a iaculis libero, id tempus lacus. " \ "Aenean convallis est non justo consectetur, a hendrerit enim consequat. In accumsan ante " \ "a egestas luctus. Etiam quis neque nec eros vestibulum faucibus. Nunc viverra ipsum " \ "lectus, vel scelerisque dui dictum a. Ut orci enim, ultrices a ultrices nec, pharetra " \ "in quam. Donec accumsan sit amet eros eget fermentum." \ "Vivamus ut odio ac odio malesuada accumsan. Aenean vehicula diam at tempus ornare. " \ "Phasellus dictum mauris a mi consequat, vitae mattis nulla fringilla. Ut laoreet " \ "tellus in nisl efficitur, a luctus justo tempus. Fusce finibus libero eget velit " \ "finibus iaculis. Morbi rhoncus purus vel vestibulum ullamcorper. Sed ac metus in urna " \ "fermentum feugiat. Nulla nunc diam, sodales aliquam mi id, varius porta nisl. Praesent " \ "vel nibh ac turpis rutrum laoreet at non odio. Phasellus ut posuere mi. Suspendisse " \ "malesuada velit nec mauris convallis porta. Vivamus sed ultrices sapien, at cras amet." expected.token = token expected.block2 = (1, 0, 1024) expected.source = "127.0.0.1", 5683 transaction = await client.send_request(req) ret = await client.receive_response(transaction, 10) if ret != expected: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) expected = Response() expected.type = defines.Type.CON expected.mid = self.server_mid + 2 expected.code = defines.Code.CONTENT expected.token = token expected.observe = 3 expected.source = "127.0.0.1", 5683 expected.payload = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sollicitudin fermentum ornare. " \ "Cras accumsan tellus quis dui lacinia eleifend. Proin ultrices rutrum orci vitae luctus. " \ "Nullam malesuada pretium elit, at aliquam odio vehicula in. Etiam nec maximus elit. " \ "Etiam at erat ac ex ornare feugiat. Curabitur sed malesuada orci, id aliquet nunc. Phasellus " \ "nec leo luctus, blandit lorem sit amet, interdum metus. Duis efficitur volutpat magna, ac " \ "ultricies nibh aliquet sit amet. Etiam tempor egestas augue in hendrerit. Nunc eget augue " \ "ultricies, dignissim lacus et, vulputate dolor. Nulla eros odio, fringilla vel massa ut, " \ "facilisis cursus quam. Fusce faucibus lobortis congue. Fusce consectetur porta neque, id " \ "sollicitudin velit maximus eu. Sed pharetra leo quam, vel finibus turpis cursus ac. " \ "Aenean ac nisi massa. Cras commodo arcu nec ante tristique ullamcorper. Quisque eu hendrerit" \ " urna. Cras fringilla eros ut nunc maximus, non porta nisl mollis. Aliquam in rutrum massa." \ " Praesent tristique turpis dui, at ultri" expected.block2 = (0, 1, 1024) transaction.response = None ret = await client.receive_response(transaction, 10) req = Message() req.code = defines.Code.EMPTY req.type = defines.Type.ACK req.mid = self.server_mid + 2 req.destination = self.server_address req.token = token transaction = await client.send_request(req) if ret == expected: print("PASS") else: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) print(ret.pretty_print()) self.assertEqual(ret, expected) self.stop_client_server(client, server)
async def test_td_coap_obs_08(self): client, server = await self.start_client_server() print("TD_COAP_OBS_08") path = "/obs" token = utils.generate_random_hex(2) req = Request() req.code = defines.Code.GET req.uri_path = path req.type = defines.Type.CON req.mid = random.randint(1, 1000) req.destination = self.server_address req.token = token req.observe = 0 expected = Response() expected.type = defines.Type.ACK expected.mid = req.mid expected.code = defines.Code.CONTENT expected.payload = "5" expected.token = token expected.observe = 2 expected.source = "127.0.0.1", 5683 transaction = await client.send_request(req) ret = await client.receive_response(transaction, 10) if ret != expected: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) else: print("PASS-EXCHANGE-01") expected = Response() expected.type = defines.Type.CON expected.mid = self.server_mid + 1 expected.code = defines.Code.CONTENT expected.payload = "6" expected.token = token expected.observe = 3 expected.source = "127.0.0.1", 5683 transaction.response = None ret = await client.receive_response(transaction, 10) if ret != expected: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) else: print("PASS-EXCHANGE-02") req = Message() req.code = defines.Code.EMPTY req.type = defines.Type.ACK req.mid = self.server_mid + 1 req.destination = self.server_address req.token = token transaction = await client.send_request(req) token2 = utils.generate_random_hex(2) req = Request() req.code = defines.Code.PUT req.uri_path = path req.type = defines.Type.CON req.mid = random.randint(1, 1000) req.destination = self.server_address req.token = token2 req.payload = "{ \"value\": 100}" req.content_type = defines.ContentType.application_json expected = Response() expected.type = defines.Type.ACK expected.mid = req.mid expected.code = defines.Code.CHANGED expected.token = token2 expected.source = self.server_address transaction2 = await client.send_request(req) ret = await client.receive_response(transaction2, 10) if ret != expected: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) else: print("PASS-EXCHANGE-03") expected = Response() expected.type = defines.Type.CON expected.mid = self.server_mid + 4 expected.code = defines.Code.NOT_ACCEPTABLE expected.token = token expected.source = self.server_address expected.payload = "Content-Type changed" transaction.response = None ret = await client.receive_response(transaction, 10) if ret == expected: print("PASS") else: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) print(ret.pretty_print()) self.assertEqual(ret, expected) self.stop_client_server(client, server)
async def test_td_coap_obs_07(self): client, server = await self.start_client_server() print("TD_COAP_OBS_07") path = "/obs" token = utils.generate_random_hex(2) req = Request() req.code = defines.Code.GET req.uri_path = path req.type = defines.Type.CON req.mid = random.randint(1, 1000) req.destination = self.server_address req.token = token req.observe = 0 expected = Response() expected.type = defines.Type.ACK expected.mid = req.mid expected.code = defines.Code.CONTENT expected.payload = "5" expected.token = token expected.observe = 2 expected.source = "127.0.0.1", 5683 transaction = await client.send_request(req) ret = await client.receive_response(transaction, 10) if ret != expected: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) expected = Response() expected.type = defines.Type.CON expected.mid = self.server_mid + 1 expected.code = defines.Code.CONTENT expected.payload = "6" expected.token = token expected.observe = 3 expected.source = "127.0.0.1", 5683 transaction.response = None ret = await client.receive_response(transaction, 10) if ret != expected: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) req = Message() req.code = defines.Code.EMPTY req.type = defines.Type.ACK req.mid = self.server_mid + 1 req.destination = self.server_address req.token = token transaction = await client.send_request(req) token2 = utils.generate_random_hex(2) req = Request() req.code = defines.Code.DELETE req.uri_path = path req.type = defines.Type.CON req.mid = random.randint(1, 1000) req.destination = self.server_address req.token = token2 expected = Response() expected.type = defines.Type.ACK expected.mid = req.mid expected.code = defines.Code.DELETED expected.token = token2 expected.source = self.server_address transaction2 = await client.send_request(req) ret = await client.receive_response(transaction2, 10) if ret != expected: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) expected = Response() expected.type = defines.Type.CON expected.mid = self.server_mid + 4 expected.code = defines.Code.NOT_FOUND expected.token = token expected.source = self.server_address transaction.response = None ret = await client.receive_response(transaction, 10) req = Message() req.code = defines.Code.EMPTY req.type = defines.Type.ACK req.mid = self.server_mid + 4 req.destination = self.server_address req.token = token transaction = await client.send_request(req) await asyncio.sleep(5) if ret == expected: print("PASS") else: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) print(ret.pretty_print()) self.assertEqual(ret, expected) self.stop_client_server(client, server)
async def test_td_coap_core_22(self): client, server = await self.start_client_server() print("TD_COAP_CORE_CLIENT_22") path = "/validate" # STEP 3 expected = Response() expected.type = defines.Type.ACK expected.code = defines.Code.CONTENT expected.payload = "Validate" expected.etag = b'\x01' expected.source = self.server_address ret = await client.get(path, timeout=10) self.assertIsInstance(ret, Response) expected.token = ret.token expected.mid = ret.mid if ret != expected: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) # STEP 6 expected = Response() expected.type = defines.Type.ACK expected.code = defines.Code.CHANGED expected.source = self.server_address expected.payload = None ret = await client.put(path, "Validate changed", timeout=10, if_match=b'\x01') self.assertIsInstance(ret, Response) expected.token = ret.token expected.mid = ret.mid if ret != expected: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) # STEP 10 expected = Response() expected.type = defines.Type.ACK expected.code = defines.Code.CONTENT expected.payload = "Validate changed" expected.etag = b'\x02' expected.source = self.server_address ret = await client.get(path, timeout=10) self.assertIsInstance(ret, Response) expected.token = ret.token expected.mid = ret.mid if ret != expected: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) # STEP 14 expected = Response() expected.type = defines.Type.ACK expected.code = defines.Code.CHANGED expected.source = self.server_address ret = await client.put(path, "Step 13", timeout=10) self.assertIsInstance(ret, Response) expected.token = ret.token expected.mid = ret.mid if ret != expected: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) # STEP 14 expected = Response() expected.type = defines.Type.ACK expected.code = defines.Code.PRECONDITION_FAILED expected.source = self.server_address expected.payload = None ret = await client.put(path, "Step 17", timeout=10, if_match=b'\x02') self.assertIsInstance(ret, Response) expected.token = ret.token expected.mid = ret.mid if ret == expected: print("PASS") else: print("Received: {0}".format(ret)) print("Expected: {0}".format(expected)) self.assertEqual(ret, expected) await self.stop_client_server(client, server)