0

I'm writing a library that calls recvmsg, and now I'm writing tests for that library. I want to test the error paths: the code that runs when recvmsg returns an error. I could mock out recvmsg but if it's possible I would prefer to test the real code as much as I can.

Is there a way to induce recvmsg to return an error? For my current use case I don't care which error, as I handle all errors the same, but being able to induce particular errors would be a nice-to-have.

In case it matters, the socket I'm receiving from is a UDP socket.

Drew
  • 12,578
  • 11
  • 58
  • 98
  • How are you calling it in the test? Would simply `EBADF` from -1 as the socket work? – Artyer Jun 03 '20 at 23:02
  • 1
    passing an invalid socket fd, like `-1`? – geza Jun 03 '20 at 23:02
  • Ah that actually seems like it might work. I can friend the test to allow it to modify the `_fd` member variable of my objects, then when they later call `recvmsg` it will fail. Thanks! – Drew Jun 03 '20 at 23:24
  • Another way to make `recvmsg()` fail, without messing with the `_fd`, is to pass it a null `msghdr*`, or pass it invalid `flags`. That may be easier for your test to simulate than making the test be a `friend` of your library. – Remy Lebeau Jun 04 '20 at 00:40

0 Answers0