Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log any errors before cancel_init() (backport #5530) #5546

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/cpp/rtps/security/SecurityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ bool SecurityManager::init(
ParticipantSecurityAttributes& attributes,
const PropertyPolicy& participant_properties)
{
SecurityException exception;
try
{
SecurityException exception;
domain_id_ = participant_->get_domain_id();
const PropertyPolicy log_properties = PropertyPolicyHelper::get_properties_with_prefix(
participant_->getRTPSParticipantAttributes().properties,
Expand Down Expand Up @@ -383,6 +383,13 @@ bool SecurityManager::init(
{
if (!e)
{
// Unexpected code path. Let's log any errors
EPROSIMA_LOG_ERROR(SECURITY, "Error while configuring security plugin.");
if (0 != strlen(exception.what()))
{
EPROSIMA_LOG_ERROR(SECURITY, exception.what());
}

cancel_init();
return false;
}
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);
}

Loading