- removed obsolete imports
- Location-Path and Location-Query only in answers with "Created" Status
- Fixed / for root-resource as RemoteResource for reverse-proxy
- Fixed parsing of long options in serializer.py
- Commented out unit tests reactivated
- Removed default logging
This commit is contained in:
Björn Freise 2019-04-23 11:44:04 +02:00
parent 2eafa64308
commit 5bebe167a9
16 changed files with 1497 additions and 1551 deletions

View file

@ -1,8 +1,9 @@
from queue import Queue from queue import Queue
import random import random
import socket
import threading import threading
import unittest import unittest
import time
from coapclient import HelperClient from coapclient import HelperClient
from coapforwardproxy import CoAPForwardProxy from coapforwardproxy import CoAPForwardProxy
from coapserver import CoAPServer from coapserver import CoAPServer
@ -10,8 +11,6 @@ from coapthon import defines
from coapthon.messages.option import Option from coapthon.messages.option import Option
from coapthon.messages.request import Request from coapthon.messages.request import Request
from coapthon.messages.response import Response from coapthon.messages.response import Response
from coapthon.serializer import Serializer
import time
__author__ = 'Emilio Vallati' __author__ = 'Emilio Vallati'
__version__ = "1.0" __version__ = "1.0"
@ -459,7 +458,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
expected.payload = None expected.payload = None
expected.etag = str(1) expected.etag = str(1)
expected.location_path = "etag"
exchange3 = (req3, expected) exchange3 = (req3, expected)

View file

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import collections import collections
import array
import struct import struct
__author__ = 'Giacomo Tanganelli' __author__ = 'Giacomo Tanganelli'
@ -167,7 +168,7 @@ class OptionRegistry(object):
:return: option flags :return: option flags
:rtype: 3-tuple (critical, unsafe, no-cache) :rtype: 3-tuple (critical, unsafe, no-cache)
""" """
opt_bytes = array.array('B', '\0\0') opt_bytes = bytearray(2)
if option_num < 256: if option_num < 256:
s = struct.Struct("!B") s = struct.Struct("!B")
s.pack_into(opt_bytes, 0, option_num) s.pack_into(opt_bytes, 0, option_num)

View file

@ -4,8 +4,6 @@ import socket
import struct import struct
import threading import threading
import os
from coapthon import defines from coapthon import defines
from coapthon.layers.blocklayer import BlockLayer from coapthon.layers.blocklayer import BlockLayer
from coapthon.layers.cachelayer import CacheLayer from coapthon.layers.cachelayer import CacheLayer

View file

@ -105,6 +105,8 @@ class ResourceLayer(object):
if resource.etag is not None: if resource.etag is not None:
transaction.response.etag = resource.etag transaction.response.etag = resource.etag
if transaction.response.code == defines.Codes.CREATED.number:
# Only on CREATED according to RFC 7252 Chapter 5.8.2 POST
transaction.response.location_path = resource.path transaction.response.location_path = resource.path
if resource.location_query is not None and len(resource.location_query) > 0: if resource.location_query is not None and len(resource.location_query) > 0:

View file

@ -4,7 +4,6 @@ import socket
import struct import struct
import threading import threading
import xml.etree.ElementTree as ElementTree import xml.etree.ElementTree as ElementTree
import os import os
import re import re
@ -173,7 +172,8 @@ class CoAP(object):
host, port = response.source host, port = response.source
if response.code == defines.Codes.CONTENT.number: if response.code == defines.Codes.CONTENT.number:
resource = Resource('server', self, visible=True, observable=False, allow_children=True) resource = RemoteResource('server', (host, port), "/",
coap_server=self, visible=True, observable=False, allow_children=True)
self.add_resource(name, resource) self.add_resource(name, resource)
self._mapping[name] = (host, port) self._mapping[name] = (host, port)
self.parse_core_link_format(response.payload, name, (host, port)) self.parse_core_link_format(response.payload, name, (host, port))
@ -209,8 +209,8 @@ class CoAP(object):
dict_att[a[0]] = a[0] dict_att[a[0]] = a[0]
link_format = link_format[result.end(0) + 1:] link_format = link_format[result.end(0) + 1:]
# TODO handle observing # TODO handle observing
resource = RemoteResource('server', remote_server, path, coap_server=self, visible=True, observable=False, resource = RemoteResource('server', remote_server, path,
allow_children=True) coap_server=self, visible=True, observable=False, allow_children=True)
resource.attributes = dict_att resource.attributes = dict_att
self.add_resource(base_path + "/" + path, resource) self.add_resource(base_path + "/" + path, resource)

View file

