Aktualizr
C++ SOTA Client
uptane_ci_test.cc
1 #include <gtest/gtest.h>
2 
3 #include <fstream>
4 #include <iostream>
5 #include <memory>
6 
7 #include <boost/filesystem.hpp>
8 #include <boost/polymorphic_pointer_cast.hpp>
9 
10 #include "libaktualizr/packagemanagerfactory.h"
11 #include "libaktualizr/packagemanagerinterface.h"
12 
13 #include "http/httpclient.h"
14 #include "logging/logging.h"
15 #include "package_manager/ostreemanager.h"
16 #include "primary/reportqueue.h"
17 #include "primary/sotauptaneclient.h"
18 #include "storage/invstorage.h"
19 #include "uptane/uptanerepository.h"
20 #include "uptane_test_common.h"
21 #include "utilities/utils.h"
22 
23 boost::filesystem::path credentials;
24 boost::filesystem::path sysroot;
25 
26 TEST(UptaneCI, ProvisionAndPutManifest) {
27  // note: see tests/shared_cred_prov_test.py which tests the same
28  // functionality, with the full aktualizr binary
29  TemporaryDirectory temp_dir;
30  Config config("tests/config/minimal.toml");
31  config.provision.provision_path = credentials;
32  config.provision.mode = ProvisionMode::kSharedCredReuse;
33  config.storage.path = temp_dir.Path();
34  config.pacman.type = PACKAGE_MANAGER_NONE;
35  config.postUpdateValues(); // re-run copy of urls
36 
37  auto storage = INvStorage::newStorage(config.storage);
38  auto sota_client = std_::make_unique<UptaneTestCommon::TestUptaneClient>(config, storage);
39  EXPECT_NO_THROW(sota_client->initialize());
40  EXPECT_TRUE(sota_client->putManifestSimple());
41 }
42 
43 TEST(UptaneCI, CheckKeys) {
44  TemporaryDirectory temp_dir;
45  Config config("tests/config/minimal.toml");
46  config.provision.provision_path = credentials;
47  config.provision.mode = ProvisionMode::kSharedCredReuse;
48  config.storage.path = temp_dir.Path();
49  config.pacman.type = PACKAGE_MANAGER_OSTREE;
50  config.pacman.sysroot = sysroot;
51  config.postUpdateValues(); // re-run copy of urls
52  boost::filesystem::remove_all(config.storage.path);
53 
54  auto storage = INvStorage::newStorage(config.storage);
55 
56  UptaneTestCommon::addDefaultSecondary(config, temp_dir, "", "secondary_hardware");
57  auto sota_client = std_::make_unique<UptaneTestCommon::TestUptaneClient>(config, storage);
58  EXPECT_NO_THROW(sota_client->initialize());
59 
60  std::string ca;
61  std::string cert;
62  std::string pkey;
63  EXPECT_TRUE(storage->loadTlsCreds(&ca, &cert, &pkey));
64  EXPECT_TRUE(ca.size() > 0);
65  EXPECT_TRUE(cert.size() > 0);
66  EXPECT_TRUE(pkey.size() > 0);
67 
68  std::string primary_public;
69  std::string primary_private;
70  EXPECT_TRUE(storage->loadPrimaryKeys(&primary_public, &primary_private));
71  EXPECT_TRUE(primary_public.size() > 0);
72  EXPECT_TRUE(primary_private.size() > 0);
73 
74  std::map<Uptane::EcuSerial, std::shared_ptr<SecondaryInterface> >::iterator it;
75  for (it = sota_client->secondaries.begin(); it != sota_client->secondaries.end(); it++) {
76  std::shared_ptr<Primary::ManagedSecondary> managed_secondary =
77  std::dynamic_pointer_cast<Primary::ManagedSecondary>(it->second);
78  EXPECT_TRUE(managed_secondary);
79 
80  std::string public_key;
81  std::string private_key;
82  EXPECT_TRUE(managed_secondary->loadKeys(&public_key, &private_key));
83  EXPECT_TRUE(public_key.size() > 0);
84  EXPECT_TRUE(private_key.size() > 0);
85  EXPECT_NE(public_key, private_key);
86  }
87 }
88 
89 #ifndef __NO_MAIN__
90 int main(int argc, char **argv) {
91  ::testing::InitGoogleTest(&argc, argv);
92  logger_init();
93  logger_set_threshold(boost::log::trivial::trace);
94 
95  if (argc != 3) {
96  std::cerr << "Error: " << argv[0]
97  << " requires a path to a credentials archive and an OSTree sysroot as input arguments.\n";
98  exit(EXIT_FAILURE);
99  }
100  credentials = argv[1];
101  sysroot = argv[2];
102  return RUN_ALL_TESTS();
103 }
104 #endif
Config
Configuration object for an aktualizr instance running on a Primary ECU.
Definition: config.h:208
TemporaryDirectory
Definition: utils.h:82