test(waku-relay): Relay (#2101)

* Implement message id tests.
* Implement relay tests.
* Update import paths to use test_all.
This commit is contained in:
Álex Cabeza Romero 2023-11-15 16:11:36 +01:00 committed by GitHub
parent 9c4fdac68e
commit f5f431382b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 1439 additions and 33 deletions

View File

@ -40,15 +40,10 @@ when defined(waku_exp_store_resume):
import ./waku_store/test_resume
# Waku relay test suite
import
./waku_relay/test_waku_relay,
./waku_relay/test_wakunode_relay
import
./waku_relay/test_all
./waku_filter_v2/test_all
# Waku filter test suite
import
./waku_filter_v2/test_waku_client,
./waku_filter_v2/test_waku_filter_protocol
import
# Waku v2 tests

View File

@ -68,3 +68,10 @@ proc getSampleJsonList*(): JsonNode =
"name": "Oberon"
},
]
proc getByteSequence*(bytesNumber: int): seq[byte] =
result = newSeq[byte](bytesNumber)
for i in 0 ..< bytesNumber:
result[i] = cast[byte](i mod 256)
return result

View File

@ -6,3 +6,6 @@ import ../../../waku/waku_core/message
proc newPushHandlerFuture*(): Future[(string, WakuMessage)] =
newFuture[(string, WakuMessage)]()
proc newBoolFuture*(): Future[bool] =
newFuture[bool]()

View File

@ -2,5 +2,4 @@
import
./test_waku_client,
./test_waku_filter_protocol,
./test_waku_filter
./test_waku_filter_protocol

View File

@ -36,7 +36,7 @@ import
sequtils
],
./waku_filter_utils.nim,
./test_data.nim
../resources/payloads.nim
let FUTURE_TIMEOUT = 1.seconds

View File

@ -0,0 +1,47 @@
# Source: nimcrypto/examples/cfb.nim
import nimcrypto
proc cfbEncode*(key: string, iv: string, data: string): seq[byte] =
var context: CFB[aes256]
var pKey: array[aes256.sizeKey, byte]
var pIv: array[aes256.sizeBlock, byte]
var pData = newSeq[byte](len(data))
var encodedData = newSeq[byte](len(data))
copyMem(addr pData[0], unsafeAddr data[0], len(data))
# WARNING! Do not use 0 byte padding in applications, this is done as example.
copyMem(addr pKey[0], unsafeAddr key[0], len(key))
copyMem(addr pIv[0], unsafeAddr iv[0], len(iv))
# Initialization of CFB[aes256] context with encryption key
context.init(pKey, pIv)
# Encryption process
context.encrypt(pData, encodedData)
# Clear context of CFB[aes256]
context.clear()
return encodedData
proc cfbDecode*(key: string, iv: string, encodedData: seq[byte]): seq[byte] =
var context: CFB[aes256]
var pKey: array[aes256.sizeKey, byte]
var pIv: array[aes256.sizeBlock, byte]
var decodedData = newSeq[byte](len(encodedData))
# copyMem(addr _data[0], addr data[0], len(data))
# WARNING! Do not use 0 byte padding in applications, this is done as example.
copyMem(addr pKey[0], unsafeAddr key[0], len(key))
copyMem(addr pIv[0], unsafeAddr iv[0], len(iv))
# Initialization of CFB[aes256] context with encryption key
context.init(pKey, pIv)
# Decryption process
context.decrypt(encodedData, decodedData)
# Clear context of CFB[aes256]
context.clear()
return decodedData

View File

@ -2,4 +2,6 @@
import
./test_waku_relay,
./test_wakunode_relay
./test_wakunode_relay,
./test_message_id,
./test_protocol

View File

