2021-05-06 15:20:54 +00:00
|
|
|
{.used.}
|
|
|
|
|
2019-10-22 12:08:25 +00:00
|
|
|
import
|
2021-12-11 18:12:55 +00:00
|
|
|
std/[json, os],
|
2022-10-11 03:02:16 +00:00
|
|
|
unittest2, stint,
|
2021-04-06 11:33:24 +00:00
|
|
|
chronos, stew/byteutils,
|
2022-10-11 03:02:16 +00:00
|
|
|
../../eth/[p2p, common],
|
2022-11-10 08:01:58 +00:00
|
|
|
../stubloglevel,
|
2022-10-11 03:02:16 +00:00
|
|
|
./p2p_test_helper,
|
|
|
|
./eth_protocol
|
2019-10-22 12:08:25 +00:00
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
let rng = newRng()
|
2019-10-22 12:08:25 +00:00
|
|
|
|
2020-07-07 08:56:26 +00:00
|
|
|
var
|
2022-06-20 16:10:50 +00:00
|
|
|
node1 = setupTestNode(rng, eth)
|
|
|
|
node2 = setupTestNode(rng, eth)
|
2019-10-22 12:08:25 +00:00
|
|
|
|
|
|
|
node2.startListening()
|
2020-07-07 08:56:26 +00:00
|
|
|
var peer = waitFor node1.rlpxConnect(newNode(node2.toENode()))
|
2019-10-22 12:08:25 +00:00
|
|
|
|
2019-10-22 13:14:14 +00:00
|
|
|
proc testThunk(payload: openArray[byte]) =
|
|
|
|
var (msgId, msgData) = recvMsgMock(payload)
|
|
|
|
waitFor peer.invokeThunk(msgId.int, msgData)
|
|
|
|
|
2019-10-22 12:08:25 +00:00
|
|
|
proc testPayloads(filename: string) =
|
|
|
|
let js = json.parseFile(filename)
|
|
|
|
|
|
|
|
suite extractFilename(filename):
|
|
|
|
for testname, testdata in js:
|
|
|
|
test testname:
|
|
|
|
let
|
|
|
|
payloadHex = testdata{"payload"}
|
|
|
|
error = testdata{"error"}
|
|
|
|
|
|
|
|
if payloadHex.isNil or payloadHex.kind != JString:
|
|
|
|
skip()
|
2021-12-11 18:12:55 +00:00
|
|
|
return
|
2019-10-22 12:08:25 +00:00
|
|
|
|
|
|
|
let payload = hexToSeqByte(payloadHex.str)
|
2019-10-22 13:14:14 +00:00
|
|
|
|
|
|
|
if error.isNil:
|
|
|
|
testThunk(payload)
|
|
|
|
else:
|
|
|
|
if error.kind != JString:
|
|
|
|
skip()
|
2021-12-11 18:12:55 +00:00
|
|
|
return
|
2019-10-22 13:14:14 +00:00
|
|
|
|
|
|
|
# TODO: can I convert the error string to an Exception type at runtime?
|
|
|
|
expect CatchableError:
|
|
|
|
try:
|
|
|
|
testThunk(payload)
|
|
|
|
except CatchableError as e:
|
|
|
|
check: e.name == error.str
|
2019-12-04 11:34:37 +00:00
|
|
|
raise e
|
2019-10-22 12:08:25 +00:00
|
|
|
|
|
|
|
testPayloads(sourceDir / "test_rlpx_thunk.json")
|