|
3 | 3 | import string |
4 | 4 |
|
5 | 5 | from eth_utils.crypto import keccak |
| 6 | +import pytest |
6 | 7 |
|
7 | 8 | from eip712_structs import Address, Array, Boolean, Bytes, Int, String, Uint, EIP712Struct, make_domain |
8 | 9 |
|
@@ -158,3 +159,31 @@ class Foo(EIP712Struct): |
158 | 159 | empty_string_hash = keccak(text='') |
159 | 160 | assert encoded_val[0:32] == empty_string_hash |
160 | 161 | assert encoded_val[32:] == bytes(32) |
| 162 | + |
| 163 | + |
| 164 | +def test_validation_errors(): |
| 165 | + bytes_type = Bytes(10) |
| 166 | + int_type = Int(8) # -128 <= i < 128 |
| 167 | + uint_type = Uint(8) # 0 <= i < 256 |
| 168 | + bool_type = Boolean() |
| 169 | + |
| 170 | + with pytest.raises(ValueError, match='bytes10 was given bytes with length 11'): |
| 171 | + bytes_type.encode_value(os.urandom(11)) |
| 172 | + |
| 173 | + with pytest.raises(OverflowError, match='too big'): |
| 174 | + int_type.encode_value(128) |
| 175 | + with pytest.raises(OverflowError, match='too big'): |
| 176 | + int_type.encode_value(-129) |
| 177 | + |
| 178 | + with pytest.raises(OverflowError, match='too big'): |
| 179 | + uint_type.encode_value(256) |
| 180 | + assert uint_type.encode_value(0) == bytes(32) |
| 181 | + with pytest.raises(OverflowError, match='negative int to unsigned'): |
| 182 | + uint_type.encode_value(-1) |
| 183 | + |
| 184 | + assert bool_type.encode_value(True) == bytes(31) + b'\x01' |
| 185 | + assert bool_type.encode_value(False) == bytes(32) |
| 186 | + with pytest.raises(ValueError, match='Must be True or False.'): |
| 187 | + bool_type.encode_value(0) |
| 188 | + with pytest.raises(ValueError, match='Must be True or False.'): |
| 189 | + bool_type.encode_value(1) |
0 commit comments