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

View file

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

View file

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

View file

@ -105,6 +105,8 @@ class ResourceLayer(object):
if resource.etag is not None:
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
if resource.location_query is not None and len(resource.location_query) > 0:

View file

@ -4,7 +4,6 @@ import socket
import struct
import threading
import xml.etree.ElementTree as ElementTree
import os
import re
@ -173,7 +172,8 @@ class CoAP(object):
host, port = response.source
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._mapping[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]
link_format = link_format[result.end(0) + 1:]
# TODO handle observing
resource = RemoteResource('server', remote_server, path, coap_server=self, visible=True, observable=False,
allow_children=True)
resource = RemoteResource('server', remote_server, path,
coap_server=self, visible=True, observable=False, allow_children=True)
resource.attributes = dict_att
self.add_resource(base_path + "/" + path, resource)

View file

@ -1,6 +1,7 @@
import logging
import struct
import ctypes
from coapthon.messages.request import Request
from coapthon.messages.response import Response
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
@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
def read_option_value_len_from_byte(byte, pos, values):
"""
@ -303,7 +282,7 @@ class Serializer(object):
pos += 1
elif h_nibble == 14:
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
else:
raise AttributeError("Unsupported option number nibble " + str(h_nibble))
@ -315,7 +294,7 @@ class Serializer(object):
pos += 1
elif l_nibble == 14:
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
else:
raise AttributeError("Unsupported option length nibble " + str(l_nibble))

View file

@ -1,14 +1,14 @@
import os
import logging.config
# -*- coding: utf-8 -*-
from queue import Queue
import random
import socket
import threading
import unittest
from coapclient import HelperClient
from coapserver import CoAPServer
from coapthon import defines
from coapthon.utils import create_logging
from coapthon.messages.message import Message
from coapthon.messages.option import Option
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 " \
"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):
@ -271,88 +267,86 @@ class Tests(unittest.TestCase):
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
# expected.max_age = 60
#
# 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.CHANGED.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])
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
expected.max_age = 60
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.CHANGED.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])
def test_post(self):
print("TEST_POST")
path = "/storage/new_res?id=1"
req = Request()
req = Request()
req.code = defines.Codes.POST.number
req.uri_path = path
req.type = defines.Types["CON"]
@ -743,7 +737,8 @@ class Tests(unittest.TestCase):
exchange8 = (req, expected)
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):
print("TEST_POST_BLOCK_BIG")
@ -884,7 +879,6 @@ class Tests(unittest.TestCase):
expected.code = defines.Codes.CHANGED.number
expected.token = None
expected.payload = None
expected.location_path = "big"
exchange7 = (req, expected)
self.current_mid += 1
@ -969,75 +963,74 @@ class Tests(unittest.TestCase):
self._test_with_client([exchange1, exchange2, exchange3])
# def test_long_options(self):
# """
# Test processing of options with extended length
# """
# print("TEST_LONG_OPTIONS")
#
# path = "/storage/"
# 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
# option = Option()
# # This option should be silently ignored by the server
# # since it is not critical
# option.number = defines.OptionRegistry.RM_MESSAGE_SWITCHING.number
# option.value = "\1\1\1\1\0\0"
# options = req.options
# req.add_option(option)
# req.payload = "test"
#
# expected = Response()
# expected.type = defines.Types["ACK"]
# expected.code = defines.Codes.CONTENT.number
# expected.token = None
# expected.payload = None
#
# exchange1 = (req, expected)
# self.current_mid += 1
#
# self._test_with_client([exchange1])
#
# # This option (244) should be silently ignored by the server
# req = ("\x40\x01\x01\x01\xd6\xe7\x01\x01\x01\x01\x00\x00", self.server_address)
#
# expected = Response()
# expected.type = defines.Types["ACK"]
# expected._mid = None
# expected.code = defines.Codes.NOT_FOUND.number
# expected.token = None
# expected.payload = None
#
# exchange21 = (req, expected)
# self.current_mid += 1
#
# # This option (245) should cause BAD REQUEST, as unrecognizable critical
# req = ("\x40\x01\x01\x01\xd6\xe8\x01\x01\x01\x01\x00\x00", self.server_address)
#
# expected = Response()
# expected.type = defines.Types["RST"]
# expected._mid = None
# expected.code = defines.Codes.BAD_REQUEST.number
#
# exchange22 = (req, expected)
# self.current_mid += 1
#
# # This option (65525) should cause BAD REQUEST, as unrecognizable critical
# req = ("\x40\x01\x01\x01\xe6\xfe\xe8\x01\x01\x01\x01\x00\x00", self.server_address)
#
# expected = Response()
# expected.type = defines.Types["RST"]
# expected._mid = None
# expected.code = defines.Codes.BAD_REQUEST.number
#
# exchange23 = (req, expected)
# self.current_mid += 1
#
# self._test_datagram([exchange21, exchange22, exchange23])
def test_long_options(self):
"""
Test processing of options with extended length
"""
print("TEST_LONG_OPTIONS")
path = "/storage/"
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
option = Option()
# This option should be silently ignored by the server
# since it is not critical
option.number = defines.OptionRegistry.RM_MESSAGE_SWITCHING.number
option.value = b'\1\1\1\1\0\0'
req.add_option(option)
req.payload = "test"
expected = Response()
expected.type = defines.Types["ACK"]
expected.code = defines.Codes.CONTENT.number
expected.token = None
expected.payload = None
exchange1 = (req, expected)
self.current_mid += 1
self._test_with_client([exchange1])
# 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)
expected = Response()
expected.type = defines.Types["ACK"]
expected._mid = None
expected.code = defines.Codes.NOT_FOUND.number
expected.token = None
expected.payload = None
exchange21 = (req, expected)
self.current_mid += 1
# 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)
expected = Response()
expected.type = defines.Types["RST"]
expected._mid = None
expected.code = defines.Codes.BAD_REQUEST.number
exchange22 = (req, expected)
self.current_mid += 1
# 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)
expected = Response()
expected.type = defines.Types["RST"]
expected._mid = None
expected.code = defines.Codes.BAD_REQUEST.number
exchange23 = (req, expected)
self.current_mid += 1
self._test_datagram([exchange21, exchange22, exchange23])
def test_content_type(self):
print("TEST_CONTENT_TYPE")
@ -1226,9 +1219,8 @@ class Tests(unittest.TestCase):
exchange10 = (req, expected)
self.current_mid += 1
# self._test_with_client([exchange1, exchange2, exchange3, exchange4, exchange5, exchange6, exchange7,
# exchange8, exchange9, exchange10])
self._test_with_client([exchange1, exchange2, exchange3, exchange4, exchange5])
self._test_with_client([exchange1, exchange2, exchange3, exchange4, exchange5,
exchange6, exchange7, exchange8, exchange9, exchange10])
def test_ETAG(self):
print("TEST_ETAG")
@ -1266,7 +1258,6 @@ class Tests(unittest.TestCase):
expected.code = defines.Codes.CHANGED.number
expected.token = None
expected.payload = None
expected.location_path = path
expected.etag = "1"
exchange2 = (req, expected)
@ -1329,7 +1320,7 @@ class Tests(unittest.TestCase):
expected.code = defines.Codes.CREATED.number
expected.token = None
expected.payload = None
expected.location_path = path
expected.location_path = "child"
exchange1 = (req, expected)
self.current_mid += 1
@ -1464,60 +1455,60 @@ class Tests(unittest.TestCase):
self._test_with_client([exchange1, exchange2, exchange3, exchange4])
# def test_invalid(self):
# print("TEST_INVALID")
#
# # version
# req = ("\x00\x01\x8c\xda", self.server_address)
#
# expected = Response()
# expected.type = defines.Types["RST"]
# expected._mid = None
# expected.code = defines.Codes.BAD_REQUEST.number
#
# exchange1 = (req, expected)
#
# # version
# req = ("\x40", self.server_address)
#
# expected = Response()
# expected.type = defines.Types["RST"]
# expected._mid = None
# expected.code = defines.Codes.BAD_REQUEST.number
#
# exchange2 = (req, expected)
#
# # code
# req = ("\x40\x05\x8c\xda", self.server_address)
#
# expected = Response()
# expected.type = defines.Types["RST"]
# expected._mid = None
# expected.code = defines.Codes.BAD_REQUEST.number
#
# exchange3 = (req, expected)
#
# # option
# req = ("\x40\x01\x8c\xda\x94", self.server_address)
#
# expected = Response()
# expected.type = defines.Types["RST"]
# expected._mid = None
# expected.code = defines.Codes.BAD_REQUEST.number
#
# exchange4 = (req, expected)
#
# # payload marker
# req = ("\x40\x02\x8c\xda\x75\x62\x61\x73\x69\x63\xff", self.server_address)
#
# expected = Response()
# expected.type = defines.Types["RST"]
# expected._mid = None
# expected.code = defines.Codes.BAD_REQUEST.number
#
# exchange5 = (req, expected)
#
# self._test_datagram([exchange1, exchange2, exchange3, exchange4, exchange5])
def test_invalid(self):
print("TEST_INVALID")
# version
req = (b'\x00\x01\x8c\xda', self.server_address)
expected = Response()
expected.type = defines.Types["RST"]
expected._mid = None
expected.code = defines.Codes.BAD_REQUEST.number
exchange1 = (req, expected)
# version
req = (b'\x40', self.server_address)
expected = Response()
expected.type = defines.Types["RST"]
expected._mid = None
expected.code = defines.Codes.BAD_REQUEST.number
exchange2 = (req, expected)
# code
req = (b'\x40\x05\x8c\xda', self.server_address)
expected = Response()
expected.type = defines.Types["RST"]
expected._mid = None
expected.code = defines.Codes.BAD_REQUEST.number
exchange3 = (req, expected)
# option
req = (b'\x40\x01\x8c\xda\x94', self.server_address)
expected = Response()
expected.type = defines.Types["RST"]
expected._mid = None
expected.code = defines.Codes.BAD_REQUEST.number
exchange4 = (req, expected)
# payload marker
req = (b'\x40\x02\x8c\xda\x75\x62\x61\x73\x69\x63\xff', self.server_address)
expected = Response()
expected.type = defines.Types["RST"]
expected._mid = None
expected.code = defines.Codes.BAD_REQUEST.number
exchange5 = (req, expected)
self._test_datagram([exchange1, exchange2, exchange3, exchange4, exchange5])
def test_post_block_big_client(self):
print("TEST_POST_BLOCK_BIG_CLIENT")
@ -1599,6 +1590,6 @@ class Tests(unittest.TestCase):
self._test_with_client_observe([exchange1, exchange2])
if __name__ == '__main__':
unittest.main()

