From bac71e5ea031b171d44f531b2df35415a04b1486 Mon Sep 17 00:00:00 2001 From: Waleed Judah Date: Sat, 7 Jun 2025 06:47:21 -0400 Subject: [PATCH] Add convenient test target and script improvements --- Makefile | 4 ++++ README.md | 4 ++-- tests/module_test.sh | 10 ++++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 122030f..e4f3a5a 100644 --- a/Makefile +++ b/Makefile @@ -50,3 +50,7 @@ clear: # Target to view the kernel log buffer view: dmesg + +# Run module tests (requires root privileges) +test: all + sudo bash tests/module_test.sh diff --git a/README.md b/README.md index 791cbf4..b198e37 100644 --- a/README.md +++ b/README.md @@ -58,9 +58,9 @@ To begin your kernel module development journey, you'll need: ``` 5. **Run Tests**: - After building, you can execute the provided test script (requires root privileges) to automatically insert and remove the sample modules: + After building, you can run the provided test script using the Makefile. This step requires root privileges and will automatically insert and remove the sample modules: ```bash - sudo bash tests/module_test.sh + make test ``` ### Usage Tips diff --git a/tests/module_test.sh b/tests/module_test.sh index fc1b6d8..50ac75e 100755 --- a/tests/module_test.sh +++ b/tests/module_test.sh @@ -1,6 +1,12 @@ #!/bin/bash set -e +# Ensure the script is executed with root privileges +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root." >&2 + exit 1 +fi + BUILD_DIR="$(dirname "$0")/../build" MODULES=(kernel_birthday_list_module kernel_timer_module kernel_workqueue_module) @@ -11,8 +17,8 @@ for mod in "${MODULES[@]}"; do exit 1 fi echo "Inserting $mod_ko" - sudo insmod "$mod_ko" + insmod "$mod_ko" echo "Removing $mod" - sudo rmmod "$mod" + rmmod "$mod" echo done