2019-01-06 19:19:48 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018-2019 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2018-07-27 17:02:59 +00:00
|
|
|
# Separated from main tests for brevity
|
|
|
|
|
2021-06-21 09:54:55 +00:00
|
|
|
import unittest2, ../../nimbus/rpc/hexstrings, json
|
2018-07-27 17:02:59 +00:00
|
|
|
|
|
|
|
proc doHexStrTests* =
|
2018-08-06 16:18:56 +00:00
|
|
|
suite "[RPC] Hex quantity":
|
2018-08-06 17:08:50 +00:00
|
|
|
test "Zero values":
|
2018-07-27 17:02:59 +00:00
|
|
|
expect ValueError:
|
|
|
|
let
|
|
|
|
source = ""
|
|
|
|
x = hexQuantityStr source
|
|
|
|
check %x == %source
|
2018-08-06 17:08:50 +00:00
|
|
|
expect ValueError:
|
|
|
|
# must have '0' for zero quantities
|
|
|
|
let
|
|
|
|
source = "0x"
|
|
|
|
x = hexQuantityStr source
|
|
|
|
check %x == %source
|
|
|
|
let
|
|
|
|
source = "0x0"
|
|
|
|
x = hexQuantityStr source
|
|
|
|
check %x == %source
|
2018-07-27 17:02:59 +00:00
|
|
|
test "Even length":
|
|
|
|
let
|
2018-08-06 17:08:50 +00:00
|
|
|
source = "0x1234"
|
2018-07-27 17:02:59 +00:00
|
|
|
x = hexQuantityStr source
|
|
|
|
check %x == %source
|
|
|
|
test "Odd length":
|
|
|
|
let
|
|
|
|
source = "0x123"
|
2018-08-06 17:08:50 +00:00
|
|
|
x = hexQuantityStr source
|
2018-07-27 17:02:59 +00:00
|
|
|
check %x == %source
|
2018-08-06 17:08:50 +00:00
|
|
|
test "\"0x\" header":
|
2018-07-27 17:02:59 +00:00
|
|
|
expect ValueError:
|
|
|
|
let
|
|
|
|
source = "1234"
|
|
|
|
x = hexQuantityStr source
|
|
|
|
check %x != %source
|
|
|
|
expect ValueError:
|
|
|
|
let
|
|
|
|
source = "01234"
|
|
|
|
x = hexQuantityStr source
|
|
|
|
check %x != %source
|
|
|
|
expect ValueError:
|
2018-08-06 17:08:50 +00:00
|
|
|
# leading zeros not allowed
|
2018-07-27 17:02:59 +00:00
|
|
|
let
|
2018-08-06 17:08:50 +00:00
|
|
|
source = "0x0123"
|
2018-07-27 17:02:59 +00:00
|
|
|
x = hexQuantityStr source
|
|
|
|
check %x != %source
|
2021-06-21 09:54:55 +00:00
|
|
|
|
2018-08-06 17:08:50 +00:00
|
|
|
suite "[RPC] Hex data":
|
|
|
|
test "Zero value":
|
|
|
|
expect ValueError:
|
|
|
|
let
|
|
|
|
source = ""
|
|
|
|
x = hexDataStr source
|
|
|
|
check %x != %source
|
|
|
|
expect ValueError:
|
|
|
|
# not even length
|
|
|
|
let
|
|
|
|
source = "0x0"
|
|
|
|
x = hexDataStr source
|
|
|
|
check %x == %source
|
|
|
|
let
|
|
|
|
source = "0x"
|
|
|
|
x = hexDataStr source
|
|
|
|
check %x == %source
|
2018-07-27 17:02:59 +00:00
|
|
|
test "Even length":
|
|
|
|
let
|
|
|
|
source = "0x1234"
|
|
|
|
x = hexDataStr source
|
|
|
|
check %x == %source
|
|
|
|
test "Odd length":
|
|
|
|
expect ValueError:
|
|
|
|
let
|
|
|
|
source = "0x123"
|
|
|
|
x = hexDataStr source
|
|
|
|
check %x != %source
|
2018-08-06 17:08:50 +00:00
|
|
|
test "\"0x\" header":
|
2018-07-27 17:02:59 +00:00
|
|
|
expect ValueError:
|
|
|
|
let
|
|
|
|
source = "1234"
|
|
|
|
x = hexDataStr source
|
|
|
|
check %x != %source
|
|
|
|
expect ValueError:
|
|
|
|
let
|
|
|
|
source = "01234"
|
|
|
|
x = hexDataStr source
|
|
|
|
check %x != %source
|
|
|
|
expect ValueError:
|
|
|
|
let
|
|
|
|
source = "x1234"
|
|
|
|
x = hexDataStr source
|
|
|
|
check %x != %source
|
2018-08-06 17:08:50 +00:00
|
|
|
let
|
|
|
|
# leading zeros allowed
|
|
|
|
source = "0x0123"
|
|
|
|
x = hexDataStr source
|
|
|
|
check %x == %source
|
2018-08-13 16:34:14 +00:00
|
|
|
|
|
|
|
suite "[RPC] Eth address strings":
|
|
|
|
test "Valid address":
|
|
|
|
let
|
|
|
|
e = "0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6"
|
|
|
|
e_addr = e.ethAddressStr
|
|
|
|
check e == e_addr.string
|
|
|
|
let
|
|
|
|
short_e = "0x0f572e5295c57f"
|
|
|
|
short_e_addr = short_e.ethAddressStr
|
|
|
|
check short_e == short_e_addr.string
|
|
|
|
test "Too long":
|
|
|
|
expect ValueError:
|
|
|
|
let
|
2018-08-13 18:03:09 +00:00
|
|
|
e = "0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec667"
|
2018-08-13 16:34:14 +00:00
|
|
|
e_addr = e.ethAddressStr
|
|
|
|
check e == e_addr.string
|
2018-08-13 18:03:09 +00:00
|
|
|
test "\"0x\" header":
|
2018-08-13 16:34:14 +00:00
|
|
|
expect ValueError:
|
|
|
|
let
|
2018-08-13 18:03:09 +00:00
|
|
|
# no 0x
|
2018-08-13 16:34:14 +00:00
|
|
|
e = "000f572e5295c57f15886f9b263e2f6d2d6c7b5ec6"
|
|
|
|
e_addr = e.ethAddressStr
|
|
|
|
check e == e_addr.string
|
2018-08-13 18:03:09 +00:00
|
|
|
|
|
|
|
suite "[RPC] Eth hash strings":
|
|
|
|
test "Valid hash":
|
|
|
|
let
|
|
|
|
e = "0x1234567890123456789012345678901234567890123456789012345678901234"
|
|
|
|
e_addr = e.ethHashStr
|
|
|
|
check e == e_addr.string
|
|
|
|
test "Too short":
|
|
|
|
expect ValueError:
|
|
|
|
let
|
|
|
|
short_e = "0x12345678901234567890123456789012345678901234567890123456789012"
|
|
|
|
short_e_addr = short_e.ethHashStr
|
|
|
|
check short_e == short_e_addr.string
|
|
|
|
test "Too long":
|
|
|
|
expect ValueError:
|
|
|
|
let
|
|
|
|
e = "0x123456789012345678901234567890123456789012345678901234567890123456"
|
|
|
|
e_addr = e.ethHashStr
|
|
|
|
check e == e_addr.string
|
|
|
|
test "\"0x\" header":
|
|
|
|
expect ValueError:
|
|
|
|
let
|
|
|
|
# no 0x
|
|
|
|
e = "000x12345678901234567890123456789012"
|
|
|
|
e_addr = e.ethHashStr
|
|
|
|
check e == e_addr.string
|