2021-04-23 06:56:04 +00:00
|
|
|
{.used.}
|
|
|
|
|
|
|
|
import
|
2023-10-09 14:38:23 +00:00
|
|
|
std/options,
|
|
|
|
std/strscans,
|
2023-02-13 10:43:49 +00:00
|
|
|
testutils/unittests,
|
2022-11-22 07:13:51 +00:00
|
|
|
chronicles,
|
2023-02-13 10:43:49 +00:00
|
|
|
chronos,
|
2022-08-02 23:47:42 +00:00
|
|
|
libp2p/crypto/crypto
|
|
|
|
import
|
2023-08-09 17:11:50 +00:00
|
|
|
../../waku/node/peer_manager,
|
|
|
|
../../waku/waku_core,
|
|
|
|
../../waku/waku_lightpush,
|
|
|
|
../../waku/waku_lightpush/client,
|
2023-10-09 14:38:23 +00:00
|
|
|
../../waku/waku_lightpush/protocol_metrics,
|
|
|
|
../../waku/waku_lightpush/rpc,
|
2022-10-25 12:55:31 +00:00
|
|
|
./testlib/common,
|
2023-04-05 14:01:51 +00:00
|
|
|
./testlib/wakucore
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
proc newTestWakuLightpushNode(switch: Switch, handler: PushMessageHandler): Future[WakuLightPush] {.async.} =
|
|
|
|
let
|
|
|
|
peerManager = PeerManager.new(switch)
|
|
|
|
proto = WakuLightPush.new(peerManager, rng, handler)
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
await proto.start()
|
|
|
|
switch.mount(proto)
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
return proto
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
proc newTestWakuLightpushClient(switch: Switch): WakuLightPushClient =
|
|
|
|
let
|
|
|
|
peerManager = PeerManager.new(switch)
|
|
|
|
WakuLightPushClient.new(peerManager, rng)
|
2021-04-23 06:56:04 +00:00
|
|
|
|
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
suite "Waku Lightpush":
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
asyncTest "push message to pubsub topic is successful":
|
|
|
|
## Setup
|
2023-02-13 10:43:49 +00:00
|
|
|
let
|
2022-10-25 12:55:31 +00:00
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
clientSwitch = newTestSwitch()
|
|
|
|
|
|
|
|
await allFutures(serverSwitch.start(), clientSwitch.start())
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
## Given
|
|
|
|
let handlerFuture = newFuture[(string, WakuMessage)]()
|
2023-02-13 10:43:49 +00:00
|
|
|
let handler = proc(peer: PeerId, pubsubTopic: PubsubTopic, message: WakuMessage): Future[WakuLightPushResult[void]] {.async.} =
|
2022-10-25 12:55:31 +00:00
|
|
|
handlerFuture.complete((pubsubTopic, message))
|
|
|
|
return ok()
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
let
|
|
|
|
server = await newTestWakuLightpushNode(serverSwitch, handler)
|
|
|
|
client = newTestWakuLightpushClient(clientSwitch)
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
let serverPeerId = serverSwitch.peerInfo.toRemotePeerInfo()
|
2021-04-23 06:56:04 +00:00
|
|
|
|
|
|
|
let
|
2022-10-25 12:55:31 +00:00
|
|
|
topic = DefaultPubsubTopic
|
|
|
|
message = fakeWakuMessage()
|
|
|
|
|
|
|
|
## When
|
2022-10-28 14:30:01 +00:00
|
|
|
let requestRes = await client.publish(topic, message, peer=serverPeerId)
|
2022-10-25 12:55:31 +00:00
|
|
|
|
|
|
|
require await handlerFuture.withTimeout(100.millis)
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
requestRes.isOk()
|
|
|
|
handlerFuture.finished()
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
let (handledMessagePubsubTopic, handledMessage) = handlerFuture.read()
|
|
|
|
check:
|
|
|
|
handledMessagePubsubTopic == topic
|
|
|
|
handledMessage == message
|
|
|
|
|
|
|
|
## Cleanup
|
|
|
|
await allFutures(clientSwitch.stop(), serverSwitch.stop())
|
|
|
|
|
|
|
|
asyncTest "push message to pubsub topic should fail":
|
|
|
|
## Setup
|
2023-02-13 10:43:49 +00:00
|
|
|
let
|
2022-10-25 12:55:31 +00:00
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
clientSwitch = newTestSwitch()
|
|
|
|
|
|
|
|
await allFutures(serverSwitch.start(), clientSwitch.start())
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-08-02 23:47:42 +00:00
|
|
|
## Given
|
2022-10-25 12:55:31 +00:00
|
|
|
let error = "test_failure"
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
let handlerFuture = newFuture[void]()
|
2023-02-13 10:43:49 +00:00
|
|
|
let handler = proc(peer: PeerId, pubsubTopic: PubsubTopic, message: WakuMessage): Future[WakuLightPushResult[void]] {.async.} =
|
2022-10-25 12:55:31 +00:00
|
|
|
handlerFuture.complete()
|
|
|
|
return err(error)
|
|
|
|
|
2022-08-02 23:47:42 +00:00
|
|
|
let
|
2022-10-25 12:55:31 +00:00
|
|
|
server = await newTestWakuLightpushNode(serverSwitch, handler)
|
|
|
|
client = newTestWakuLightpushClient(clientSwitch)
|
|
|
|
|
|
|
|
let serverPeerId = serverSwitch.peerInfo.toRemotePeerInfo()
|
|
|
|
|
|
|
|
let
|
|
|
|
topic = DefaultPubsubTopic
|
|
|
|
message = fakeWakuMessage()
|
2022-08-02 23:47:42 +00:00
|
|
|
|
|
|
|
## When
|
2022-10-28 14:30:01 +00:00
|
|
|
let requestRes = await client.publish(topic, message, peer=serverPeerId)
|
2022-10-25 12:55:31 +00:00
|
|
|
|
|
|
|
require await handlerFuture.withTimeout(100.millis)
|
2021-04-23 06:56:04 +00:00
|
|
|
|
2022-08-02 23:47:42 +00:00
|
|
|
## Then
|
2021-04-23 06:56:04 +00:00
|
|
|
check:
|
2022-10-25 12:55:31 +00:00
|
|
|
requestRes.isErr()
|
|
|
|
handlerFuture.finished()
|
2022-08-02 23:47:42 +00:00
|
|
|
|
2022-10-25 12:55:31 +00:00
|
|
|
let requestError = requestRes.error
|
|
|
|
check:
|
|
|
|
requestError == error
|
2023-02-13 10:43:49 +00:00
|
|
|
|
2022-08-02 23:47:42 +00:00
|
|
|
## Cleanup
|
2023-10-09 14:38:23 +00:00
|
|
|
await allFutures(clientSwitch.stop(), serverSwitch.stop())
|
|
|
|
|
|
|
|
asyncTest "incorrectly encoded request should return an erring response":
|
|
|
|
## Setup
|
|
|
|
let
|
|
|
|
serverSwitch = newTestSwitch()
|
|
|
|
handler = proc(peer: PeerId, pubsubTopic: PubsubTopic, message: WakuMessage): Future[WakuLightPushResult[void]] {.async.} =
|
|
|
|
## this handler will never be called: request must fail earlier
|
|
|
|
return ok()
|
|
|
|
server = await newTestWakuLightpushNode(serverSwitch, handler)
|
|
|
|
|
|
|
|
## Given
|
|
|
|
let
|
|
|
|
fakeBuffer = @[byte(42)]
|
|
|
|
fakePeerId = PeerId.init(PrivateKey.random(ECDSA, (newRng())[]).tryGet()).tryGet()
|
|
|
|
|
|
|
|
## When
|
|
|
|
let
|
|
|
|
pushRpcResponse = await server.handleRequest(fakePeerId, fakeBuffer)
|
|
|
|
requestId = pushRpcResponse.requestId
|
|
|
|
|
|
|
|
## Then
|
|
|
|
check:
|
|
|
|
requestId == ""
|
|
|
|
pushRpcResponse.response.isSome()
|
|
|
|
|
|
|
|
let resp = pushRpcResponse.response.get()
|
|
|
|
|
|
|
|
check:
|
|
|
|
resp.isSuccess == false
|
|
|
|
resp.info.isSome()
|
|
|
|
## the error message should start with decodeRpcFailure
|
|
|
|
scanf(resp.info.get(), decodeRpcFailure)
|