[edk2-devel] Problems mocking UEFI Protocol in gMock
CrossedCarpet
crossedcarpet at hotmail.com
Thu Sep 14 15:22:14 UTC 2023
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/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listman.redhat.com/archives/edk2-devel-archive/attachments/20230914/82e4a0cd/attachment.htm>
More information about the edk2-devel-archive
mailing list