Skip to content

Commit

Permalink
add unit tests for Message encode and decode
Browse files Browse the repository at this point in the history
  • Loading branch information
mwasilak committed Nov 21, 2013
1 parent 71590f6 commit f118948
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Expand Up @@ -213,3 +213,9 @@ pip-log.txt

#Mr Developer
.mr.developer.cfg


#############
## PyCharm
#############
.idea
5 changes: 4 additions & 1 deletion iot/coap.py
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 31 additions & 1 deletion iot/test/test_coap.py
Expand Up @@ -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):

Expand Down Expand Up @@ -110,4 +140,4 @@ def test_setUriPath(self):
opt3 = coap.Options()
self.assertRaises(ValueError, setattr, opt3, "uri_path", "core")


0 comments on commit f118948

Please sign in to comment.