@@ -38,11 +38,14 @@ using namespace score::mw::lifecycle::internal::osal;
3838alignas (IpcCommsSync) static char g_fake_ipc_storage[sizeof (IpcCommsSync)];
3939
4040// Helper to create a fake IPC structure with specified properties
41- void * CreateFakeIpcComms (pid_t pid)
41+ void * CreateFakeIpcComms (
42+ std::uint32_t magic = IpcCommsSync::kMagic ,
43+ std::uint32_t version = IpcCommsSync::kLayoutVersion )
4244{
4345 std::memset (g_fake_ipc_storage, 0 , sizeof (IpcCommsSync));
4446 IpcCommsSync* fake_comms = reinterpret_cast <IpcCommsSync*>(g_fake_ipc_storage);
45- fake_comms->pid_ = pid;
47+ fake_comms->magic_ = magic;
48+ fake_comms->version_ = version;
4649
4750 return g_fake_ipc_storage;
4851}
@@ -90,15 +93,12 @@ class ReportRunningImplTest : public ::testing::Test
9093 // By default, close succeeds
9194 EXPECT_CALL (*g_syscall_mock, close (_)).WillRepeatedly (Return (0 ));
9295
93- // By default, getpid returns current PID
94- EXPECT_CALL (*g_syscall_mock, getpid ()).WillRepeatedly (Return (::getpid ()));
95-
9696 // By default, mmap succeeds and returns valid IPC structure
9797 EXPECT_CALL (*g_syscall_mock, mmap (_, _, _, _, _, _))
9898 .WillRepeatedly (Invoke ([](void *, size_t length, int , int , int , off_t ) -> void * {
9999 if (length == sizeof (IpcCommsSync))
100100 {
101- return CreateFakeIpcComms (:: getpid () );
101+ return CreateFakeIpcComms ();
102102 }
103103 return MAP_FAILED ;
104104 }));
@@ -296,15 +296,47 @@ TEST_F(ReportRunningImplTest, GivenReportRunningImplInstance_ExceptGetCommsObjec
296296 EXPECT_EQ (result.error (), ExecErrc::kCommunicationError );
297297}
298298
299- TEST_F (ReportRunningImplTest, GivenWrongProcessId_WhenPidDoesNotMatch_ThenReturnsCommunicationError )
299+ TEST_F (ReportRunningImplTest, GivenWrongMagic_WhenReportRunningStateCalled_ThenReturnsCommunicationError )
300300{
301301 RecordProperty (" DerivationTechnique" , " explorative-testing" );
302302 RecordProperty (
303303 " Description" ,
304- " Given a ReportRunningImpl instace . "
305- " Expect a pid mismatch . "
304+ " Given a ReportRunningImpl instance . "
305+ " Expect the shared memory not to contain the Launch Manager magic number . "
306306 " When ReportRunningState() is called. "
307- " Then ReportRunningState() returns a kCommunicationError error. " );
307+ " Then ReportRunningState() returns a kCommunicationError error and does not post the semaphore. " );
308+
309+ // Given
310+ ReportRunningImpl impl;
311+
312+ // Expect
313+ EXPECT_CALL (*g_syscall_mock, mmap (_, _, _, _, _, _))
314+ .WillOnce ([](void *, size_t length, int , int , int , off_t ) -> void * {
315+ if (length == sizeof (IpcCommsSync))
316+ {
317+ return CreateFakeIpcComms (0U , IpcCommsSync::kLayoutVersion );
318+ }
319+ return MAP_FAILED ;
320+ });
321+ EXPECT_CALL (*semaphore_mock_, sem_post (_)).Times (0 );
322+
323+ // When
324+ auto result = impl.ReportRunningState ();
325+
326+ // Then
327+ EXPECT_FALSE (result.has_value ());
328+ EXPECT_EQ (result.error (), ExecErrc::kCommunicationError );
329+ }
330+
331+ TEST_F (ReportRunningImplTest, GivenWrongLayoutVersion_WhenReportRunningStateCalled_ThenReturnsCommunicationError)
332+ {
333+ RecordProperty (" DerivationTechnique" , " explorative-testing" );
334+ RecordProperty (
335+ " Description" ,
336+ " Given a ReportRunningImpl instance. "
337+ " Expect the shared memory to contain a different layout version. "
338+ " When ReportRunningState() is called. "
339+ " Then ReportRunningState() returns a kCommunicationError error and does not post the semaphore. " );
308340
309341 // Given
310342 ReportRunningImpl impl;
@@ -314,10 +346,11 @@ TEST_F(ReportRunningImplTest, GivenWrongProcessId_WhenPidDoesNotMatch_ThenReturn
314346 .WillOnce ([](void *, size_t length, int , int , int , off_t ) -> void * {
315347 if (length == sizeof (IpcCommsSync))
316348 {
317- return CreateFakeIpcComms (99999 );
349+ return CreateFakeIpcComms (IpcCommsSync:: kMagic , IpcCommsSync:: kLayoutVersion + 1U );
318350 }
319351 return MAP_FAILED ;
320352 });
353+ EXPECT_CALL (*semaphore_mock_, sem_post (_)).Times (0 );
321354
322355 // When
323356 auto result = impl.ReportRunningState ();
0 commit comments