2019-03-23 20:54:28 +00:00
|
|
|
import
|
2019-07-07 10:12:01 +00:00
|
|
|
unittest, strformat, options, stew/byteutils, json_rpc/[rpcserver, rpcclient],
|
2020-01-20 16:45:49 +00:00
|
|
|
eth/common as eth_common, eth/[rlp, keys],
|
|
|
|
eth/p2p/rlpx_protocols/whisper_protocol,
|
|
|
|
../nimbus/rpc/[common, hexstrings, rpc_types, whisper, key_storage],
|
|
|
|
./test_helpers
|
2019-03-23 20:54:28 +00:00
|
|
|
|
|
|
|
from os import DirSep
|
|
|
|
from strutils import rsplit
|
|
|
|
template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]
|
|
|
|
|
|
|
|
## Generate client convenience marshalling wrappers from forward declarations
|
|
|
|
## For testing, ethcallsigs needs to be kept in sync with ../nimbus/rpc/[common, p2p]
|
|
|
|
const sigPath = &"{sourceDir}{DirSep}rpcclient{DirSep}ethcallsigs.nim"
|
|
|
|
createRpcSigs(RpcSocketClient, sigPath)
|
|
|
|
|
2019-05-16 20:20:17 +00:00
|
|
|
proc doTests {.async.} =
|
2019-04-24 20:45:29 +00:00
|
|
|
var ethNode = setupEthNode(Whisper)
|
2019-03-23 20:54:28 +00:00
|
|
|
|
|
|
|
# Create Ethereum RPCs
|
|
|
|
let RPC_PORT = 8545
|
|
|
|
var
|
|
|
|
rpcServer = newRpcSocketServer(["localhost:" & $RPC_PORT])
|
|
|
|
client = newRpcSocketClient()
|
2020-01-20 16:45:49 +00:00
|
|
|
let keys = newKeyStorage()
|
2019-03-23 20:54:28 +00:00
|
|
|
setupWhisperRPC(ethNode, keys, rpcServer)
|
|
|
|
|
|
|
|
# Begin tests
|
|
|
|
rpcServer.start()
|
2019-05-16 20:20:17 +00:00
|
|
|
await client.connect("localhost", Port(RPC_PORT))
|
2019-03-23 20:54:28 +00:00
|
|
|
|
|
|
|
suite "Whisper Remote Procedure Calls":
|
|
|
|
test "shh_version":
|
2019-05-16 20:20:17 +00:00
|
|
|
check await(client.shh_version()) == whisperVersionStr
|
2019-03-23 20:54:28 +00:00
|
|
|
test "shh_info":
|
2019-05-16 20:20:17 +00:00
|
|
|
let info = await client.shh_info()
|
2019-03-23 20:54:28 +00:00
|
|
|
check info.maxMessageSize == defaultMaxMsgSize
|
|
|
|
test "shh_setMaxMessageSize":
|
|
|
|
let testValue = 1024'u64
|
2019-05-16 20:20:17 +00:00
|
|
|
check await(client.shh_setMaxMessageSize(testValue)) == true
|
|
|
|
var info = await client.shh_info()
|
2019-03-23 20:54:28 +00:00
|
|
|
check info.maxMessageSize == testValue
|
2019-05-16 20:20:17 +00:00
|
|
|
expect ValueError:
|
|
|
|
discard await(client.shh_setMaxMessageSize(defaultMaxMsgSize + 1))
|
|
|
|
info = await client.shh_info()
|
2019-03-23 20:54:28 +00:00
|
|
|
check info.maxMessageSize == testValue
|
|
|
|
test "shh_setMinPoW":
|
|
|
|
let testValue = 0.0001
|
2019-05-16 20:20:17 +00:00
|
|
|
check await(client.shh_setMinPoW(testValue)) == true
|
|
|
|
let info = await client.shh_info()
|
2019-03-23 20:54:28 +00:00
|
|
|
check info.minPow == testValue
|
|
|
|
# test "shh_markTrustedPeer":
|
|
|
|
# TODO: need to connect a peer to test
|
|
|
|
test "shh asymKey tests":
|
2019-05-16 20:20:17 +00:00
|
|
|
let keyID = await client.shh_newKeyPair()
|
2019-03-23 20:54:28 +00:00
|
|
|
check:
|
2019-05-16 20:20:17 +00:00
|
|
|
await(client.shh_hasKeyPair(keyID)) == true
|
|
|
|
await(client.shh_deleteKeyPair(keyID)) == true
|
|
|
|
await(client.shh_hasKeyPair(keyID)) == false
|
|
|
|
expect ValueError:
|
|
|
|
discard await(client.shh_deleteKeyPair(keyID))
|
2019-03-23 20:54:28 +00:00
|
|
|
|
|
|
|
let privkey = "0x5dc5381cae54ba3174dc0d46040fe11614d0cc94d41185922585198b4fcef9d3"
|
|
|
|
let pubkey = "0x04e5fd642a0f630bbb1e4cd7df629d7b8b019457a9a74f983c0484a045cebb176def86a54185b50bbba6bbf97779173695e92835d63109c23471e6da382f922fdb"
|
2019-05-16 20:20:17 +00:00
|
|
|
let keyID2 = await client.shh_addPrivateKey(privkey)
|
2019-03-23 20:54:28 +00:00
|
|
|
check:
|
2019-05-16 20:20:17 +00:00
|
|
|
await(client.shh_getPublicKey(keyID2)) == pubkey.toPublicKey
|
|
|
|
await(client.shh_getPrivateKey(keyID2)) == privkey.toPrivateKey
|
|
|
|
await(client.shh_hasKeyPair(keyID2)) == true
|
|
|
|
await(client.shh_deleteKeyPair(keyID2)) == true
|
|
|
|
await(client.shh_hasKeyPair(keyID2)) == false
|
|
|
|
expect ValueError:
|
|
|
|
discard await(client.shh_deleteKeyPair(keyID2))
|
2019-03-23 20:54:28 +00:00
|
|
|
test "shh symKey tests":
|
2019-05-16 20:20:17 +00:00
|
|
|
let keyID = await client.shh_newSymKey()
|
2019-03-23 20:54:28 +00:00
|
|
|
check:
|
2019-05-16 20:20:17 +00:00
|
|
|
await(client.shh_hasSymKey(keyID)) == true
|
|
|
|
await(client.shh_deleteSymKey(keyID)) == true
|
|
|
|
await(client.shh_hasSymKey(keyID)) == false
|
|
|
|
expect ValueError:
|
|
|
|
discard await(client.shh_deleteSymKey(keyID))
|
2019-03-23 20:54:28 +00:00
|
|
|
|
|
|
|
let symKey = "0x0000000000000000000000000000000000000000000000000000000000000001"
|
2019-05-16 20:20:17 +00:00
|
|
|
let keyID2 = await client.shh_addSymKey(symKey)
|
2019-03-23 20:54:28 +00:00
|
|
|
check:
|
2019-05-16 20:20:17 +00:00
|
|
|
await(client.shh_getSymKey(keyID2)) == symKey.toSymKey
|
|
|
|
await(client.shh_hasSymKey(keyID2)) == true
|
|
|
|
await(client.shh_deleteSymKey(keyID2)) == true
|
|
|
|
await(client.shh_hasSymKey(keyID2)) == false
|
|
|
|
expect ValueError:
|
|
|
|
discard await(client.shh_deleteSymKey(keyID2))
|
|
|
|
|
|
|
|
let keyID3 = await client.shh_generateSymKeyFromPassword("password")
|
|
|
|
let keyID4 = await client.shh_generateSymKeyFromPassword("password")
|
|
|
|
let keyID5 = await client.shh_generateSymKeyFromPassword("nimbus!")
|
2019-04-05 14:36:52 +00:00
|
|
|
check:
|
2019-05-16 20:20:17 +00:00
|
|
|
await(client.shh_getSymKey(keyID3)) ==
|
|
|
|
await(client.shh_getSymKey(keyID4))
|
|
|
|
await(client.shh_getSymKey(keyID3)) !=
|
|
|
|
await(client.shh_getSymKey(keyID5))
|
|
|
|
await(client.shh_hasSymKey(keyID3)) == true
|
|
|
|
await(client.shh_deleteSymKey(keyID3)) == true
|
|
|
|
await(client.shh_hasSymKey(keyID3)) == false
|
|
|
|
expect ValueError:
|
|
|
|
discard await(client.shh_deleteSymKey(keyID3))
|
2019-04-05 14:36:52 +00:00
|
|
|
|
2019-04-08 15:55:50 +00:00
|
|
|
# Some defaults for the filter & post tests
|
|
|
|
let
|
|
|
|
ttl = 30'u64
|
2019-04-23 12:45:37 +00:00
|
|
|
topicStr = "0x12345678"
|
2019-04-08 15:55:50 +00:00
|
|
|
payload = "0x45879632"
|
|
|
|
# A very low target and long time so we are sure the test never fails
|
|
|
|
# because of this
|
|
|
|
powTarget = 0.001
|
|
|
|
powTime = 1.0
|
|
|
|
|
2019-04-24 20:45:29 +00:00
|
|
|
test "shh filter create and delete":
|
|
|
|
let
|
|
|
|
topic = topicStr.toTopic()
|
2019-05-16 20:20:17 +00:00
|
|
|
symKeyID = await client.shh_newSymKey()
|
2019-04-24 20:45:29 +00:00
|
|
|
options = WhisperFilterOptions(symKeyID: some(symKeyID),
|
|
|
|
topics: some(@[topic]))
|
2019-05-16 20:20:17 +00:00
|
|
|
filterID = await client.shh_newMessageFilter(options)
|
2019-04-24 20:45:29 +00:00
|
|
|
|
|
|
|
check:
|
|
|
|
filterID.string.isValidIdentifier
|
2019-05-16 20:20:17 +00:00
|
|
|
await(client.shh_deleteMessageFilter(filterID)) == true
|
|
|
|
expect ValueError:
|
|
|
|
discard await(client.shh_deleteMessageFilter(filterID))
|
2019-04-24 20:45:29 +00:00
|
|
|
|
2019-04-08 15:55:50 +00:00
|
|
|
test "shh symKey post and filter loop":
|
|
|
|
let
|
2019-04-23 12:45:37 +00:00
|
|
|
topic = topicStr.toTopic()
|
2019-05-16 20:20:17 +00:00
|
|
|
symKeyID = await client.shh_newSymKey()
|
2019-04-08 15:55:50 +00:00
|
|
|
options = WhisperFilterOptions(symKeyID: some(symKeyID),
|
2019-04-23 12:45:37 +00:00
|
|
|
topics: some(@[topic]))
|
2019-05-16 20:20:17 +00:00
|
|
|
filterID = await client.shh_newMessageFilter(options)
|
2019-04-08 15:55:50 +00:00
|
|
|
message = WhisperPostMessage(symKeyID: some(symKeyID),
|
|
|
|
ttl: ttl,
|
2019-04-23 12:45:37 +00:00
|
|
|
topic: some(topic),
|
2019-04-08 15:55:50 +00:00
|
|
|
payload: payload.HexDataStr,
|
|
|
|
powTime: powTime,
|
|
|
|
powTarget: powTarget)
|
2019-03-23 20:54:28 +00:00
|
|
|
check:
|
2019-05-16 20:20:17 +00:00
|
|
|
await(client.shh_setMinPoW(powTarget)) == true
|
|
|
|
await(client.shh_post(message)) == true
|
2019-03-23 20:54:28 +00:00
|
|
|
|
2019-05-16 20:20:17 +00:00
|
|
|
let messages = await client.shh_getFilterMessages(filterID)
|
2019-04-08 15:55:50 +00:00
|
|
|
check:
|
|
|
|
messages.len == 1
|
|
|
|
messages[0].sig.isNone()
|
|
|
|
messages[0].recipientPublicKey.isNone()
|
|
|
|
messages[0].ttl == ttl
|
2019-04-23 12:45:37 +00:00
|
|
|
messages[0].topic == topic
|
|
|
|
messages[0].payload == hexToSeqByte(payload)
|
2019-04-08 15:55:50 +00:00
|
|
|
messages[0].padding.len > 0
|
|
|
|
messages[0].pow >= powTarget
|
|
|
|
|
2019-05-16 20:20:17 +00:00
|
|
|
await(client.shh_deleteMessageFilter(filterID)) == true
|
2019-04-24 20:45:29 +00:00
|
|
|
|
2019-04-08 15:55:50 +00:00
|
|
|
test "shh asymKey post and filter loop":
|
|
|
|
let
|
2019-04-23 12:45:37 +00:00
|
|
|
topic = topicStr.toTopic()
|
2019-05-16 20:20:17 +00:00
|
|
|
privateKeyID = await client.shh_newKeyPair()
|
2019-04-08 15:55:50 +00:00
|
|
|
options = WhisperFilterOptions(privateKeyID: some(privateKeyID))
|
2019-05-16 20:20:17 +00:00
|
|
|
filterID = await client.shh_newMessageFilter(options)
|
|
|
|
pubKey = await client.shh_getPublicKey(privateKeyID)
|
2019-04-08 15:55:50 +00:00
|
|
|
message = WhisperPostMessage(pubKey: some(pubKey),
|
|
|
|
ttl: ttl,
|
2019-04-23 12:45:37 +00:00
|
|
|
topic: some(topic),
|
2019-04-08 15:55:50 +00:00
|
|
|
payload: payload.HexDataStr,
|
|
|
|
powTime: powTime,
|
|
|
|
powTarget: powTarget)
|
|
|
|
check:
|
2019-05-16 20:20:17 +00:00
|
|
|
await(client.shh_setMinPoW(powTarget)) == true
|
|
|
|
await(client.shh_post(message)) == true
|
2019-04-08 15:55:50 +00:00
|
|
|
|
2019-05-16 20:20:17 +00:00
|
|
|
let messages = await client.shh_getFilterMessages(filterID)
|
2019-04-08 15:55:50 +00:00
|
|
|
check:
|
|
|
|
messages.len == 1
|
|
|
|
messages[0].sig.isNone()
|
2019-04-23 12:45:37 +00:00
|
|
|
messages[0].recipientPublicKey.get() == pubKey
|
2019-04-08 15:55:50 +00:00
|
|
|
messages[0].ttl == ttl
|
2019-04-23 12:45:37 +00:00
|
|
|
messages[0].topic == topic
|
|
|
|
messages[0].payload == hexToSeqByte(payload)
|
2019-04-08 15:55:50 +00:00
|
|
|
messages[0].padding.len > 0
|
|
|
|
messages[0].pow >= powTarget
|
|
|
|
|
2019-05-16 20:20:17 +00:00
|
|
|
await(client.shh_deleteMessageFilter(filterID)) == true
|
2019-04-24 20:45:29 +00:00
|
|
|
|
2019-04-08 15:55:50 +00:00
|
|
|
test "shh signature in post and filter loop":
|
|
|
|
let
|
2019-04-23 12:45:37 +00:00
|
|
|
topic = topicStr.toTopic()
|
2019-05-16 20:20:17 +00:00
|
|
|
symKeyID = await client.shh_newSymKey()
|
|
|
|
privateKeyID = await client.shh_newKeyPair()
|
|
|
|
pubKey = await client.shh_getPublicKey(privateKeyID)
|
2019-04-08 15:55:50 +00:00
|
|
|
options = WhisperFilterOptions(symKeyID: some(symKeyID),
|
2019-04-23 12:45:37 +00:00
|
|
|
topics: some(@[topic]),
|
2019-04-08 15:55:50 +00:00
|
|
|
sig: some(pubKey))
|
2019-05-16 20:20:17 +00:00
|
|
|
filterID = await client.shh_newMessageFilter(options)
|
2019-04-08 15:55:50 +00:00
|
|
|
message = WhisperPostMessage(symKeyID: some(symKeyID),
|
|
|
|
sig: some(privateKeyID),
|
|
|
|
ttl: ttl,
|
2019-04-23 12:45:37 +00:00
|
|
|
topic: some(topic),
|
2019-04-08 15:55:50 +00:00
|
|
|
payload: payload.HexDataStr,
|
|
|
|
powTime: powTime,
|
|
|
|
powTarget: powTarget)
|
2019-03-23 20:54:28 +00:00
|
|
|
check:
|
2019-05-16 20:20:17 +00:00
|
|
|
await(client.shh_setMinPoW(powTarget)) == true
|
|
|
|
await(client.shh_post(message)) == true
|
2019-04-08 15:55:50 +00:00
|
|
|
|
2019-05-16 20:20:17 +00:00
|
|
|
let messages = await client.shh_getFilterMessages(filterID)
|
2019-04-08 15:55:50 +00:00
|
|
|
check:
|
|
|
|
messages.len == 1
|
2019-04-23 12:45:37 +00:00
|
|
|
messages[0].sig.get() == pubKey
|
2019-04-08 15:55:50 +00:00
|
|
|
messages[0].recipientPublicKey.isNone()
|
|
|
|
messages[0].ttl == ttl
|
2019-04-23 12:45:37 +00:00
|
|
|
messages[0].topic == topic
|
|
|
|
messages[0].payload == hexToSeqByte(payload)
|
2019-04-08 15:55:50 +00:00
|
|
|
messages[0].padding.len > 0
|
|
|
|
messages[0].pow >= powTarget
|
2019-03-23 20:54:28 +00:00
|
|
|
|
2019-05-16 20:20:17 +00:00
|
|
|
await(client.shh_deleteMessageFilter(filterID)) == true
|
2019-04-24 20:45:29 +00:00
|
|
|
|
2019-03-23 20:54:28 +00:00
|
|
|
rpcServer.stop()
|
|
|
|
rpcServer.close()
|
|
|
|
|
2019-05-16 20:20:17 +00:00
|
|
|
waitFor doTests()
|