forked from vyperlang/vyper
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_bytes.py
More file actions
284 lines (220 loc) · 6.39 KB
/
Copy pathtest_bytes.py
File metadata and controls
284 lines (220 loc) · 6.39 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from vyper.exceptions import (
TypeMismatchException,
)
def test_test_bytes(get_contract_with_gas_estimation, assert_tx_failed):
test_bytes = """
@public
def foo(x: bytes[100]) -> bytes[100]:
return x
"""
c = get_contract_with_gas_estimation(test_bytes)
moo_result = c.foo(b'cow')
assert moo_result == b'cow'
print('Passed basic bytes test')
assert c.foo(b'\x35' * 100) == b'\x35' * 100
print('Passed max-length bytes test')
# test for greater than 100 bytes, should raise exception
assert_tx_failed(lambda: c.foo(b'\x35' * 101))
print('Passed input-too-long test')
def test_test_bytes2(get_contract_with_gas_estimation):
test_bytes2 = """
@public
def foo(x: bytes[100]) -> bytes[100]:
y: bytes[100] = x
return y
"""
c = get_contract_with_gas_estimation(test_bytes2)
assert c.foo(b'cow') == b'cow'
assert c.foo(b'') == b''
assert c.foo(b'\x35' * 63) == b'\x35' * 63
assert c.foo(b'\x35' * 64) == b'\x35' * 64
assert c.foo(b'\x35' * 65) == b'\x35' * 65
print('Passed string copying test')
def test_test_bytes3(get_contract_with_gas_estimation):
test_bytes3 = """
x: int128
maa: bytes[60]
y: int128
@public
def __init__():
self.x = 27
self.y = 37
@public
def set_maa(inp: bytes[60]):
self.maa = inp
@public
def set_maa2(inp: bytes[60]):
ay: bytes[60] = inp
self.maa = ay
@public
def get_maa() -> bytes[60]:
return self.maa
@public
def get_maa2() -> bytes[60]:
ay: bytes[60] = self.maa
return ay
@public
def get_xy() -> int128:
return self.x * self.y
"""
c = get_contract_with_gas_estimation(test_bytes3)
c.set_maa(b"pig", transact={})
assert c.get_maa() == b"pig"
assert c.get_maa2() == b"pig"
c.set_maa2(b"", transact={})
assert c.get_maa() == b""
assert c.get_maa2() == b""
c.set_maa(b"\x44" * 60, transact={})
assert c.get_maa() == b"\x44" * 60
assert c.get_maa2() == b"\x44" * 60
c.set_maa2(b"mongoose", transact={})
assert c.get_maa() == b"mongoose"
assert c.get_xy() == 999
print('Passed advanced string copying test')
def test_test_bytes4(get_contract_with_gas_estimation):
test_bytes4 = """
a: bytes[60]
@public
def foo(inp: bytes[60]) -> bytes[60]:
self.a = inp
self.a = ""
return self.a
@public
def bar(inp: bytes[60]) -> bytes[60]:
b: bytes[60] = inp
b = ""
return b
"""
c = get_contract_with_gas_estimation(test_bytes4)
assert c.foo(b"") == b"", c.foo()
assert c.bar(b"") == b""
print('Passed string deleting test')
def test_test_bytes5(get_contract_with_gas_estimation):
test_bytes5 = """
struct G:
a: bytes[50]
b: bytes[50]
struct H:
a: bytes[40]
b: bytes[45]
g: G
@public
def foo(inp1: bytes[40], inp2: bytes[45]):
self.g = G({a: inp1, b: inp2})
@public
def check1() -> bytes[50]:
return self.g.a
@public
def check2() -> bytes[50]:
return self.g.b
@public
def bar(inp1: bytes[40], inp2: bytes[45]) -> bytes[50]:
h: H = H({a: inp1, b: inp2})
return h.a
@public
def bat(inp1: bytes[40], inp2: bytes[45]) -> bytes[50]:
h: H = H({a: inp1, b: inp2})
return h.b
@public
def quz(inp1: bytes[40], inp2: bytes[45]):
h: H = H({a: inp1, b: inp2})
self.g.a = h.a
self.g.b = h.b
"""
c = get_contract_with_gas_estimation(test_bytes5)
c.foo(b"cow", b"horse", transact={})
assert c.check1() == b"cow"
assert c.check2() == b"horse"
assert c.bar(b"pig", b"moose") == b"pig"
assert c.bat(b"pig", b"moose") == b"moose"
c.quz(b"badminton", b"fluffysheep", transact={})
assert c.check1() == b"badminton"
assert c.check2() == b"fluffysheep"
print('Passed string struct test')
def test_binary_literal(get_contract_with_gas_estimation):
bytes_to_num_code = """
r: bytes[1]
@public
def get(a: bytes[1]) -> bytes[2]:
return concat(a, 0b00000001)
@public
def getsome() -> bytes[1]:
return 0b00001110
@public
def testsome(a: bytes[1]) -> bool:
return a == 0b01100001
@public
def testsome_storage(y: bytes[1]) -> bool:
self.r = 0b01100001
return self.r == y
"""
c = get_contract_with_gas_estimation(bytes_to_num_code)
assert c.getsome() == b'\x0e'
assert c.testsome(b'a')
assert c.testsome(b'\x61')
assert c.testsome(0b1100001.to_bytes(1, 'big'))
assert not c.testsome(b'b')
assert c.testsome_storage(b'a')
assert not c.testsome_storage(b'x')
def test_bytes_comparison(get_contract_with_gas_estimation):
code = """
@public
def get_mismatch(a: bytes[1]) -> bool:
b: bytes[2] = 'ab'
return a == b
@public
def get_large(a: bytes[100]) -> bool:
b: bytes[100] = 'ab'
return a == b
"""
c = get_contract_with_gas_estimation(code)
assert c.get_mismatch(b'\x00') is False
assert c.get_large(b'\x00') is False
assert c.get_large(b'ab') is True
def test_bytes32_literals(get_contract):
code = """
@public
def test() -> bool:
l: bytes32 = b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x80\\xac\\x58\\xcd' # noqa: E501
j: bytes32 = 0x0000000000000000000000000000000000000000000000000000000080ac58cd
return l == j
"""
c = get_contract(code)
assert c.test() is True
def test_zero_padding_with_private(get_contract):
code = """
counter: uint256
@private
@constant
def to_little_endian_64(value: uint256) -> bytes[8]:
y: uint256 = 0
x: uint256 = value
for _ in range(8):
y = shift(y, 8)
y = y + bitwise_and(x, 255)
x = shift(x, -8)
return slice(convert(y, bytes32), start=24, len=8)
@public
def set_count(i: uint256):
self.counter = i
@public
@constant
def get_count() -> bytes[24]:
return self.to_little_endian_64(self.counter)
"""
c = get_contract(code)
assert c.get_count() == b'\x00\x00\x00\x00\x00\x00\x00\x00'
c.set_count(1, transact={})
assert c.get_count() == b'\x01\x00\x00\x00\x00\x00\x00\x00'
c.set_count(0xf0f0f0, transact={})
assert c.get_count() == b'\xf0\xf0\xf0\x00\x00\x00\x00\x00'
c.set_count(0x0101010101010101, transact={})
assert c.get_count() == b'\x01\x01\x01\x01\x01\x01\x01\x01'
def test_bytes_to_bytes32_assigment(get_contract, assert_compile_failed):
code = """
@public
def assign():
xs: bytes[32] = b'abcdef'
y: bytes32 = xs
"""
assert_compile_failed(lambda: get_contract(code), TypeMismatchException)