def test_fix_size_0(self): s = BArrayFixSize0() item1 = BItem() item1.foo = 1 item2 = BItem() item2.foo = 2 s.arr = [item1, item2] buf = s.dumps(BinarySerializer()) s2 = BArrayFixSize0() s2.loads(buf, BinarySerializer()) self.assertEqual(s.dumps(), s2.dumps())
def test_size_ref(self): s = BArrayRefSize() s.size = 3 item = s.arr.add() item.foo = 1 item = s.arr.add() item.foo = 2 item = s.arr.add() item.foo = 3 buf = s.dumps(BinarySerializer()) s2 = BArrayRefSize() s2.loads(buf, BinarySerializer()) self.assertEqual(s.dumps(), s2.dumps())
def test_load_byte_sized(self): sized_buff = SizedBuff() sized_buff.code = 10 sized_buff.buff = "test" sized_buff.buff1 = "abcdefg" sized_buff.buff2 = "1234567" data = sized_buff.dumps(BinarySerializer()) loaded_buff = SizedBuff() remain_data = loaded_buff.loads(data, BinarySerializer()) self.assertEqual(remain_data, None) self.assertEqual(loaded_buff.code, 10) self.assertEqual(loaded_buff.buff1_len, 7) self.assertEqual(loaded_buff.buff1, "abcdefg") self.assertEqual(loaded_buff.buff2, "1234567")
class FooRequest(RequestBase): _struct_ = [Field("buffer", Buffer)] _serializer_ = BinarySerializer() response_class = FooResponse def get_sequence_id(self): return None
def test_dumps_and_loads(self): foo = FooMessage() foo.x = 3 foo.s = "xxxx" foo.jce.id = 1 foo.jce.name = "foo" foo.jce.array = [1, 2, 3] foo.jce.mapping = {"a": 1} person = foo.pb.people.add() person.id = 1 person.name = "foo" person.email = "*****@*****.**" phone = person.phones.add() phone.type = person.MOBILE phone.number = "13312345678" foo.fill_size_ref(BinarySerializer()) foo_json = str(foo) foo_json_dict = json.loads(foo_json) self.assertEqual(foo_json_dict["unknown"], str(Uninitialized)) self.assertRaises(ValueError, foo.dumps) foo.unknown = "xxx" foo_dict = foo.dumps() self.assertEqual(foo_dict["x"], 3) self.assertEqual(foo_dict["s_len"], len(foo_dict["s"])) self.assertEqual(foo_dict["s"], foo.s) self.assertEqual(foo_dict["jce_len"], foo.jce_len) self.assertEqual(foo_dict["jce"], foo.jce.dumps()) self.assertEqual(foo_dict["pb_len"], foo.pb_len) self.assertEqual(foo_dict["pb"], foo.pb.dumps()) self.assertTrue("option1" not in foo_dict) self.assertTrue("option2" not in foo_dict) self.assertEqual(foo_dict["unknown"], "xxx")
def on_request(self, req): rsp = HelloResponse() rsp.result = "Hello, %s!" % req.username rsp.seq = req.seq rsp.len = 8 + len(rsp.result) rspbuf = rsp.dumps(BinarySerializer()) sent_size = 0 while sent_size < rsp.len: sent_size += self.connection.send(rspbuf[sent_size:])
def test_dump_without_serializer(self): foo_buff = FooBuff() foo_buff.buf = "xxx" foo_buff.a = 1.1111 self.assertRaises(ValueError, foo_buff.dumps) foo_buff.fill_size_ref(BinarySerializer()) data = foo_buff.dumps() self.assertEqual(data, {"buf" : "xxx", "a" : 1.1111, "buf_len" : 7})
class HelloResponse(ResponseBase): """hello response definition """ _struct_ = [ Field('len', Uint32), Field('seq', Uint32), Field('result', String, byte_size=0) ] _serializer_ = BinarySerializer() _length_field_ = "len" def get_sequence_id(self): return self.seq
class Binary(Message, HttpBody): _struct_ = [ Field("data", Buffer) ] _serializer_ = BinarySerializer() @property def content_type(self): return "" def loads(self, value, confs={}, deserializer=None): return Message.loads(self, value, deserializer=deserializer) def dumps(self, serializer=None): return Message.dumps(self, serializer=serializer)
class HelloRequest(RequestBase): """hello request definition """ _struct_ = [ Field('len', Uint32), Field('seq', Uint32), Field('username', String, byte_size=0) ] _serializer_ = BinarySerializer() _length_field_ = "len" response_class = HelloResponse def get_sequence_id(self): return self.seq def pre_process(self, chan): self.seq = chan.create_seq()
def test_load_size_ref(self): foo_buff = FooBuff() foo_buff.loads(self.data, BinarySerializer()) self.assertEqual(foo_buff.buf_len, 11) self.assertEqual(foo_buff.buf, "abcdefg") self.assertTrue((foo_buff.a - 3.1415926) < 0.00001)
def test_field_size_of_array(self): self.assertEqual(field_size_of(self.foo, "uint32_list", BinarySerializer()), 20) self.assertEqual(field_size_of(self.foo, "string_list", BinarySerializer()), 14)
def test_field_size_of_number(self): self.assertEqual(field_size_of(self.foo, "version", BinarySerializer()), 4) self.assertEqual(field_size_of(self.foo, "timestamp", BinarySerializer()), 4) self.assertEqual(field_size_of(self.foo, "double_data", BinarySerializer()), 8)
def test_field_size_of_string(self): self.foo.dumps(BinarySerializer()) self.assertEqual(field_size_of(self.foo, "cmd", BinarySerializer()), 16) self.assertEqual(field_size_of(self.foo, "username", BinarySerializer()), 4)
def test_field_size_of_map(self): foo_map = FooMap() foo_map.map["xxx"] = 1 self.assertRaises(TypeError, size_of, self.foo, "mapping", BinarySerializer())
def setUpClass(cls): cls.foo_buff = FooBuff() cls.foo_buff.buf = "abcdefg" cls.foo_buff.a = 3.1415926 cls.data = cls.foo_buff.dumps(BinarySerializer())
def test_size_of_nested(self): self.assertEqual(field_size_of(self.foo, "bar.byte_len_string.len", BinarySerializer()), 1) self.assertEqual(field_size_of(self.foo, "bar.byte_len_string.string", BinarySerializer()), 7) self.assertEqual(field_size_of(self.foo, "bar.byte_len_string", BinarySerializer()), 8)
def test_size_offset_of(self): self.assertEqual(offset_of(self.foo, "cmd", BinarySerializer()), 0) self.assertEqual(offset_of(self.foo, "username", BinarySerializer()), 17) self.assertEqual(offset_of(self.foo, "version", BinarySerializer()), 21)