Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 224
andrei Avatar asked Dec 14 '15 09:12

andrei


People also ask

Which command is used to check kernel modules?

modinfo command in Linux system is used to display the information about a Linux Kernel module. This command extracts the information from the Linux kernel modules given on the command line.

What is a kernel test?

kselftest is a testing framework available in the Linux source code at tools/testing/selftests, capable of testing specific parts of the kernel. The tests are written in C language or shell script and run in user space to test some specific part of the kernel.

How can I tell what is using a kernel module?

You can try lsmod | grep <module name> to see all loaded kernel modules that are using a module. You can also try dmesg | grep <module name> to see if the kernel logs have any clues as to which processes may be using a module. You may be able to remove the module using rmmod --force <module_name> .


1 Answers

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
like image 191
Claudio Avatar answered Oct 12 '22 23:10

Claudio