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 if (vector_[
"director"][
"update"][
"err_msg"].asString() == e.what() ||
26 vector_[
"director"][
"targets"][e.getName()][
"err_msg"].asString() == e.what() ||
27 vector_[
"image_repo"][
"update"][
"err_msg"].asString() == e.what() ||
28 vector_[
"image_repo"][
"targets"][e.getName()][
"err_msg"].asString() == e.what()) {
31 std::cout <<
"aktualizr failed with unmatched exception " <<
typeid(e).name() <<
": " << e.what() <<
"\n";
32 std::cout <<
"Expected error: " << vector_ <<
"\n";
37 bool should_fail =
false;
38 if (!vector_[
"director"][
"update"][
"is_success"].asBool() ||
39 !vector_[
"image_repo"][
"update"][
"is_success"].asBool()) {
42 for (
const auto& t : vector_[
"director"][
"targets"]) {
43 if (!t[
"is_success"].asBool()) {
48 for (
const auto& t : vector_[
"image_repo"][
"targets"]) {
49 if (!t[
"is_success"].asBool()) {
58 void printExpectedFailure() {
59 std::cout <<
"No exceptions occurred, but expected ";
60 if (!vector_[
"director"][
"update"][
"is_success"].asBool()) {
61 std::cout <<
"exception from director: '" << vector_[
"director"][
"update"][
"err"]
62 <<
" with message: " << vector_[
"director"][
"update"][
"err_msg"] <<
"\n";
63 }
else if (!vector_[
"image_repo"][
"update"][
"is_success"].asBool()) {
64 std::cout <<
"exception from image_repo: '" << vector_[
"image_repo"][
"update"][
"err"]
65 <<
" with message: " << vector_[
"image_repo"][
"update"][
"err_msg"] <<
"\n";
67 std::cout <<
"an exception while fetching targets metadata.\n";
75 class UptaneVector :
public ::testing::TestWithParam<std::string> {};
84 const std::string test_name = GetParam();
85 std::cout <<
"Running test vector " << test_name <<
"\n";
89 config.provision.primary_ecu_serial =
"test_primary_ecu_serial";
90 config.provision.primary_ecu_hardware_id =
"test_primary_hardware_id";
91 config.uptane.director_server = address + test_name +
"/director";
92 config.uptane.repo_server = address + test_name +
"/image_repo";
93 config.storage.path = temp_dir.Path();
94 config.storage.uptane_metadata_path =
BasedPath(temp_dir.Path() /
"metadata");
95 config.pacman.type = PackageManager::kNone;
96 logger_set_threshold(boost::log::trivial::trace);
98 auto storage = INvStorage::newStorage(config.storage);
99 auto uptane_client = std_::make_unique<SotaUptaneClient>(config, storage);
102 uptane_client->hw_ids.insert(std::make_pair(ecu_serial, hw_id));
103 uptane_client->primary_ecu_serial_ = ecu_serial;
104 Uptane::EcuMap ecu_map{{ecu_serial, hw_id}};
105 Uptane::Target target(
"test_filename", ecu_map, {{Uptane::Hash::Type::kSha256,
"sha256"}}, 1,
"");
106 storage->saveInstalledVersion(ecu_serial.ToString(), target, InstalledVersionUpdateMode::kCurrent);
110 HttpResponse response = http_client.post(address + test_name +
"/step", Json::Value());
111 if (response.http_status_code == 204) {
114 const auto vector_json(response.getJson());
115 std::cout <<
"VECTOR: " << vector_json;
118 bool should_fail = vector.shouldFail();
129 if (!uptane_client->uptaneIteration(
nullptr,
nullptr)) {
130 ASSERT_TRUE(should_fail) <<
"uptaneIteration unexpectedly failed.";
131 throw uptane_client->getLastException();
134 if (updates.status == result::UpdateStatus::kError) {
135 ASSERT_TRUE(should_fail) <<
"checkUpdates unexpectedly failed.";
136 throw uptane_client->getLastException();
138 if (updates.ecus_count > 0) {
142 if (
result.status != result::DownloadStatus::kSuccess) {
143 ASSERT_TRUE(should_fail) <<
"downloadImages unexpectedly failed.";
144 throw uptane_client->getLastException();
149 ASSERT_TRUE(vector.matchError(e)) <<
"libaktualizr threw a different exception than expected!";
151 }
catch (
const std::exception& e) {
152 FAIL() <<
"libaktualizr failed with unrecognized exception " <<
typeid(e).name() <<
": " << e.what();
156 vector.printExpectedFailure();
160 FAIL() <<
"Step sequence unexpectedly aborted.";
163 std::vector<std::string> GetVectors() {
165 const Json::Value json_vectors = http_client.get(address, HttpInterface::kNoLimit).getJson();
166 std::vector<std::string> vectors;
167 for (Json::ValueConstIterator it = json_vectors.begin(); it != json_vectors.end(); it++) {
168 vectors.emplace_back((*it).asString());
173 INSTANTIATE_TEST_SUITE_P(UptaneVectorSuite,
UptaneVector, ::testing::ValuesIn(GetVectors()));
175 int main(
int argc,
char* argv[]) {
177 logger_set_threshold(boost::log::trivial::trace);
180 std::cerr <<
"This program is intended to be run from run_vector_tests.sh!\n";
187 const std::string port = argv[1];
188 address =
"http://127.0.0.1:" + port +
"/";
190 ::testing::InitGoogleTest(&argc, argv);
191 return RUN_ALL_TESTS();