Skip to content

Commit

Permalink
testing: add testing abilities for commandline parsing
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
MegaMaddin committed Aug 26, 2020
1 parent d0186dc commit 5485458
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 :,, $*))"
10 changes: 10 additions & 0 deletions grub/boot/grub/grub-test.cfg
Original file line number Diff line number Diff line change
@@ -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
}
49 changes: 49 additions & 0 deletions tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,62 @@
#include <ktf.h>
#include <sched.h>

#ifdef KTF_UNIT_TEST
#include <cmdline.h>
#include <string.h>

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) {
printk("\nTest:\n");

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");
Expand Down

0 comments on commit 5485458

Please sign in to comment.