1
1
#include < gtest/gtest.h>
2
2
3
- #include < stdio.h>
4
- #include < unistd.h>
5
- #include < cstdlib>
6
3
#include < iostream>
7
4
#include < memory>
8
5
#include < string>
15
12
#include " storage/invstorage.h"
16
13
#include " utilities/utils.h"
17
14
18
- std::string address;
15
+ using std::string;
16
+
17
+ string address; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
18
+ string tests_path; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
19
19
20
20
class VectorWrapper {
21
21
public:
22
- VectorWrapper (Json::Value vector) : vector_(std::move(vector)) {}
22
+ explicit VectorWrapper (Json::Value vector) : vector_(std::move(vector)) {}
23
23
24
24
bool matchError (const Uptane::Exception& e) {
25
- auto me = [this , &e](const std:: string r) {
25
+ auto me = [this , &e](const string r) {
26
26
if (vector_[r][" update" ][" err_msg" ].asString () == e.what ()) {
27
27
return true ;
28
28
}
@@ -81,7 +81,26 @@ class VectorWrapper {
81
81
Json::Value vector_;
82
82
};
83
83
84
- class UptaneVector : public ::testing::TestWithParam<std::string> {};
84
+ class UptaneVector : public ::testing::TestWithParam<string> {};
85
+
86
+ class HttpWrapper : public HttpClient {
87
+ public:
88
+ HttpResponse post (const string& url, const string& content_type, const string& data) override {
89
+ if (url.find (" /devices" ) != string::npos) {
90
+ LOG_TRACE << " HttpWrapper intercepting device registration" ;
91
+ return {Utils::readFile (tests_path + " /test_data/cred.p12" ), 200 , CURLE_OK, " " };
92
+ }
93
+
94
+ if (url.find (" /director/ecus" ) != string::npos) {
95
+ LOG_TRACE << " HttpWrapper intercepting Uptane ECU registration" ;
96
+ return {" " , 200 , CURLE_OK, " " };
97
+ }
98
+
99
+ LOG_TRACE << " HttpWrapper letting " << url << " pass" ;
100
+ return HttpClient::post (url, content_type, data);
101
+ }
102
+ HttpResponse post (const string& url, const Json::Value& data) override { return HttpClient::post (url, data); }
103
+ };
85
104
86
105
/* *
87
106
* Check that aktualizr fails on expired metadata.
@@ -90,23 +109,27 @@ class UptaneVector : public ::testing::TestWithParam<std::string> {};
90
109
* RecordProperty("zephyr_key", "REQ-153,TST-52");
91
110
*/
92
111
TEST_P (UptaneVector, Test) {
93
- const std:: string test_name = GetParam ();
112
+ const string test_name = GetParam ();
94
113
std::cout << " Running test vector " << test_name << " \n " ;
95
114
96
115
TemporaryDirectory temp_dir;
97
116
Config config;
98
117
config.provision .primary_ecu_serial = " test_primary_ecu_serial" ;
99
118
config.provision .primary_ecu_hardware_id = " test_primary_hardware_id" ;
119
+ config.provision .provision_path = tests_path + " /test_data/cred.zip" ;
120
+ config.provision .mode = ProvisionMode::kSharedCredReuse ;
100
121
config.uptane .director_server = address + test_name + " /director" ;
101
122
config.uptane .repo_server = address + test_name + " /image_repo" ;
102
123
config.storage .path = temp_dir.Path ();
103
124
config.storage .uptane_metadata_path = utils::BasedPath (temp_dir.Path () / " metadata" );
104
125
config.pacman .images_path = temp_dir.Path () / " images" ;
105
126
config.pacman .type = PACKAGE_MANAGER_NONE;
127
+ config.postUpdateValues ();
106
128
logger_set_threshold (boost::log ::trivial::trace);
107
129
108
130
auto storage = INvStorage::newStorage (config.storage );
109
- auto uptane_client = std_::make_unique<SotaUptaneClient>(config, storage);
131
+ auto http_client = std::make_shared<HttpWrapper>();
132
+ auto uptane_client = std_::make_unique<SotaUptaneClient>(config, storage, http_client, nullptr );
110
133
auto ecu_serial = uptane_client->provisioner_ .PrimaryEcuSerial ();
111
134
auto hw_id = uptane_client->provisioner_ .PrimaryHardwareIdentifier ();
112
135
EXPECT_EQ (ecu_serial.ToString (), config.provision .primary_ecu_serial );
@@ -115,9 +138,10 @@ TEST_P(UptaneVector, Test) {
115
138
Uptane::Target target (" test_filename" , ecu_map, {{Hash::Type::kSha256 , " sha256" }}, 1 , " " );
116
139
storage->saveInstalledVersion (ecu_serial.ToString (), target, InstalledVersionUpdateMode::kCurrent );
117
140
118
- HttpClient http_client;
141
+ uptane_client->initialize ();
142
+ ASSERT_TRUE (uptane_client->attemptProvision ()) << " Provisioning Failed. Can't continue test" ;
119
143
while (true ) {
120
- HttpResponse response = http_client. post (address + test_name + " /step" , Json::Value ());
144
+ HttpResponse response = http_client-> post (address + test_name + " /step" , Json::Value ());
121
145
if (response.http_status_code == 204 ) {
122
146
return ;
123
147
}
@@ -172,10 +196,10 @@ TEST_P(UptaneVector, Test) {
172
196
FAIL () << " Step sequence unexpectedly aborted." ;
173
197
}
174
198
175
- std::vector<std:: string> GetVectors () {
199
+ std::vector<string> GetVectors () {
176
200
HttpClient http_client;
177
201
const Json::Value json_vectors = http_client.get (address, HttpInterface::kNoLimit ).getJson ();
178
- std::vector<std:: string> vectors;
202
+ std::vector<string> vectors;
179
203
for (Json::ValueConstIterator it = json_vectors.begin (); it != json_vectors.end (); it++) {
180
204
vectors.emplace_back ((*it).asString ());
181
205
}
@@ -188,17 +212,19 @@ int main(int argc, char* argv[]) {
188
212
logger_init ();
189
213
logger_set_threshold (boost::log ::trivial::trace);
190
214
191
- if (argc < 2 ) {
215
+ if (argc < 3 ) {
192
216
std::cerr << " This program is intended to be run from run_vector_tests.sh!\n " ;
193
217
return 1 ;
194
218
}
195
219
196
220
/* Use ports to distinguish both the server connection and local storage so
197
221
* that parallel runs of this code don't cause problems that are difficult to
198
222
* debug. */
199
- const std:: string port = argv[1 ];
223
+ const string port = argv[1 ];
200
224
address = " http://localhost:" + port + " /" ;
201
225
226
+ tests_path = argv[2 ];
227
+
202
228
::testing::InitGoogleTest (&argc, argv);
203
229
return RUN_ALL_TESTS ();
204
230
}
0 commit comments