-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathtest_abi_is_encodable.py
153 lines (138 loc) · 5.45 KB
/
test_abi_is_encodable.py
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
import pytest
from web3._utils.abi import (
is_encodable,
)
@pytest.mark.parametrize(
'value,_type,expected',
(
# Some sanity checks to make sure our custom registrations didn't bork
# anything
('0x' + '00' * 20, 'address', True),
('0x' + '00' * 32, 'address', False), # no oversized addresses
('0xff', 'address', False), # no undersized addresses
(None, 'address', False), # no wrong types
(b'\x01\x23', 'bytes2', True), # valid cases should be same
(b'\x01\x23', 'bytes1', False), # no oversize bytes
(True, 'bytes32', False), # no wrong types
(0, 'bytes32', False), # no wrong types
(b'\x12', 'bytes', True),
('', 'string', True),
('anything', 'string', True),
(['0x' + '00' * 20, 0], '(address,uint256)', True),
(('0x' + '00' * 20, 0), '(address,uint256)', True),
([0], '(address,uint256)', False),
(['0x' + '00' * 20], '(uint256)', False),
# Special address behavior
('dennisthepeasant.eth', 'address', True), # passes because eth_utils converts to bytes :/
('autonomouscollective.eth', 'address', True),
('all-TLDs-valid-now.test', 'address', True),
('-rejects-invalid-names.test', 'address', False),
('ff', 'address', True), # this could theoretically be a top-level domain (TLD)
('0xname.eth', 'address', True), # 0x in name is fine, if it is not a TLD
# Special bytes<M> behavior
('12', 'bytes2', True), # undersize OK
('0x12', 'bytes2', True), # with or without 0x OK
('0123', 'bytes2', True), # exact size OK
(b'\x12', 'bytes2', True), # as bytes value undersize OK
('0123', 'bytes1', False), # no oversize hex strings
('1', 'bytes2', False), # no odd length
('0x1', 'bytes2', False), # no odd length
# Special bytes behavior
('12', 'bytes', True),
('0x12', 'bytes', True),
('1', 'bytes', False),
('0x1', 'bytes', False),
# Special string behavior
(b'', 'string', True),
(b'anything', 'string', True),
(b'\x80', 'string', False), # bytes that cannot be decoded with utf-8 are invalid
# Special tuple behavior
(('0x1234', 0), '(bytes2,int128)', True),
(('0x123456', 0), '(bytes2,int128)', False),
(('0x1234', 0), '(bytes,int128)', True),
(('1', 0), '(bytes,int128)', False),
(('dennisthepeasant.eth', 0), '(address,int128)', True),
(('-rejects-invalid-names.test', 0), '(address,int128)', False),
((b'anything', 0), '(string,int128)', True),
((b'\x80', 0), '(string,int128)', False),
(['0x' + '00' * 20, 0], '(address,uint256)', True),
(('0x' + '00' * 20, 0), '(address,uint256)', True),
([0], '(address,uint256)', False),
(['0x' + '00' * 20], '(uint256)', False),
([], '(address)', False),
((1, (2, 3), 0), '(uint256,(uint256,uint256),uint256)', True),
((0, (2, 3)), '(uint256,(uint256,uint256))', True),
(((2, 3), 0), '((uint256,uint256),uint256)', True),
((((0,),),), '(((uint256)))', True),
([0, 1, 2, 3], 'uint256[]', True),
([(0, 1), (2, 3)], '(uint256,uint256)[]', True),
([(0, 1, 2), (3, 4, 5)], '(uint256,uint256,uint256)[]', True),
(
[0, ['0x' + '00' * 20, '0x' + '00' * 20], ['0x' + '00' * 20, '0x' + '00' * 20], 5],
'(int,(address,address),(address,address),int)',
True,
),
(
(0, ('0x' + '00' * 20, '0x' + '00' * 20), ('0x' + '00' * 20, '0x' + '00' * 20), 5),
'(int,(address,address),(address,address),int)',
True,
),
(
[
['0x' + '00' * 20, '0x' + '00' * 20],
2,
['0x' + '00' * 20, '0x' + '00' * 20],
5,
['0x' + '00' * 20, '0x' + '00' * 20],
],
'((address,address),int,(address,address),int,(address,address))',
True,
),
(
(
('0x' + '00' * 20, '0x' + '00' * 20),
2,
('0x' + '00' * 20, '0x' + '00' * 20),
5,
('0x' + '00' * 20, '0x' + '00' * 20),
),
'((address,address),int,(address,address),int,(address,address))',
True,
),
(
[0, ['0x' + '00' * 20, [1, 2]], 3],
'(int,(address,(int,int)),int)',
True,
),
(
(0, ('0x' + '00' * 20, (1, 2)), 3),
'(int,(address,(int,int)),int)',
True,
),
(
[
[['0x' + '00' * 20], '0x' + '00' * 20],
1,
['0x' + '00' * 20, '0x' + '00' * 20],
2,
[[3, [4, 5]], '0x' + '00' * 20]
],
'(((address),address),int,(address,address),int,((int,(int,int)),address))',
True,
),
(
(
(('0x' + '00' * 20,), '0x' + '00' * 20),
1,
('0x' + '00' * 20, '0x' + '00' * 20),
2,
((3, (4, 5)), '0x' + '00' * 20)
),
'(((address),address),int,(address,address),int,((int,(int,int)),address))',
True,
),
),
)
def test_is_encodable(value, _type, expected):
actual = is_encodable(_type, value)
assert actual is expected