From 54854584751a1f9dd68ae1245af62f6733b5951a Mon Sep 17 00:00:00 2001 From: Martin Mazein Date: Thu, 20 Aug 2020 14:24:50 +0200 Subject: [PATCH] testing: add testing abilities for commandline parsing Adding some IFDEFs for testing the commandline parsing implementation. This could be extendted for further testing and can be triggered via: UNITTEST=1 make $target Signed-off-by: Martin Mazein --- Makefile | 12 +++++++-- grub/boot/grub/grub-test.cfg | 10 ++++++++ tests/test.c | 49 ++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 grub/boot/grub/grub-test.cfg diff --git a/Makefile b/Makefile index 934af27e..43b051f7 100644 --- a/Makefile +++ b/Makefile @@ -21,11 +21,19 @@ LD := ld GRUB_FILE := grub-file GRUB_MKIMAGE := grub-mkimage GRUB_MODULES := multiboot iso9660 biosdisk +ifneq ($(UNITTEST),) +GRUB_CONFIG := grub/boot/grub/grub-test.cfg +else +GRUB_CONFIG := grub/boot/grub/grub.cfg +endif XORRISO := xorriso QEMU_BIN := qemu-system-x86_64 GDB := gdb COMMON_FLAGS := -I$(ROOT)/include -I$(ROOT)/include/arch/x86 -pipe -MP -MMD -m64 -D__x86_64__ +ifneq ($(UNITTEST),) +COMMON_FLAGS += -DKTF_UNIT_TEST +endif AFLAGS := $(COMMON_FLAGS) -D__ASSEMBLY__ -nostdlib -nostdinc CFLAGS := $(COMMON_FLAGS) -std=gnu99 -O3 -g -Wall -Wextra -ffreestanding @@ -107,7 +115,7 @@ $(ISO_FILE): all @echo "GEN ISO" $(ISO_FILE) @ $(GRUB_FILE) --is-x86-multiboot $(TARGET) || { echo "Multiboot not supported"; exit 1; } @ cp $(TARGET) grub/boot/ - @ $(GRUB_MKIMAGE) --format i386-pc-eltorito -p /boot/grub -o grub/boot.img $(GRUB_MODULES) + @ $(GRUB_MKIMAGE) --format i386-pc-eltorito -c $(GRUB_CONFIG) -p /boot/grub -o grub/boot.img $(GRUB_MODULES) @ $(XORRISO) -as mkisofs -U -b boot.img -no-emul-boot -boot-load-size 4 -boot-info-table -o $(ISO_FILE) grub 2>> /dev/null endif @@ -160,4 +168,4 @@ dockerimage: .PHONY: docker% docker%: dockerimage @echo "running target '$(strip $(subst :,, $*))' in docker" - @ docker run -it -v $(PWD):$(PWD) -w $(PWD) $(DOCKERIMAGE) bash -c "make -j $(strip $(subst :,, $*))" + @ docker run -it -e UNITTEST=$(UNITTEST) -v $(PWD):$(PWD) -w $(PWD) $(DOCKERIMAGE) bash -c "make -j $(strip $(subst :,, $*))" diff --git a/grub/boot/grub/grub-test.cfg b/grub/boot/grub/grub-test.cfg new file mode 100644 index 00000000..1386fa46 --- /dev/null +++ b/grub/boot/grub/grub-test.cfg @@ -0,0 +1,10 @@ +set timeout=0 +set default=0 +serial --speed=115200 --word=8 --parity=no --stop=1 +terminal_input --append serial +terminal_output --append serial + +menuentry "kernel64" { + multiboot /boot/kernel64.bin integer=42 boolean=1 string=foo booleantwo + boot +} diff --git a/tests/test.c b/tests/test.c index 7b2141a4..2832c2aa 100644 --- a/tests/test.c +++ b/tests/test.c @@ -26,6 +26,25 @@ #include #include +#ifdef KTF_UNIT_TEST +#include +#include + +extern char *kernel_cmdline; + +static char opt_string[4]; +string_cmd("string", opt_string); + +static unsigned long opt_ulong; +ulong_cmd("integer", opt_ulong); + +static bool opt_bool = 0; +bool_cmd("boolean", opt_bool); + +static bool opt_booltwo = 0; +bool_cmd("booleantwo", opt_booltwo); +#endif + static int __user_text func(void *arg) { return 0; } void test_main(void) { @@ -33,6 +52,36 @@ void test_main(void) { usermode_call(func, NULL); +#ifdef KTF_UNIT_TEST + printk("\nLet the UNITTESTs begin\n"); + printk("Commandline parsing: %s\n", kernel_cmdline); + + if (strcmp(opt_string, "foo")) { + printk("String parameter opt_string != foo: %s\n", opt_string); + BUG(); + } + else { + printk("String parameter parsing works!\n"); + } + + if (opt_ulong != 42) { + printk("Integer parameter opt_ulong != 42: %d\n", opt_ulong); + BUG(); + } + else { + printk("Integer parameter parsing works!\n"); + } + + if (!opt_bool || !opt_booltwo) { + printk("Boolean parameter opt_bool != true: %d\n", opt_bool); + printk("Boolean parameter opt_booltwo != true: %d\n", opt_booltwo); + BUG(); + } + else { + printk("Boolean parameter parsing works!\n"); + } +#endif + wait_for_all_tasks(); printk("Test done\n");