def test_rlp_complex_homo(): my_data = { "foo": 123, "bar": '0x12345678', "baz": [{ "x": '0x11', "y": 1234 }, { "x": '0x12', "y": 5678 }] } my_wrapper = m_rlp.DictWrapper([ ("foo", m_rlp.NumericKind()), ("bar", m_rlp.FixedBlobKind(4)), ("baz", m_rlp.HomoListWrapper(codec=m_rlp.DictWrapper([( "x", m_rlp.BlobKind()), ("y", m_rlp.NumericKind())]))) ]) cc = m_rlp.ComplexCodec(my_wrapper) assert cc.encode(my_data).hex() == 'd17b8412345678cac4118204d2c41282162e' assert cc.decode( bytes.fromhex('d17b8412345678cac4118204d2c41282162e')) == my_data
def test_numericKind_encode(): # Set up a max 8 bytes width NumericKind. kind = m_rlp.NumericKind(8) # Should pass assert kind.serialize('0x0').hex() == '' assert kind.serialize('0x123').hex() == '0123' assert kind.serialize('0').hex() == '' assert kind.serialize('100').hex() == '64' assert kind.serialize(0).hex() == '' assert kind.serialize(0x123).hex() == '0123' # Should Throw with pytest.raises(SerializationError): kind.serialize('0x123z') with pytest.raises(SerializationError): kind.serialize({}) with pytest.raises(SerializationError): kind.serialize('0x') with pytest.raises(SerializationError): kind.serialize(-1) with pytest.raises(SerializationError): kind.serialize('0x12345678123456780')
def test_numericKind_decode(): # Set up a max 8 bytes width NumericKind. kind = m_rlp.NumericKind(8) # Should pass. assert kind.deserialize(bytes(0)) == 0 assert kind.deserialize(bytes([1, 2, 3])) == int('0x010203', 16) assert kind.deserialize(bytes([1, 2, 3, 4, 5, 6, 7, 8])) == int('0x102030405060708', 16) # Should fail. with pytest.raises(DeserializationError): kind.deserialize(bytes([1] * 9)) with pytest.raises(DeserializationError): kind.deserialize(bytes([0, 1, 2]))
def test_rlp_complex_strange(): my_data = { "foo": 123, "bar": '0x12345678', "baz": [ { "x": '0x11', "y": 1234 }, { "x": '0x12', "y": 5678 }, 789, [ 123, { "a": 1 } ] ] } my_wrapper = m_rlp.DictWrapper([ ("foo", m_rlp.NumericKind()), ("bar", m_rlp.FixedBlobKind(4)), ("baz", m_rlp.ListWrapper([ m_rlp.DictWrapper([ ("x", m_rlp.BlobKind()), ("y", m_rlp.NumericKind()) ]), m_rlp.DictWrapper([ ("x", m_rlp.BlobKind()), ("y", m_rlp.NumericKind()) ]), m_rlp.NumericKind(), m_rlp.ListWrapper([ m_rlp.NumericKind(), m_rlp.DictWrapper([ ("a", m_rlp.NumericKind()) ]) ]) ])) ]) cc = m_rlp.ComplexCodec(my_wrapper) my_bytes = cc.encode(my_data) # encode assert cc.decode(my_bytes) == my_data # decode