-
Notifications
You must be signed in to change notification settings - Fork 647
Expand file tree
/
Copy pathtest_serialize.py
More file actions
161 lines (133 loc) · 5.79 KB
/
test_serialize.py
File metadata and controls
161 lines (133 loc) · 5.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright (C) 2013-2014 The python-bitcoinlib developers
#
# This file is part of python-bitcoinlib.
#
# It is subject to the license terms in the LICENSE file found in the top-level
# directory of this distribution.
#
# No part of python-bitcoinlib, including this file, may be copied, modified,
# propagated, or distributed except according to the terms contained in the
# LICENSE file.
from __future__ import absolute_import, division, print_function, unicode_literals
import unittest, random
from binascii import unhexlify
from bitcoin.core.serialize import *
class Test_Serializable(unittest.TestCase):
def test_extra_data(self):
"""Serializable.deserialize() fails if extra data is present"""
class FooSerializable(Serializable):
@classmethod
def stream_deserialize(cls, f):
return cls()
def stream_serialize(self, f):
pass
try:
FooSerializable.deserialize(b'\x00')
except DeserializationExtraDataError as err:
self.assertEqual(err.obj, FooSerializable())
self.assertEqual(err.padding, b'\x00')
else:
self.fail("DeserializationExtraDataError not raised")
FooSerializable.deserialize(b'\x00', allow_padding=True)
class Test_VarIntSerializer(unittest.TestCase):
def test(self):
def T(value, expected):
expected = unhexlify(expected)
expected_int = VarIntSerializer.deserialize(expected)
self.assertEqual(value, expected_int)
actual = VarIntSerializer.serialize(value)
self.assertEqual(actual, expected)
roundtrip = VarIntSerializer.deserialize(actual)
self.assertEqual(value, roundtrip)
T(0x0, b'00')
T(0xfc, b'fc')
T(0xfd, b'fdfd00')
T(0xffff, b'fdffff')
T(0x1234, b'fd3412')
T(0x10000, b'fe00000100')
T(0x1234567, b'fe67452301')
T(0x2000000, b'fe00000002')
with self.assertRaises(DeserializationValueBoundsError):
T(0x2000001, b'fe01000002')
with self.assertRaises(DeserializationValueBoundsError):
T(0xffffffff, b'feffffffff')
with self.assertRaises(DeserializationValueBoundsError):
T(0x100000000, b'ff0000000001000000')
with self.assertRaises(DeserializationValueBoundsError):
T(0xffffffffffffffff, b'ffffffffffffffffff')
with self.assertRaises(DeserializationValueBoundsError):
T(0, b'fd0000')
with self.assertRaises(DeserializationValueBoundsError):
T(0, b'fe00000000')
with self.assertRaises(DeserializationValueBoundsError):
T(0, b'ff0000000000000000')
with self.assertRaises(DeserializationValueBoundsError):
T(0x123456789abcdef, b'ffefcdab8967452301')
def test_truncated(self):
def T(serialized):
serialized = unhexlify(serialized)
with self.assertRaises(SerializationTruncationError):
VarIntSerializer.deserialize(serialized)
T(b'')
T(b'fd')
T(b'fd00')
T(b'fe')
T(b'fe00')
T(b'fe0000')
T(b'fe000000')
T(b'ff')
T(b'ff00000000000000')
class Test_BytesSerializer(unittest.TestCase):
def test(self):
def T(value, expected):
value = unhexlify(value)
expected = unhexlify(expected)
actual = BytesSerializer.serialize(value)
self.assertEqual(actual, expected)
roundtrip = BytesSerializer.deserialize(actual)
self.assertEqual(value, roundtrip)
T(b'', b'00')
T(b'00', b'0100')
T(b'00'*0xffff, b'fdffff' + b'00'*0xffff)
def test_truncated(self):
def T(serialized, ex_cls=SerializationTruncationError):
serialized = unhexlify(serialized)
with self.assertRaises(ex_cls):
BytesSerializer.deserialize(serialized)
T(b'')
T(b'01')
T(b'0200')
T(b'ff00000000000000ff11223344', SerializationError) # > max_size
class Test_Compact(unittest.TestCase):
def test_from_compact_zero(self):
self.assertEqual(uint256_from_compact(0x00123456), 0)
self.assertEqual(uint256_from_compact(0x01003456), 0)
self.assertEqual(uint256_from_compact(0x02000056), 0)
self.assertEqual(uint256_from_compact(0x03000000), 0)
self.assertEqual(uint256_from_compact(0x04000000), 0)
self.assertEqual(uint256_from_compact(0x00923456), 0)
def test_from_compact_negative_zero(self):
# Negative bit isn't supported yet
# self.assertEqual(uint256_from_compact(0x01803456), 0)
# self.assertEqual(uint256_from_compact(0x02800056), 0)
# self.assertEqual(uint256_from_compact(0x03800000), 0)
# self.assertEqual(uint256_from_compact(0x04800000), 0)
return
def test_twelve(self):
self.assertEqual(uint256_from_compact(0x01123456), 0x0012)
self.assertEqual(compact_from_uint256(0x0012), 0x01120000)
def test_from_uint256(self):
self.assertEqual(compact_from_uint256(0x1234), 0x02123400)
self.assertEqual(compact_from_uint256(0x123456), 0x03123456)
self.assertEqual(compact_from_uint256(0x12345600), 0x04123456)
self.assertEqual(compact_from_uint256(0x92340000), 0x05009234)
self.assertEqual(compact_from_uint256(0x1234560000000000000000000000000000000000000000000000000000000000), 0x20123456)
class Test_Uint256_Serialize(unittest.TestCase):
def test_fixed(self):
values = []
values.append(0)
values.append(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
for x in range(100):
values.append(random.getrandbits(256))
for n in values:
assert(uint256_from_str(uint256_to_str(n)) == n)