@ -0,0 +1,37 @@
import
unittest,
stew/shims/net,
stew/[results, byteutils]
import
stew/results,
nimcrypto/sha2,
libp2p/protocols/pubsub/rpc/messages,
../../../waku/waku_relay/message_id,
../testlib/sequtils
suite "Message ID Provider":
test "Non-empty string":
let message = Message(data: "Hello, world!".toBytes())
let result = defaultMessageIdProvider(message)
let expected = MDigest[256].fromHex("315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3")
check:
result.isOk()
result.get() == expected.data
test "Empty string":
let message = Message(data: "".toBytes())
let result = defaultMessageIdProvider(message)
let expected = MDigest[256].fromHex("E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855")
check:
result.isOk()
result.get() == expected.data
test "Empty array":
let message = Message(data: @[])
let result = defaultMessageIdProvider(message)
let expected = MDigest[256].fromHex("E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855")
check:
result.isOk()
result.get() == expected.data

File diff suppressed because it is too large Load Diff

View File

@ -8,30 +8,14 @@ import
chronos,
libp2p/protocols/pubsub/pubsub,
libp2p/protocols/pubsub/rpc/messages
import
../../../waku/node/peer_manager,
../../../waku/waku_core,
../../../waku/waku_relay,
../testlib/common,
../testlib/wakucore
proc noopRawHandler(): WakuRelayHandler =
var handler: WakuRelayHandler
handler = proc(topic: PubsubTopic, msg: WakuMessage): Future[void] {.async, gcsafe.} = discard
handler
proc newTestWakuRelay(switch = newTestSwitch()): Future[WakuRelay] {.async.} =
let proto = WakuRelay.new(switch).tryGet()
await proto.start()
let protocolMatcher = proc(proto: string): bool {.gcsafe.} =
return proto.startsWith(WakuRelayCodec)
switch.mount(proto, protocolMatcher)
return proto
../testlib/wakucore,
./utils
suite "Waku Relay":

View File

@ -0,0 +1,29 @@
{.used.}
import
std/[strutils],
stew/shims/net as stewNet,
chronos
import
../../../waku/waku_relay,
../../../waku/waku_core,
../testlib/wakucore
proc noopRawHandler*(): WakuRelayHandler =
var handler: WakuRelayHandler
handler = proc(topic: PubsubTopic, msg: WakuMessage): Future[void] {.async, gcsafe.} = discard
handler
proc newTestWakuRelay*(switch = newTestSwitch()): Future[WakuRelay] {.async.} =
let proto = WakuRelay.new(switch).tryGet()
await proto.start()
let protocolMatcher = proc(proto: string): bool {.gcsafe.} =
return proto.startsWith(WakuRelayCodec)
switch.mount(proto, protocolMatcher)
return proto

View File

@ -217,7 +217,7 @@ proc generateOrderedValidator*(w: WakuRelay): auto {.gcsafe.} =
proc subscribe*(w: WakuRelay, pubsubTopic: PubsubTopic, handler: WakuRelayHandler): TopicHandler =
debug "subscribe", pubsubTopic=pubsubTopic
# we need to wrap the handler since gossipsub doesnt understand WakuMessage
# We need to wrap the handler since gossipsub doesnt understand WakuMessage
let wrappedHandler =
proc(pubsubTopic: string, data: seq[byte]): Future[void] {.gcsafe, raises: [].} =
let decMsg = WakuMessage.decode(data)
@ -230,7 +230,9 @@ proc subscribe*(w: WakuRelay, pubsubTopic: PubsubTopic, handler: WakuRelayHandle
else:
return handler(pubsubTopic, decMsg.get())
# add the ordered validator to the topic
# Add the ordered validator to the topic
# This assumes that if `w.validatorInserted.hasKey(pubSubTopic) is true`, it contains the ordered validator.
# Otherwise this might lead to unintended behaviour.
if not w.validatorInserted.hasKey(pubSubTopic):
procCall GossipSub(w).addValidator(pubSubTopic, w.generateOrderedValidator())
w.validatorInserted[pubSubTopic] = true