Aktualizr
C++ SOTA Client
target_mismatch_test.cc
1 #include <gtest/gtest.h>
2 
3 #include <string>
4 
5 #include "httpfake.h"
6 #include "libaktualizr/aktualizr.h"
7 #include "test_utils.h"
8 #include "uptane_test_common.h"
9 
10 boost::filesystem::path uptane_generator_path;
11 
12 /*
13  * Detect a mismatch in the Targets metadata from Director and Image repo.
14  */
15 TEST(Aktualizr, HardwareMismatch) {
16  TemporaryDirectory temp_dir;
17  TemporaryDirectory meta_dir;
18  auto http = std::make_shared<HttpFake>(temp_dir.Path(), "", meta_dir.Path() / "repo");
19  Config conf = UptaneTestCommon::makeTestConfig(temp_dir, http->tls_server);
20  logger_set_threshold(boost::log::trivial::trace);
21 
22  Process uptane_gen(uptane_generator_path.string());
23  uptane_gen.run({"generate", "--path", meta_dir.PathString(), "--correlationid", "abc123"});
24  // Note bad hardware ID in the image repo metadata. If we were to use an
25  // invalid value in the Director, it gets rejected even earlier.
26  uptane_gen.run({"image", "--path", meta_dir.PathString(), "--filename", "tests/test_data/firmware.txt",
27  "--targetname", "firmware.txt", "--hwid", "hw-mismatch", "--url", "test-url"});
28  uptane_gen.run({"addtarget", "--path", meta_dir.PathString(), "--targetname", "firmware.txt", "--hwid", "primary_hw",
29  "--serial", "CA:FE:A6:D2:84:9D"});
30  uptane_gen.run({"signtargets", "--path", meta_dir.PathString()});
31 
32  auto storage = INvStorage::newStorage(conf.storage);
33  UptaneTestCommon::TestAktualizr aktualizr(conf, storage, http);
34  aktualizr.Initialize();
35 
36  result::UpdateCheck update_result = aktualizr.CheckUpdates().get();
37  EXPECT_EQ(update_result.status, result::UpdateStatus::kError);
38  EXPECT_EQ(update_result.message, "Target mismatch.");
39 }
40 
41 #ifndef __NO_MAIN__
42 int main(int argc, char **argv) {
43  ::testing::InitGoogleTest(&argc, argv);
44  if (argc != 2) {
45  std::cerr << "Error: " << argv[0] << " requires the path to the uptane-generator utility\n";
46  return EXIT_FAILURE;
47  }
48  uptane_generator_path = argv[1];
49 
50  logger_init();
51  logger_set_threshold(boost::log::trivial::trace);
52 
53  return RUN_ALL_TESTS();
54 }
55 #endif
56 
57 // vim: set tabstop=2 shiftwidth=2 expandtab:
UptaneTestCommon::TestAktualizr
Definition: uptane_test_common.h:21
result::UpdateCheck
Container for information about available updates.
Definition: results.h:37
Config
Configuration object for an aktualizr instance running on a Primary ECU.
Definition: config.h:208
Aktualizr
This class provides the main APIs necessary for launching and controlling libaktualizr.
Definition: aktualizr.h:24
Process
Definition: test_utils.h:19
TemporaryDirectory
Definition: utils.h:82