Skip to content

Commit

Permalink
Adding Makefile tests for lkm_debugfs and little renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
tpiekarski committed Apr 10, 2020
1 parent e10e932 commit 3f4c103
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
28 changes: 26 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ test:
@$(MAKE) test-module name=lkm_process
@$(MAKE) test-module name=lkm_sandbox
@$(MAKE) test-module name=lkm_skeleton
@$(MAKE) test-debugfs
@$(MAKE) test-device
@$(MAKE) test-memory
@$(MAKE) test-parameters
Expand All @@ -57,7 +58,7 @@ test:
#

test-module:
$(info Testing module '$(name)' by loading and displaying Kernel Message Ring Buffer...)
$(info Testing module '$(name)' by loading and displaying Kernel Message Ring Buffer)
$(info Root permissions are needed for clearing buffer with dmesg and loading/unloading with insmod/rmmod)
$(call test_module_exists,$(name))
$(call test_module,$(name))
Expand All @@ -66,6 +67,29 @@ test-module:
# Targets for additional concept-based module tests
#

test-debugfs:
$(info Running additional tests for module 'lkm_debugfs' by loading, and testing both files in debugfs)
$(info Root permissions are needed for loading/unloading module with insmod/rmmod and accessing /sys/kernel/debug)

$(eval module = lkm_debugfs)
$(eval number_file = /sys/kernel/debug/lkm/number)
$(eval message_file = /sys/kernel/debug/lkm/message)
$(eval message = Hello, debugfs!)
$(eval number = 42)

$(call test_module_exists,$(module))
$(call load_module,$(module))

$(call test_file_exists,$(number_file),"-r", "sudo")
$(eval number_file_content = `sudo cat $(number_file)`)
$(call test_compare_values,$(number_file_content),"-eq",$(number))

$(eval message_file_content = `sudo cat $(message_file) | tr -d '\0'`)
$(call test_file_exists,$(message_file),"-r", "sudo")
$(call test_compare_values,"\"$(message_file_content)\"","=","\"$(message)\"")

@sudo rmmod $(module_filename)

test-device:
$(info Running additional tests for module 'lkm_device' by loading, accessing major device number in /proc and creating device)
$(info Root permissions are needed for loading/unloading module with insmod/rmmod and creating device with mknod)
Expand Down Expand Up @@ -117,7 +141,7 @@ test-parameters:
@sudo rmmod $(module)

test-proc:
$(info Running additional tests for 'lkm_proc' to access /proc filesystem by loading and cating '$(proc_file)'...)
$(info Running additional tests for 'lkm_proc' to access /proc filesystem by loading and cating '$(proc_file)')
$(info Root permissions are needed for loading/unloading with insmod/rmmod)

$(eval module = lkm_proc)
Expand Down
9 changes: 5 additions & 4 deletions lkm_debugfs.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* LKM Sandbox::DebugFS
* LKM Sandbox::Debug Filesystem
* <https://github.com/tpiekarski/lkm-sandbox>
* ---
* Copyright 2020 Thomas Piekarski <[email protected]>
Expand Down Expand Up @@ -45,7 +45,7 @@ static ssize_t debug_write(struct file *fp, const char *buffer, size_t count,
char content[LKM_DEBUGFS_CONTENT_LEN];
static struct dentry *debug_root;
static struct dentry *debug_file_entry;
static u64 value;
static u64 number;
static int file_value;

static const struct file_operations fops = { .owner = THIS_MODULE,
Expand Down Expand Up @@ -91,8 +91,9 @@ static int __init lkm_debugfs_init(void)
return -ENODEV;
}

value = 42;
debugfs_create_u64("value", LKM_DEBUGFS_PERMISSION, debug_root, &value);
number = 42;
debugfs_create_u64("number", LKM_DEBUGFS_PERMISSION, debug_root,
&number);

debug_file_entry =
debugfs_create_file("message", LKM_DEBUGFS_PERMISSION,
Expand Down
2 changes: 1 addition & 1 deletion tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ endef

define test_file_exists
@echo "Testing if file $(1) exists."
@test $(2) $(1) \
@$(3) test $(2) $(1) \
|| (echo " !! The file $(1) could not be found."; exit $(BUILD_ERROR))
endef

Expand Down

0 comments on commit 3f4c103

Please sign in to comment.