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