<p>Greetings,<br />I have successfully mocked standalone functions employing the <span style="font-family: 'comic sans ms';">FunctionMockLib</span> macros. <br />Nonetheless, I have been stuck in making progress regarding mocking a protocol.<br />The problem is that the function pointers of the protocol mock structure are not being properly initialized. <br />Below I try to illustrate with a made up UEFI protocol.</p>
<hr />
<p>This is the test I want to mock:</p>
<p><span style="font-family: 'comic sans ms';">// function that uses my mocked function</span></p>
<p><span style="font-family: 'comic sans ms';">INTN getCount() {</span></p>
<p><span style="font-family: 'comic sans ms';">// successfully mocked: CounterProtocol = mockCounterProtocol;</span></p>
<p><span style="font-family: 'comic sans ms';">gBS->LocateProtocol(&gCounterProtocol, (VOID **)&CounterProtocol);</span><br /><span style="font-family: 'comic sans ms';">return CounterProtocol->Count();</span></p>
<p><span style="font-family: 'comic sans ms';">}</span></p>
<p><span style="font-family: 'comic sans ms';">// gTest file</span></p>
<p><span style="font-family: 'comic sans ms';">TEST(getCountTest, returnCount) {</span></p>
<p><span style="font-family: 'comic sans ms';">  MockUefiBootServicesTable  mockBootServices;</span></p>
<p><span style="font-family: 'comic sans ms';">  MockCounterProtocol         mockCounterProtocol;</span></p>
<p><span style="font-family: 'comic sans ms';">// expect_call on locate_protocol, where I assign &mockCounterProtocol with the custom action macro, ACTION_P</span></p>
<p><span style="font-family: 'comic sans ms';">EXPECT_CALL (mockCounterProtocol, Count)</span><span style="font-family: 'comic sans ms';">.WillOnce (Return (42));</span></p>
<p><span style="font-family: 'comic sans ms';">getCount(); // seg fault, gdb shows CounterProtocol->Count points to NULL</span></p>
<p><span style="font-family: 'comic sans ms';">}</span></p>
<hr />
<p>My first attempt employed FunctionMockLib:<br /><br /><span style="font-family: 'comic sans ms';">// header file</span><br /><span style="font-family: 'comic sans ms';">struct MockCounterProtocol {</span><br /><span style="font-family: 'comic sans ms';">   MOCK_INTERFACE_DECLARATION(MockCounterProtocol);</span><br /><span style="font-family: 'comic sans ms';">   MOCK_FUNCTION_DECLARTION(INTN, Count, ());</span><br /><span style="font-family: 'comic sans ms';">}</span></p>
<p><span style="font-family: 'comic sans ms';">// source file</span></p>
<p><span style="font-family: 'comic sans ms';">MOCK_INTERFACE_DEFINITION (MockCounterProtocol)<br /></span><span style="font-family: 'comic sans ms';">MOCK_FUNCTION_DEFINITION (MockCounterProtocol, Count, 0, EFIAPI)</span></p>
<hr />
<div>No success, then I realized I probably needed to follow the usual C++ pattern for mocking a class:</div>
<div> </div>
<div><span style="font-family: 'comic sans ms';">// header file again</span></div>
<div><span style="font-family: 'comic sans ms';">struct MockCounterProtocol: CounterProtocolStruct {</span>
<div><span style="font-family: 'comic sans ms';">  MOCK_METHOD (INTN, Count, ());</span></div>
<div><span style="font-family: 'comic sans ms';">};</span></div>
<div><span style="font-family: 'comic sans ms';">// source file empty...</span></div>
<div><hr /></div>
<div>I have also tried setting the function pointer directly in the test but no success so far. </div>
<div>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.</div>
<div> </div>
<div>Regards,<br />C.C.</div>
</div>


<div width="1" style="color:white;clear:both">_._,_._,_</div>
<hr>


Groups.io Links:<p>


  
    You receive all messages sent to this group.
  
  


<p>
<a target="_blank" href="https://edk2.groups.io/g/devel/message/108649">View/Reply Online (#108649)</a> |


  

|

  <a target="_blank" href="https://groups.io/mt/101360319/1813853">Mute This Topic</a>


| <a href="https://edk2.groups.io/g/devel/post">New Topic</a>

<br>




<a href="https://edk2.groups.io/g/devel/editsub/1813853">Your Subscription</a> |
<a href="mailto:devel+owner@edk2.groups.io">Contact Group Owner</a> |

<a href="https://edk2.groups.io/g/devel/unsub">Unsubscribe</a>

 [edk2-devel-archive@redhat.com]<br>
<div width="1" style="color:white;clear:both">_._,_._,_</div>