def setUp(self): defs = [ {'name': 'type', 'struct': binmsg.uchar()}, {'name': 'name', 'struct': binmsg.string()}, {'name': 'age', 'struct': binmsg.uint()}, ] self.binmsg = binmsg.BinMsg(definitions=defs)
def test_char(self): defs = [{'name': 'char', 'struct': binmsg.char()},] b = binmsg.BinMsg(definitions=defs) msg = {'char': b'z'} out = b.pack(msg) self.assertEqual(struct.pack('!Ic', 1, b'z'), out, "Wrong value for packed char") out = b.unpack(out) self.assertEqual(out['char'], 'z', "Wrong value for char")
def test_double(self): defs = [{'name': 'number', 'struct': binmsg.Double()},] b = binmsg.BinMsg(definitions=defs) msg = {'number': 1234.12} out = b.pack(msg) self.assertTrue(struct.unpack('!Id', out)[1] > 1234.11999999 and \ struct.unpack('!Id', out)[1] < 1234.120001, "Wrong value for packed number") out = b.unpack(out) self.assertEqual(out['number'], 1234.12, "Wrong value for number")
def test_unsignedbiginteger(self): defs = [{'name': 'number', 'struct': binmsg.UnsignedBigInteger()},] b = binmsg.BinMsg(definitions=defs) msg = {'number': 123456789} out = b.pack(msg) s = struct.Struct('!q') self.assertEqual(struct.pack('!Iq', s.size, 123456789), out, "Wrong value for packed number") out = b.unpack(out) self.assertEqual(out['number'], 123456789, "Wrong value for number")
def test_invalid_type(self): defs = [{'name': 'number', 'struct': binmsg.uchar()},] b = binmsg.BinMsg(definitions=defs) msg = {'number': 'x'} try: b.pack(msg) except binmsg.CannotPack as e: pass except Exception as e: self.fail("Wrong exception (%s) thrown for invalid type." % e) else: self.fail("Invalid type should raise CannotPack error")
def test_string(self): defs = [{'name': 'string', 'struct': binmsg.string()},] b = binmsg.BinMsg(definitions=defs) out = b.pack({'string': 'test abc 123 ?*= abc abc test test test'}) out = b.unpack(out) self.assertEqual(out['string'], 'test abc 123 ?*= abc abc test test test', "Wrong value for string %s" % out['string']) out = b.pack({'string': 'unicode chars ä í ☃'}) out = b.unpack(out) self.assertEqual(out['string'], 'unicode chars ä í ☃', "Wrong value for string %s" % (out['string'],)) out = b.pack({'string': '☃☃☃☃☃☃'}) out = b.unpack(out) self.assertEqual(out['string'], '☃☃☃☃☃☃', "Wrong value for string %s" % (out['string'],))
def test_contains(self): defs = [{'name': 'type', 'struct': binmsg.uchar()}, {'name': 'number', 'struct': binmsg.Double(), 'condition': binmsg.Contains('nonexist')}, {'name': 'string', 'struct': binmsg.String(), 'condition': binmsg.Contains('type')}, ] b = binmsg.BinMsg(definitions=defs) msg = {'type': 3, 'string': 'asdf'} b.pack(msg) msg = {'type': 3, 'number': 3.4, 'string': 'asdf'} try: b.pack(msg) self.fail("Contains should filter field with false condition") except binmsg.CannotPack: pass
def test_length(self): defs = [{'name': 'age', 'struct': binmsg.uint()}] b = binmsg.BinMsg(definitions=defs) x = struct.pack("!II", 1, 25) try: out = self.binmsg.unpack(x) self.fail("Too short message shouldn't get parsed") except binmsg.CannotUnpack: pass x = struct.pack("!II", 5, 25) try: out = self.binmsg.unpack(x) self.fail("Too long message shouldn't get parsed") except binmsg.CannotUnpack: pass
def test_valueis(self): defs = [{'name': 'type', 'struct': binmsg.uchar()}, {'name': 'number', 'struct': binmsg.Double(), 'condition': binmsg.ValueIs('type', 3)}, {'name': 'string', 'struct': binmsg.String(), 'condition': binmsg.ValueIs('type', 2)}, ] b = binmsg.BinMsg(definitions=defs) msg = {'type': 2, 'string': 'asdf'} b.pack(msg) msg = {'type': 3, 'number': 3.4} b.pack(msg) msg = {'type': 3, 'number': 3.4, 'string': 'asdf'} try: b.pack(msg) self.fail("Contains should filter field with ValueIs condition which expands to False") except binmsg.CannotPack: pass
def test_uchar(self): defs = [{'name': 'char', 'struct': binmsg.UnsignedChar()},] b = binmsg.BinMsg(definitions=defs) msg = {'char': 129} out = b.pack(msg) self.assertEqual(struct.pack('!IB', 1, 129), out, "Wrong value for packed unsigned char") out = b.unpack(out) self.assertEqual(out['char'], 129, "Wrong value for unsigned char") msg = {'char': -1} try: out = b.pack(msg) except binmsg.CannotPack as e: pass except Exception as e: self.fail("Wrong exception (%s) thrown for neagtive value." % e) else: self.fail("Negative value should raise CannotPack error")
def test_integer(self): defs = [{'name': 'integer', 'struct': binmsg.Integer()},] b = binmsg.BinMsg(definitions=defs) msg = {'integer': 12345} out = b.pack(msg) s = struct.Struct('!I') self.assertEqual(struct.pack('!II', s.size, 12345), out, "Wrong value for packed integer") out = b.unpack(out) self.assertEqual(out['integer'], 12345, "Wrong value for unpacked integer") # Test negative value msg = {'integer': -12345} out = b.pack(msg) s = struct.Struct("!I") self.assertEqual(struct.pack('!Ii', s.size, -12345), out, "Wrong value for packed neagtive integer") out = b.unpack(out) self.assertEqual(out['integer'], -12345, "Wrong value for unpacked negative integer")
def test_unsignedinteger(self): defs = [{'name': 'integer', 'struct': binmsg.UnsignedInteger()},] b = binmsg.BinMsg(definitions=defs) msg = {'integer': 12345} out = b.pack(msg) s = struct.Struct('!I') self.assertEqual(struct.pack('!II', s.size,12345), out, "Wrong value for packed integer") out = b.unpack(out) self.assertEqual(out['integer'], 12345, "Wrong value for unpacked integer") msg = {'integer': -12345} try: out = b.pack(msg) except binmsg.CannotPack as e: pass except Exception as e: self.fail("Wrong exception (%s) thrown for neagtive value." % e) else: self.fail("Negative value should raise CannotPack error")