Skip to content
Advertisement

How to Mock Linux System Calls in C

When writing C modules that use linux system calls, what is the best practice to mock those calls in order to test the module? Are there maybe any libraries that offer mocks for linux system calls?

“my-module.h” – The interface description

JavaScript

“my-module.c” – The module to test.

JavaScript

“my-module.test.cpp” – The unit test the way I would expect this to work (I am using google unit test for example).

JavaScript

When I want to mock something that is no system call, I can just create an alternative implementation of the interface from the header file and compile a mocking implementation into the test. But how do I do this with system calls. Can I just reimplement (override) a function call? Should my module call a wrapper module for function calls?

Advertisement

Answer

In general, you want to create a wrapper for each system call you’re testing. A good way to do this and make your code easy to follow, would be just to use the ld option: --wrap. This lets you swap out symbols at build time without having to re-write your code to use your wrapper functions in place of the real libc functions.

Additionally, you could use LD_PRELOAD to override libc system call wrapper functions, like how rootkit/virus writers do, but there is more work involved than just using --wrap functionality.

Finally, you could utilize a unit test framework (i.e. CUnit) in your wrapper functions, simplifying your testing.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement