2019-10-22 12:08:25 +00:00
|
|
|
import
|
|
|
|
json, os, stew/byteutils, unittest, chronos,
|
|
|
|
eth/p2p, eth/p2p/rlpx_protocols/[whisper_protocol, eth_protocol],
|
|
|
|
../p2p/p2p_test_helper
|
|
|
|
|
|
|
|
var
|
|
|
|
node1: EthereumNode
|
|
|
|
node2: EthereumNode
|
|
|
|
peer: Peer
|
|
|
|
|
|
|
|
|
|
|
|
node1 = setupTestNode(eth, Whisper)
|
|
|
|
node2 = setupTestNode(eth, Whisper)
|
|
|
|
|
|
|
|
node2.startListening()
|
|
|
|
peer = waitFor node1.rlpxConnect(newNode(initENode(node2.keys.pubKey,
|
|
|
|
node2.address)))
|
|
|
|
|
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()
|
|
|
|
continue
|
|
|
|
|
|
|
|
let payload = hexToSeqByte(payloadHex.str)
|
2019-10-22 13:14:14 +00:00
|
|
|
|
|
|
|
if error.isNil:
|
|
|
|
testThunk(payload)
|
|
|
|
else:
|
|
|
|
if error.kind != JString:
|
|
|
|
skip()
|
|
|
|
continue
|
|
|
|
|
|
|
|
# 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")
|