diff --git a/tests/all_tests_v2.nim b/tests/all_tests_v2.nim index 9dacc80cc..aed2565a7 100644 --- a/tests/all_tests_v2.nim +++ b/tests/all_tests_v2.nim @@ -3,6 +3,7 @@ import # TODO: enable this when it is altered into a proper waku relay test # ./v2/test_waku, ./v2/test_wakunode, + ./v2/test_waku_store_rpc_codec, ./v2/test_waku_store, ./v2/test_waku_filter, ./v2/test_waku_payload, diff --git a/tests/v2/test_waku_store.nim b/tests/v2/test_waku_store.nim index 4572b01a5..ac0b22dfd 100644 --- a/tests/v2/test_waku_store.nim +++ b/tests/v2/test_waku_store.nim @@ -502,97 +502,6 @@ procSuite "Waku Store": await allFutures(dialSwitch.stop(), listenSwitch.stop()) - test "Index Protobuf encoder/decoder test": - let - index = computeIndex(WakuMessage(payload: @[byte 1], contentTopic: defaultContentTopic)) - pb = index.encode() - decodedIndex = Index.init(pb.buffer) - - check: - # the fields of decodedIndex must be the same as the original index - decodedIndex.isErr == false - decodedIndex.value == index - - let - emptyIndex = Index() - epb = emptyIndex.encode() - decodedEmptyIndex = Index.init(epb.buffer) - - check: - # check the correctness of init and encode for an empty Index - decodedEmptyIndex.isErr == false - decodedEmptyIndex.value == emptyIndex - - test "PagingInfo Protobuf encod/init test": - let - index = computeIndex(WakuMessage(payload: @[byte 1], contentTopic: defaultContentTopic)) - pagingInfo = PagingInfo(pageSize: 1, cursor: index, direction: PagingDirection.FORWARD) - pb = pagingInfo.encode() - decodedPagingInfo = PagingInfo.init(pb.buffer) - - check: - # the fields of decodedPagingInfo must be the same as the original pagingInfo - decodedPagingInfo.isErr == false - decodedPagingInfo.value == pagingInfo - decodedPagingInfo.value.direction == pagingInfo.direction - - let - emptyPagingInfo = PagingInfo() - epb = emptyPagingInfo.encode() - decodedEmptyPagingInfo = PagingInfo.init(epb.buffer) - - check: - # check the correctness of init and encode for an empty PagingInfo - decodedEmptyPagingInfo.isErr == false - decodedEmptyPagingInfo.value == emptyPagingInfo - - test "HistoryQuery Protobuf encode/init test": - let - index = computeIndex(WakuMessage(payload: @[byte 1], contentTopic: defaultContentTopic)) - pagingInfo = PagingInfo(pageSize: 1, cursor: index, direction: PagingDirection.BACKWARD) - query = HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: defaultContentTopic), HistoryContentFilter(contentTopic: defaultContentTopic)], pagingInfo: pagingInfo, startTime: Timestamp(10), endTime: Timestamp(11)) - pb = query.encode() - decodedQuery = HistoryQuery.init(pb.buffer) - - check: - # the fields of decoded query decodedQuery must be the same as the original query query - decodedQuery.isErr == false - decodedQuery.value == query - - let - emptyQuery=HistoryQuery() - epb = emptyQuery.encode() - decodedEmptyQuery = HistoryQuery.init(epb.buffer) - - check: - # check the correctness of init and encode for an empty HistoryQuery - decodedEmptyQuery.isErr == false - decodedEmptyQuery.value == emptyQuery - - test "HistoryResponse Protobuf encode/init test": - let - wm = WakuMessage(payload: @[byte 1], contentTopic: defaultContentTopic) - index = computeIndex(wm) - pagingInfo = PagingInfo(pageSize: 1, cursor: index, direction: PagingDirection.BACKWARD) - res = HistoryResponse(messages: @[wm], pagingInfo:pagingInfo, error: HistoryResponseError.INVALID_CURSOR) - pb = res.encode() - decodedRes = HistoryResponse.init(pb.buffer) - - check: - # the fields of decoded response decodedRes must be the same as the original response res - decodedRes.isErr == false - decodedRes.value == res - - let - emptyRes=HistoryResponse() - epb = emptyRes.encode() - decodedEmptyRes = HistoryResponse.init(epb.buffer) - - check: - # check the correctness of init and encode for an empty HistoryResponse - decodedEmptyRes.isErr == false - decodedEmptyRes.value == emptyRes - asyncTest "temporal history queries": let key = PrivateKey.random(ECDSA, rng[]).get() diff --git a/tests/v2/test_waku_store_rpc_codec.nim b/tests/v2/test_waku_store_rpc_codec.nim new file mode 100644 index 000000000..e1409a796 --- /dev/null +++ b/tests/v2/test_waku_store_rpc_codec.nim @@ -0,0 +1,172 @@ +{.used.} + +import + std/[options, times], + stew/byteutils, + testutils/unittests, + chronos, + chronicles +import + ../../waku/v2/protocol/waku_message, + ../../waku/v2/protocol/waku_store, + ../../waku/v2/utils/pagination, + ../../waku/v2/utils/time + +const + DEFAULT_PUBSUB_TOPIC = "/waku/2/default-waku/proto" + DEFAULT_CONTENT_TOPIC = ContentTopic("/waku/2/default-content/proto") + + +proc fakeWakuMessage( + payload = "TEST-PAYLOAD", + contentTopic = DEFAULT_CONTENT_TOPIC, + ts = getNanosecondTime(epochTime()) +): WakuMessage = + WakuMessage( + payload: toBytes(payload), + contentTopic: contentTopic, + version: 1, + timestamp: ts + ) + + +procSuite "Waku Store - RPC codec": + + test "Index protobuf codec": + ## Given + let index = Index.compute(fakeWakuMessage(), receivedTime=getNanosecondTime(epochTime()), pubsubTopic=DEFAULT_PUBSUB_TOPIC) + + ## When + let encodedIndex = index.encode() + let decodedIndexRes = Index.init(encodedIndex.buffer) + + ## Then + check: + decodedIndexRes.isOk() + + let decodedIndex = decodedIndexRes.tryGet() + check: + # The fields of decodedIndex must be the same as the original index + decodedIndex == index + + test "Index protobuf codec - empty index": + ## Given + let emptyIndex = Index() + + let encodedIndex = emptyIndex.encode() + let decodedIndexRes = Index.init(encodedIndex.buffer) + + ## Then + check: + decodedIndexRes.isOk() + + let decodedIndex = decodedIndexRes.tryGet() + check: + # Check the correctness of init and encode for an empty Index + decodedIndex == emptyIndex + + test "PagingInfo protobuf codec": + ## Given + let + index = Index.compute(fakeWakuMessage(), receivedTime=getNanosecondTime(epochTime()), pubsubTopic=DEFAULT_PUBSUB_TOPIC) + pagingInfo = PagingInfo(pageSize: 1, cursor: index, direction: PagingDirection.FORWARD) + + ## When + let pb = pagingInfo.encode() + let decodedPagingInfo = PagingInfo.init(pb.buffer) + + ## Then + check: + decodedPagingInfo.isOk() + + check: + # the fields of decodedPagingInfo must be the same as the original pagingInfo + decodedPagingInfo.value == pagingInfo + decodedPagingInfo.value.direction == pagingInfo.direction + + test "PagingInfo protobuf codec - empty paging info": + ## Given + let emptyPagingInfo = PagingInfo() + + ## When + let epb = emptyPagingInfo.encode() + let decodedEmptyPagingInfo = PagingInfo.init(epb.buffer) + + ## Then + check: + decodedEmptyPagingInfo.isOk() + + check: + # check the correctness of init and encode for an empty PagingInfo + decodedEmptyPagingInfo.value == emptyPagingInfo + + test "HistoryQuery protobuf codec": + ## Given + let + index = Index.compute(fakeWakuMessage(), receivedTime=getNanosecondTime(epochTime()), pubsubTopic=DEFAULT_PUBSUB_TOPIC) + pagingInfo = PagingInfo(pageSize: 1, cursor: index, direction: PagingDirection.BACKWARD) + query = HistoryQuery(contentFilters: @[HistoryContentFilter(contentTopic: DEFAULT_CONTENT_TOPIC), HistoryContentFilter(contentTopic: DEFAULT_CONTENT_TOPIC)], pagingInfo: pagingInfo, startTime: Timestamp(10), endTime: Timestamp(11)) + + ## When + let pb = query.encode() + let decodedQuery = HistoryQuery.init(pb.buffer) + + ## Then + check: + decodedQuery.isOk() + + check: + # the fields of decoded query decodedQuery must be the same as the original query query + decodedQuery.value == query + + test "HistoryQuery protobuf codec - empty history query": + ## Given + let emptyQuery = HistoryQuery() + + ## When + let epb = emptyQuery.encode() + let decodedEmptyQuery = HistoryQuery.init(epb.buffer) + + ## Then + check: + decodedEmptyQuery.isOk() + + check: + # check the correctness of init and encode for an empty HistoryQuery + decodedEmptyQuery.value == emptyQuery + + test "HistoryResponse protobuf codec": + ## Given + let + message = fakeWakuMessage() + index = Index.compute(message, receivedTime=getNanosecondTime(epochTime()), pubsubTopic=DEFAULT_PUBSUB_TOPIC) + pagingInfo = PagingInfo(pageSize: 1, cursor: index, direction: PagingDirection.BACKWARD) + res = HistoryResponse(messages: @[message], pagingInfo:pagingInfo, error: HistoryResponseError.INVALID_CURSOR) + + ## When + let pb = res.encode() + let decodedRes = HistoryResponse.init(pb.buffer) + + ## Then + check: + decodedRes.isOk() + + check: + # the fields of decoded response decodedRes must be the same as the original response res + decodedRes.value == res + + test "HistoryResponse protobuf codec - empty history response": + ## Given + let emptyRes = HistoryResponse() + + ## When + let epb = emptyRes.encode() + let decodedEmptyRes = HistoryResponse.init(epb.buffer) + + ## Then + check: + decodedEmptyRes.isOk() + + check: + # check the correctness of init and encode for an empty HistoryResponse + decodedEmptyRes.value == emptyRes \ No newline at end of file