diff --git a/tests/all_tests_v2.nim b/tests/all_tests_v2.nim index 2b722ff51..f810eccbb 100644 --- a/tests/all_tests_v2.nim +++ b/tests/all_tests_v2.nim @@ -5,9 +5,10 @@ import ./v2/test_wakunode, ./v2/test_waku_store, ./v2/test_waku_filter, - ./v2/test_waku_pagination, ./v2/test_waku_payload, ./v2/test_waku_swap, + ./v2/test_utils_pagination, + ./v2/test_message_store_queue_pagination, ./v2/test_message_store, ./v2/test_jsonrpc_waku, ./v2/test_rest_serdes, @@ -27,7 +28,6 @@ import ./v2/test_waku_discv5, ./v2/test_enr_utils, ./v2/test_waku_store_queue, - ./v2/test_pagination_utils, ./v2/test_peer_exchange, ./v2/test_waku_noise diff --git a/tests/v2/test_waku_pagination.nim b/tests/v2/test_message_store_queue_pagination.nim similarity index 73% rename from tests/v2/test_waku_pagination.nim rename to tests/v2/test_message_store_queue_pagination.nim index 3c3356fec..851564e1f 100644 --- a/tests/v2/test_waku_pagination.nim +++ b/tests/v2/test_message_store_queue_pagination.nim @@ -1,7 +1,7 @@ {.used.} import - std/[options, sequtils], + std/[options, sequtils, times], testutils/unittests, nimcrypto/sha2, libp2p/protobuf/minprotobuf @@ -13,48 +13,39 @@ import ../../waku/v2/utils/pagination -proc createSampleStoreQueue(s: int): StoreQueueRef = - ## takes s as input and outputs a StoreQueue with s amount of IndexedWakuMessage - - let testStoreQueue = StoreQueueRef.new(s) +const + DEFAULT_PUBSUB_TOPIC = "/waku/2/default-waku/proto" + DEFAULT_CONTENT_TOPIC = ContentTopic("/waku/2/default-content/proto") + + +proc getTestStoreQueue(numMessages: int): StoreQueueRef = + let testStoreQueue = StoreQueueRef.new(numMessages) var data {.noinit.}: array[32, byte] for x in data.mitems: x = 1 - for i in 0.. 0 cmp(largeIndex1, largeIndex2) > 0 - # Index comparison when equal + # Index comparison when equal + check: cmp(eqIndex1, eqIndex2) == 0 - # pubsubTopic difference + # pubsubTopic difference + check: cmp(smallIndex1, diffPsTopic) < 0 - # receiverTime diff plays no role when senderTime set + # receiverTime diff plays no role when senderTime set + check: cmp(eqIndex1, eqIndex3) == 0 - # receiverTime diff plays no role when digest/pubsubTopic equal + # receiverTime diff plays no role when digest/pubsubTopic equal + check: cmp(noSenderTime1, noSenderTime2) == 0 - # sort on receiverTime with no senderTimestamp and unequal pubsubTopic + # sort on receiverTime with no senderTimestamp and unequal pubsubTopic + check: cmp(noSenderTime1, noSenderTime3) < 0 - # sort on receiverTime with no senderTimestamp and unequal digest + # sort on receiverTime with no senderTimestamp and unequal digest + check: cmp(noSenderTime1, noSenderTime4) < 0 - # sort on receiverTime if no senderTimestamp on only one side + # sort on receiverTime if no senderTimestamp on only one side + check: cmp(smallIndex1, noSenderTime1) < 0 cmp(noSenderTime1, smallIndex1) > 0 # Test symmetry cmp(noSenderTime2, eqIndex3) < 0 cmp(eqIndex3, noSenderTime2) > 0 # Test symmetry - asyncTest "Index equality": + test "Index equality": + # Exactly equal check: - # Exactly equal eqIndex1 == eqIndex2 - # Receiver time plays no role, even without sender time + # Receiver time plays no role, even without sender time + check: eqIndex1 == eqIndex3 noSenderTime1 == noSenderTime2 # only receiver time differs, indices are equal noSenderTime1 != noSenderTime3 # pubsubTopics differ noSenderTime1 != noSenderTime4 # digests differ - # Unequal sender time + # Unequal sender time + check: smallIndex1 != largeIndex1 - # Unequal digest + # Unequal digest + check: smallIndex1 != smallIndex2 - # Unequal hash and digest + # Unequal hash and digest + check: smallIndex1 != eqIndex1 - # Unequal pubsubTopic + # Unequal pubsubTopic + check: smallIndex1 != diffPsTopic + + test "Index computation should not be empty": + ## Given + let ts = getTestTimestamp() + let wm = WakuMessage(payload: @[byte 1, 2, 3], timestamp: ts) + + ## When + let ts2 = getTestTimestamp() + 10 + let index = Index.compute(wm, ts2, DEFAULT_CONTENT_TOPIC) + + ## Then + check: + index.digest.data.len != 0 + index.digest.data.len == 32 # sha2 output length in bytes + index.receiverTime == ts2 # the receiver timestamp should be a non-zero value + index.senderTime == ts + index.pubsubTopic == DEFAULT_CONTENT_TOPIC + + test "Index digest of two identical messsage should be the same": + ## Given + let topic = ContentTopic("test-content-topic") + let + wm1 = WakuMessage(payload: @[byte 1, 2, 3], contentTopic: topic) + wm2 = WakuMessage(payload: @[byte 1, 2, 3], contentTopic: topic) + + ## When + let ts = getTestTimestamp() + let + index1 = Index.compute(wm1, ts, DEFAULT_PUBSUB_TOPIC) + index2 = Index.compute(wm2, ts, DEFAULT_PUBSUB_TOPIC) + + ## Then + check: + index1.digest == index2.digest + \ No newline at end of file diff --git a/tests/v2/test_waku_payload.nim b/tests/v2/test_waku_payload.nim index 795cb33cd..22833d10c 100644 --- a/tests/v2/test_waku_payload.nim +++ b/tests/v2/test_waku_payload.nim @@ -3,7 +3,8 @@ import testutils/unittests, ../../waku/v2/protocol/waku_message, - ../../waku/v2/node/waku_payload + ../../waku/v2/node/waku_payload, + ../../waku/v2/utils/time procSuite "Waku Payload": let rng = newRng() @@ -109,3 +110,46 @@ procSuite "Waku Payload": check: decoded.isErr() + + test "Encode/Decode waku message with timestamp": + ## Test encoding and decoding of the timestamp field of a WakuMessage + + ## Given + let + version = 0'u32 + payload = @[byte 0, 1, 2] + timestamp = Timestamp(10) + msg = WakuMessage(payload: payload, version: version, timestamp: timestamp) + + ## When + let pb = msg.encode() + let msgDecoded = WakuMessage.init(pb.buffer) + + ## Then + check: + msgDecoded.isOk() + + let timestampDecoded = msgDecoded.value.timestamp + check: + timestampDecoded == timestamp + + test "Encode/Decode waku message without timestamp": + ## Test the encoding and decoding of a WakuMessage with an empty timestamp field + + ## Given + let + version = 0'u32 + payload = @[byte 0, 1, 2] + msg = WakuMessage(payload: payload, version: version) + + ## When + let pb = msg.encode() + let msgDecoded = WakuMessage.init(pb.buffer) + + ## Then + check: + msgDecoded.isOk() + + let timestampDecoded = msgDecoded.value.timestamp + check: + timestampDecoded == Timestamp(0) \ No newline at end of file