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

* Re: [edk2-devel] Problems mocking UEFI Protocol in gMock
  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
  0 siblings, 1 reply; 3+ messages in thread
From: CrossedCarpet @ 2023-09-22 15:04 UTC (permalink / raw)
  To: CrossedCarpet, devel

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

Greetings,
A week long collaboration between me and Gpt yielded this functioning mock of a sample UEFI Protocol:

struct SampleProtocol {
INTN    (*GetVersion)(
);
};

struct MockSampleProtocol : public SampleProtocol {
static MockSampleProtocol    *instance;

MockSampleProtocol (
)
{
// Assign the instance pointer
instance = this;

// Point the function pointer in TestStruct to the static method
GetVersion = StaticGetVersion;
}

// Define a static method that forwards the call to the mock method
static INTN
StaticGetVersion (
)
{
return instance->MockGetVersion ();
}

MOCK_METHOD (INTN, MockGetVersion, ());
};

MockSampleProtocol  *MockSampleProtocol::instance = nullptr;

TEST (SampleProtocolTest, SampleTest) {
MockSampleProtocol  mockSampleProtocol;
SampleProtocol      *sampleProtocol;

sampleProtocol = &mockSampleProtocol;
EXPECT_CALL (mockSampleProtocol, MockGetVersion)
.WillOnce (Return (42));

ASSERT_EQ (42, sampleProtocol->GetVersion ());
}

In your opinion, is this an appropriate approach?
I suppose that mocking a UEFI protocol is something common, so I wonder if this w ould be worth documenting?

Best Regards,
Me & Gpt


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#109013): https://edk2.groups.io/g/devel/message/109013
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: 5776 bytes --]

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

* Re: [edk2-devel] Problems mocking UEFI Protocol in gMock
  2023-09-22 15:04 ` CrossedCarpet
@ 2023-09-29 16:47   ` CrossedCarpet
  0 siblings, 0 replies; 3+ messages in thread
From: CrossedCarpet @ 2023-09-29 16:47 UTC (permalink / raw)
  To: CrossedCarpet, devel

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

Greetings,
A week long collaboration between me and GDB, figuring out how to reproduce my CounterProtocol with function arguments, made me reach the conclusion that mocking a UEFI protocol is indeed documented and the solution had to do with copying the style of a global table Mock.

This is the relevant piece of documentation: https://fuchsia.googlesource.com/third_party/edk2/+/refs/heads/main/UnitTestFrameworkPkg/#functionmocklib-mocks-declaration

And this would be my Sample Protocol:

// header file
struct MockCounterProtocol: CounterProtocol {
MOCK_INTERFACE_DECLARATION(MockCounterProtocol);
MOCK_FUNCTION_DECLARTION(INTN, CounterProtocol_Count, ());
}

extern "C" {
extern CounterProtocol *gCounterProtocol;
}

// source file

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

static CounterProtocol localCounterProtocol = {
current_count
};

extern "C" {
CounterProtocol *gCounterProtocol = &localCounterProtocol;
}

Best Regards,
Me & Gdb


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#109213): https://edk2.groups.io/g/devel/message/109213
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: 2692 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