2024-08-30 17:27:09 +00:00
|
|
|
# nim-eth
|
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at
|
|
|
|
# https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at
|
|
|
|
# https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
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],
|
2023-09-20 07:24:24 +00:00
|
|
|
unittest2,
|
2021-04-06 11:33:24 +00:00
|
|
|
chronos, stew/byteutils,
|
2023-09-20 07:24:24 +00:00
|
|
|
../../eth/[p2p],
|
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()
|
2023-03-16 15:45:12 +00:00
|
|
|
let res = waitFor node1.rlpxConnect(newNode(node2.toENode()))
|
|
|
|
check res.isOk()
|
|
|
|
|
|
|
|
let peer = res.get()
|
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)
|
2024-08-30 17:27:09 +00:00
|
|
|
waitFor peer.invokeThunk(msgId, msgData)
|
2019-10-22 13:14:14 +00:00
|
|
|
|
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")
|