11

I'm interested in mocking functions and global variables in order to unit test a kernel module.

Initially tried with https://github.com/ThrowTheSwitch/CMock but ran into issues. Any articles links on how to do this would also be great. (for kernel modules). To give more details here: compiling as a kernel module would error because stdio wouldn't be available, compiling for user space would error because it wouldn't find stuff like printk.

Ideally I would have either a user level executable or a kernel module that would run unit test on my functions. The parts I am having trouble with is mocking global dependencies like structs that the functions rely on in order to write a decent test.

I've gone through a few questions and articles about this but haven't found an answer, or a definitive reason on why this wouldn't be possible.

Community
  • 1
  • 1
andrei
  • 8,252
  • 14
  • 51
  • 66

1 Answers1

7

I would proceed as follows:

  • Implement your kernel module
  • Define an API to let a user-level program test your module, which can be based either on:
    • a character device in /dev/ (where you can define proper ioctls);
    • a file in /proc/ (discouraged);
    • specific system calls (discouraged);
    • an entry in /sys/
  • Implement at user-level a program (in case, using a proper framework like CUnit or googletest), which interacts with the kernel module testing the various functionalities
Claudio
  • 10,614
  • 4
  • 31
  • 71
  • I like the idea but it's difficult to put into practice for code that has already been written. – andrei Dec 14 '15 at 21:13