def run(self): while True: sleep(0.5) coap = CoAPServer(self.ip,self.port,self.multicast) print "***************************Entering Server******************" coap.main_2() print "****************************Leaving Server *****************"
def setUp(self): self.server_address = (defines.ALL_COAP_NODES, 5683) self.current_mid = random.randint(1, 1000) self.server_mid = random.randint(1000, 2000) self.server = CoAPServer("0.0.0.0", 5683, multicast=True) self.server_thread = threading.Thread(target=self.server.listen, args=(10,)) self.server_thread.start() self.queue = Queue()
def setUp(self): self.server_address = ("::1", 5683) self.current_mid = random.randint(1, 1000) self.server_mid = random.randint(1000, 2000) self.server = CoAPServer("::1", 5683) self.server_thread = threading.Thread(target=self.server.listen, args=(10,)) self.server_thread.start() self.queue = Queue()
def setUp(self): self.server_address = ("127.0.0.1", 5683) self.current_mid = random.randint(1, 1000) self.server_mid = random.randint(1000, 2000) self.server = CoAPServer("127.0.0.1", 5684) self.server_thread = threading.Thread(target=self.server.listen, args=(10,)) self.server_thread.start() self.proxy = CoAPForwardProxy("127.0.0.1", 5683, cache=True) self.proxy_thread = threading.Thread(target=self.proxy.listen, args=(10,)) self.proxy_thread.start() self.queue = Queue()
class Tests(unittest.TestCase): def setUp(self): self.server_address = ("127.0.0.1", 5683) self.current_mid = random.randint(1, 1000) self.server = CoAPServer("127.0.0.1", 5683) self.server_thread = threading.Thread(target=self.server.listen, args=(10,)) self.server_thread.start() def tearDown(self): self.server.close() self.server_thread.join(timeout=25) self.server = None # time.sleep(15) def _test(self, message, expected): serializer = Serializer() datagram = serializer.serialize(message) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(datagram, message.destination) host, port = message.destination # print "Message sent to " + host + ":" + str(port) # print "----------------------------------------" # print message # print "----------------------------------------" datagram, source = sock.recvfrom(4096) host, port = source message = serializer.deserialize(datagram, host, port) self.assertEqual(message.type, expected.type) self.assertEqual(message.mid, expected.mid) self.assertEqual(message.code, expected.code) self.assertEqual(message.source, source) self.assertEqual(message.token, expected.token) self.assertEqual(message.payload, expected.payload) self.assertEqual(message.options, expected.options) # print "Message received from " + host + ":" + str(port) # print "----------------------------------------" # print message # print "----------------------------------------" def _test_modular(self, lst): serializer = Serializer() sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) for t in lst: message, expected = t send_ack = False if message is not None: datagram = serializer.serialize(message) sock.sendto(datagram, self.server_address) else: send_ack = True datagram, source = sock.recvfrom(4096) host, port = source message = serializer.deserialize(datagram, host, port) self.assertEqual(message.type, expected.type) if not send_ack: self.assertEqual(message.mid, expected.mid) self.assertEqual(message.code, expected.code) self.assertEqual(message.source, source) self.assertEqual(message.token, expected.token) self.assertEqual(message.payload, expected.payload) self.assertEqual(message.options, expected.options) if send_ack: message = Message.new_ack(message) datagram = serializer.serialize(message) sock.sendto(datagram, self.server_address) sock.close() def _test_separate(self, message, notification): serializer = Serializer() datagram = serializer.serialize(message) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.sendto(datagram, self.server_address) datagram, source = sock.recvfrom(4096) host, port = source message = serializer.deserialize(datagram, host, port) self.assertEqual(message.type, defines.inv_types["ACK"]) self.assertEqual(message.code, None) self.assertEqual(message.mid, self.current_mid - 1) self.assertEqual(message.source, source) datagram, source = sock.recvfrom(4096) host, port = source message = serializer.deserialize(datagram, host, port) self.assertEqual(message.type, notification.type) self.assertEqual(message.code, notification.code) self.assertEqual(message.source, source) self.assertEqual(message.token, notification.token) self.assertEqual(message.payload, notification.payload) self.assertEqual(message.options, notification.options) message = Message.new_ack(message) datagram = serializer.serialize(message) sock.sendto(datagram, self.server_address) sock.close() def test_get_storage(self): print "\nGET /storage\n" args = ("/storage",) kwargs = {} path = args[0] req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CONTENT"] expected.token = None expected.payload = "Storage Resource for PUT, POST and DELETE" self.current_mid += 1 self._test(req, expected) def test_get_not_found(self): print "\nGET /not_found\n" args = ("/not_found",) kwargs = {} path = args[0] req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["NOT_FOUND"] expected.token = None expected.payload = None self.current_mid += 1 self._test(req, expected) def test_post_and_get_storage(self): print "\nPOST /storage/data1 - GET /storage/data1\n" args = ("/storage/data1",) kwargs = {} path = args[0] req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['POST'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.payload = "Created" req.destination = self.server_address expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CREATED"] expected.token = None expected.payload = None option = Option() option.number = defines.inv_options["Location-Path"] option.value = "/storage/data1" expected.add_option(option) self.current_mid += 1 self._test(req, expected) req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CONTENT"] expected.token = None expected.payload = "Created" self.current_mid += 1 self._test(req, expected) def test_post_get_delete_get_storage(self): print "\nPOST /storage/data1 - GET /storage/data1 - DELETE /storage/data1 - GET /storage/data1\n" args = ("/storage/data1",) kwargs = {} path = args[0] req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['POST'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.payload = "Created" req.destination = self.server_address expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CREATED"] expected.token = None expected.payload = None option = Option() option.number = defines.inv_options["Location-Path"] option.value = "/storage/data1" expected.add_option(option) self.current_mid += 1 self._test(req, expected) req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CONTENT"] expected.token = None expected.payload = "Created" self.current_mid += 1 self._test(req, expected) req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['DELETE'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["DELETED"] expected.token = None expected.payload = None self.current_mid += 1 self._test(req, expected) req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["NOT_FOUND"] expected.token = None expected.payload = None self.current_mid += 1 self._test(req, expected) def test_long(self): print "\nGET /long\n" args = ("/long",) kwargs = {} path = args[0] req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = None expected.token = None expected.payload = None expected2 = Response() expected2.type = defines.inv_types["CON"] expected2.code = defines.responses["CONTENT"] expected2.token = None expected2.payload = "Long Time" self.current_mid += 1 self._test_modular([(req, expected), (None, expected2)]) def test_big(self): print "\nGET /big\n" args = ("/big",) path = args[0] req = Request() req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.inv_types["ACK"] expected._mid = self.current_mid expected.code = defines.responses["CONTENT"] expected.token = None 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" option = Option() option.number = defines.inv_options["Block2"] option.value = 14 expected.add_option(option) self.current_mid += 1 req2 = Request() req2.code = defines.inv_codes['GET'] req2.uri_path = path req2.type = defines.inv_types["CON"] req2._mid = self.current_mid req.destination = self.server_address option = Option() option.number = defines.inv_options["Block2"] option.value = 22 req2.add_option(option) expected2 = Response() expected2.type = defines.inv_types["ACK"] expected2.code = defines.responses["CONTENT"] expected2._mid = self.current_mid expected2.token = None expected2.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." option = Option() option.number = defines.inv_options["Block2"] option.value = 22 expected2.add_option(option) self.current_mid += 1 self._test_modular([(req, expected), (req2, expected2)]) def test_get_separate(self): print "\nGET /separate\n" args = ("/separate",) kwargs = {} path = args[0] req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.inv_types["CON"] expected.code = defines.responses["CONTENT"] expected.token = None expected.payload = "Separate" self.current_mid += 1 self._test_separate(req, expected)
class Tests(unittest.TestCase): def setUp(self): self.server_address = ("127.0.0.1", 5683) self.current_mid = random.randint(1, 1000) self.server_mid = random.randint(1000, 2000) self.server = CoAPServer("127.0.0.1", 5683) self.server_thread = threading.Thread(target=self.server.listen, args=(10,)) self.server_thread.start() self.queue = Queue() def tearDown(self): self.server.close() self.server_thread.join(timeout=25) self.server = None def _test_with_client(self, message_list): # pragma: no cover client = HelperClient(self.server_address) for message, expected in message_list: if message is not None: received_message = client.send_request(message) if expected is not None: if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.options: self.assertEqual(len(received_message.options), len(expected.options)) for o in expected.options: assert isinstance(o, Option) option_value = getattr(expected, o.name.lower().replace("-", "_")) option_value_rec = getattr(received_message, o.name.lower().replace("-", "_")) self.assertEqual(option_value, option_value_rec) client.stop() def client_callback(self, response): print("Callback") self.queue.put(response) def test_advanced(self): print("TEST_ADVANCED") path = "/advanced" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.max_age = 20 expected.token = None exchange1 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CREATED.number expected.payload = "Response changed through POST" expected.token = None exchange2 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.PUT.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CHANGED.number expected.payload = "Response changed through PUT" expected.token = None exchange3 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.DELETE.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.DELETED.number expected.payload = "Response deleted" expected.token = None exchange4 = (req, expected) self.current_mid += 1 self._test_with_client([exchange1, exchange2, exchange3, exchange4]) def test_advanced_separate(self): print("TEST_ADVANCED_SEPARATE") path = "/advancedSeparate" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["CON"] expected._mid = None expected.code = defines.Codes.CONTENT.number expected.max_age = 20 expected.token = None exchange1 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["CON"] expected._mid = None expected.code = defines.Codes.CREATED.number expected.payload = "Response changed through POST" expected.token = None exchange2 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.PUT.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["CON"] expected._mid = None expected.code = defines.Codes.CHANGED.number expected.payload = "Response changed through PUT" expected.token = None exchange3 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.DELETE.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["CON"] expected._mid = None expected.code = defines.Codes.DELETED.number expected.payload = "Response deleted" expected.token = None exchange4 = (req, expected) self.current_mid += 1 self._test_with_client([exchange1, exchange2, exchange3, exchange4])
class Tests(unittest.TestCase): def setUp(self): self.server_address = (defines.ALL_COAP_NODES, 5683) self.current_mid = random.randint(1, 1000) self.server_mid = random.randint(1000, 2000) self.server = CoAPServer("0.0.0.0", 5683, multicast=True) self.server_thread = threading.Thread(target=self.server.listen, args=(10, )) self.server_thread.start() self.queue = Queue() def tearDown(self): self.server.close() self.server_thread.join(timeout=25) self.server = None def _test_with_client(self, message_list): # pragma: no cover client = HelperClient(self.server_address) for message, expected in message_list: if message is not None: received_message = client.send_request(message) if expected is not None: if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.options: self.assertEqual(len(received_message.options), len(expected.options)) for o in expected.options: assert isinstance(o, Option) option_value = getattr( expected, o.name.lower().replace("-", "_")) option_value_rec = getattr( received_message, o.name.lower().replace("-", "_")) self.assertEqual(option_value, option_value_rec) client.stop() def _test_with_client_observe(self, message_list): # pragma: no cover client = HelperClient(self.server_address) for message, expected in message_list: if message is not None: client.send_request(message, self.client_callback) if expected is not None: received_message = self.queue.get() if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.options: self.assertEqual(len(received_message.options), len(expected.options)) for o in expected.options: assert isinstance(o, Option) option_value = getattr( expected, o.name.lower().replace("-", "_")) option_value_rec = getattr( received_message, o.name.lower().replace("-", "_")) self.assertEqual(option_value, option_value_rec) client.stop() def client_callback(self, response): print("Callback") self.queue.put(response) def test_not_allowed(self): print("TEST_NOT_ALLOWED") path = "/void" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.METHOD_NOT_ALLOWED.number expected.token = None exchange1 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.METHOD_NOT_ALLOWED.number expected.token = None exchange2 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.PUT.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.METHOD_NOT_ALLOWED.number expected.token = None exchange3 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.DELETE.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.METHOD_NOT_ALLOWED.number expected.token = None exchange4 = (req, expected) self.current_mid += 1 self._test_with_client([exchange1, exchange2, exchange3, exchange4])
class Tests(unittest.TestCase): def setUp(self): self.server_address = ("::1", 5683) self.current_mid = random.randint(1, 1000) self.server_mid = random.randint(1000, 2000) self.server = CoAPServer("::1", 5683) self.server_thread = threading.Thread(target=self.server.listen, args=(10,)) self.server_thread.start() self.queue = Queue() def tearDown(self): self.server.close() self.server_thread.join(timeout=25) self.server = None def _test_with_client(self, message_list): # pragma: no cover client = HelperClient(self.server_address) for message, expected in message_list: if message is not None: received_message = client.send_request(message) if expected is not None: if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.options: self.assertEqual(len(received_message.options), len(expected.options)) for o in expected.options: assert isinstance(o, Option) option_value = getattr(expected, o.name.lower().replace("-", "_")) option_value_rec = getattr(received_message, o.name.lower().replace("-", "_")) self.assertEqual(option_value, option_value_rec) client.stop() def _test_with_client_observe(self, message_list): # pragma: no cover client = HelperClient(self.server_address) for message, expected in message_list: if message is not None: client.send_request(message, self.client_callback) if expected is not None: received_message = self.queue.get() if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.options: self.assertEqual(len(received_message.options), len(expected.options)) for o in expected.options: assert isinstance(o, Option) option_value = getattr(expected, o.name.lower().replace("-", "_")) option_value_rec = getattr(received_message, o.name.lower().replace("-", "_")) self.assertEqual(option_value, option_value_rec) client.stop() def client_callback(self, response): print "Callback" self.queue.put(response) def test_not_allowed(self): print "TEST_NOT_ALLOWED" path = "/void" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.METHOD_NOT_ALLOWED.number expected.token = None exchange1 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.METHOD_NOT_ALLOWED.number expected.token = None exchange2 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.PUT.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.METHOD_NOT_ALLOWED.number expected.token = None exchange3 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.DELETE.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.METHOD_NOT_ALLOWED.number expected.token = None exchange4 = (req, expected) self.current_mid += 1 self._test_with_client([exchange1, exchange2, exchange3, exchange4])
def setUp(self): self.proto = CoAPServer("127.0.0.1", 5683) self.tr = proto_helpers.FakeDatagramTransport() self.proto.makeConnection(self.tr) self.current_mid = random.randint(1, 1000)
class Tests(unittest.TestCase): def setUp(self): self.server_address = ("127.0.0.1", 5683) self.current_mid = random.randint(1, 1000) self.server_mid = random.randint(1000, 2000) self.server = CoAPServer("127.0.0.1", 5684) self.server_thread = threading.Thread(target=self.server.listen, args=(10, )) self.server_thread.start() self.proxy = CoAPForwardProxy("127.0.0.1", 5683) self.proxy_thread = threading.Thread(target=self.proxy.listen, args=(10, )) self.proxy_thread.start() self.queue = Queue() def tearDown(self): self.server.close() self.server_thread.join(timeout=25) self.server = None self.proxy.close() self.proxy_thread.join(timeout=25) self.proxy = None def _test_with_client_delayed(self, message_list): # pragma: no cover client = HelperClient(self.server_address) start_etag = None for message, expected in message_list: if message is not None: received_message = client.send_request(message) time.sleep(5) if received_message.etag is not None: if start_etag is None: start_etag = received_message.etag if expected is not None: if expected.etag is not None: if expected.etag == 1: self.assertEqual(received_message, start_etag) else: self.assertNotEqual(received_message, start_etag) if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.max_age is not None: if expected.max_age != 60: self.assertNotEqual(received_message.max_age, 60) else: self.assertEqual(received_message.max_age, expected.max_age) if expected.options: self.assertEqual(len(received_message.options), len(expected.options)) for o in expected.options: assert isinstance(o, Option) if o.name != defines.OptionRegistry.MAX_AGE.name and o.name != defines.OptionRegistry.ETAG.name: option_value = getattr( expected, o.name.lower().replace("-", "_")) option_value_rec = getattr( received_message, o.name.lower().replace("-", "_")) self.assertEqual(option_value, option_value_rec) client.stop() def client_callback(self, response): print "Callback" self.queue.put(response) def test_get_multiple(self): print "TEST_GET_MULTIPLE" path = "/basic" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.proxy_uri = "coap://127.0.0.1:5684/basic" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Basic Resource" exchange1 = (req, expected) self.current_mid += 1 #PREPARING SECOND EXPECTED RESPONSE (MAX AGE MUST BE CHECKED) req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/basic" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Basic Resource" expected.max_age = 61 exchange2 = (req2, expected) self._test_with_client_delayed([exchange1, exchange2]) def test_get_post(self): print "TEST_GET_POST" path = "/basic" req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.proxy_uri = "coap://127.0.0.1:5684/storage/new" req.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CREATED.number expected.token = None expected.payload = None exchange1 = (req, expected) self.current_mid += 1 # PREPARING SECOND EXPECTED RESPONSE req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" exchange2 = (req2, expected) self.current_mid += 1 # PREPARING THIRD EXPECTED RESPONSE req3 = Request() req3.code = defines.Codes.POST.number req3.uri_path = path req3.type = defines.Types["CON"] req3._mid = self.current_mid req3.destination = self.server_address req3.proxy_uri = "coap://127.0.0.1:5684/storage/new" req3.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CREATED.number expected.token = None expected.payload = None exchange3 = (req3, expected) self.current_mid += 1 #PREPARING FOURTH EXPECTED RESPONSE req4 = Request() req4.code = defines.Codes.GET.number req4.uri_path = path req4.type = defines.Types["CON"] req4._mid = self.current_mid req4.destination = self.server_address req4.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" exchange4 = (req4, expected) self.current_mid += 1 self._test_with_client_delayed( [exchange1, exchange2, exchange3, exchange4]) def test_get_put(self): print "TEST_GET_PUT" path = "/basic" req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.proxy_uri = "coap://127.0.0.1:5684/storage/new" req.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CREATED.number expected.token = None expected.payload = None exchange1 = (req, expected) self.current_mid += 1 # PREPARING SECOND EXPECTED RESPONSE req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" exchange2 = (req2, expected) self.current_mid += 1 # PREPARING THIRD EXPECTED RESPONSE req3 = Request() req3.code = defines.Codes.PUT.number req3.uri_path = path req3.type = defines.Types["CON"] req3._mid = self.current_mid req3.destination = self.server_address req3.proxy_uri = "coap://127.0.0.1:5684/storage/new" req3.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CHANGED.number expected.token = None expected.payload = None exchange3 = (req3, expected) self.current_mid += 1 # PREPARING FOURTH EXPECTED RESPONSE req4 = Request() req4.code = defines.Codes.GET.number req4.uri_path = path req4.type = defines.Types["CON"] req4._mid = self.current_mid req4.destination = self.server_address req4.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" exchange4 = (req4, expected) self.current_mid += 1 self._test_with_client_delayed( [exchange1, exchange2, exchange3, exchange4]) def test_get_delete(self): print "TEST_GET_DELETE" path = "/basic" req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.NOT_FOUND.number expected.token = None expected.payload = None exchange0 = (req2, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.proxy_uri = "coap://127.0.0.1:5684/storage/new" req.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CREATED.number expected.token = None expected.payload = None exchange1 = (req, expected) self.current_mid += 1 # PREPARING SECOND EXPECTED RESPONSE req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" exchange2 = (req2, expected) self.current_mid += 1 # PREPARING THIRD EXPECTED RESPONSE req3 = Request() req3.code = defines.Codes.DELETE.number req3.uri_path = path req3.type = defines.Types["CON"] req3._mid = self.current_mid req3.destination = self.server_address req3.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.DELETED.number expected.token = None expected.payload = None exchange3 = (req3, expected) self.current_mid += 1 # PREPARING FOURTH EXPECTED RESPONSE req4 = Request() req4.code = defines.Codes.GET.number req4.uri_path = path req4.type = defines.Types["CON"] req4._mid = self.current_mid req4.destination = self.server_address req4.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.NOT_FOUND.number expected.token = None exchange4 = (req4, expected) self.current_mid += 1 self._test_with_client_delayed( [exchange0, exchange1, exchange2, exchange3, exchange4]) def test_get_etag(self): print "TEST_GET_ETAG" path = "/etag" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.proxy_uri = "coap://127.0.0.1:5684/etag" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = None exchange1 = (req, expected) self.current_mid += 1 # PREPARING SECOND EXPECTED RESPONSE req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/etag" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.etag = 1 expected.max_age = 1 exchange2 = (req2, expected) self.current_mid += 1 # PREPARING THIRD EXPECTED RESPONSE req3 = Request() req3.code = defines.Codes.POST.number req3.uri_path = path req3.type = defines.Types["CON"] req3._mid = self.current_mid req3.destination = self.server_address req3.proxy_uri = "coap://127.0.0.1:5684/etag" req3.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CHANGED.number expected.token = None expected.payload = None expected.etag = 2 expected.location_path = "etag" exchange3 = (req3, expected) self.current_mid += 1 # PREPARING FOURTH EXPECTED RESPONSE req4 = Request() req4.code = defines.Codes.GET.number req4.uri_path = path req4.type = defines.Types["CON"] req4._mid = self.current_mid req4.destination = self.server_address req4.proxy_uri = "coap://127.0.0.1:5684/etag" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" expected.etag = 2 exchange4 = (req4, expected) self.current_mid += 1 self._test_with_client_delayed( [exchange1, exchange2, exchange3, exchange4])
class Tests(unittest.TestCase): def setUp(self): self.proto = CoAPServer() self.tr = proto_helpers.FakeDatagramTransport() self.proto.makeConnection(self.tr) self.current_mid = random.randint(1, 1000) def _test(self, message, expected): serializer = Serializer() datagram = serializer.serialize(message) self.proto.datagramReceived(datagram, ("127.0.0.1", 5632)) datagram, source = self.tr.written[-1] host, port = source message = serializer.deserialize(datagram, host, port) self.assertEqual(message.type, expected.type) self.assertEqual(message.mid, expected.mid) self.assertEqual(message.code, expected.code) self.assertEqual(message.source, source) self.assertEqual(message.token, expected.token) self.assertEqual(message.payload, expected.payload) self.assertEqual(message.options, expected.options) self.tr.written = [] def _test_separate(self, message, expected): serializer = Serializer() datagram = serializer.serialize(message) self.proto.datagramReceived(datagram, ("127.0.0.1", 5632)) datagram, source = self.tr.written[0] host, port = source message = serializer.deserialize(datagram, host, port) self.assertEqual(message.type, defines.inv_types["ACK"]) self.assertEqual(message.code, None) self.assertEqual(message.mid, self.current_mid) self.assertEqual(message.source, source) datagram, source = self.tr.written[1] host, port = source message = serializer.deserialize(datagram, host, port) self.assertEqual(message.type, expected.type) self.assertEqual(message.code, expected.code) self.assertEqual(message.source, source) self.assertEqual(message.token, expected.token) self.assertEqual(message.payload, expected.payload) self.assertEqual(message.options, expected.options) self.tr.written = [] message = Message.new_ack(message) datagram = serializer.serialize(message) self.proto.datagramReceived(datagram, ("127.0.0.1", 5632)) def tearDown(self): self.proto.stopProtocol() del self.proto del self.tr def test_get_storage(self): args = ("/storage",) kwargs = {} path = args[0] req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req.mid = self.current_mid expected = Response() expected.type = defines.inv_types["ACK"] expected.mid = self.current_mid expected.code = defines.responses["CONTENT"] expected.token = None expected.payload = "Storage Resource for PUT, POST and DELETE" self._test(req, expected) def test_get_not_found(self): args = ("/not_found",) kwargs = {} path = args[0] req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req.mid = self.current_mid expected = Response() expected.type = defines.inv_types["NON"] expected.mid = self.current_mid expected.code = defines.responses["NOT_FOUND"] expected.token = None expected.payload = None self._test(req, expected) def test_post_and_get_storage(self): args = ("/storage/data1",) kwargs = {} path = args[0] req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['POST'] req.uri_path = path req.type = defines.inv_types["CON"] req.mid = self.current_mid req.payload = "Created" expected = Response() expected.type = defines.inv_types["ACK"] expected.mid = self.current_mid expected.code = defines.responses["CREATED"] expected.token = None expected.payload = None option = Option() option.number = defines.inv_options["Location-Path"] option.value = "storage/data1" expected.add_option(option) self._test(req, expected) req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req.mid = self.current_mid expected = Response() expected.type = defines.inv_types["ACK"] expected.mid = self.current_mid expected.code = defines.responses["CONTENT"] expected.token = None expected.payload = "Created" def test_get_separate(self): args = ("/separate",) kwargs = {} path = args[0] req = Request() for key in kwargs: o = Option() o.number = defines.inv_options[key] o.value = kwargs[key] req.add_option(o) req.code = defines.inv_codes['GET'] req.uri_path = path req.type = defines.inv_types["CON"] req.mid = self.current_mid expected = Response() expected.type = defines.inv_types["CON"] expected.code = defines.responses["CONTENT"] expected.token = None expected.payload = "Separate" self._test_separate(req, expected)
class Tests(unittest.TestCase): def setUp(self): self.server_address = ("127.0.0.1", 5683) self.current_mid = random.randint(1, 1000) self.server_mid = random.randint(1000, 2000) self.server = CoAPServer("127.0.0.1", 5684) self.server_thread = threading.Thread(target=self.server.listen, args=(10,)) self.server_thread.start() self.proxy = CoAPForwardProxy("127.0.0.1", 5683, cache=True) self.proxy_thread = threading.Thread(target=self.proxy.listen, args=(10,)) self.proxy_thread.start() self.queue = Queue() def tearDown(self): self.server.close() self.server_thread.join(timeout=25) self.server = None self.proxy.close() self.proxy_thread.join(timeout=25) self.proxy = None def _test_with_client_delayed(self, message_list): # pragma: no cover client = HelperClient(self.server_address) for message, expected in message_list: if message is not None: received_message = client.send_request(message) time.sleep(5) if expected is not None: if expected.etag is not None: self.assertEqual(received_message.etag, expected.etag) if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.max_age is not None: if expected.max_age != 60: self.assertNotEqual(received_message.max_age, 60) else: self.assertEqual(received_message.max_age, expected.max_age) if expected.options: self.assertEqual(len(received_message.options), len(expected.options)) for o in expected.options: assert isinstance(o, Option) if o.name != defines.OptionRegistry.MAX_AGE.name and o.name != defines.OptionRegistry.ETAG.name: option_value = getattr(expected, o.name.lower().replace("-", "_")) option_value_rec = getattr(received_message, o.name.lower().replace("-", "_")) self.assertEqual(option_value, option_value_rec) client.stop() def client_callback(self, response): print "Callback" self.queue.put(response) def test_get_multiple(self): print "TEST_GET_MULTIPLE" path = "/basic" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.proxy_uri = "coap://127.0.0.1:5684/basic" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Basic Resource" exchange1 = (req, expected) self.current_mid += 1 # PREPARING SECOND EXPECTED RESPONSE (MAX AGE MUST BE CHECKED) req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/basic" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Basic Resource" expected.max_age = 61 exchange2 = (req2, expected) self._test_with_client_delayed([exchange1, exchange2]) def test_get_post(self): print "TEST_GET_POST" path = "/basic" req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.proxy_uri = "coap://127.0.0.1:5684/storage/new" req.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CREATED.number expected.token = None expected.payload = None exchange1 = (req, expected) self.current_mid += 1 # PREPARING SECOND EXPECTED RESPONSE req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" exchange2 = (req2, expected) self.current_mid += 1 # PREPARING THIRD EXPECTED RESPONSE req3 = Request() req3.code = defines.Codes.POST.number req3.uri_path = path req3.type = defines.Types["CON"] req3._mid = self.current_mid req3.destination = self.server_address req3.proxy_uri = "coap://127.0.0.1:5684/storage/new" req3.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CREATED.number expected.token = None expected.payload = None exchange3 = (req3, expected) self.current_mid += 1 # PREPARING FOURTH EXPECTED RESPONSE req4 = Request() req4.code = defines.Codes.GET.number req4.uri_path = path req4.type = defines.Types["CON"] req4._mid = self.current_mid req4.destination = self.server_address req4.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" exchange4 = (req4, expected) self.current_mid += 1 self._test_with_client_delayed([exchange1, exchange2, exchange3, exchange4]) def test_get_put(self): print "TEST_GET_PUT" path = "/basic" req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.proxy_uri = "coap://127.0.0.1:5684/storage/new" req.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CREATED.number expected.token = None expected.payload = None exchange1 = (req, expected) self.current_mid += 1 # PREPARING SECOND EXPECTED RESPONSE req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" exchange2 = (req2, expected) self.current_mid += 1 # PREPARING THIRD EXPECTED RESPONSE req3 = Request() req3.code = defines.Codes.PUT.number req3.uri_path = path req3.type = defines.Types["CON"] req3._mid = self.current_mid req3.destination = self.server_address req3.proxy_uri = "coap://127.0.0.1:5684/storage/new" req3.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CHANGED.number expected.token = None expected.payload = None exchange3 = (req3, expected) self.current_mid += 1 # PREPARING FOURTH EXPECTED RESPONSE req4 = Request() req4.code = defines.Codes.GET.number req4.uri_path = path req4.type = defines.Types["CON"] req4._mid = self.current_mid req4.destination = self.server_address req4.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" exchange4 = (req4, expected) self.current_mid += 1 self._test_with_client_delayed([exchange1, exchange2, exchange3, exchange4]) def test_get_delete(self): print "TEST_GET_DELETE" path = "/basic" req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.NOT_FOUND.number expected.token = None expected.payload = None exchange0 = (req2, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.proxy_uri = "coap://127.0.0.1:5684/storage/new" req.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CREATED.number expected.token = None expected.payload = None exchange1 = (req, expected) self.current_mid += 1 # PREPARING SECOND EXPECTED RESPONSE req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" exchange2 = (req2, expected) self.current_mid += 1 # PREPARING THIRD EXPECTED RESPONSE req3 = Request() req3.code = defines.Codes.DELETE.number req3.uri_path = path req3.type = defines.Types["CON"] req3._mid = self.current_mid req3.destination = self.server_address req3.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.DELETED.number expected.token = None expected.payload = None exchange3 = (req3, expected) self.current_mid += 1 # PREPARING FOURTH EXPECTED RESPONSE req4 = Request() req4.code = defines.Codes.GET.number req4.uri_path = path req4.type = defines.Types["CON"] req4._mid = self.current_mid req4.destination = self.server_address req4.proxy_uri = "coap://127.0.0.1:5684/storage/new" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.NOT_FOUND.number expected.token = None exchange4 = (req4, expected) self.current_mid += 1 self._test_with_client_delayed([exchange0, exchange1, exchange2, exchange3, exchange4]) def test_get_etag(self): print "TEST_GET_ETAG" path = "/etag" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.proxy_uri = "coap://127.0.0.1:5684/etag" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = None expected.etag = str(0) exchange1 = (req, expected) self.current_mid += 1 # PREPARING SECOND EXPECTED RESPONSE req2 = Request() req2.code = defines.Codes.GET.number req2.uri_path = path req2.type = defines.Types["CON"] req2._mid = self.current_mid req2.destination = self.server_address req2.proxy_uri = "coap://127.0.0.1:5684/etag" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.etag = str(0) expected.max_age = 1 exchange2 = (req2, expected) self.current_mid += 1 # PREPARING THIRD EXPECTED RESPONSE req3 = Request() req3.code = defines.Codes.POST.number req3.uri_path = path req3.type = defines.Types["CON"] req3._mid = self.current_mid req3.destination = self.server_address req3.proxy_uri = "coap://127.0.0.1:5684/etag" req3.payload = "Hello" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CHANGED.number expected.token = None expected.payload = None expected.etag = str(1) expected.location_path = "etag" exchange3 = (req3, expected) self.current_mid += 1 # PREPARING FOURTH EXPECTED RESPONSE req4 = Request() req4.code = defines.Codes.GET.number req4.uri_path = path req4.type = defines.Types["CON"] req4._mid = self.current_mid req4.destination = self.server_address req4.proxy_uri = "coap://127.0.0.1:5684/etag" expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.CONTENT.number expected.token = None expected.payload = "Hello" expected.etag = str(1) exchange4 = (req4, expected) self.current_mid += 1 self._test_with_client_delayed([exchange1, exchange2, exchange3, exchange4])
class Tests(unittest.TestCase): def setUp(self): self.server_address = ("127.0.0.1", 5683) self.current_mid = random.randint(1, 1000) self.server_mid = random.randint(1000, 2000) self.server = CoAPServer("127.0.0.1", 5683) self.server_thread = threading.Thread(target=self.server.listen, args=(10,)) self.server_thread.start() self.queue = Queue() def tearDown(self): self.server.close() self.server_thread.join(timeout=25) self.server = None def _test_with_client(self, message_list): client = HelperClient(self.server_address) for message, expected in message_list: if message is not None: received_message = client.send_request(message) if expected is not None: if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, self.server_address) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.options: self.assertEqual(received_message.options, expected.options) client.stop() def client_callback(self, response): print "Callback" self.queue.put(response) def _test_plugtest(self, message_list): serializer = Serializer() sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) for message, expected in message_list: if message is not None: datagram = serializer.serialize(message) sock.sendto(datagram, message.destination) if expected is not None: datagram, source = sock.recvfrom(4096) received_message = serializer.deserialize(datagram, source) if expected.type is not None: self.assertEqual(received_message.type, expected.type) if expected.mid is not None: self.assertEqual(received_message.mid, expected.mid) self.assertEqual(received_message.code, expected.code) if expected.source is not None: self.assertEqual(received_message.source, source) if expected.token is not None: self.assertEqual(received_message.token, expected.token) if expected.payload is not None: self.assertEqual(received_message.payload, expected.payload) if expected.options is not None: self.assertEqual(received_message.options, expected.options) sock.close() def test_not_allowed(self): print "TEST_NOT_ALLOWED" path = "/void" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.METHOD_NOT_ALLOWED.number expected.token = None exchange1 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.METHOD_NOT_ALLOWED.number expected.token = None exchange2 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.PUT.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.METHOD_NOT_ALLOWED.number expected.token = None exchange3 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.DELETE.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["ACK"] expected._mid = self.current_mid expected.code = defines.Codes.METHOD_NOT_ALLOWED.number expected.token = None exchange4 = (req, expected) self.current_mid += 1 self._test_with_client([exchange1, exchange2, exchange3, exchange4]) def test_separate(self): print "TEST_SEPARATE" path = "/separate" req = Request() req.code = defines.Codes.GET.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["CON"] expected._mid = None expected.code = defines.Codes.CONTENT.number expected.token = None exchange1 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.POST.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.payload = "POST" expected = Response() expected.type = defines.Types["CON"] expected._mid = None expected.code = defines.Codes.CREATED.number expected.token = None expected.options = None exchange2 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.PUT.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address req.payload = "PUT" expected = Response() expected.type = defines.Types["CON"] expected._mid = None expected.code = defines.Codes.CHANGED.number expected.token = None expected.options = None exchange3 = (req, expected) self.current_mid += 1 req = Request() req.code = defines.Codes.DELETE.number req.uri_path = path req.type = defines.Types["CON"] req._mid = self.current_mid req.destination = self.server_address expected = Response() expected.type = defines.Types["CON"] expected._mid = None expected.code = defines.Codes.DELETED.number expected.token = None exchange4 = (req, expected) self.current_mid += 1 self._test_with_client([exchange1, exchange2, exchange3, exchange4])