View file

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

View file

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

View file

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

View file

@ -1,11 +1,13 @@
# -*- coding: utf-8 -*-
from queue import Queue
import random
import threading
import unittest
from coapclient import HelperClient
from coapserver import CoAPServer
from coapthon import defines
from coapthon.messages.message import Message
from coapthon.messages.option import Option
from coapthon.messages.request import Request
from coapthon.messages.response import Response
@ -90,6 +92,7 @@ class Tests(unittest.TestCase):
def test_not_allowed(self):
print("TEST_NOT_ALLOWED")
path = "/void"
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
@ -104,7 +107,6 @@ class Tests(unittest.TestCase):
expected.token = None
exchange1 = (req, expected)
self.current_mid += 1
req = Request()
@ -121,7 +123,6 @@ class Tests(unittest.TestCase):
expected.token = None
exchange2 = (req, expected)
self.current_mid += 1
req = Request()
@ -138,7 +139,6 @@ class Tests(unittest.TestCase):
expected.token = None
exchange3 = (req, expected)
self.current_mid += 1
req = Request()
@ -155,11 +155,10 @@ class Tests(unittest.TestCase):
expected.token = None
exchange4 = (req, expected)
self.current_mid += 1
self._test_with_client([exchange1, exchange2, exchange3, exchange4])
if __name__ == '__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 -*-
from queue import Queue
import random
import socket
import threading
import unittest
from coapthon.messages.message import Message
from coapclient import HelperClient
from coapthon.messages.response import Response
@ -127,6 +129,7 @@ class Tests(unittest.TestCase):
def test_td_coap_link_01(self):
print("TD_COAP_LINK_01")
path = "/.well-known/core"
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
@ -147,6 +150,7 @@ class Tests(unittest.TestCase):
def test_td_coap_link_02(self):
print("TD_COAP_LINK_02")
path = "/.well-known/core"
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
@ -168,6 +172,7 @@ class Tests(unittest.TestCase):
def test_td_coap_core_01(self):
print("TD_COAP_CORE_01")
path = "/test"
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
@ -188,8 +193,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_02(self):
print("TD_COAP_CORE_02")
path = "/test_post"
req = Request()
req = Request()
req.code = defines.Codes.POST.number
req.uri_path = path
req.type = defines.Types["CON"]
@ -204,7 +209,7 @@ class Tests(unittest.TestCase):
expected.code = defines.Codes.CREATED.number
expected.token = None
expected.payload = None
expected.location_path = "/test_post"
expected.location_path = "test_post"
self.current_mid += 1
self._test_with_client([(req, expected)])
@ -212,8 +217,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_03(self):
print("TD_COAP_CORE_03")
path = "/test"
req = Request()
req = Request()
req.code = defines.Codes.PUT.number
req.uri_path = path
req.type = defines.Types["CON"]
@ -233,7 +238,6 @@ class Tests(unittest.TestCase):
exchange1 = (req, expected)
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
@ -251,7 +255,6 @@ class Tests(unittest.TestCase):
exchange2 = (req, expected)
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
@ -274,8 +277,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_04(self):
print("TD_COAP_CORE_04")
path = "/test"
req = Request()
req = Request()
req.code = defines.Codes.DELETE.number
req.uri_path = path
req.type = defines.Types["CON"]
@ -295,8 +298,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_05(self):
print("TD_COAP_CORE_05")
path = "/test"
req = Request()
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["NON"]
@ -316,8 +319,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_06(self):
print("TD_COAP_CORE_06")
path = "/test_post"
req = Request()
req = Request()
req.code = defines.Codes.POST.number
req.uri_path = path
req.type = defines.Types["NON"]
@ -332,7 +335,7 @@ class Tests(unittest.TestCase):
expected.code = defines.Codes.CREATED.number
expected.token = None
expected.payload = None
expected.location_path = "/test_post"
expected.location_path = "test_post"
self.current_mid += 1
self._test_with_client([(req, expected)])
@ -340,8 +343,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_07(self):
print("TD_COAP_CORE_07")
path = "/test"
req = Request()
req = Request()
req.code = defines.Codes.PUT.number
req.uri_path = path
req.type = defines.Types["NON"]
@ -363,8 +366,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_08(self):
print("TD_COAP_CORE_08")
path = "/test"
req = Request()
req = Request()
req.code = defines.Codes.DELETE.number
req.uri_path = path
req.type = defines.Types["NON"]
@ -384,8 +387,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_09(self):
print("TD_COAP_CORE_09")
path = "/separate"
req = Request()
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
@ -412,8 +415,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_10(self):
print("TD_COAP_CORE_10")
path = "/test"
req = Request()
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
@ -435,8 +438,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_12(self):
print("TD_COAP_CORE_12")
path = "/seg1/seg2/seg3"
req = Request()
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
@ -455,8 +458,8 @@ class Tests(unittest.TestCase):
def test_td_coap_core_13(self):
print("TD_COAP_CORE_13")
path = "/query?first=1&second=2&third=3"
req = Request()
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
@ -476,8 +479,8 @@ class Tests(unittest.TestCase):
def test_td_coap_obs_01(self):
print("TD_COAP_OBS_01")
path = "/obs"
req = Request()
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
req.type = defines.Types["CON"]
@ -508,8 +511,8 @@ class Tests(unittest.TestCase):
def test_td_coap_obs_03(self):
print("TD_COAP_OBS_03")
path = "/obs"
req = Request()
req = Request()
req.code = defines.Codes.GET.number
req.uri_path = path
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):
print("TEST_DUPLICATE")
path = "/test"
req = Request()
req.code = defines.Codes.GET.number
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
self.current_mid += 1
self._test_plugtest([(req, expected), (req, expected)])
def test_duplicate_not_completed(self):
print("TEST_DUPLICATE_NOT_COMPLETED")
path = "/long"
req = Request()
req.code = defines.Codes.GET.number
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
self.current_mid += 1
self._test_plugtest([(req, None), (req, expected), (None, expected2)])
def test_no_response(self):
print("TEST_NO_RESPONSE")
path = "/long"
req = Request()
req.code = defines.Codes.GET.number
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
self.current_mid += 1
self._test_plugtest([(req, expected), (None, expected2), (None, expected2), (None, expected2)])
def test_edit_resource(self):
print("TEST_EDIT_RESOURCE")
path = "/obs"
req = Request()
req = Request()
req.code = defines.Codes.POST.number
req.uri_path = path
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.token = None
expected.payload = None
expected.location_path = "/obs"
self.current_mid += 1
self._test_with_client([(req, expected)])
if __name__ == '__main__':
unittest.main()

