Skip to content

Commit e0ad456

Browse files
Add test for security initialization error (#5550)
* Regression tests Signed-off-by: Eugenio Collado <[email protected]> * Update log macro Signed-off-by: Eugenio Collado <[email protected]> * Uncrustify Signed-off-by: Eugenio Collado <[email protected]> * Fix CI log flush Signed-off-by: Eugenio Collado <[email protected]> --------- Signed-off-by: Eugenio Collado <[email protected]>
1 parent f8ddcd4 commit e0ad456

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/cpp/rtps/security/SecurityManager.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,10 @@ bool SecurityManager::init(
397397
if (!e)
398398
{
399399
// Unexpected code path. Let's log any errors
400-
logError(SECURITY, "Error while configuring security plugin.")
400+
EPROSIMA_LOG_ERROR(SECURITY, "Error while configuring security plugin.");
401401
if (0 != strlen(exception.what()))
402402
{
403-
logError(SECURITY, exception.what())
403+
EPROSIMA_LOG_ERROR(SECURITY, exception.what());
404404
}
405405

406406
cancel_init();

test/unittest/rtps/security/SecurityInitializationTests.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "SecurityTests.hpp"
1616

17+
#include "../../logging/mock/MockConsumer.h"
18+
1719
const char* const MockIdentity::class_id_ = "MockIdentityHandle";
1820
const char* const MockHandshake::class_id_ = "MockHandshakeHandle";
1921
const char* const SharedSecret::class_id_ = "SharedSecretHandle";
@@ -208,3 +210,43 @@ TEST_F(SecurityTest, initialization_ok)
208210

209211
}
210212

213+
/* Regression test for Redmine 22545.
214+
*
215+
* Triggering a throw false in SecurityManager::init() should be logged properly as
216+
* the error: "Error while configuring security plugin.".
217+
*/
218+
TEST_F(SecurityTest, initialization_logging_error)
219+
{
220+
DefaultValue<const GUID_t&>::Set(guid);
221+
DefaultValue<const ParticipantSecurityAttributes&>::Set(security_attributes_);
222+
223+
EXPECT_CALL(*auth_plugin_, validate_local_identity(_, _, _, _, _, _)).Times(1).
224+
WillOnce(DoAll(SetArgPointee<0>(&local_identity_handle_), Return(ValidationResult_t::VALIDATION_OK)));
225+
EXPECT_CALL(crypto_plugin_->cryptokeyfactory_,
226+
register_local_participant(Ref(local_identity_handle_), _, _, _, _)).Times(1).
227+
WillOnce(Return(nullptr));
228+
229+
eprosima::fastdds::dds::MockConsumer* mockConsumer = new eprosima::fastdds::dds::MockConsumer();
230+
eprosima::fastdds::dds::Log::RegisterConsumer(std::unique_ptr<eprosima::fastdds::dds::LogConsumer>(mockConsumer));
231+
eprosima::fastdds::dds::Log::SetVerbosity(eprosima::fastdds::dds::Log::Error);
232+
233+
security_activated_ = manager_.init(security_attributes_, participant_properties_);
234+
235+
// Check that the error message was logged.
236+
// First flush the log to make sure the message is there.
237+
eprosima::fastdds::dds::Log::Flush();
238+
239+
auto log_entries = mockConsumer->ConsumedEntries();
240+
ASSERT_GE(log_entries.size(), 1);
241+
bool found = false;
242+
for (auto entry : log_entries)
243+
{
244+
if (entry.message.find("Error while configuring security plugin.") != std::string::npos)
245+
{
246+
found = true;
247+
break;
248+
}
249+
}
250+
ASSERT_TRUE(found);
251+
}
252+

0 commit comments

Comments
 (0)