@ -1,6 +1,7 @@
import logging import logging
import struct import struct
import ctypes import ctypes
from coapthon.messages.request import Request from coapthon.messages.request import Request
from coapthon.messages.response import Response from coapthon.messages.response import Response
from coapthon.messages.option import Option from coapthon.messages.option import Option
@ -262,28 +263,6 @@ class Serializer(object):
""" """
return defines.RESPONSE_CODE_LOWER_BOUND <= code <= defines.RESPONSE_CODE_UPPER_BOUND return defines.RESPONSE_CODE_LOWER_BOUND <= code <= defines.RESPONSE_CODE_UPPER_BOUND
@staticmethod
def read_option_value_from_nibble(nibble, pos, values):
"""
Calculates the value used in the extended option fields.
:param nibble: the 4-bit option header value.
:return: the value calculated from the nibble and the extended option value.
"""
if nibble <= 12:
return nibble, pos
elif nibble == 13:
tmp = struct.unpack("!B", values[pos].to_bytes(1, "big"))[0] + 13
pos += 1
return tmp, pos
elif nibble == 14:
s = struct.Struct("!H")
tmp = s.unpack_from(values[pos:].to_bytes(2, "big"))[0] + 269
pos += 2
return tmp, pos
else:
raise AttributeError("Unsupported option nibble " + str(nibble))
@staticmethod @staticmethod
def read_option_value_len_from_byte(byte, pos, values): def read_option_value_len_from_byte(byte, pos, values):
""" """
@ -303,7 +282,7 @@ class Serializer(object):
pos += 1 pos += 1
elif h_nibble == 14: elif h_nibble == 14:
s = struct.Struct("!H") s = struct.Struct("!H")
value = s.unpack_from(values[pos:].to_bytes(2, "big"))[0] + 269 value = s.unpack_from(values[pos:pos+2])[0] + 269
pos += 2 pos += 2
else: else:
raise AttributeError("Unsupported option number nibble " + str(h_nibble)) raise AttributeError("Unsupported option number nibble " + str(h_nibble))
@ -315,7 +294,7 @@ class Serializer(object):
pos += 1 pos += 1
elif l_nibble == 14: elif l_nibble == 14:
s = struct.Struct("!H") s = struct.Struct("!H")
length = s.unpack_from(values[pos:].to_bytes(2, "big"))[0] + 269 length = s.unpack_from(values[pos:pos+2])[0] + 269
pos += 2 pos += 2
else: else:
raise AttributeError("Unsupported option length nibble " + str(l_nibble)) raise AttributeError("Unsupported option length nibble " + str(l_nibble))

View file

