1 #include <gtest/gtest.h> 3 #include <netinet/tcp.h> 5 #include "aktualizr_secondary_interface.h" 6 #include "ipuptanesecondary.h" 7 #include "logging/logging.h" 8 #include "msg_dispatcher.h" 9 #include "secondary_tcp_server.h" 10 #include "test_utils.h" 16 : serial_(serial), hdw_id_(hdw_id), pub_key_(pub_key), manifest_(manifest), msg_dispatcher_{*
this} {}
22 PublicKey getPublicKey()
const {
return pub_key_; }
24 std::tuple<Uptane::EcuSerial, Uptane::HardwareIdentifier, PublicKey> getInfo()
const override {
25 return {serial_, hdw_id_, pub_key_};
31 metapack_ = meta_pack;
35 bool sendFirmware(
const std::string&
data)
override {
42 return data::ResultCode::Numeric::kOk;
59 return (lhs.director_root == rhs.director_root) && (lhs.image_root == rhs.image_root) &&
60 (lhs.director_targets == rhs.director_targets) && (lhs.image_snapshot == rhs.image_snapshot) &&
61 (lhs.image_timestamp == rhs.image_timestamp) && (lhs.image_targets == rhs.image_targets);
73 std::thread secondary_server_thread{[&secondary_server]() { secondary_server.
run(); }};
75 secondary_server.wait_until_running();
78 Uptane::SecondaryInterface::Ptr ip_secondary =
79 Uptane::IpUptaneSecondary::connectAndCreate(
"localhost", secondary_server.port());
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());
88 "image_targets",
"image_timestamp",
"image_snapshot"};
90 EXPECT_TRUE(ip_secondary->putMetadata(meta_pack));
91 EXPECT_TRUE(meta_pack == secondary.metapack_);
93 std::string firmware =
"firmware";
94 EXPECT_TRUE(ip_secondary->sendFirmware(firmware));
95 EXPECT_EQ(firmware, secondary.data_);
97 EXPECT_EQ(ip_secondary->install(
""), data::ResultCode::Numeric::kOk);
99 secondary_server.stop();
100 secondary_server_thread.join();
104 in_port_t secondary_port = TestUtils::getFreePortAsInt();
105 Uptane::SecondaryInterface::Ptr ip_secondary;
108 ip_secondary = Uptane::IpUptaneSecondary::connectAndCreate(
"localhost", secondary_port);
109 EXPECT_EQ(ip_secondary,
nullptr);
112 ip_secondary = std::make_shared<Uptane::IpUptaneSecondary>(
"localhost", secondary_port,
Uptane::EcuSerial(
"serial"),
117 "image_targets",
"image_timestamp",
"image_snapshot"};
120 EXPECT_EQ(ip_secondary->getManifest(), Json::Value());
121 EXPECT_FALSE(ip_secondary->sendFirmware(
"firmware"));
122 EXPECT_FALSE(ip_secondary->putMetadata(meta_pack));
129 : secondary_server_{msg_dispatcher_,
"", 0}, secondary_server_thread_{[&]() { secondary_server_.run(); }} {
130 msg_dispatcher_.registerHandler(AKIpUptaneMes_PR_installReq, [](
Asn1Message& in_msg,
Asn1Message& out_msg) {
132 out_msg.present(AKIpUptaneMes_PR_installResp).installResp()->result = AKInstallationResultCode_ok;
134 return MsgDispatcher::HandleStatusCode::kOk;
136 secondary_server_.wait_until_running();
140 secondary_server_.stop();
141 secondary_server_thread_.join();
144 AKIpUptaneMes_PR sendInstallMsg() {
147 req->present(AKIpUptaneMes_PR_installReq);
150 auto req_mes = req->installReq();
151 SetString(&req_mes->hash,
"target_name");
153 std::pair<std::string, uint16_t> secondary_server_addr{
"127.0.0.1", secondary_server_.port()};
154 auto resp = Asn1Rpc(req, secondary_server_addr);
156 return resp->present();
162 std::thread secondary_server_thread_;
179 ASSERT_EQ(sendInstallMsg(), AKIpUptaneMes_PR_installResp);
186 uint8_t garbage[] = {0x30, 0x13, 0x02, 0x01, 0x05, 0x16, 0x0e, 0x41, 0x6e, 0x79, 0x62,
187 0x6f, 0x64, 0x79, 0x20, 0x74, 0x68, 0x65, 0x72, 0x65, 0x3f};
188 send(*con_sock, garbage,
sizeof(garbage), 0);
192 ASSERT_EQ(sendInstallMsg(), AKIpUptaneMes_PR_installResp);
199 uint8_t garbage[] = {0x30, 0x99, 0x02, 0x01, 0x05, 0x16, 0x0e, 0x41, 0x6e, 0x79,
200 0x62, 0x6f, 0x64, 0x79, 0x20, 0x74, 0x68, 0x72, 0x65, 0x3f};
201 send(*con_sock, garbage,
sizeof(garbage), 0);
205 ASSERT_EQ(sendInstallMsg(), AKIpUptaneMes_PR_installResp);
212 uint8_t garbage[] =
"some garbage message";
213 send(*con_sock, garbage,
sizeof(garbage), 0);
217 ASSERT_EQ(sendInstallMsg(), AKIpUptaneMes_PR_installResp);
220 int main(
int argc,
char** argv) {
221 ::testing::InitGoogleTest(&argc, argv);
224 logger_set_threshold(boost::log::trivial::debug);
226 return RUN_ALL_TESTS();
void run()
Accept connections on the socket, decode requests and respond using the secondary implementation...
static Asn1Message::Ptr Empty()
Create a new Asn1Message, in order to fill it with data and send it.
Listens on a socket, decodes calls (ASN.1) and forwards them to an Uptane Secondary implementation...
Reference counted holder for the top-level ASN1 message structure.
SWM Internal integrity error.