Skip to content

Commit

Permalink
Fix SecurityTest unittests memory error (#4750)
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>
  • Loading branch information
Mario-DL committed May 6, 2024
1 parent fc5e851 commit 28f8da9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 deletions.
28 changes: 16 additions & 12 deletions test/unittest/rtps/security/SecurityHandshakeProcessTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,13 @@ TEST_F(SecurityTest, discovered_participant_process_message_ok_begin_handshake_r
info.guid = participant_data.m_guid;
EXPECT_CALL(*participant_.getListener(), onParticipantAuthentication(_, info)).Times(1);

CacheChange_t* kx_change = new CacheChange_t(500);
expect_kx_exchange(kx_change);
CacheChange_t kx_change_to_add;
CacheChange_t* kx_change_to_remove = new CacheChange_t(500);
expect_kx_exchange(kx_change_to_add, kx_change_to_remove);

stateless_reader_->listener_->onNewCacheChangeAdded(stateless_reader_, change);

volatile_writer_->listener_->onWriterChangeReceivedByAll(volatile_writer_, kx_change);
volatile_writer_->listener_->onWriterChangeReceivedByAll(volatile_writer_, kx_change_to_remove);

return_handle(remote_identity_handle);
return_handle(handshake_handle);
Expand Down Expand Up @@ -528,14 +529,15 @@ TEST_F(SecurityTest, discovered_participant_process_message_pending_handshake_re
WillOnce(DoAll(SetArgPointee<0>(&remote_identity_handle),
Return(ValidationResult_t::VALIDATION_PENDING_HANDSHAKE_MESSAGE)));

CacheChange_t* kx_change = new CacheChange_t(500);
expect_kx_exchange(kx_change);
CacheChange_t kx_change_to_add;
CacheChange_t* kx_change_to_remove = new CacheChange_t(500);
expect_kx_exchange(kx_change_to_add, kx_change_to_remove);

ParticipantProxyData participant_data;
fill_participant_key(participant_data.m_guid);
ASSERT_TRUE(manager_.discovered_participant(participant_data));

volatile_writer_->listener_->onWriterChangeReceivedByAll(volatile_writer_, kx_change);
volatile_writer_->listener_->onWriterChangeReceivedByAll(volatile_writer_, kx_change_to_remove);

ParticipantGenericMessage message;
message.message_identity().source_guid(participant_data.m_guid);
Expand Down Expand Up @@ -733,12 +735,13 @@ TEST_F(SecurityTest, discovered_participant_process_message_ok_process_handshake
info.guid = remote_participant_key;
EXPECT_CALL(*participant_.getListener(), onParticipantAuthentication(_, info)).Times(1);

CacheChange_t* kx_change = new CacheChange_t(500);
expect_kx_exchange(kx_change);
CacheChange_t kx_change_to_add;
CacheChange_t* kx_change_to_remove = new CacheChange_t(500);
expect_kx_exchange(kx_change_to_add, kx_change_to_remove);

stateless_reader_->listener_->onNewCacheChangeAdded(stateless_reader_, change);

volatile_writer_->listener_->onWriterChangeReceivedByAll(volatile_writer_, kx_change);
volatile_writer_->listener_->onWriterChangeReceivedByAll(volatile_writer_, kx_change_to_remove);
}

TEST_F(SecurityTest, discovered_participant_process_message_process_handshake_reply_new_change_fail)
Expand Down Expand Up @@ -1132,12 +1135,13 @@ TEST_F(SecurityTest, discovered_participant_process_message_ok_process_handshake
info.guid = remote_participant_key;
EXPECT_CALL(*participant_.getListener(), onParticipantAuthentication(_, info)).Times(1);

CacheChange_t* kx_change = new CacheChange_t(500);
expect_kx_exchange(kx_change);
CacheChange_t kx_change_to_add;
CacheChange_t* kx_change_to_remove = new CacheChange_t(500);
expect_kx_exchange(kx_change_to_add, kx_change_to_remove);

stateless_reader_->listener_->onNewCacheChangeAdded(stateless_reader_, change);

volatile_writer_->listener_->onWriterChangeReceivedByAll(volatile_writer_, kx_change);
volatile_writer_->listener_->onWriterChangeReceivedByAll(volatile_writer_, kx_change_to_remove);
}

int main(
Expand Down
20 changes: 11 additions & 9 deletions test/unittest/rtps/security/SecurityTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,13 @@ void SecurityTest::final_message_process_ok(
info.guid = remote_participant_key;
EXPECT_CALL(*participant_.getListener(), onParticipantAuthentication(_, info)).Times(1);

CacheChange_t* kx_change = new CacheChange_t(200);
expect_kx_exchange(kx_change);
CacheChange_t kx_change_to_add;
CacheChange_t* kx_change_to_remove = new CacheChange_t(200);
expect_kx_exchange(kx_change_to_add, kx_change_to_remove);

stateless_reader_->listener_->onNewCacheChangeAdded(stateless_reader_, change);

volatile_writer_->listener_->onWriterChangeReceivedByAll(volatile_writer_, kx_change);
volatile_writer_->listener_->onWriterChangeReceivedByAll(volatile_writer_, kx_change_to_remove);

if (final_message_change == nullptr)
{
Expand All @@ -263,17 +264,18 @@ void SecurityTest::final_message_process_ok(
}

void SecurityTest::expect_kx_exchange(
CacheChange_t* kx_change)
CacheChange_t& kx_change_to_add,
CacheChange_t* kx_change_to_remove)
{
EXPECT_CALL(*volatile_writer_, new_change(_, _, _)).Times(1).WillOnce(
DoAll(Invoke([kx_change](const std::function<uint32_t()>& f, ChangeKind_t, InstanceHandle_t)
DoAll(Invoke([&kx_change_to_add](const std::function<uint32_t()>& f, ChangeKind_t, InstanceHandle_t)
{
kx_change->serializedPayload.reserve(f());
kx_change_to_add.serializedPayload.reserve(f());
}),
Return(kx_change)));
EXPECT_CALL(*volatile_writer_->history_, add_change_mock(kx_change)).Times(1).
Return(&kx_change_to_add)));
EXPECT_CALL(*volatile_writer_->history_, add_change_mock(&kx_change_to_add)).Times(1).
WillOnce(Return(true));
EXPECT_CALL(*volatile_writer_->history_, remove_change_mock(kx_change)).Times(1).
EXPECT_CALL(*volatile_writer_->history_, remove_change_mock(kx_change_to_remove)).Times(1).
WillOnce(Return(true));
}

Expand Down
3 changes: 2 additions & 1 deletion test/unittest/rtps/security/SecurityTests.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ class SecurityTest : public ::testing::Test
CacheChange_t** final_message_change = nullptr);

void expect_kx_exchange(
CacheChange_t* kx_change);
CacheChange_t& kx_change_to_add,
CacheChange_t* kx_change_to_remove);

void destroy_manager_and_change(
CacheChange_t*& change,
Expand Down
14 changes: 8 additions & 6 deletions test/unittest/rtps/security/SecurityValidationRemoteTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ TEST_F(SecurityTest, discovered_participant_validation_remote_identity_pending_h
info.guid = participant_data.m_guid;
EXPECT_CALL(*participant_.getListener(), onParticipantAuthentication(_, info)).Times(1);

CacheChange_t* kx_change = new CacheChange_t(500);
expect_kx_exchange(kx_change);
CacheChange_t kx_change_to_add;
CacheChange_t* kx_change_to_remove = new CacheChange_t(500);
expect_kx_exchange(kx_change_to_add, kx_change_to_remove);

ASSERT_TRUE(manager_.discovered_participant(participant_data));

volatile_writer_->listener_->onWriterChangeReceivedByAll(volatile_writer_, kx_change);
volatile_writer_->listener_->onWriterChangeReceivedByAll(volatile_writer_, kx_change_to_remove);

return_handle(remote_identity_handle);
return_handle(handshake_handle);
Expand Down Expand Up @@ -330,12 +331,13 @@ TEST_F(SecurityTest, discovered_participant_validation_remote_identity_pending_h
info.guid = participant_data.m_guid;
EXPECT_CALL(*participant_.getListener(), onParticipantAuthentication(_, info)).Times(1);

CacheChange_t* kx_change = new CacheChange_t(500);
expect_kx_exchange(kx_change);
CacheChange_t kx_change_to_add;
CacheChange_t* kx_change_to_remove = new CacheChange_t(500);
expect_kx_exchange(kx_change_to_add, kx_change_to_remove);

ASSERT_TRUE(manager_.discovered_participant(participant_data));

volatile_writer_->listener_->onWriterChangeReceivedByAll(volatile_writer_, kx_change);
volatile_writer_->listener_->onWriterChangeReceivedByAll(volatile_writer_, kx_change_to_remove);

destroy_manager_and_change(change);

Expand Down

0 comments on commit 28f8da9

Please sign in to comment.