From f1189485369cba94fc94122be93054e4759fba4f Mon Sep 17 00:00:00 2001 From: Maciej Wasilak Date: Thu, 21 Nov 2013 19:23:54 +0100 Subject: [PATCH] add unit tests for Message encode and decode --- .gitignore | 6 ++++++ iot/coap.py | 5 ++++- iot/test/test_coap.py | 32 +++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index b9d6bd9..53bfa6d 100644 --- a/.gitignore +++ b/.gitignore @@ -213,3 +213,9 @@ pip-log.txt #Mr Developer .mr.developer.cfg + + +############# +## PyCharm +############# +.idea \ No newline at end of file diff --git a/iot/coap.py b/iot/coap.py index c37c6f7..2ed4028 100644 --- a/iot/coap.py +++ b/iot/coap.py @@ -277,7 +277,7 @@ def decode(cls, rawdata, remote=None, protocol=None): (vttkl, code, mid) = struct.unpack('!BBH', rawdata[:4]) version = (vttkl & 0xC0) >> 6 if version is not 1: - raise Exception() + raise ValueError("Fatal Error: Protocol Version must be 1") mtype = (vttkl & 0x30) >> 4 token_length = (vttkl & 0x0F) msg = Message(mtype=mtype, mid=mid, code=code) @@ -289,6 +289,8 @@ def decode(cls, rawdata, remote=None, protocol=None): def encode(self): """Create binary representation of message from Message object.""" + if self.mtype is None or self.mid is None: + raise TypeError("Fatal Error: Message Type and Message ID must not be None.") rawdata = chr((self.version << 6) + ((self.mtype & 0x03) << 4) + (len(self.token) & 0x0F)) rawdata += struct.pack('!BH', self.code, self.mid) rawdata += self.token @@ -1236,6 +1238,7 @@ def sendResponse(self, response, request): #if isResponse(response.code) is False: #raise ValueError("Message code is not valid for a response.") response.token = request.token + print "Token: %s" % (response.token) response.remote = request.remote if request.opt.block1 is not None: response.opt.block1 = request.opt.block1 diff --git a/iot/test/test_coap.py b/iot/test/test_coap.py index dfe9589..a371f4a 100644 --- a/iot/test/test_coap.py +++ b/iot/test/test_coap.py @@ -7,6 +7,36 @@ import iot.coap as coap import struct +class TestMessage(unittest.TestCase): + + def test_encode(self): + msg1 = coap.Message(mtype=coap.CON, mid=0) + binary1 = chr(64)+chr(0)+chr(0)+chr(0) + self.assertEqual(msg1.encode(), binary1, "wrong encode operation for empty CON message") + + msg2 = coap.Message(mtype=coap.ACK, mid=0xBC90, code=coap.CONTENT, payload="temp = 22.5 C", token='q') + msg2.opt.etag = "abcd" + binary2 = chr(97)+chr(69)+chr(188)+chr(144)+chr(113)+chr(68)+"abcd"+chr(255)+"temp = 22.5 C" + self.assertEqual(msg2.encode(), binary2, "wrong encode operation for ACK message with payload, and Etag option") + + msg3 = coap.Message() + self.assertRaises(TypeError, msg3.encode) + + def test_decode(self): + rawdata1 = chr(64)+chr(0)+chr(0)+chr(0) + self.assertEqual(coap.Message.decode(rawdata1).mtype, coap.CON, "wrong message type for decode operation") + self.assertEqual(coap.Message.decode(rawdata1).mid, 0, "wrong message ID for decode operation") + self.assertEqual(coap.Message.decode(rawdata1).code, coap.EMPTY, "wrong message code for decode operation") + self.assertEqual(coap.Message.decode(rawdata1).token, '', "wrong message token for decode operation") + self.assertEqual(coap.Message.decode(rawdata1).payload, '', "wrong message payload for decode operation") + rawdata2 = chr(97)+chr(69)+chr(188)+chr(144)+chr(113)+chr(68)+"abcd"+chr(255)+"temp = 22.5 C" + self.assertEqual(coap.Message.decode(rawdata2).mtype, coap.ACK, "wrong message type for decode operation") + self.assertEqual(coap.Message.decode(rawdata2).mid, 0xBC90, "wrong message ID for decode operation") + self.assertEqual(coap.Message.decode(rawdata2).code, coap.CONTENT, "wrong message code for decode operation") + self.assertEqual(coap.Message.decode(rawdata2).token, 'q', "wrong message token for decode operation") + self.assertEqual(coap.Message.decode(rawdata2).payload, 'temp = 22.5 C', "wrong message payload for decode operation") + self.assertEqual(coap.Message.decode(rawdata2).opt.etags, ["abcd"], "problem with etag option decoding for decode operation") + self.assertEqual(len(coap.Message.decode(rawdata2).opt._options), 1, "wrong number of options after decode operation") class TestReadExtendedFieldValue(unittest.TestCase): @@ -110,4 +140,4 @@ def test_setUriPath(self): opt3 = coap.Options() self.assertRaises(ValueError, setattr, opt3, "uri_path", "core") - \ No newline at end of file + \ No newline at end of file