View file

@ -1,9 +1,9 @@
import getopt
import logging
import sys
from coapthon.server.coap import CoAP
from plugtest_resources import TestResource, SeparateResource, ObservableResource, LargeResource, LargeUpdateResource, \
LongResource
from coapthon.server.coap import CoAP
__author__ = 'Giacomo Tanganelli'
@ -11,33 +11,15 @@ __author__ = 'Giacomo Tanganelli'
class CoAPServerPlugTest(CoAP):
def __init__(self, host, port, multicast=False, starting_mid=None):
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('separate/', SeparateResource())
self.add_resource('seg1/', TestResource())
self.add_resource('seg1/seg2/', TestResource())
self.add_resource('seg1/seg2/seg3/', TestResource())
self.add_resource('query/', TestResource())
self.add_resource("obs/", ObservableResource(coap_server=self))
self.add_resource("large/", LargeResource(coap_server=self))
self.add_resource("large-update/", LargeUpdateResource(coap_server=self))
self.add_resource('obs/', ObservableResource(coap_server=self))
self.add_resource('large/', LargeResource(coap_server=self))
self.add_resource('large-update/', LargeUpdateResource(coap_server=self))
self.add_resource('long/', LongResource())
@ -66,9 +48,7 @@ def main(argv): # pragma: no cover
try:
server.listen(10)
except KeyboardInterrupt:
print("Server Shutdown")
server.close()
print("Exiting...")
if __name__ == "__main__":

View file

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