@ -1,14 +1,14 @@
import os # -*- coding: utf-8 -*-
import logging.config
from queue import Queue from queue import Queue
import random import random
import socket import socket
import threading import threading
import unittest import unittest
from coapclient import HelperClient from coapclient import HelperClient
from coapserver import CoAPServer from coapserver import CoAPServer
from coapthon import defines from coapthon import defines
from coapthon.utils import create_logging
from coapthon.messages.message import Message from coapthon.messages.message import Message
from coapthon.messages.option import Option from coapthon.messages.option import Option
from coapthon.messages.request import Request from coapthon.messages.request import Request
@ -60,10 +60,6 @@ PAYLOAD = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam no
"erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd " \ "erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd " \
"gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." "gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet."
if not os.path.isfile("logging.conf"):
create_logging()
logging.config.fileConfig("logging.conf", disable_existing_loggers=False)
class Tests(unittest.TestCase): class Tests(unittest.TestCase):
@ -271,88 +267,86 @@ class Tests(unittest.TestCase):
self.current_mid += 1 self.current_mid += 1
self._test_with_client([exchange1, exchange2, exchange3, exchange4]) self._test_with_client([exchange1, exchange2, exchange3, exchange4])
# def test_separate(self): def test_separate(self):
# print "TEST_SEPARATE" print("TEST_SEPARATE")
# path = "/separate" path = "/separate"
# req = Request()
# req.code = defines.Codes.GET.number req = Request()
# req.uri_path = path req.code = defines.Codes.GET.number
# req.type = defines.Types["CON"] req.uri_path = path
# req._mid = self.current_mid req.type = defines.Types["CON"]
# req.destination = self.server_address req._mid = self.current_mid
# req.destination = self.server_address
# expected = Response()
# expected.type = defines.Types["CON"] expected = Response()
# expected._mid = None expected.type = defines.Types["CON"]
# expected.code = defines.Codes.CONTENT.number expected._mid = None
# expected.token = None expected.code = defines.Codes.CONTENT.number
# expected.max_age = 60 expected.token = None
# expected.max_age = 60
# exchange1 = (req, expected)
# exchange1 = (req, expected)
# self.current_mid += 1 self.current_mid += 1
#
# req = Request() req = Request()
# req.code = defines.Codes.POST.number req.code = defines.Codes.POST.number
# req.uri_path = path req.uri_path = path
# req.type = defines.Types["CON"] req.type = defines.Types["CON"]
# req._mid = self.current_mid req._mid = self.current_mid
# req.destination = self.server_address req.destination = self.server_address
# req.payload = "POST" req.payload = "POST"
#
# expected = Response() expected = Response()
# expected.type = defines.Types["CON"] expected.type = defines.Types["CON"]
# expected._mid = None expected._mid = None
# expected.code = defines.Codes.CHANGED.number expected.code = defines.Codes.CHANGED.number
# expected.token = None expected.token = None
# expected.options = None expected.options = None
#
# exchange2 = (req, expected) exchange2 = (req, expected)
# self.current_mid += 1
# self.current_mid += 1
# req = Request()
# req = Request() req.code = defines.Codes.PUT.number
# req.code = defines.Codes.PUT.number req.uri_path = path
# req.uri_path = path req.type = defines.Types["CON"]
# req.type = defines.Types["CON"] req._mid = self.current_mid
# req._mid = self.current_mid req.destination = self.server_address
# req.destination = self.server_address req.payload = "PUT"
# req.payload = "PUT"
# expected = Response()
# expected = Response() expected.type = defines.Types["CON"]
# expected.type = defines.Types["CON"] expected._mid = None
# expected._mid = None expected.code = defines.Codes.CHANGED.number
# expected.code = defines.Codes.CHANGED.number expected.token = None
# expected.token = None expected.options = None
# expected.options = None
# exchange3 = (req, expected)
# exchange3 = (req, expected) self.current_mid += 1
#
# self.current_mid += 1 req = Request()
# req.code = defines.Codes.DELETE.number
# req = Request() req.uri_path = path
# req.code = defines.Codes.DELETE.number req.type = defines.Types["CON"]
# req.uri_path = path req._mid = self.current_mid
# req.type = defines.Types["CON"] req.destination = self.server_address
# req._mid = self.current_mid
# req.destination = self.server_address expected = Response()
# expected.type = defines.Types["CON"]
# expected = Response() expected._mid = None
# expected.type = defines.Types["CON"] expected.code = defines.Codes.DELETED.number
# expected._mid = None expected.token = None
# expected.code = defines.Codes.DELETED.number
# expected.token = None exchange4 = (req, expected)
# self.current_mid += 1
# exchange4 = (req, expected)
# self._test_with_client([exchange1, exchange2, exchange3, exchange4])
# self.current_mid += 1
# self._test_with_client([exchange1, exchange2, exchange3, exchange4])
def test_post(self): def test_post(self):
print("TEST_POST") print("TEST_POST")
path = "/storage/new_res?id=1" path = "/storage/new_res?id=1"
req = Request()
req = Request()
req.code = defines.Codes.POST.number req.code = defines.Codes.POST.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["CON"] req.type = defines.Types["CON"]
@ -743,7 +737,8 @@ class Tests(unittest.TestCase):
exchange8 = (req, expected) exchange8 = (req, expected)
self.current_mid += 1 self.current_mid += 1
self._test_plugtest([exchange0, exchange1, exchange2, exchange3, exchange4, exchange5, exchange6, exchange7, exchange8]) self._test_plugtest([exchange0, exchange1, exchange2, exchange3, exchange4,
exchange5, exchange6, exchange7, exchange8])
def test_post_block_big(self): def test_post_block_big(self):
print("TEST_POST_BLOCK_BIG") print("TEST_POST_BLOCK_BIG")
@ -884,7 +879,6 @@ class Tests(unittest.TestCase):
expected.code = defines.Codes.CHANGED.number expected.code = defines.Codes.CHANGED.number
expected.token = None expected.token = None
expected.payload = None expected.payload = None
expected.location_path = "big"
exchange7 = (req, expected) exchange7 = (req, expected)
self.current_mid += 1 self.current_mid += 1
@ -969,75 +963,74 @@ class Tests(unittest.TestCase):
self._test_with_client([exchange1, exchange2, exchange3]) self._test_with_client([exchange1, exchange2, exchange3])
# def test_long_options(self): def test_long_options(self):
# """ """
# Test processing of options with extended length Test processing of options with extended length
# """ """
# print("TEST_LONG_OPTIONS") print("TEST_LONG_OPTIONS")
# path = "/storage/"
# path = "/storage/"
# req = Request() req = Request()
# req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
# req.uri_path = path req.uri_path = path
# req.type = defines.Types["CON"] req.type = defines.Types["CON"]
# req._mid = self.current_mid req._mid = self.current_mid
# req.destination = self.server_address req.destination = self.server_address
# option = Option() option = Option()
# # This option should be silently ignored by the server # This option should be silently ignored by the server
# # since it is not critical # since it is not critical
# option.number = defines.OptionRegistry.RM_MESSAGE_SWITCHING.number option.number = defines.OptionRegistry.RM_MESSAGE_SWITCHING.number
# option.value = "\1\1\1\1\0\0" option.value = b'\1\1\1\1\0\0'
# options = req.options req.add_option(option)
# req.add_option(option) req.payload = "test"
# req.payload = "test"
# expected = Response()
# expected = Response() expected.type = defines.Types["ACK"]
# expected.type = defines.Types["ACK"] expected.code = defines.Codes.CONTENT.number
# expected.code = defines.Codes.CONTENT.number expected.token = None
# expected.token = None expected.payload = None
# expected.payload = None
# exchange1 = (req, expected)
# exchange1 = (req, expected) self.current_mid += 1
# self.current_mid += 1
# self._test_with_client([exchange1])
# self._test_with_client([exchange1])
# # This option (244) should be silently ignored by the server
# # This option (244) should be silently ignored by the server req = (b'\x40\x01\x01\x01\xd6\xe7\x01\x01\x01\x01\x00\x00', self.server_address)
# req = ("\x40\x01\x01\x01\xd6\xe7\x01\x01\x01\x01\x00\x00", self.server_address)
# expected = Response()
# expected = Response() expected.type = defines.Types["ACK"]
# expected.type = defines.Types["ACK"] expected._mid = None
# expected._mid = None expected.code = defines.Codes.NOT_FOUND.number
# expected.code = defines.Codes.NOT_FOUND.number expected.token = None
# expected.token = None expected.payload = None
# expected.payload = None
# exchange21 = (req, expected)
# exchange21 = (req, expected) self.current_mid += 1
# self.current_mid += 1
# # This option (245) should cause BAD REQUEST, as unrecognizable critical
# # This option (245) should cause BAD REQUEST, as unrecognizable critical req = (b'\x40\x01\x01\x01\xd6\xe8\x01\x01\x01\x01\x00\x00', self.server_address)
# req = ("\x40\x01\x01\x01\xd6\xe8\x01\x01\x01\x01\x00\x00", self.server_address)
# expected = Response()
# expected = Response() expected.type = defines.Types["RST"]
# expected.type = defines.Types["RST"] expected._mid = None
# expected._mid = None expected.code = defines.Codes.BAD_REQUEST.number
# expected.code = defines.Codes.BAD_REQUEST.number
# exchange22 = (req, expected)
# exchange22 = (req, expected) self.current_mid += 1
# self.current_mid += 1
# # This option (65525) should cause BAD REQUEST, as unrecognizable critical
# # This option (65525) should cause BAD REQUEST, as unrecognizable critical req = (b'\x40\x01\x01\x01\xe6\xfe\xe8\x01\x01\x01\x01\x00\x00', self.server_address)
# req = ("\x40\x01\x01\x01\xe6\xfe\xe8\x01\x01\x01\x01\x00\x00", self.server_address)
# expected = Response()
# expected = Response() expected.type = defines.Types["RST"]
# expected.type = defines.Types["RST"] expected._mid = None
# expected._mid = None expected.code = defines.Codes.BAD_REQUEST.number
# expected.code = defines.Codes.BAD_REQUEST.number
# exchange23 = (req, expected)
# exchange23 = (req, expected) self.current_mid += 1
# self.current_mid += 1
# self._test_datagram([exchange21, exchange22, exchange23])
# self._test_datagram([exchange21, exchange22, exchange23])
def test_content_type(self): def test_content_type(self):
print("TEST_CONTENT_TYPE") print("TEST_CONTENT_TYPE")
@ -1226,9 +1219,8 @@ class Tests(unittest.TestCase):
exchange10 = (req, expected) exchange10 = (req, expected)
self.current_mid += 1 self.current_mid += 1
# self._test_with_client([exchange1, exchange2, exchange3, exchange4, exchange5, exchange6, exchange7, self._test_with_client([exchange1, exchange2, exchange3, exchange4, exchange5,
# exchange8, exchange9, exchange10]) exchange6, exchange7, exchange8, exchange9, exchange10])
self._test_with_client([exchange1, exchange2, exchange3, exchange4, exchange5])
def test_ETAG(self): def test_ETAG(self):
print("TEST_ETAG") print("TEST_ETAG")
@ -1266,7 +1258,6 @@ class Tests(unittest.TestCase):
expected.code = defines.Codes.CHANGED.number expected.code = defines.Codes.CHANGED.number
expected.token = None expected.token = None
expected.payload = None expected.payload = None
expected.location_path = path
expected.etag = "1" expected.etag = "1"
exchange2 = (req, expected) exchange2 = (req, expected)
@ -1329,7 +1320,7 @@ class Tests(unittest.TestCase):
expected.code = defines.Codes.CREATED.number expected.code = defines.Codes.CREATED.number
expected.token = None expected.token = None
expected.payload = None expected.payload = None
expected.location_path = path expected.location_path = "child"
exchange1 = (req, expected) exchange1 = (req, expected)
self.current_mid += 1 self.current_mid += 1
@ -1464,60 +1455,60 @@ class Tests(unittest.TestCase):
self._test_with_client([exchange1, exchange2, exchange3, exchange4]) self._test_with_client([exchange1, exchange2, exchange3, exchange4])
# def test_invalid(self): def test_invalid(self):
# print("TEST_INVALID") print("TEST_INVALID")
#
# # version # version
# req = ("\x00\x01\x8c\xda", self.server_address) req = (b'\x00\x01\x8c\xda', self.server_address)
#
# expected = Response() expected = Response()
# expected.type = defines.Types["RST"] expected.type = defines.Types["RST"]
# expected._mid = None expected._mid = None
# expected.code = defines.Codes.BAD_REQUEST.number expected.code = defines.Codes.BAD_REQUEST.number
#
# exchange1 = (req, expected) exchange1 = (req, expected)
#
# # version # version
# req = ("\x40", self.server_address) req = (b'\x40', self.server_address)
#
# expected = Response() expected = Response()
# expected.type = defines.Types["RST"] expected.type = defines.Types["RST"]
# expected._mid = None expected._mid = None
# expected.code = defines.Codes.BAD_REQUEST.number expected.code = defines.Codes.BAD_REQUEST.number
#
# exchange2 = (req, expected) exchange2 = (req, expected)
#
# # code # code
# req = ("\x40\x05\x8c\xda", self.server_address) req = (b'\x40\x05\x8c\xda', self.server_address)
#
# expected = Response() expected = Response()
# expected.type = defines.Types["RST"] expected.type = defines.Types["RST"]
# expected._mid = None expected._mid = None
# expected.code = defines.Codes.BAD_REQUEST.number expected.code = defines.Codes.BAD_REQUEST.number
#
# exchange3 = (req, expected) exchange3 = (req, expected)
#
# # option # option
# req = ("\x40\x01\x8c\xda\x94", self.server_address) req = (b'\x40\x01\x8c\xda\x94', self.server_address)
#
# expected = Response() expected = Response()
# expected.type = defines.Types["RST"] expected.type = defines.Types["RST"]
# expected._mid = None expected._mid = None
# expected.code = defines.Codes.BAD_REQUEST.number expected.code = defines.Codes.BAD_REQUEST.number
#
# exchange4 = (req, expected) exchange4 = (req, expected)
#
# # payload marker # payload marker
# req = ("\x40\x02\x8c\xda\x75\x62\x61\x73\x69\x63\xff", self.server_address) req = (b'\x40\x02\x8c\xda\x75\x62\x61\x73\x69\x63\xff', self.server_address)
#
# expected = Response() expected = Response()
# expected.type = defines.Types["RST"] expected.type = defines.Types["RST"]
# expected._mid = None expected._mid = None
# expected.code = defines.Codes.BAD_REQUEST.number expected.code = defines.Codes.BAD_REQUEST.number
#
# exchange5 = (req, expected) exchange5 = (req, expected)
#
# self._test_datagram([exchange1, exchange2, exchange3, exchange4, exchange5]) self._test_datagram([exchange1, exchange2, exchange3, exchange4, exchange5])
def test_post_block_big_client(self): def test_post_block_big_client(self):
print("TEST_POST_BLOCK_BIG_CLIENT") print("TEST_POST_BLOCK_BIG_CLIENT")
@ -1599,6 +1590,6 @@ class Tests(unittest.TestCase):
self._test_with_client_observe([exchange1, exchange2]) self._test_with_client_observe([exchange1, exchange2])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -1,7 +1,10 @@
# -*- coding: utf-8 -*-
from queue import Queue from queue import Queue
import random import random
import threading import threading
import unittest import unittest
from coapclient import HelperClient from coapclient import HelperClient
from coapserver import CoAPServer from coapserver import CoAPServer
from coapthon import defines from coapthon import defines
@ -89,6 +92,7 @@ class Tests(unittest.TestCase):
def test_not_allowed(self): def test_not_allowed(self):
print("TEST_NOT_ALLOWED") print("TEST_NOT_ALLOWED")
path = "/void" path = "/void"
req = Request() req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
@ -103,7 +107,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange1 = (req, expected) exchange1 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -120,7 +123,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange2 = (req, expected) exchange2 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -137,7 +139,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange3 = (req, expected) exchange3 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -154,11 +155,10 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange4 = (req, expected) exchange4 = (req, expected)
self.current_mid += 1 self.current_mid += 1
self._test_with_client([exchange1, exchange2, exchange3, exchange4]) self._test_with_client([exchange1, exchange2, exchange3, exchange4])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-
import threading import threading
import unittest import unittest
from queue import Queue from queue import Queue
@ -61,6 +63,7 @@ class Tests(unittest.TestCase):
def test_advanced(self): def test_advanced(self):
print("TEST_ADVANCED") print("TEST_ADVANCED")
path = "/advanced" path = "/advanced"
req = Request() req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
@ -76,7 +79,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange1 = (req, expected) exchange1 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -94,7 +96,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange2 = (req, expected) exchange2 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -112,7 +113,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange3 = (req, expected) exchange3 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -130,13 +130,14 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange4 = (req, expected) exchange4 = (req, expected)
self.current_mid += 1 self.current_mid += 1
self._test_with_client([exchange1, exchange2, exchange3, exchange4]) self._test_with_client([exchange1, exchange2, exchange3, exchange4])
def test_advanced_separate(self): def test_advanced_separate(self):
print("TEST_ADVANCED_SEPARATE") print("TEST_ADVANCED_SEPARATE")
path = "/advancedSeparate" path = "/advancedSeparate"
req = Request() req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
@ -152,7 +153,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange1 = (req, expected) exchange1 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -170,7 +170,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange2 = (req, expected) exchange2 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -188,7 +187,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange3 = (req, expected) exchange3 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -206,9 +204,10 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange4 = (req, expected) exchange4 = (req, expected)
self.current_mid += 1 self.current_mid += 1
self._test_with_client([exchange1, exchange2, exchange3, exchange4]) self._test_with_client([exchange1, exchange2, exchange3, exchange4])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -1,11 +1,13 @@
# -*- coding: utf-8 -*-
from queue import Queue from queue import Queue
import random import random
import threading import threading
import unittest import unittest
from coapclient import HelperClient from coapclient import HelperClient
from coapserver import CoAPServer from coapserver import CoAPServer
from coapthon import defines from coapthon import defines
from coapthon.messages.message import Message
from coapthon.messages.option import Option from coapthon.messages.option import Option
from coapthon.messages.request import Request from coapthon.messages.request import Request
from coapthon.messages.response import Response from coapthon.messages.response import Response
@ -90,6 +92,7 @@ class Tests(unittest.TestCase):
def test_not_allowed(self): def test_not_allowed(self):
print("TEST_NOT_ALLOWED") print("TEST_NOT_ALLOWED")
path = "/void" path = "/void"
req = Request() req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
@ -104,7 +107,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange1 = (req, expected) exchange1 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -121,7 +123,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange2 = (req, expected) exchange2 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -138,7 +139,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange3 = (req, expected) exchange3 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -155,11 +155,10 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange4 = (req, expected) exchange4 = (req, expected)
self.current_mid += 1 self.current_mid += 1
self._test_with_client([exchange1, exchange2, exchange3, exchange4]) self._test_with_client([exchange1, exchange2, exchange3, exchange4])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -1,11 +1,13 @@
# -*- coding: utf-8 -*-
from queue import Queue from queue import Queue
import random import random
import threading import threading
import unittest import unittest
from coapclient import HelperClient from coapclient import HelperClient
from coapserver import CoAPServer from coapserver import CoAPServer
from coapthon import defines from coapthon import defines
from coapthon.messages.message import Message
from coapthon.messages.option import Option from coapthon.messages.option import Option
from coapthon.messages.request import Request from coapthon.messages.request import Request
from coapthon.messages.response import Response from coapthon.messages.response import Response
@ -90,6 +92,7 @@ class Tests(unittest.TestCase):
def test_not_allowed(self): def test_not_allowed(self):
print("TEST_NOT_ALLOWED") print("TEST_NOT_ALLOWED")
path = "/void" path = "/void"
req = Request() req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
@ -104,7 +107,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange1 = (req, expected) exchange1 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -121,7 +123,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange2 = (req, expected) exchange2 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -138,7 +139,6 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange3 = (req, expected) exchange3 = (req, expected)
self.current_mid += 1 self.current_mid += 1
req = Request() req = Request()
@ -155,11 +155,10 @@ class Tests(unittest.TestCase):
expected.token = None expected.token = None
exchange4 = (req, expected) exchange4 = (req, expected)
self.current_mid += 1 self.current_mid += 1
self._test_with_client([exchange1, exchange2, exchange3, exchange4]) self._test_with_client([exchange1, exchange2, exchange3, exchange4])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from queue import Queue from queue import Queue
import random import random
import socket import socket
import threading import threading
import unittest import unittest
from coapthon.messages.message import Message from coapthon.messages.message import Message
from coapclient import HelperClient from coapclient import HelperClient
from coapthon.messages.response import Response from coapthon.messages.response import Response
@ -127,6 +129,7 @@ class Tests(unittest.TestCase):
def test_td_coap_link_01(self): def test_td_coap_link_01(self):
print("TD_COAP_LINK_01") print("TD_COAP_LINK_01")
path = "/.well-known/core" path = "/.well-known/core"
req = Request() req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
@ -147,6 +150,7 @@ class Tests(unittest.TestCase):
def test_td_coap_link_02(self): def test_td_coap_link_02(self):
print("TD_COAP_LINK_02") print("TD_COAP_LINK_02")
path = "/.well-known/core" path = "/.well-known/core"
req = Request() req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
@ -168,6 +172,7 @@ class Tests(unittest.TestCase):
def test_td_coap_core_01(self): def test_td_coap_core_01(self):
print("TD_COAP_CORE_01") print("TD_COAP_CORE_01")
path = "/test" path = "/test"
req = Request() req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
@ -188,8 +193,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_02(self): def test_td_coap_core_02(self):
print("TD_COAP_CORE_02") print("TD_COAP_CORE_02")
path = "/test_post" path = "/test_post"
req = Request()
req = Request()
req.code = defines.Codes.POST.number req.code = defines.Codes.POST.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["CON"] req.type = defines.Types["CON"]
@ -204,7 +209,7 @@ class Tests(unittest.TestCase):
expected.code = defines.Codes.CREATED.number expected.code = defines.Codes.CREATED.number
expected.token = None expected.token = None
expected.payload = None expected.payload = None
expected.location_path = "/test_post" expected.location_path = "test_post"
self.current_mid += 1 self.current_mid += 1
self._test_with_client([(req, expected)]) self._test_with_client([(req, expected)])
@ -212,8 +217,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_03(self): def test_td_coap_core_03(self):
print("TD_COAP_CORE_03") print("TD_COAP_CORE_03")
path = "/test" path = "/test"
req = Request()
req = Request()
req.code = defines.Codes.PUT.number req.code = defines.Codes.PUT.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["CON"] req.type = defines.Types["CON"]
@ -233,7 +238,6 @@ class Tests(unittest.TestCase):
exchange1 = (req, expected) exchange1 = (req, expected)
req = Request() req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["CON"] req.type = defines.Types["CON"]
@ -251,7 +255,6 @@ class Tests(unittest.TestCase):
exchange2 = (req, expected) exchange2 = (req, expected)
req = Request() req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["CON"] req.type = defines.Types["CON"]
@ -274,8 +277,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_04(self): def test_td_coap_core_04(self):
print("TD_COAP_CORE_04") print("TD_COAP_CORE_04")
path = "/test" path = "/test"
req = Request()
req = Request()
req.code = defines.Codes.DELETE.number req.code = defines.Codes.DELETE.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["CON"] req.type = defines.Types["CON"]
@ -295,8 +298,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_05(self): def test_td_coap_core_05(self):
print("TD_COAP_CORE_05") print("TD_COAP_CORE_05")
path = "/test" path = "/test"
req = Request()
req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["NON"] req.type = defines.Types["NON"]
@ -316,8 +319,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_06(self): def test_td_coap_core_06(self):
print("TD_COAP_CORE_06") print("TD_COAP_CORE_06")
path = "/test_post" path = "/test_post"
req = Request()
req = Request()
req.code = defines.Codes.POST.number req.code = defines.Codes.POST.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["NON"] req.type = defines.Types["NON"]
@ -332,7 +335,7 @@ class Tests(unittest.TestCase):
expected.code = defines.Codes.CREATED.number expected.code = defines.Codes.CREATED.number
expected.token = None expected.token = None
expected.payload = None expected.payload = None
expected.location_path = "/test_post" expected.location_path = "test_post"
self.current_mid += 1 self.current_mid += 1
self._test_with_client([(req, expected)]) self._test_with_client([(req, expected)])
@ -340,8 +343,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_07(self): def test_td_coap_core_07(self):
print("TD_COAP_CORE_07") print("TD_COAP_CORE_07")
path = "/test" path = "/test"
req = Request()
req = Request()
req.code = defines.Codes.PUT.number req.code = defines.Codes.PUT.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["NON"] req.type = defines.Types["NON"]
@ -363,8 +366,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_08(self): def test_td_coap_core_08(self):
print("TD_COAP_CORE_08") print("TD_COAP_CORE_08")
path = "/test" path = "/test"
req = Request()
req = Request()
req.code = defines.Codes.DELETE.number req.code = defines.Codes.DELETE.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["NON"] req.type = defines.Types["NON"]
@ -384,8 +387,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_09(self): def test_td_coap_core_09(self):
print("TD_COAP_CORE_09") print("TD_COAP_CORE_09")
path = "/separate" path = "/separate"
req = Request()
req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["CON"] req.type = defines.Types["CON"]
@ -412,8 +415,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_10(self): def test_td_coap_core_10(self):
print("TD_COAP_CORE_10") print("TD_COAP_CORE_10")
path = "/test" path = "/test"
req = Request()
req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["CON"] req.type = defines.Types["CON"]
@ -435,8 +438,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_12(self): def test_td_coap_core_12(self):
print("TD_COAP_CORE_12") print("TD_COAP_CORE_12")
path = "/seg1/seg2/seg3" path = "/seg1/seg2/seg3"
req = Request()
req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["CON"] req.type = defines.Types["CON"]
@ -455,8 +458,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_13(self): def test_td_coap_core_13(self):
print("TD_COAP_CORE_13") print("TD_COAP_CORE_13")
path = "/query?first=1&second=2&third=3" path = "/query?first=1&second=2&third=3"
req = Request()
req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["CON"] req.type = defines.Types["CON"]
@ -476,8 +479,8 @@ class Tests(unittest.TestCase):
def test_td_coap_obs_01(self): def test_td_coap_obs_01(self):
print("TD_COAP_OBS_01") print("TD_COAP_OBS_01")
path = "/obs" path = "/obs"
req = Request()
req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["CON"] req.type = defines.Types["CON"]
@ -508,8 +511,8 @@ class Tests(unittest.TestCase):
def test_td_coap_obs_03(self): def test_td_coap_obs_03(self):
print("TD_COAP_OBS_03") print("TD_COAP_OBS_03")
path = "/obs" path = "/obs"
req = Request()
req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["CON"] req.type = defines.Types["CON"]
@ -802,6 +805,7 @@ I say, looked for all the world like a strip of that same patchwork quilt. Indee
def test_duplicate(self): def test_duplicate(self):
print("TEST_DUPLICATE") print("TEST_DUPLICATE")
path = "/test" path = "/test"
req = Request() req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
@ -816,11 +820,13 @@ I say, looked for all the world like a strip of that same patchwork quilt. Indee
expected.token = None expected.token = None
self.current_mid += 1 self.current_mid += 1
self._test_plugtest([(req, expected), (req, expected)]) self._test_plugtest([(req, expected), (req, expected)])
def test_duplicate_not_completed(self): def test_duplicate_not_completed(self):
print("TEST_DUPLICATE_NOT_COMPLETED") print("TEST_DUPLICATE_NOT_COMPLETED")
path = "/long" path = "/long"
req = Request() req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
@ -841,11 +847,13 @@ I say, looked for all the world like a strip of that same patchwork quilt. Indee
expected2.token = None expected2.token = None
self.current_mid += 1 self.current_mid += 1
self._test_plugtest([(req, None), (req, expected), (None, expected2)]) self._test_plugtest([(req, None), (req, expected), (None, expected2)])
def test_no_response(self): def test_no_response(self):
print("TEST_NO_RESPONSE") print("TEST_NO_RESPONSE")
path = "/long" path = "/long"
req = Request() req = Request()
req.code = defines.Codes.GET.number req.code = defines.Codes.GET.number
req.uri_path = path req.uri_path = path
@ -866,13 +874,14 @@ I say, looked for all the world like a strip of that same patchwork quilt. Indee
expected2.token = None expected2.token = None
self.current_mid += 1 self.current_mid += 1
self._test_plugtest([(req, expected), (None, expected2), (None, expected2), (None, expected2)]) self._test_plugtest([(req, expected), (None, expected2), (None, expected2), (None, expected2)])
def test_edit_resource(self): def test_edit_resource(self):
print("TEST_EDIT_RESOURCE") print("TEST_EDIT_RESOURCE")
path = "/obs" path = "/obs"
req = Request()
req = Request()
req.code = defines.Codes.POST.number req.code = defines.Codes.POST.number
req.uri_path = path req.uri_path = path
req.type = defines.Types["CON"] req.type = defines.Types["CON"]
@ -886,10 +895,11 @@ I say, looked for all the world like a strip of that same patchwork quilt. Indee
expected.code = defines.Codes.CHANGED.number expected.code = defines.Codes.CHANGED.number
expected.token = None expected.token = None
expected.payload = None expected.payload = None
expected.location_path = "/obs"
self.current_mid += 1 self.current_mid += 1
self._test_with_client([(req, expected)]) self._test_with_client([(req, expected)])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View file

