Search code examples
cgoogletestgooglemock

how to return the error code when mocking a recvmsg function?


I am using google test and google mock to test some C code:

struct msghdr message;
int retval;

retval = recvmsg(fd,&message,0);

if(retval == EINTR || retval == EAGAIN)
{
    // do something here
}

in my mock file, I mocked the recvmsg function:

MOCK_CONST_METHOD3(recvmsg, int(int fd, struct msghdr *msg, int flags));

but how to set the errno as EINTR or EAGAIN when I set the return value of the mock function recvmsg as -1?


Solution

  • There is a standard action called SetErrnoAndReturn(errnum, value) which is described in Google Mock docs.