I am new to unit testing in C++. I am using googletest for performing unit testing(and gcovr for coverage report) of the sources which contains linux system calls. To test them I thought of 2 approaches
1. Using **LD_PRELOAD (by stubbing own implementation of system calls such as write,read etc.)**
2. Using --wrap linker options to wrap actual system call with my stub implementation.
Using option 2 is little infeasible as the number of system calls used are more (open,write,read,fseek,socket and so on), hence I thought of using Method 1.
However, I am not able to generate coverage report using Method 1 (I do compile my stub library with --coverage options). It is getting locked when I see strace. I don't know how to solve this problem. So I thought of keeping system calls instead create dummy device files and work on them. (for example: /sys/devices/system/cpu1/online --> /home/user/dummy/cpu/online).
Now, I want to simulate a error condition from write system call to this file, but it does not seem to work (I already set for example O_NONBLOCK flag of the dummy file using fcntl function).
I need suggestions on
1. Using method 1, how to generate coverage report?
2. If this is not possible, how to set the same properties of the dummy file as device file.
Just a snapshot of my code: openstub.c
#include <sys/types.h>
#include <sys/stat.h>
int open(const char *pathname, int flags, mode_t mode)
{
if(mode ==1)
{
return 1;
}
else
{
return 0;
}
}
usingopen.c
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
char dummypath[]="dummypath";
int filehandle;
filehandle = open(dummypath,O_WRONLY);
if(0>filehandle)
printf("i am NOT using stubs\n");
else if(filehandle ==1) {
printf("I am using stub\n");
}
else
{
printf("I am using stub with Not the WRITE mode\n");
}
}
compile command:
gcc -shared -fPIC -ftest-coverage -fprofile-arcs openstub.c -o openstub.so
for usingopen
gcc -ftest-coverage -fprofile-arcs -o usingopen usingopen.c
gcovr -f usingopen
(this gets stuck after LD_PRELOAD set to openstub.so)
Thank you for your suggestions!