From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 9BD557803D8 for ; Thu, 14 Sep 2023 15:22:16 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=7NznCaTEB6PBcbYHW7443oPc5LGXgFYlWU1uNRQIQN8=; c=relaxed/simple; d=groups.io; h=Subject:To:From:User-Agent:MIME-Version:Date:Message-ID:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Type; s=20140610; t=1694704935; v=1; b=JqQ6YtkKCVAmDytlnptst3UD3hCYTQncyCYsOcXBBvS80X41adO0lY9JhWIdRItizVPQvMfs ruiqRqlLJLky283yCAS2LGkwYiX9iUmbggiPbN+OTgs94ruTxZlNFyDo2ONa5LSe9cTEKrjleg9 mUkAPVtkBDH7pCIfsPCcyseU= X-Received: by 127.0.0.2 with SMTP id xxtXYY7687511x2yuvgWrW0T; Thu, 14 Sep 2023 08:22:15 -0700 Subject: [edk2-devel] Problems mocking UEFI Protocol in gMock To: devel@edk2.groups.io From: "CrossedCarpet" X-Originating-Location: PT (94.46.24.37) X-Originating-Platform: Linux Chrome 116 User-Agent: GROUPS.IO Web Poster MIME-Version: 1.0 Date: Thu, 14 Sep 2023 08:22:14 -0700 Message-ID: Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,crossedcarpet@hotmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: CWQgwUG2wC1ruE5QIk4r2iCax7686176AA= Content-Type: multipart/alternative; boundary="yfS7IXz2AQHdiF6WLmdM" X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=JqQ6YtkK; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=hotmail.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io --yfS7IXz2AQHdiF6WLmdM Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Greetings, I have successfully mocked standalone functions employing the FunctionMockL= ib macros. Nonetheless, I have been stuck in making progress regarding=C2=A0mocking a = protocol. The problem is that the function pointers of the protocol mock structure ar= e 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 =3D mockCounterProtocol; gBS->LocateProtocol(&gCounterProtocol, (VOID **)&CounterProtocol); return CounterProtocol->Count(); } // gTest file TEST(getCountTest, returnCount) { MockUefiBootServicesTable=C2=A0 mockBootServices; MockCounterProtocol=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0mockCounterProtocol; // 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=C2=A0CounterProtocol->Count points to N= ULL } 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++ patte= rn 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 b= een around this for days (weeks if I include setting up mocking in general)= your help is greatly appreciated. Regards, C.C. -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- 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] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- --yfS7IXz2AQHdiF6WLmdM Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable

Greetings,
I have successfully mocked standalone functions employin= g the FunctionMockLib macros.
Nonetheless, I have been stuck in making progress regard= ing 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 m= ocked function

INTN getCount() {

// successfully mocked: Co= unterProtocol =3D mockCounterProtocol;

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

}

// gTest file

TEST(getCountTest, returnC= ount) {

  MockUefiBootService= sTable  mockBootServices;

  MockCounterProtocol=          mockCounterProtocol;

// expect_call on locate_p= rotocol, where I assign &mockCounterProtocol with the custom action mac= ro, ACTION_P

EXPECT_CALL (mockCounterPr= otocol, Count).WillOnc= e (Return (42));

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

}


My first attempt employed FunctionMockLib:

// header file
struct MockCounterProtocol {
   MOCK_INTERFACE_DECLARATI= ON(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 MockCounterProtoc= ol: CounterProtocolStruct {
  MOCK_METHOD (INTN= , Count, ());
};
// source file empty...<= /span>

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

Groups.io Links:

=20 You receive all messages sent to this group. =20 =20

View/Reply Online (#108649) | =20 | Mute= This Topic | New Topic
Your Subscriptio= n | Contact Group Owner | Unsubscribe [rebecca@openfw.io]

_._,_._,_
--yfS7IXz2AQHdiF6WLmdM--