@ -1,9 +1,9 @@
import getopt import getopt
import logging
import sys import sys
from coapthon.server.coap import CoAP
from plugtest_resources import TestResource, SeparateResource, ObservableResource, LargeResource, LargeUpdateResource, \ from plugtest_resources import TestResource, SeparateResource, ObservableResource, LargeResource, LargeUpdateResource, \
LongResource LongResource
from coapthon.server.coap import CoAP
__author__ = 'Giacomo Tanganelli' __author__ = 'Giacomo Tanganelli'
@ -11,33 +11,15 @@ __author__ = 'Giacomo Tanganelli'
class CoAPServerPlugTest(CoAP): class CoAPServerPlugTest(CoAP):
def __init__(self, host, port, multicast=False, starting_mid=None): def __init__(self, host, port, multicast=False, starting_mid=None):
CoAP.__init__(self, (host, port), multicast, starting_mid) CoAP.__init__(self, (host, port), multicast, starting_mid)
# create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
self.add_resource('test/', TestResource()) self.add_resource('test/', TestResource())
self.add_resource('separate/', SeparateResource()) self.add_resource('separate/', SeparateResource())
self.add_resource('seg1/', TestResource()) self.add_resource('seg1/', TestResource())
self.add_resource('seg1/seg2/', TestResource()) self.add_resource('seg1/seg2/', TestResource())
self.add_resource('seg1/seg2/seg3/', TestResource()) self.add_resource('seg1/seg2/seg3/', TestResource())
self.add_resource('query/', TestResource()) self.add_resource('query/', TestResource())
self.add_resource("obs/", ObservableResource(coap_server=self)) self.add_resource('obs/', ObservableResource(coap_server=self))
self.add_resource("large/", LargeResource(coap_server=self)) self.add_resource('large/', LargeResource(coap_server=self))
self.add_resource("large-update/", LargeUpdateResource(coap_server=self)) self.add_resource('large-update/', LargeUpdateResource(coap_server=self))
self.add_resource('long/', LongResource()) self.add_resource('long/', LongResource())
@ -66,9 +48,7 @@ def main(argv): # pragma: no cover
try: try:
server.listen(10) server.listen(10)
except KeyboardInterrupt: except KeyboardInterrupt:
print("Server Shutdown")
server.close() server.close()
print("Exiting...")
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -1,10 +1,9 @@
# coding=utf-8 # -*- coding: utf-8 -*-
import logging import logging
import threading import threading
import time import time
import datetime
from coapthon import defines from coapthon import defines
from coapthon.resources.resource import Resource from coapthon.resources.resource import Resource