andrei
andrei

Reputation: 8582

Testing a kernel module

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.

Upvotes: 11

Views: 3104

Answers (1)

Claudio
Claudio

Reputation: 10947

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

Upvotes: 7

Related Questions