Skip to content

Commit

Permalink
update cpp tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ftheirs committed Aug 11, 2023
1 parent 68aa90a commit 4d189e9
Show file tree
Hide file tree
Showing 6 changed files with 397 additions and 195 deletions.
165 changes: 165 additions & 0 deletions tests/common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*******************************************************************************
* (c) 2018 - 2023 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#include "gmock/gmock.h"
#include "parser_impl.h"

#include <parser.h>
#include <sstream>
#include <string>
#include <fmt/core.h>
#include "common.h"
#include <iostream>
#include <fstream>
#include <json/json.h>
#include <app_mode.h>
#include <hexutils.h>
#include "parser.h"

std::vector<std::string> dumpUI(parser_context_t *ctx,
uint16_t maxKeyLen,
uint16_t maxValueLen) {
auto answer = std::vector<std::string>();

uint8_t numItems;
parser_error_t err = parser_getNumItems(ctx, &numItems);
if (err != parser_ok) {
return answer;
}

for (uint8_t idx = 0; idx < numItems; idx++) {
char keyBuffer[1000];
char valueBuffer[1000];
uint8_t pageIdx = 0;
uint8_t pageCount = 1;

while (pageIdx < pageCount) {
std::stringstream ss;

err = parser_getItem(ctx, idx,
keyBuffer, maxKeyLen,
valueBuffer, maxValueLen,
pageIdx, &pageCount);

ss << fmt::format("{} | {}", idx, keyBuffer);
if (pageCount > 1) {
ss << fmt::format(" [{}/{}]", pageIdx + 1, pageCount);
}
ss << " : ";

if (err == parser_ok) {
// Model multiple lines
ss << fmt::format("{}", valueBuffer);
} else {
ss << parser_getErrorDescription(err);
}

auto output = ss.str();
answer.push_back(output);

pageIdx++;
}
}

return answer;
}

std::string CleanTestname(std::string s) {
s.erase(remove_if(s.begin(), s.end(), [](char v) -> bool {
return v == ':' || v == ' ' || v == '/' || v == '-' || v == '.' || v == '_' || v == '#';
}), s.end());
return s;
}

// Retrieve testcases from json file
std::vector<testcase_t> GetJsonTestCases(const std::string &jsonFile) {
auto answer = std::vector<testcase_t>();

Json::CharReaderBuilder builder;
Json::Value obj;

std::string fullPathJsonFile = std::string(TESTVECTORS_DIR) + jsonFile;

std::ifstream inFile(fullPathJsonFile);
if (!inFile.is_open()) {
return answer;
}

// Retrieve all test cases
JSONCPP_STRING errs;
Json::parseFromStream(builder, inFile, &obj, &errs);
std::cout << "Number of testcases: " << obj.size() << std::endl;

for (int i = 0; i < obj.size(); i++) {

auto outputs = std::vector<std::string>();
for (auto s : obj[i]["output"]) {
outputs.push_back(s.asString());
}

auto outputs_expert = std::vector<std::string>();
for (auto s : obj[i]["output_expert"]) {
outputs_expert.push_back(s.asString());
}

answer.push_back(testcase_t{
obj[i]["index"].asUInt64(),
obj[i]["name"].asString(),
obj[i]["blob"].asString(),
outputs,
outputs_expert
});
}

return answer;
}

void check_testcase(const testcase_t &tc, bool expert_mode) {
app_mode_set_expert(expert_mode);

parser_context_t ctx = {0};
parser_error_t err = parser_unexpected_error;

uint8_t buffer[10000] = {0};
const uint16_t bufferLen = parseHexString(buffer, sizeof(buffer), tc.blob.c_str());

parser_tx_t tx_obj;
memset(&tx_obj, 0, sizeof(tx_obj));

err = parser_parse(&ctx, buffer, bufferLen, &tx_obj);
ASSERT_EQ(err, parser_ok) << parser_getErrorDescription(err);

err = parser_validate(&ctx);
ASSERT_EQ(err, parser_ok) << parser_getErrorDescription(err);


auto output = dumpUI(&ctx, 39, 39);

std::cout << std::endl;
for (const auto &i : output) {
std::cout << i << std::endl;
}
std::cout << std::endl << std::endl;

#if 1
std::vector<std::string> expected = app_mode_expert() ? tc.expected_expert : tc.expected;
EXPECT_EQ(output.size(), expected.size());
for (size_t i = 0; i < expected.size(); i++) {
if (i < output.size()) {
EXPECT_THAT(output[i], testing::Eq(expected[i]));
}
}
#endif
}
51 changes: 51 additions & 0 deletions tests/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*******************************************************************************
* (c) 2018 - 2023 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#pragma once

