forked from petertodd/python-bitcoinlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_net.py
More file actions
119 lines (87 loc) · 3.37 KB
/
Copy pathtest_net.py
File metadata and controls
119 lines (87 loc) · 3.37 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
# 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.
import unittest
from bitcoin.net import CAddress, CAlert, CUnsignedAlert, CInv
from io import BytesIO
class TestUnsignedAlert(unittest.TestCase):
def test_serialization(self):
alert = CUnsignedAlert()
alert.setCancel = [1, 2, 3]
alert.strComment = b"Comment"
stream = BytesIO()
alert.stream_serialize(stream)
serialized = BytesIO(stream.getvalue())
deserialized = CUnsignedAlert.stream_deserialize(serialized)
self.assertEqual(deserialized, alert)
class TestCAlert(unittest.TestCase):
def test_serialization(self):
alert = CAlert()
alert.setCancel = [1, 2, 3]
alert.strComment = b"Comment"
stream = BytesIO()
alert.stream_serialize(stream)
serialized = BytesIO(stream.getvalue())
deserialized = CAlert.stream_deserialize(serialized)
self.assertEqual(deserialized, alert)
class TestCInv(unittest.TestCase):
def test_serialization(self):
inv = CInv()
inv.type = 123
inv.hash = b"0" * 32
stream = BytesIO()
inv.stream_serialize(stream)
serialized = BytesIO(stream.getvalue())
deserialized = CInv.stream_deserialize(serialized)
self.assertEqual(deserialized, inv)
class TestCAddress(unittest.TestCase):
def test_serializationSimple(self):
c = CAddress()
cSerialized = c.serialize()
cDeserialized = CAddress.deserialize(cSerialized)
cSerializedTwice = cDeserialized.serialize()
self.assertEqual(cSerialized, cSerializedTwice)
def test_serializationIPv4(self):
c = CAddress()
c.ip = "1.1.1.1"
c.port = 8333
c.nTime = 1420576401
cSerialized = c.serialize()
cDeserialized = CAddress.deserialize(cSerialized)
self.assertEqual(c, cDeserialized)
cSerializedTwice = cDeserialized.serialize()
self.assertEqual(cSerialized, cSerializedTwice)
def test_serializationIPv6(self):
c = CAddress()
c.ip = "1234:ABCD:1234:ABCD:1234:00:ABCD:1234"
c.port = 8333
c.nTime = 1420576401
cSerialized = c.serialize()
cDeserialized = CAddress.deserialize(cSerialized)
self.assertEqual(c, cDeserialized)
cSerializedTwice = cDeserialized.serialize()
self.assertEqual(cSerialized, cSerializedTwice)
def test_serializationDiff(self):
# Sanity check that the serialization code preserves differences
c1 = CAddress()
c1.ip = "1.1.1.1"
c1.port = 8333
c1.nTime = 1420576401
c2 = CAddress()
c2.ip = "1.1.1.2"
c2.port = 8333
c2.nTime = 1420576401
self.assertNotEqual(c1, c2)
c1Serialized = c1.serialize()
c2Serialized = c2.serialize()
self.assertNotEqual(c1Serialized, c2Serialized)
c1Deserialized = CAddress.deserialize(c1Serialized)
c2Deserialized = CAddress.deserialize(c2Serialized)
self.assertNotEqual(c1Deserialized, c2Deserialized)