1

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!

Dirk Herrmann
  • 5,550
  • 1
  • 21
  • 47
PrvnSrv
  • 11
  • 2
  • "Unit Testing of system calls in Linux" - Why do you want to test Linux system calls? Don't you trust that they work? Or do you want to help out with kernel development (if so, good for you)? – Jesper Juhl Aug 24 '18 at 13:50
  • @JesperJuhl I think OP wants to test _their_ code which calls system functions. – YSC Aug 24 '18 at 13:59
  • @YSC Probably. But then the question should *clearly* state *that*. There's ambiguity in the question and ambiguity in questions and programming is rarely a good thing. – Jesper Juhl Aug 24 '18 at 14:00
  • @JesperJuhl I'm more than convinced. – YSC Aug 24 '18 at 14:05
  • @JesperJuhl its not testing system calls, but just simulating them. It is used in my unit, so I want to mock them. – PrvnSrv Aug 24 '18 at 21:42
  • @YSC you are right. I want to test code which uses system calls and also I want to generate code coverage. – PrvnSrv Aug 24 '18 at 21:45

0 Answers0