forked from eq-lab/app-substrate-common
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathui_tests.cpp
165 lines (132 loc) · 4.81 KB
/
ui_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*******************************************************************************
* (c) 2019 Zondax GmbH
*
* 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 <iostream>
#include <fstream>
#include <json/json.h>
#include <hexutils.h>
#include <parser_txdef.h>
#include "parser.h"
#include "app_mode.h"
#include "utils/common.h"
using ::testing::TestWithParam;
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 JsonTestsA : 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();
}
};
};
class JsonTestsB : 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();
}
};
};
// 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;
parser_error_t err;
uint8_t buffer[5000];
uint16_t bufferLen = parseHexString(buffer, sizeof(buffer), tc.blob.c_str());
parser_tx_t tx_obj;
err = parser_parse(&ctx, buffer, bufferLen, &tx_obj);
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;
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]));
}
}
}
INSTANTIATE_TEST_SUITE_P
(
JsonTestCasesCurrentTxVer,
JsonTestsA,
::testing::ValuesIn(GetJsonTestCases("testcases_current.json")),
JsonTestsA::PrintToStringParamName()
);
INSTANTIATE_TEST_SUITE_P
(
JsonTestCasesPreviousTxVer,
JsonTestsB,
::testing::ValuesIn(GetJsonTestCases("testcases_previous.json")),
JsonTestsB::PrintToStringParamName()
);
// Parametric test using current runtime:
TEST_P(JsonTestsA, CheckUIOutput_CurrentTX_Normal) { check_testcase(GetParam(), false); }
TEST_P(JsonTestsA, CheckUIOutput_CurrentTX_Expert) { check_testcase(GetParam(), true); }
// Parametric test using previous runtime:
TEST_P(JsonTestsB, CheckUIOutput_PreviousTX_Normal) { check_testcase(GetParam(), false); }
TEST_P(JsonTestsB, CheckUIOutput_PreviousTX_Expert) { check_testcase(GetParam(), true); }