public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [edk2-devel] Problems mocking UEFI Protocol in gMock
@ 2023-09-14 15:22 CrossedCarpet
  2023-09-22 15:04 ` CrossedCarpet
  0 siblings, 1 reply; 3+ messages in thread
From: CrossedCarpet @ 2023-09-14 15:22 UTC (permalink / raw)
  To: devel

[-- Attachment #1: Type: text/plain, Size: 2278 bytes --]

Greetings,
I have successfully mocked standalone functions employing the FunctionMockLib macros.
Nonetheless, I have been stuck in making progress regarding mocking a protocol.
The problem is that the function pointers of the protocol mock structure are not being properly initialized.
Below I try to illustrate with a made up UEFI protocol.

This is the test I want to mock:

// function that uses my mocked function

INTN getCount() {

// successfully mocked: CounterProtocol = mockCounterProtocol;

gBS->LocateProtocol(&gCounterProtocol, (VOID **)&CounterProtocol);
return CounterProtocol->Count();

}

// gTest file

TEST(getCountTest, returnCount) {

MockUefiBootServicesTable  mockBootServices;

MockCounterProtocol         mockCounterProtocol;

// expect_call on locate_protocol, where I assign &mockCounterProtocol with the custom action macro, ACTION_P

EXPECT_CALL (mockCounterProtocol, Count).WillOnce (Return (42));

getCount(); // seg fault, gdb shows CounterProtocol->Count points to NULL

}

My first attempt employed FunctionMockLib:

// header file
struct MockCounterProtocol {
MOCK_INTERFACE_DECLARATION(MockCounterProtocol);
MOCK_FUNCTION_DECLARTION(INTN, Count, ());
}

// source file

MOCK_INTERFACE_DEFINITION (MockCounterProtocol)
MOCK_FUNCTION_DEFINITION (MockCounterProtocol, Count, 0, EFIAPI)

No success, then I realized I probably needed to follow the usual C++ pattern for mocking a class:

// header file again
struct MockCounterProtocol: CounterProtocolStruct {
MOCK_METHOD (INTN, Count, ());
};
// source file empty...

I have also tried setting the function pointer directly in the test but no success so far.
I believe the solution might be simple, as it always is, but since I have been around this for days (weeks if I include setting up mocking in general) your help is greatly appreciated.

Regards,
C.C.


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#108649): https://edk2.groups.io/g/devel/message/108649
Mute This Topic: https://groups.io/mt/101360319/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



[-- Attachment #2: Type: text/html, Size: 4605 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-09-29 16:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-14 15:22 [edk2-devel] Problems mocking UEFI Protocol in gMock CrossedCarpet
2023-09-22 15:04 ` CrossedCarpet
2023-09-29 16:47   ` CrossedCarpet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox