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/21656
Group Owner: [email protected]
Unsubscribe: https://edk2.groups.io/g/devel/unsub [[email protected]]
-=-=-=-=-=-=-=-=-=-=-=-