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],
|
|
|
|
unittest2,
|
2021-04-06 11:33:24 +00:00
|
|
|
chronos, stew/byteutils,
|
|
|
|
../../eth/p2p, ../../eth/p2p/rlpx_protocols/[whisper_protocol, eth_protocol],
|
|
|
|
./p2p_test_helper
|
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
|
|
|
|
node1 = setupTestNode(rng, eth, Whisper)
|
|
|
|
node2 = setupTestNode(rng, eth, Whisper)
|
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")
|