1 #include <gtest/gtest.h> 11 #include "config/config.h" 12 #include "http/httpclient.h" 13 #include "logging/logging.h" 14 #include "primary/sotauptaneclient.h" 15 #include "storage/invstorage.h" 16 #include "utilities/utils.h" 22 VectorWrapper(Json::Value vector) : vector_(std::move(vector)) {}
25 auto me = [
this, &e](
const std::string r) {
26 if (vector_[r][
"update"][
"err_msg"].asString() == e.what()) {
29 const Json::Value& targets = vector_[r][
"targets"];
30 for (Json::Value::const_iterator it = targets.begin(); it != targets.end(); it++) {
31 if ((*it)[
"err_msg"].asString() == e.what()) {
37 if (me(
"director") || me(
"image_repo")) {
40 std::cout <<
"aktualizr failed with unmatched exception " <<
typeid(e).name() <<
": " << e.what() <<
"\n";
41 std::cout <<
"Expected error: " << vector_ <<
"\n";
46 bool should_fail =
false;
47 if (!vector_[
"director"][
"update"][
"is_success"].asBool() ||
48 !vector_[
"image_repo"][
"update"][
"is_success"].asBool()) {
51 for (
const auto& t : vector_[
"director"][
"targets"]) {
52 if (!t[
"is_success"].asBool()) {
57 for (
const auto& t : vector_[
"image_repo"][
"targets"]) {
58 if (!t[
"is_success"].asBool()) {
67 void printExpectedFailure() {
68 std::cout <<
"No exceptions occurred, but expected ";
69 if (!vector_[
"director"][
"update"][
"is_success"].asBool()) {
70 std::cout <<
"exception from director: '" << vector_[
"director"][
"update"][
"err"]
71 <<
" with message: " << vector_[
"director"][
"update"][
"err_msg"] <<
"\n";
72 }
else if (!vector_[
"image_repo"][
"update"][
"is_success"].asBool()) {
73 std::cout <<
"exception from image_repo: '" << vector_[
"image_repo"][
"update"][
"err"]
74 <<
" with message: " << vector_[
"image_repo"][
"update"][
"err_msg"] <<
"\n";
76 std::cout <<
"an exception while fetching Targets metadata.\n";
84 class UptaneVector :
public ::testing::TestWithParam<std::string> {};
93 const std::string test_name = GetParam();
94 std::cout <<
"Running test vector " << test_name <<
"\n";
98 config.provision.primary_ecu_serial =
"test_primary_ecu_serial";
99 config.provision.primary_ecu_hardware_id =
"test_primary_hardware_id";
100 config.uptane.director_server = address + test_name +
"/director";
101 config.uptane.repo_server = address + test_name +
"/image_repo";
102 config.storage.path = temp_dir.Path();
103 config.storage.uptane_metadata_path =
BasedPath(temp_dir.Path() /
"metadata");
104 config.pacman.type = PACKAGE_MANAGER_NONE;
105 logger_set_threshold(boost::log::trivial::trace);
107 auto storage = INvStorage::newStorage(config.storage);
108 auto uptane_client = std_::make_unique<SotaUptaneClient>(config, storage);
111 uptane_client->primary_ecu_serial_ = ecu_serial;
112 uptane_client->primary_ecu_hw_id_ = hw_id;
113 Uptane::EcuMap ecu_map{{ecu_serial, hw_id}};
114 Uptane::Target target(
"test_filename", ecu_map, {{Hash::Type::kSha256,
"sha256"}}, 1,
"");
115 storage->saveInstalledVersion(ecu_serial.ToString(), target, InstalledVersionUpdateMode::kCurrent);
119 HttpResponse response = http_client.post(address + test_name +
"/step", Json::Value());
120 if (response.http_status_code == 204) {
123 const auto vector_json(response.getJson());
124 std::cout <<
"VECTOR: " << vector_json;
127 bool should_fail = vector.shouldFail();
138 uptane_client->uptaneIteration(
nullptr,
nullptr);
141 if (updates.status == result::UpdateStatus::kError) {
142 ASSERT_TRUE(should_fail) <<
"checkUpdates unexpectedly failed.";
143 if (uptane_client->getLastException() !=
nullptr) {
144 std::rethrow_exception(uptane_client->getLastException());
147 if (updates.ecus_count > 0) {
151 if (result.status != result::DownloadStatus::kSuccess) {
152 ASSERT_TRUE(should_fail) <<
"downloadImages unexpectedly failed.";
153 if (uptane_client->getLastException() !=
nullptr) {
154 std::rethrow_exception(uptane_client->getLastException());
160 ASSERT_TRUE(vector.matchError(e)) <<
"libaktualizr threw a different exception than expected!";
162 }
catch (
const std::exception& e) {
163 FAIL() <<
"libaktualizr failed with unrecognized exception " <<
typeid(e).name() <<
": " << e.what();
167 vector.printExpectedFailure();
171 FAIL() <<
"Step sequence unexpectedly aborted.";
174 std::vector<std::string> GetVectors() {
176 const Json::Value json_vectors = http_client.get(address, HttpInterface::kNoLimit).getJson();
177 std::vector<std::string> vectors;
178 for (Json::ValueConstIterator it = json_vectors.begin(); it != json_vectors.end(); it++) {
179 vectors.emplace_back((*it).asString());
184 INSTANTIATE_TEST_SUITE_P(UptaneVectorSuite,
UptaneVector, ::testing::ValuesIn(GetVectors()));
186 int main(
int argc,
char* argv[]) {
188 logger_set_threshold(boost::log::trivial::trace);
191 std::cerr <<
"This program is intended to be run from run_vector_tests.sh!\n";
198 const std::string port = argv[1];
199 address =
"http://localhost:" + port +
"/";
201 ::testing::InitGoogleTest(&argc, argv);
202 return RUN_ALL_TESTS();
Container for information about downloading an update.
Configuration object for an aktualizr instance running on a Primary ECU.
Container for information about available updates.
Results of libaktualizr API calls.