Skip to content

Commit

Permalink
Add test for security initialization error (#5550)
Browse files Browse the repository at this point in the history
* Regression tests

Signed-off-by: Eugenio Collado <eugeniocollado@eprosima.com>

* Update log macro

Signed-off-by: Eugenio Collado <eugeniocollado@eprosima.com>

* Uncrustify

Signed-off-by: Eugenio Collado <eugeniocollado@eprosima.com>

* Fix CI log flush

Signed-off-by: Eugenio Collado <eugeniocollado@eprosima.com>

---------

Signed-off-by: Eugenio Collado <eugeniocollado@eprosima.com>
  • Loading branch information
EugenioCollado committed Jan 13, 2025
1 parent f8ddcd4 commit e0ad456
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/cpp/rtps/security/SecurityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,10 @@ bool SecurityManager::init(
if (!e)
{
// Unexpected code path. Let's log any errors
logError(SECURITY, "Error while configuring security plugin.")
EPROSIMA_LOG_ERROR(SECURITY, "Error while configuring security plugin.");
if (0 != strlen(exception.what()))
{
logError(SECURITY, exception.what())
EPROSIMA_LOG_ERROR(SECURITY, exception.what());
}

cancel_init();
Expand Down
42 changes: 42 additions & 0 deletions test/unittest/rtps/security/SecurityInitializationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "SecurityTests.hpp"

#include "../../logging/mock/MockConsumer.h"

const char* const MockIdentity::class_id_ = "MockIdentityHandle";
const char* const MockHandshake::class_id_ = "MockHandshakeHandle";
const char* const SharedSecret::class_id_ = "SharedSecretHandle";
Expand Down Expand Up @@ -208,3 +210,43 @@ TEST_F(SecurityTest, initialization_ok)

}

/* Regression test for Redmine 22545.
*
* Triggering a throw false in SecurityManager::init() should be logged properly as
* the error: "Error while configuring security plugin.".
*/
TEST_F(SecurityTest, initialization_logging_error)
{
DefaultValue<const GUID_t&>::Set(guid);
DefaultValue<const ParticipantSecurityAttributes&>::Set(security_attributes_);

EXPECT_CALL(*auth_plugin_, validate_local_identity(_, _, _, _, _, _)).Times(1).
WillOnce(DoAll(SetArgPointee<0>(&local_identity_handle_), Return(ValidationResult_t::VALIDATION_OK)));
EXPECT_CALL(crypto_plugin_->cryptokeyfactory_,
register_local_participant(Ref(local_identity_handle_), _, _, _, _)).Times(1).
WillOnce(Return(nullptr));

eprosima::fastdds::dds::MockConsumer* mockConsumer = new eprosima::fastdds::dds::MockConsumer();
eprosima::fastdds::dds::Log::RegisterConsumer(std::unique_ptr<eprosima::fastdds::dds::LogConsumer>(mockConsumer));
eprosima::fastdds::dds::Log::SetVerbosity(eprosima::fastdds::dds::Log::Error);

security_activated_ = manager_.init(security_attributes_, participant_properties_);

// Check that the error message was logged.
// First flush the log to make sure the message is there.
eprosima::fastdds::dds::Log::Flush();

auto log_entries = mockConsumer->ConsumedEntries();
ASSERT_GE(log_entries.size(), 1);
bool found = false;
for (auto entry : log_entries)
{
if (entry.message.find("Error while configuring security plugin.") != std::string::npos)
{
found = true;
break;
}
}
ASSERT_TRUE(found);
}

0 comments on commit e0ad456

Please sign in to comment.