Mocking side effects for file functions

146 views
Skip to first unread message

Bob McConnell

unread,
Nov 7, 2012, 3:04:44 PM11/7/12
to cmoc...@googlegroups.com
I am trying to mock several file functions from an embedded system. But I can't see any way to write back a binary structure from the read() function. The call has three parameters, a file handle, a pointer to the empty structure and the size of the structure. What do I use to write return data into that structure? (cmockery 0.1.2)

Stewart Miles

unread,
Nov 12, 2012, 11:34:43 AM11/12/12
to cmoc...@googlegroups.com
Assuming you're talking about:

ssize_t read(int fd, void *buf, size_t count);

You can mock this function with something like this, depending upon how strict you want to be:

ssize_t read(int fd, void *buf, size_t count) {
  void *return_buffer = mock();
  ssize_t return_buffer_size = mock();
  check_expected(fd);
  check_expected(count);
  if (return_buffer_size) {
    memcpy(buf, return_buffer, return_buffer_size);
  }
  return return_buffer_size;
}

your test case could look something like this:

void a_test(void **unused_data)
{
  static char output_buffer[8] = { 't', 'e', 's', 't', '1', '2', '3', '4' };
  expect_not_value(read, buf, -1);
  expect_value(count, 8);
  will_return(read, buffer);
  will_return(read, sizeof(buffer));
  a_function_that_ends_up_calling_read();
}






On Wed, Nov 7, 2012 at 12:04 PM, Bob McConnell <r...@cbord.com> wrote:
I am trying to mock several file functions from an embedded system. But I can't see any way to write back a binary structure from the read() function. The call has three parameters, a file handle, a pointer to the empty structure and the size of the structure. What do I use to write return data into that structure? (cmockery 0.1.2)


--
You received this message because you are subscribed to the Google Groups "Cmockery" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cmockery/-/LGVCyW968cIJ.
To post to this group, send email to cmoc...@googlegroups.com.
To unsubscribe from this group, send email to cmockery+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cmockery?hl=en.

Bob McConnell

unread,
Nov 12, 2012, 1:45:21 PM11/12/12
to cmoc...@googlegroups.com
Yes, that's the call I need to mock. I'll have to play with that a bit. Can I really just initialize an arbitrary structure and use will_return() to stuff it into the queue? That was not apparent from what I could read. Because the function being tested knows and uses elements of that structure, it has to be populated with appropriate values prior to putting it into the queue. Then rinse and repeat for several boundary values and error conditions.

Thank you,

Stewart Miles

unread,
Nov 12, 2012, 2:48:36 PM11/12/12
to cmoc...@googlegroups.com

You can add a value / pointer to anything in using will_return(). If you want to get fancy you could implement a fake filesystem if you want.

On Nov 12, 2012 10:45 AM, "Bob McConnell" <r...@cbord.com> wrote:
Yes, that's the call I need to mock. I'll have to play with that a bit. Can I really just initialize an arbitrary structure and use will_return() to stuff it into the queue? That was not apparent from what I could read. Because the function being tested knows and uses elements of that structure, it has to be populated with appropriate values prior to putting it into the queue. Then rinse and repeat for several boundary values and error conditions.

Thank you,

--
You received this message because you are subscribed to the Google Groups "Cmockery" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cmockery/-/ddGiLEBF62cJ.
Reply all
Reply to author
Forward
0 new messages