Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
secondary_tcp_server_test.cc
1 #include <gtest/gtest.h>
2 
3 #include "ipuptanesecondary.h"
4 #include "logging/logging.h"
5 #include "secondary_tcp_server.h"
6 #include "test_utils.h"
7 #include "uptane/secondaryinterface.h"
8 
10  public:
11  SecondaryMock(const Uptane::EcuSerial& serial, const Uptane::HardwareIdentifier& hdw_id, const PublicKey& pub_key,
12  const Uptane::Manifest& manifest)
13  : _serial(serial), _hdw_id(hdw_id), _pub_key(pub_key), _manifest(manifest) {}
14 
15  public:
16  virtual std::string Type() const { return "mock"; }
17  virtual Uptane::EcuSerial getSerial() const { return _serial; }
18  virtual Uptane::HardwareIdentifier getHwId() const { return _hdw_id; }
19  virtual PublicKey getPublicKey() const { return _pub_key; }
20  virtual Uptane::Manifest getManifest() const { return _manifest; }
21  virtual bool ping() const { return true; }
22  virtual bool putMetadata(const Uptane::RawMetaPack& meta_pack) {
23  _metapack = meta_pack;
24  return true;
25  }
26  virtual int32_t getRootVersion(bool director) const {
27  (void)director;
28  return 0;
29  }
30  virtual bool putRoot(const std::string& root, bool director) {
31  (void)root;
32  (void)director;
33  return true;
34  }
35 
36  virtual bool sendFirmware(const std::string& data) {
37  _data = data;
38  return true;
39  }
40 
41  virtual data::ResultCode::Numeric install(const std::string& target_name) {
42  (void)target_name;
43  return data::ResultCode::Numeric::kOk;
44  }
45 
46  public:
47  const Uptane::EcuSerial _serial;
48  const Uptane::HardwareIdentifier _hdw_id;
49  const PublicKey _pub_key;
50  const Uptane::Manifest _manifest;
51 
52  Uptane::RawMetaPack _metapack;
53  std::string _data;
54 };
55 
56 bool operator==(const Uptane::RawMetaPack& lhs, const Uptane::RawMetaPack& rhs) {
57  return (lhs.director_root == rhs.director_root) && (lhs.image_root == rhs.image_root) &&
58  (lhs.director_targets == rhs.director_targets) && (lhs.image_snapshot == rhs.image_snapshot) &&
59  (lhs.image_timestamp == rhs.image_timestamp) && (lhs.image_targets == rhs.image_targets);
60 }
61 
62 // Test the serialization/deserialization and the TCP/IP communication implementation
63 // that occurs during communication between Primary and IP Secondary
64 TEST(SecondaryTcpServer, TestIpSecondaryRPC) {
65  // secondary object on Secondary ECU
66  SecondaryMock secondary(Uptane::EcuSerial("serial"), Uptane::HardwareIdentifier("hardware-id"),
67  PublicKey("pub-key", KeyType::kED25519), Uptane::Manifest());
68 
69  // create Secondary on Secondary ECU, and run it in a dedicated thread
70  SecondaryTcpServer secondary_server(secondary, "", 0);
71  std::thread secondary_server_thread{[&secondary_server]() { secondary_server.run(); }};
72 
73  // create Secondary on Primary ECU, try it a few times since the secondary thread
74  // might not be ready at the moment of the first try
75  const int max_try = 5;
76  Uptane::SecondaryInterface::Ptr ip_secondary;
77  for (int ii = 0; ii < max_try && ip_secondary == nullptr; ++ii) {
78  ip_secondary = Uptane::IpUptaneSecondary::connectAndCreate("localhost", secondary_server.port());
79  }
80 
81  ASSERT_TRUE(ip_secondary != nullptr) << "Failed to create IP Secondary";
82  EXPECT_EQ(ip_secondary->getSerial(), secondary.getSerial());
83  EXPECT_EQ(ip_secondary->getHwId(), secondary.getHwId());
84  EXPECT_EQ(ip_secondary->getPublicKey(), secondary.getPublicKey());
85  EXPECT_EQ(ip_secondary->getManifest(), secondary.getManifest());
86 
87  Uptane::RawMetaPack meta_pack{"director-root", "director-target", "image_root",
88  "image_targets", "image_timestamp", "image_snapshot"};
89 
90  EXPECT_TRUE(ip_secondary->putMetadata(meta_pack));
91  EXPECT_TRUE(meta_pack == secondary._metapack);
92 
93  std::string firmware = "firmware";
94  EXPECT_TRUE(ip_secondary->sendFirmware(firmware));
95  EXPECT_EQ(firmware, secondary._data);
96 
97  EXPECT_EQ(ip_secondary->install(""), data::ResultCode::Numeric::kOk);
98 
99  secondary_server.stop();
100  secondary_server_thread.join();
101 }
102 
103 TEST(SecondaryTcpServer, TestIpSecondaryIfSecondaryIsNotRunning) {
104  in_port_t secondary_port = TestUtils::getFreePortAsInt();
105  Uptane::SecondaryInterface::Ptr ip_secondary;
106 
107  // trying to connect to a non-running Secondary and create a corresponding instance on Primary
108  ip_secondary = Uptane::IpUptaneSecondary::connectAndCreate("localhost", secondary_port);
109  EXPECT_EQ(ip_secondary, nullptr);
110 
111  // create Primary's secondary without connecting to Secondary
112  ip_secondary = std::make_shared<Uptane::IpUptaneSecondary>("localhost", secondary_port, Uptane::EcuSerial("serial"),
114  PublicKey("key", KeyType::kED25519));
115 
116  Uptane::RawMetaPack meta_pack{"director-root", "director-target", "image_root",
117  "image_targets", "image_timestamp", "image_snapshot"};
118 
119  // expect failures since the secondary is not running
120  EXPECT_EQ(ip_secondary->getManifest(), Json::Value());
121  EXPECT_FALSE(ip_secondary->sendFirmware("firmware"));
122  EXPECT_FALSE(ip_secondary->putMetadata(meta_pack));
123  EXPECT_EQ(ip_secondary->install(""), data::ResultCode::Numeric::kInternalError);
124 }
125 
126 int main(int argc, char** argv) {
127  ::testing::InitGoogleTest(&argc, argv);
128 
129  logger_init();
130  logger_set_threshold(boost::log::trivial::info);
131 
132  return RUN_ALL_TESTS();
133 }
SecondaryMock
Definition: secondary_tcp_server_test.cc:9
data
General data structures.
Definition: types.cc:55
Uptane::HardwareIdentifier
Definition: tuf.h:143
Uptane::RawMetaPack
Definition: tuf.h:535
Uptane::EcuSerial
Definition: tuf.h:174
SecondaryTcpServer
Listens on a socket, decodes calls (ASN.1) and forwards them to an Uptane Secondary implementation.
Definition: secondary_tcp_server.h:15
PublicKey
Definition: crypto.h:26
data::ResultCode::Numeric::kInternalError
SWM Internal integrity error.
data::ResultCode::Numeric
Numeric
Definition: types.h:128
Uptane::SecondaryInterface
Definition: secondaryinterface.h:12
Uptane::Manifest
Definition: manifest.h:13