2019-02-05 12:01:10 +00:00
|
|
|
import
|
2021-04-06 11:33:24 +00:00
|
|
|
std/[json, unittest],
|
|
|
|
stew/byteutils,
|
|
|
|
../../../eth/rlp
|
2019-02-05 12:01:10 +00:00
|
|
|
|
|
|
|
proc append(output: var RlpWriter, js: JsonNode) =
|
|
|
|
case js.kind
|
|
|
|
of JNull, JFloat, JObject:
|
|
|
|
raise newException(ValueError, "Unsupported JSON value type " & $js.kind)
|
|
|
|
of JBool:
|
|
|
|
output.append js.bval.int
|
|
|
|
of JInt:
|
|
|
|
output.append int(js.num)
|
|
|
|
of JString:
|
|
|
|
output.append js.str
|
|
|
|
of JArray:
|
|
|
|
output.append js.elems
|
|
|
|
|
|
|
|
proc `==`(lhs: JsonNode, rhs: string): bool =
|
|
|
|
lhs.kind == JString and lhs.str == rhs
|
|
|
|
|
|
|
|
proc runTests*(filename: string) =
|
|
|
|
let js = json.parseFile(filename)
|
|
|
|
|
2019-10-16 16:20:11 +00:00
|
|
|
suite filename:
|
|
|
|
for testname, testdata in js:
|
|
|
|
test testname:
|
|
|
|
let
|
|
|
|
input = testdata{"in"}
|
|
|
|
output = testdata{"out"}
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2019-10-16 16:20:11 +00:00
|
|
|
if input.isNil or output.isNil or output.kind != JString:
|
|
|
|
skip()
|
|
|
|
continue
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2019-10-16 16:20:11 +00:00
|
|
|
if input == "VALID":
|
|
|
|
var rlp = rlpFromHex(output.str)
|
|
|
|
discard rlp.inspect
|
|
|
|
elif input == "INVALID":
|
|
|
|
var success = true
|
|
|
|
var inspectOutput = ""
|
|
|
|
expect MalformedRlpError, UnsupportedRlpError, ValueError:
|
|
|
|
var rlp = rlpFromHex(output.str)
|
|
|
|
inspectOutput = rlp.inspect(1)
|
|
|
|
discard rlp.getType
|
|
|
|
while rlp.hasData: discard rlp.toNodes
|
|
|
|
success = false
|
|
|
|
if not success:
|
|
|
|
echo " ACCEPTED MALFORMED BYTES: ", output.str
|
|
|
|
echo " INTERPRETATION:\n", inspectOutput
|
|
|
|
else:
|
|
|
|
if input.kind == JString and input.str.len != 0 and input.str[0] == '#':
|
|
|
|
continue
|
2019-02-05 12:01:10 +00:00
|
|
|
|
2019-10-16 16:20:11 +00:00
|
|
|
var outRlp = initRlpWriter()
|
|
|
|
outRlp.append input
|
|
|
|
let
|
2020-04-20 18:14:39 +00:00
|
|
|
actual = outRlp.finish.toHex
|
2019-10-16 16:20:11 +00:00
|
|
|
expected = output.str
|
|
|
|
check actual == expected
|
2019-02-05 12:01:10 +00:00
|
|
|
|