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