#include <vector>
#include <string>
#include "parser_common.h"

typedef struct {
uint64_t index;
std::string name;
std::string blob;
std::vector<std::string> expected;
std::vector<std::string> expected_expert;
} testcase_t;

class JsonTests_Base : public ::testing::TestWithParam<testcase_t> {
public:
struct PrintToStringParamName {
template<class ParamType>
std::string operator()(const testing::TestParamInfo<ParamType> &info) const {
auto p = static_cast<testcase_t>(info.param);
std::stringstream ss;
ss << p.index << "_" << p.name;
return ss.str();
}
};
};

#define EXPECT_EQ_STR(_STR1, _STR2, _errorMessage) { if (_STR1 != nullptr & _STR2 != nullptr) \
EXPECT_TRUE(!strcmp(_STR1, _STR2)) << _errorMessage << ", expected: " << _STR2 << ", received: " << _STR1; \
else FAIL() << "One of the strings is null"; }

std::vector<std::string> dumpUI(parser_context_t *ctx, uint16_t maxKeyLen, uint16_t maxValueLen);

std::vector<testcase_t> GetJsonTestCases(const std::string &jsonFile);

void check_testcase(const testcase_t &tc, bool expert_mode);
166 changes: 166 additions & 0 deletions tests/ibc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
[
{
"blob": "1e0000006532652d746573742e3161313862653132353965653931326361666363380023000000323032332d30382d30335431343a31353a32352e3438313532393236352b30303a3030119ebad0a1d922d4d0ae97c34025898a87e2c3de2d22911c3a25dc9252b58341fb24d8d4c9878f6565d717e03e3ccabe8d4380bb2b398eee8394996cab0bc5680100e1f50500000000000000000000000000000000000000000000000000000000004b88fb913a0766e30a00b2fb8aa2949a710e24e600a5f1ed44f01daedeef67994b25409d69045b49def08c927db56ea503ffce0eb00200000000000000000000000000000000000000000000000000000000000000000000000000000000020000000029dac0bb890100000c0100000a292f6962632e6170706c69636174696f6e732e7472616e736665722e76312e4d73675472616e7366657212de010a087472616e73666572120b6368616e6e656c2d3134311a590a54617465737431763465686777333678647a727976653567736335327665656735636e73763279783565796776703338716372766432397879367279733670387963357876703478667079327636393477677763701201302254617465737431763465686777333678356d357a736568387975357a333279787636726a64656a6763367276643678787663357877663367347a726a73666e787070797673333438397a6e7a7633636461386a396d2a08636872697374656c320038ebb8ea8c9a8dfabb170229dac0bb8901000000f8056f465d17043d2864309d1c50a897a028b6ebf9702e5c403e1bf8c670f6e6",
"index": 0,
"name": "IBC_0",
"output": [
"0 | Type : IBC",
"1 | Source port : transfer",
"2 | Source channel : channel-141",
"3 | Token [1/3] : 0 atest1v4ehgw36xdzryve5gsc52veeg5cnsv",
"3 | Token [2/3] : 2yx5eygvp38qcrvd29xy6rys6p8yc5xvp4xfpy",
"3 | Token [3/3] : 2v694wgwcp",
"4 | Sender [1/3] : atest1v4ehgw36x5m5zseh8yu5z32yxv6rjdej",
"4 | Sender [2/3] : gc6rvd6xxvc5xwf3g4zrjsfnxppyvs3489znzv",
"4 | Sender [3/3] : 3cda8j9m",
"5 | Receiver : christel",
"6 | Timeout height : no timeout",
"7 | Timeout timestamp : 2023-08-03T15:15:25.481385067Z"
],
"output_expert": [
"0 | Code hash [1/2] : f8056f465d17043d2864309d1c50a897a028b6",
"0 | Code hash [2/2] : ebf9702e5c403e1bf8c670f6e6",
"1 | Source port : transfer",
"2 | Source channel : channel-141",
"3 | Token [1/3] : 0 atest1v4ehgw36xdzryve5gsc52veeg5cnsv",
"3 | Token [2/3] : 2yx5eygvp38qcrvd29xy6rys6p8yc5xvp4xfpy",
"3 | Token [3/3] : 2v694wgwcp",
"4 | Sender [1/3] : atest1v4ehgw36x5m5zseh8yu5z32yxv6rjdej",
"4 | Sender [2/3] : gc6rvd6xxvc5xwf3g4zrjsfnxppyvs3489znzv",
"4 | Sender [3/3] : 3cda8j9m",
"5 | Receiver : christel",
"6 | Timeout height : no timeout",
"7 | Timeout timestamp : 2023-08-03T15:15:25.481385067Z",
"8 | Timestamp : 2023-08-03 14:15:25.481529265 UTC",
"9 | Pubkey [1/2] : 00a5f1ed44f01daedeef67994b25409d69045b",
"9 | Pubkey [2/2] : 49def08c927db56ea503ffce0eb0",
"10 | Epoch : 2",
"11 | Gas limit : 0",
"12 | Fees : NAM 100.0"
],
"valid": true
},
{
"blob": "1e0000006532652d746573742e3161313862653132353965653931326361666363380023000000323032332d30382d30335431343a31353a32392e3036323233383937342b30303a3030e5137edd6ed821e7c5ba732d3068c0de34a238162db9ed8e1ecfff46083e07eb5021bd5e5c0d582e4a90b4b940c5701ace4f3623bd1f975970c32bf0bea517f60100e1f50500000000000000000000000000000000000000000000000000000000004b88fb913a0766e30a00b2fb8aa2949a710e24e6008cec0b9cf24b3264780e978bbdbec858f5a6b9b462053b8cf34ec880026d9a5b0200000000000000000000000000000000000000000000000000000000000000000000000000000001010a000000000000005600000000000000020000000026e8c0bb89010000080100000a292f6962632e6170706c69636174696f6e732e7472616e736665722e76312e4d73675472616e7366657212da010a087472616e7366657212096368616e6e656c2d301a590a5461746573743176346568677733367833707273777a786767756e7a76367078716d6e76646a39787663797a76707367676579767333636739716e797766353839716e77766673673565726733666b6c3039726735120130225461746573743176346568677733367876757976337a706739713532736535786763723233337378756d6e77777a726773637979736a397834706e78647065677375723273367067766d72327365346634787574392a0662657274686132003897e0a2b8a78dfabb170226e8c0bb8901000000f8056f465d17043d2864309d1c50a897a028b6ebf9702e5c403e1bf8c670f6e6",
"index": 1,
"name": "IBC_0",
"output": [
"0 | Type : IBC",
"1 | Source port : transfer",
"2 | Source channel : channel-0",
"3 | Token [1/3] : 0 atest1v4ehgw36x3prswzxggunzv6pxqmnvd",
"3 | Token [2/3] : j9xvcyzvpsggeyvs3cg9qnywf589qnwvfsg5er",
"3 | Token [3/3] : g3fkl09rg5",
"4 | Sender [1/3] : atest1v4ehgw36xvuyv3zpg9q52se5xgcr233s",
"4 | Sender [2/3] : xumnwwzrgscyysj9x4pnxdpegsur2s6pgvmr2s",
"4 | Sender [3/3] : e4f4xut9",
"5 | Receiver : bertha",
"6 | Timeout height : no timeout",
"7 | Timeout timestamp : 2023-08-03T15:15:29.062146071Z"
],
"output_expert": [
"0 | Code hash [1/2] : f8056f465d17043d2864309d1c50a897a028b6",
"0 | Code hash [2/2] : ebf9702e5c403e1bf8c670f6e6",
"1 | Source port : transfer",
"2 | Source channel : channel-0",
"3 | Token [1/3] : 0 atest1v4ehgw36x3prswzxggunzv6pxqmnvd",
"3 | Token [2/3] : j9xvcyzvpsggeyvs3cg9qnywf589qnwvfsg5er",
"3 | Token [3/3] : g3fkl09rg5",
"4 | Sender [1/3] : atest1v4ehgw36xvuyv3zpg9q52se5xgcr233s",
"4 | Sender [2/3] : xumnwwzrgscyysj9x4pnxdpegsur2s6pgvmr2s",
"4 | Sender [3/3] : e4f4xut9",
"5 | Receiver : bertha",
"6 | Timeout height : no timeout",
"7 | Timeout timestamp : 2023-08-03T15:15:29.062146071Z",
"8 | Timestamp : 2023-08-03 14:15:29.062238974 UTC",
"9 | Pubkey [1/2] : 008cec0b9cf24b3264780e978bbdbec858f5a6",
"9 | Pubkey [2/2] : b9b462053b8cf34ec880026d9a5b",
"10 | Epoch : 2",
"11 | Gas limit : 0",
"12 | Fees : NAM 100.0"
],
"valid": true
},
{
"blob": "1e0000006532652d746573742e3161313862653132353965653931326361666363380023000000323032332d30382d30335431343a31353a33332e3031333938323434352b30303a30308d78fa56e1af140db1d9fc4bd38bc155d33c5e2f4a8dabefb275530d62f62b2c7c507f0fbbfe45425b5483f1c97f28b76ef988f3b0bc1a0087158a0f6c7395c60100e1f50500000000000000000000000000000000000000000000000000000000004b88fb913a0766e30a00b2fb8aa2949a710e24e600a5f1ed44f01daedeef67994b25409d69045b49def08c927db56ea503ffce0eb00200000000000000000000000000000000000000000000000000000000000000000000000000000000020000000095f7c0bb89010000080100000a292f6962632e6170706c69636174696f6e732e7472616e736665722e76312e4d73675472616e7366657212da010a087472616e7366657212096368616e6e656c2d301a590a546174657374316439337878773336787663783276656b7835657879633335783433726a7666337673636b7a77743978336a6e766470357673636b6765747a78706a78766465657863366e7a7674703334373361611201302254617465737431763465686777333678356d357a736568387975357a333279787636726a64656a6763367276643678787663357877663367347a726a73666e787070797673333438397a6e7a7633636461386a396d2a06616c62657274320038ef93cb94b68dfabb170296f7c0bb8901000000f8056f465d17043d2864309d1c50a897a028b6ebf9702e5c403e1bf8c670f6e6",
"index": 2,
"name": "IBC_0",
"output": [
"0 | Type : IBC",
"1 | Source port : transfer",
"2 | Source channel : channel-0",
"3 | Token [1/3] : 0 atest1d93xxw36xvcx2vekx5exyc35x43rjv",
"3 | Token [2/3] : f3vsckzwt9x3jnvdp5vsckgetzxpjxvdeexc6n",
"3 | Token [3/3] : zvtp3473aa",
"4 | Sender [1/3] : atest1v4ehgw36x5m5zseh8yu5z32yxv6rjdej",
"4 | Sender [2/3] : gc6rvd6xxvc5xwf3g4zrjsfnxppyvs3489znzv",
"4 | Sender [3/3] : 3cda8j9m",
"5 | Receiver : albert",
"6 | Timeout height : no timeout",
"7 | Timeout timestamp : 2023-08-03T15:15:33.013842415Z"
],
"output_expert": [
"0 | Code hash [1/2] : f8056f465d17043d2864309d1c50a897a028b6",
"0 | Code hash [2/2] : ebf9702e5c403e1bf8c670f6e6",
"1 | Source port : transfer",
"2 | Source channel : channel-0",
"3 | Token [1/3] : 0 atest1d93xxw36xvcx2vekx5exyc35x43rjv",
"3 | Token [2/3] : f3vsckzwt9x3jnvdp5vsckgetzxpjxvdeexc6n",
"3 | Token [3/3] : zvtp3473aa",
"4 | Sender [1/3] : atest1v4ehgw36x5m5zseh8yu5z32yxv6rjdej",
"4 | Sender [2/3] : gc6rvd6xxvc5xwf3g4zrjsfnxppyvs3489znzv",
"4 | Sender [3/3] : 3cda8j9m",
"5 | Receiver : albert",
"6 | Timeout height : no timeout",
"7 | Timeout timestamp : 2023-08-03T15:15:33.013842415Z",
"8 | Timestamp : 2023-08-03 14:15:33.013982445 UTC",
"9 | Pubkey [1/2] : 00a5f1ed44f01daedeef67994b25409d69045b",
"9 | Pubkey [2/2] : 49def08c927db56ea503ffce0eb0",
"10 | Epoch : 2",
"11 | Gas limit : 0",
"12 | Fees : NAM 100.0"
],
"valid": true
},
{
"blob": "1e0000006532652d746573742e3161313862653132353965653931326361666363380023000000323032332d30382d30335431343a31353a33372e3631373835393137322b30303a303054899b432b930a6de6b6adb1eb0caecdda6be456faaf63d4f832a1e9f77ca827e4945708279deb7fc54ea63337a854bc017ebbdf939287c5639127df3088706d0100e1f50500000000000000000000000000000000000000000000000000000000004b88fb913a0766e30a00b2fb8aa2949a710e24e6008cec0b9cf24b3264780e978bbdbec858f5a6b9b462053b8cf34ec880026d9a5b0200000000000000000000000000000000000000000000000000000000000000000000000000000001010b00000000000000a20000000000000002000000009109c1bb89010000080100000a292f6962632e6170706c69636174696f6e732e7472616e736665722e76312e4d73675472616e7366657212da010a087472616e7366657212096368616e6e656c2d301a590a5461746573743176346568677733367833707273777a786767756e7a76367078716d6e76646a39787663797a76707367676579767333636739716e797766353839716e77766673673565726733666b6c3039726735120130225461746573743176346568677733367876757976337a706739713532736535786763723233337378756d6e77777a726773637979736a397834706e78647065677375723273367067766d72327365346634787574392a06626572746861320038acfea6f2f6a4f9bb17029109c1bb8901000000f8056f465d17043d2864309d1c50a897a028b6ebf9702e5c403e1bf8c670f6e6",
"index": 3,
"name": "IBC_0",
"output": [
"0 | Type : IBC",
"1 | Source port : transfer",
"2 | Source channel : channel-0",
"3 | Token [1/3] : 0 atest1v4ehgw36x3prswzxggunzv6pxqmnvd",
"3 | Token [2/3] : j9xvcyzvpsggeyvs3cg9qnywf589qnwvfsg5er",
"3 | Token [3/3] : g3fkl09rg5",
"4 | Sender [1/3] : atest1v4ehgw36xvuyv3zpg9q52se5xgcr233s",
"4 | Sender [2/3] : xumnwwzrgscyysj9x4pnxdpegsur2s6pgvmr2s",
"4 | Sender [3/3] : e4f4xut9",
"5 | Receiver : bertha",
"6 | Timeout height : no timeout",
"7 | Timeout timestamp : 2023-08-03T14:15:42.617722668Z"
],
"output_expert": [
"0 | Code hash [1/2] : f8056f465d17043d2864309d1c50a897a028b6",
"0 | Code hash [2/2] : ebf9702e5c403e1bf8c670f6e6",
"1 | Source port : transfer",
"2 | Source channel : channel-0",
"3 | Token [1/3] : 0 atest1v4ehgw36x3prswzxggunzv6pxqmnvd",
"3 | Token [2/3] : j9xvcyzvpsggeyvs3cg9qnywf589qnwvfsg5er",
"3 | Token [3/3] : g3fkl09rg5",
"4 | Sender [1/3] : atest1v4ehgw36xvuyv3zpg9q52se5xgcr233s",
"4 | Sender [2/3] : xumnwwzrgscyysj9x4pnxdpegsur2s6pgvmr2s",
"4 | Sender [3/3] : e4f4xut9",
"5 | Receiver : bertha",
"6 | Timeout height : no timeout",
"7 | Timeout timestamp : 2023-08-03T14:15:42.617722668Z",
"8 | Timestamp : 2023-08-03 14:15:37.617859172 UTC",
"9 | Pubkey [1/2] : 008cec0b9cf24b3264780e978bbdbec858f5a6",
"9 | Pubkey [2/2] : b9b462053b8cf34ec880026d9a5b",
"10 | Epoch : 2",
"11 | Gas limit : 0",
"12 | Fees : NAM 100.0"
],
"valid": true
}
]
Loading

0 comments on commit 4d189e9

Please sign in to comment.