1 #include <gtest/gtest.h>
11 #include "http/httpclient.h"
12 #include "libaktualizr/config.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 =
utils::BasedPath(temp_dir.Path() /
"metadata");
104 config.pacman.images_path = temp_dir.Path() /
"images";
105 config.pacman.type = PACKAGE_MANAGER_NONE;
106 logger_set_threshold(boost::log::trivial::trace);
108 auto storage = INvStorage::newStorage(config.storage);
109 auto uptane_client = std_::make_unique<SotaUptaneClient>(config, storage);
112 uptane_client->primary_ecu_serial_ = ecu_serial;
113 uptane_client->primary_ecu_hw_id_ = hw_id;
114 Uptane::EcuMap ecu_map{{ecu_serial, hw_id}};
115 Uptane::Target target(
"test_filename", ecu_map, {{Hash::Type::kSha256,
"sha256"}}, 1,
"");
116 storage->saveInstalledVersion(ecu_serial.ToString(), target, InstalledVersionUpdateMode::kCurrent);
120 HttpResponse response = http_client.post(address + test_name +
"/step", Json::Value());
121 if (response.http_status_code == 204) {
124 const auto vector_json(response.getJson());
125 std::cout <<
"VECTOR: " << vector_json;
128 bool should_fail = vector.shouldFail();
139 uptane_client->uptaneIteration(
nullptr,
nullptr);
142 if (updates.status == result::UpdateStatus::kError) {
143 ASSERT_TRUE(should_fail) <<
"checkUpdates unexpectedly failed.";
144 if (uptane_client->getLastException() !=
nullptr) {
145 std::rethrow_exception(uptane_client->getLastException());
148 if (updates.ecus_count > 0) {
152 if (
result.status != result::DownloadStatus::kSuccess) {
153 ASSERT_TRUE(should_fail) <<
"downloadImages unexpectedly failed.";
154 if (uptane_client->getLastException() !=
nullptr) {
155 std::rethrow_exception(uptane_client->getLastException());
161 ASSERT_TRUE(vector.matchError(e)) <<
"libaktualizr threw a different exception than expected!";
163 }
catch (
const std::exception& e) {
164 FAIL() <<
"libaktualizr failed with unrecognized exception " <<
typeid(e).name() <<
": " << e.what();
168 vector.printExpectedFailure();
172 FAIL() <<
"Step sequence unexpectedly aborted.";
175 std::vector<std::string> GetVectors() {
177 const Json::Value json_vectors = http_client.get(address, HttpInterface::kNoLimit).getJson();
178 std::vector<std::string> vectors;
179 for (Json::ValueConstIterator it = json_vectors.begin(); it != json_vectors.end(); it++) {
180 vectors.emplace_back((*it).asString());
185 INSTANTIATE_TEST_SUITE_P(UptaneVectorSuite,
UptaneVector, ::testing::ValuesIn(GetVectors()));
187 int main(
int argc,
char* argv[]) {
189 logger_set_threshold(boost::log::trivial::trace);
192 std::cerr <<
"This program is intended to be run from run_vector_tests.sh!\n";
199 const std::string port = argv[1];
200 address =
"http://localhost:" + port +
"/";
202 ::testing::InitGoogleTest(&argc, argv);
203 return RUN_ALL_TESTS();