Reputation:
I am in the process of implementing using testing for some C program. For this purpose I am using GCC's -Wl,--wrap=open
in order to mock stdlib's open()
function and to check that it has been called with the correct options.
When doing so however gcov is having some issues in writing its .gcda
file. I guess the mock I define is not only used by my tests but also by gcov. Here is a small example of how to reproduce this:
#include <stdio.h>
int __wrap_open(const char *path, int flags, int mode)
{
printf("hello from __wrap_open\n");
return -1;
}
int main(void)
{
return 0;
}
And compile it with gcc main.c -Wl,--wrap=open -fprofile-arcs -ftest-coverage -lgcov
. In order to keep the example simple I have removed the unit testing part using CMocka in order to show the bug I am encountering.
When running the executable a.out
I have the following output with GCC and gcov 6.3.0:
$ ./a.out
hello from __wrap_open
hello from __wrap_open
profiling:/home/romain/wrap-bug/main.gcda:Cannot open
Is there a way to mock the open()
function for my unit testing purposes and to be able to use gcov in order to generate code coverage data? Maybe there is a way to tell gcov to use __real_open()
?
Upvotes: 4
Views: 1490
Reputation: 18430
An easy way to accomplish that would be:
open()
is your profiling file__real_open()
i.e.
int __real_open(const char *path, int flags, int mode);
int __wrap_open(const char *path, int flags, int mode)
{
if (strlen(path) > 5 && !strcmp(path + strlen(path) - 5, ".gcda"))
return __real_open(path, flags, mode);
printf("hello from __wrap_open\n");
return -1;
}
Using this way you can of course add more files, which should not be tampered with, easily.
Upvotes: 2