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.
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.
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.
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> .
I would proceed as follows:
/dev/
(where you can define proper ioctls);/proc/
(discouraged);/sys/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With