def test_bad_enum_hash_same(self) -> None: x = deserialize(File, b'{"name": "something", "type": 64}', Protocol.JSON) y = deserialize(File, b'{"name": "something", "type": 64}', Protocol.JSON) self.assertEqual(hash(x), hash(y)) self.assertEqual(hash(x.type), hash(y.type)) self.assertFalse(x.type is y.type) self.assertEqual(x.type, y.type) self.assertFalse(x.type != y.type)
def test_string_with_non_utf8_data(self) -> None: encoded = b"\x0b\x00\x01\x00\x00\x00\x03foo\x00" sb = deserialize(StringBucket, encoded, protocol=Protocol.BINARY) self.assertEqual("foo", sb.one) encoded = b"\x0b\x00\x01\x00\x00\x00\x03\xfa\xf0\xef\x00" sb = deserialize(StringBucket, encoded, protocol=Protocol.BINARY) with self.assertRaises(UnicodeDecodeError): # Accessing the property is when the string is decoded as UTF-8. sb.one
def test_sanity(self) -> None: with self.assertRaises(TypeError): serialize(1, Protocol.COMPACT) # type: ignore with self.assertRaises(TypeError): serialize(easy(), None) # type: ignore with self.assertRaises(TypeError): deserialize(Protocol, b"") # type: ignore with self.assertRaises(TypeError): deserialize(easy, Protocol) # type: ignore
def test_isset_Struct(self) -> None: serialized = b'{"name":"/dev/null","type":8}' file = deserialize(File, serialized, protocol=Protocol.JSON) self.assertTrue(Struct.isset(file).type) self.assertFalse(Struct.isset(file).permissions) # required fields are always set self.assertTrue(Struct.isset(file).name) serialized = b'{"name":"/dev/null"}' file = deserialize(File, serialized, protocol=Protocol.JSON) self.assertEqual(file.type, Kind.REGULAR) self.assertFalse(Struct.isset(file).type)
def test_bad_enum_in_list_index(self) -> None: x = deserialize(ColorGroups, b'{"color_list": [1, 5, 0]}', Protocol.JSON) self.assertEqual(len(x.color_list), 3) self.assertEqual(x.color_list[0], Color.blue) self.assertBadEnum(cast(BadEnum, x.color_list[1]), Color, 5) self.assertEqual(x.color_list[2], Color.red)
def test_flag_enum_serialization_roundtrip(self) -> None: x = File(name="/dev/null", type=Kind.CHAR, permissions=Perm.read | Perm.write) y = deserialize(File, serialize(x)) self.assertEqual(x, y) self.assertEqual(x.permissions, Perm.read | Perm.write) self.assertIsInstance(x.permissions, Perm)
def thrift_serialization_round_robin( self, control: Struct, fixtures: Mapping[Protocol, bytes]) -> None: for proto in Protocol: encoded = serialize(control, protocol=proto) self.assertIsInstance(encoded, bytes) decoded = deserialize(type(control), encoded, protocol=proto) self.assertIsInstance(decoded, type(control)) self.assertEqual(control, decoded) self.assertEqual((proto, encoded), (proto, fixtures.get(proto)))
def test_sanity(self) -> None: with self.assertRaises(TypeError): # pyre-fixme[6]: Expected `sT` for 1st param but got `int`. serialize(1, Protocol.COMPACT) with self.assertRaises(TypeError): # pyre-fixme[6]: Expected `Protocol` for 2nd param but got `None`. serialize(easy(), None) with self.assertRaises(TypeError): # pyre-fixme[6]: Expected `Type[Variable[thrift.py3.serializer.sT (bound # to Struct)]]` for 1st param but got `Type[Protocol]`. deserialize(Protocol, b"") with self.assertRaises(TypeError): # pyre-fixme[6]: Expected `Union[bytearray, bytes, folly.iobuf.IOBuf, # memoryview]` for 2nd param but got `Type[Protocol]`. deserialize(easy, Protocol)
def test_bad_enum_in_list_reverse(self) -> None: x = deserialize(ColorGroups, b'{"color_list": [1, 5, 0]}', Protocol.JSON) for idx, v in enumerate(reversed(x.color_list)): if idx == 0: self.assertEqual(v, Color.red) elif idx == 1: self.assertBadEnum(cast(BadEnum, v), Color, 5) else: self.assertEqual(v, Color.blue)
def test_isset_Struct(self) -> None: serialized = b'{"name":"/dev/null","type":8}' file = deserialize(File, serialized, protocol=Protocol.JSON) # pyre-fixme[6]: Expected `HasIsSet[Variable[thrift.py3.types._T]]` for 1st # param but got `File`. self.assertTrue(Struct.isset(file).type) # pyre-fixme[6]: Expected `HasIsSet[Variable[thrift.py3.types._T]]` for 1st # param but got `File`. self.assertFalse(Struct.isset(file).permissions) # required fields are always set # pyre-fixme[6]: Expected `HasIsSet[Variable[thrift.py3.types._T]]` for 1st # param but got `File`. self.assertTrue(Struct.isset(file).name) serialized = b'{"name":"/dev/null"}' file = deserialize(File, serialized, protocol=Protocol.JSON) self.assertEqual(file.type, Kind.REGULAR) # pyre-fixme[6]: Expected `HasIsSet[Variable[thrift.py3.types._T]]` for 1st # param but got `File`. self.assertFalse(Struct.isset(file).type)
def test_isset_repr(self) -> None: serialized = b'{"name":"/dev/null","type":8}' file = deserialize(File, serialized, protocol=Protocol.JSON) self.assertEqual( "Struct.isset(<File>, name=True, permissions=False, type=True)", repr(Struct.isset(file)), ) self.assertEqual( "Struct.isset(<File>, name=True, permissions=False, type=True)", str(Struct.isset(file)), )
def test_bad_enum_in_map_values(self) -> None: json = b'{"color_map": {"1": 2, "0": 5, "6": 1, "7": 8}}' x = deserialize(ColorGroups, json, Protocol.JSON) s = set() for k in x.color_map.values(): s.add(k) self.assertEqual(len(s), 4) s.discard(Color.green) s.discard(Color.blue) lst = sorted(s, key=lambda e: cast(BadEnum, e).value) self.assertBadEnum(cast(BadEnum, lst[0]), Color, 5) self.assertBadEnum(cast(BadEnum, lst[1]), Color, 8)
def test_bad_enum_in_map_items(self) -> None: json = b'{"color_map": {"1": 2, "0": 5, "6": 1, "7": 8}}' x = deserialize(ColorGroups, json, Protocol.JSON) for k, v in x.color_map.items(): if k == Color.blue: self.assertEqual(v, Color.green) elif k == Color.red: self.assertBadEnum(cast(BadEnum, v), Color, 5) else: ck = cast(BadEnum, k) if ck.value == 6: self.assertEqual(v, Color.blue) else: self.assertBadEnum(cast(BadEnum, v), Color, 8)
def test_bad_deserialize(self) -> None: with self.assertRaises(Error): deserialize(easy, b"", protocol=Protocol.JSON) with self.assertRaises(Error): deserialize(easy, b"\x05AAAAAAAA") with self.assertRaises(Error): deserialize(easy, b"\x02\xDE\xAD\xBE\xEF", protocol=Protocol.BINARY)
def test_isset_repr(self) -> None: serialized = b'{"name":"/dev/null","type":8}' file = deserialize(File, serialized, protocol=Protocol.JSON) self.assertEqual( "Struct.isset(<File>, name=True, permissions=False, type=True)", # pyre-fixme[6]: Expected `HasIsSet[Variable[thrift.py3.types._T]]` for # 1st param but got `File`. repr(Struct.isset(file)), ) self.assertEqual( "Struct.isset(<File>, name=True, permissions=False, type=True)", # pyre-fixme[6]: Expected `HasIsSet[Variable[thrift.py3.types._T]]` for # 1st param but got `File`. str(Struct.isset(file)), )
def test_bad_deserialize(self) -> None: with self.assertRaises(Error): deserialize(easy, b"", protocol=Protocol.JSON) with self.assertRaises(Error): deserialize(easy, b"\x05AAAAAAAA") with self.assertRaises(Error): deserialize(easy, b"\x02\xDE\xAD\xBE\xEF", protocol=Protocol.BINARY) with self.assertRaises(BufferError): deserialize_from_header(easy, b"\x02\xDE\xAD\xBE\xEF") with self.assertRaises(Error): control = easy(val=5, val_list=[4, 3, 2, 1]) buf = serialize_with_header(control, transform=Transform.ZSTD_TRANSFORM) newBytes = bytearray(buf) newBytes[4] += 1 deserialize_from_header(easy, bytes(newBytes))
def test_deserialize_empty(self) -> None: x = deserialize(Integers, b"{}", Protocol.JSON) self.assertEqual(x.type, Integers.Type.EMPTY)
def test_serialize_deserialize(self) -> None: err = HardError(errortext="err", code=2) serialized = serialize_iobuf(err) deserialized = deserialize(HardError, serialized) self.assertIsNot(err, deserialized) self.assertEqual(err, deserialized)
def test_serialize_iobuf(self) -> None: control = easy(val=5, val_list=[1, 2, 3, 4, 5]) iobuf = serialize_iobuf(control) decoded = deserialize(type(control), iobuf) self.assertEqual(control, decoded)
def test_bad_enum_in_set_iter(self) -> None: x = deserialize(ColorGroups, b'{"color_set": [1, 5, 0]}', Protocol.JSON) for v in x.color_set: if v not in (Color.blue, Color.red): self.assertBadEnum(cast(BadEnum, v), Color, 5)
def test_bad_enum_in_struct(self) -> None: x = deserialize(File, b'{"name": "something", "type": 64}', Protocol.JSON) self.assertBadEnum(cast(BadEnum, x.type), Kind, 64)
def test_enum_value_rename(self) -> None: """The value name is None but we auto rename it to None_""" x = deserialize(File, b'{"name":"blah", "type":0}', Protocol.JSON) self.assertEqual(x.type, Kind.None_)
def test_bad_enum_in_map_lookup(self) -> None: json = b'{"color_map": {"1": 2, "0": 5, "6": 1, "7": 8}}' x = deserialize(ColorGroups, json, Protocol.JSON) val = x.color_map[Color.red] self.assertBadEnum(cast(BadEnum, val), Color, 5)