From d42853496fc976ef3d067af421a1a3811660033d Mon Sep 17 00:00:00 2001 From: kartofen Date: Sun, 30 Apr 2023 13:21:44 +0300 Subject: i am getting tired of the makefile --- Makefile | 61 +++++++++++++++++++++++++++-------------- src/daemon/main.c | 2 ++ src/include/test-util.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ src/include/test-util.sh | 53 ++++++++++++++++++++++++++++++++++++ src/initramfs-init.sh | 2 +- src/libs/testing-library | 53 ------------------------------------ src/tests/test1.sh | 4 +-- src/tests/test2.c | 13 +++++++++ vm.sh | 16 +++++------ 9 files changed, 190 insertions(+), 85 deletions(-) create mode 100644 src/include/test-util.h create mode 100755 src/include/test-util.sh delete mode 100755 src/libs/testing-library create mode 100644 src/tests/test2.c diff --git a/Makefile b/Makefile index 544ff31..64ec306 100644 --- a/Makefile +++ b/Makefile @@ -1,62 +1,83 @@ CC := gcc -CFLAGS = -std=c99 -Wall -Wextra -Wpedantic -g + +ifeq (PROD,1) +CFLAGS := -std=c99 -O2 # production flags +else +CFLAGS := -std=c99 -Wall -Wextra -Wpedantic -g -DDEBUG # debug flags +endif + +# compile as static for the virtual machine +ifeq (vm,$(filter vm,$(MAKECMDGOALS))) +SFLAGS += -static +endif SRCD := src OBJD := obj BIND := bin -FILES = $(shell find $(SRCD)/$(SUBD) -type f) +DEPD := include +TSTD := tests +CFLAGS += -I$(SRCD)/$(DEPD) +# all the subdirs of the source dir (except DEPD and TSTD) +DIRS := $(filter-out $(DEPD) $(TSTD),$(shell find $(SRCD) -mindepth 1 -maxdepth 1 -type d -exec basename {} \;)) + +FILES = $(shell find $(SRCD)/$(SUBD) -type f) CSRCS = $(filter %.c, $(FILES)) -HSRCS = $(filter %.h, $(FILES)) COBJS = $(CSRCS:$(SRCD)/%.c=$(OBJD)/%.o) +# get deps +CDEPS = $(COBJS:%.o=%.d) +-include $(CDEPS) + MODULE_TARGET := keylogger.ko KERNEL_NAME := kernel-$(shell uname -r) INITFS_NAME := initramfs.img export -all: daemon client module tests vm +.PHONY: all clean $(DIRS) $(TSTD) $(DEPD) + +all: $(DIRS) $(DEPD) $(TSTD) clean: rm -rf $(BIND) rm -rf $(OBJD) -daemon client module tests libs: +$(DIRS): $(eval TARGET := $($(shell echo $@ | tr 'a-z' 'A-Z')_TARGET)) $(MAKE) $(BIND)/$(if $(TARGET),$(TARGET),$@) SUBD=$@ -# generic link -$(BIND)/%: $(COBJS) $(HSRCS) +$(DEPD) $(TSTD): $(DEPD) + $(MAKE) $(BIND)/$@/ SUBD=$@ + +# generic build +$(BIND)/%: $(COBJS) mkdir -p $(dir $@) $(CC) $(CFLAGS) $(SFLAGS) $^ -o $@ -# generic bulid +# generic compile to obj $(OBJD)/%.o: $(SRCD)/%.c mkdir -p $(dir $@) - $(CC) $(CFLAGS) $(SFLAGS) -c $^ -o $@ + $(CC) $(CFLAGS) $(SFLAGS) -MMD -MF $(@:%.o=%.d) -c $< -o $@ +# generic build and copy dir +$(BIND)/%/: $(FILES) $(CSRCS:$(SRCD)/%.c=$(BIND)/%) + mkdir -p $(dir $@) + find $(@:$(BIND)/%=$(SRCD)/%) -type f ! -name "*.c" ! -name "*.h" -exec cp {} $@ \; -# build a module -$(BIND)/%.ko: $(CSRCS) $(HSRCS) +# generic build a module +$(BIND)/%.ko: $(FILES) mkdir -p $(dir $@); mkdir -p $(OBJD)/$(SUBD) make -C $(SRCD)/$(SUBD) name=$(@:$(BIND)/%.ko=%) mv $(SRCD)/$(SUBD)/*.o $(OBJD)/$(SUBD) mv $(SRCD)/$(SUBD)/*.ko $@ make -C $(SRCD)/$(SUBD) clean -# copy and build the testing files and other shared files (no .c files) -$(BIND)/tests: libs -$(BIND)/tests $(BIND)/libs: $(FILES) $(CSRCS:$(SRCD)/%.c=$(BIND)/%) - mkdir -p $(BIND)/$(SUBD) - find $(SRCD)/$(SUBD) -type f ! -name '*.c' -exec cp {} $(BIND)/$(SUBD) \; - - # VM things vm: vm.sh $(BIND)/$(INITFS_NAME) $(BIND)/$(KERNEL_NAME) ./vm.sh run -$(BIND)/$(INITFS_NAME): $(SRCD)/initramfs-init.sh module - ./vm.sh initramfs $@ $^ +$(BIND)/$(INITFS_NAME): $(SRCD)/initramfs-init.sh module tests + ./vm.sh initramfs $@ $< $(BIND)/$(KERNEL_NAME): ./vm.sh kernel $@ diff --git a/src/daemon/main.c b/src/daemon/main.c index 1e562d6..876c004 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -1,6 +1,8 @@ #include +#include "test-util.h" int main(void) { + hello(); return 0; } diff --git a/src/include/test-util.h b/src/include/test-util.h new file mode 100644 index 0000000..4ec36fc --- /dev/null +++ b/src/include/test-util.h @@ -0,0 +1,71 @@ +#ifndef TEST_UTIL_H +#define TEST_UTIL_H + +#include +#include + +unsigned int tests = 0, failed = 0, cur_test = 0; + +void description(char *str) +{ + printf("# DESCRIPTION: %s\n", str); +} + +void plan(int n) +{ + tests = n; failed = 0; cur_test = 0; + printf("1..%d\n", tests); +} + +int conclude() +{ + float perc = 100.0f * (1.0f - ((float)failed / tests)); + printf("# CONCLUSION: (%.2f%%) %d out of %d tests failed\n", perc, failed, tests); + + if(tests != cur_test) { + printf("# expected %d, but got %d tests\n", tests, cur_test); + } + + if(failed != 0) { + return 1; + } + + return 0; +} + +void ok(int val, char *desc) +{ + cur_test++; + if(val == 0) { + printf("ok %d - %s\n", cur_test, desc); + } else { + printf("not ok %d - %s\n", cur_test, desc); + printf(" returned \'%d\'\n", val); + failed++; + } +} +void is_i(int val, int exp, char *desc) +{ + cur_test++; + if(val == exp) { + printf("ok %d - %s\n", cur_test, desc); + } else { + printf("not ok %d - %s\n", cur_test, desc); + printf(" expected \'%d\', but got \'%d\'\n", exp, val); + failed++; + } +} + +void is_s(char *val, char *exp, char *desc) +{ + cur_test++; + if(strcmp(val, exp) == 0) { + printf("ok %d - %s\n", cur_test, desc); + } else { + printf("not ok %d - %s\n", cur_test, desc); + printf(" expected \'%s\', but got \'%s\'\n", exp, val); + failed++; + } +} + +#endif diff --git a/src/include/test-util.sh b/src/include/test-util.sh new file mode 100755 index 0000000..8af522f --- /dev/null +++ b/src/include/test-util.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +function description { + echo "# DESCRIPTION: $1" +} + +# $1 is the number of total tests +function plan { + TESTS=$1; FAILED=0; CUR_TEST=0 + echo "1..$TESTS" +} + +function conclude { + PERC=$(echo "scale=2;100 * (1 - ($FAILED / $TESTS))" | bc) + echo "# CONCLUSION: ($PERC%) $FAILED out of $TESTS tests failed" + + if [ $TESTS -ne $CUR_TEST ]; then + echo -e "# expected $TESTS, but got $CUR_TEST tests" + fi + + if [ $FAILED -ne 0 ]; then + exit 1 + fi +} + +# $1 is the value to be tested +# $2 description +function ok { + CUR_TEST=$((CUR_TEST + 1)) + + if [ $1 -eq 0 ]; then + echo "ok $CUR_TEST - $2" + else + echo "not ok $CUR_TEST - $2" + echo -e " returned '$1'" + FAILED=$((FAILED + 1)) + fi +} + +# $1 is the expected value +# $2 is the actual value +# $3 description +function is { + CUR_TEST=$((CUR_TEST + 1)) + + if [ "$1" = "$2" ]; then + echo "ok $CUR_TEST - $3" + else + echo "not ok $CUR_TEST - $3" + echo -e " exptected '$2', but got '$1'" + FAILED=$((FAILED + 1)) + fi +} diff --git a/src/initramfs-init.sh b/src/initramfs-init.sh index 07272c0..a566b57 100644 --- a/src/initramfs-init.sh +++ b/src/initramfs-init.sh @@ -6,7 +6,7 @@ mount -t proc proc /proc insmod /usr/keylogger.ko -# test the module +# run each test echo "$(cd usr; ls -v1 tests | while read line; do tests/$line; echo; done)" exec /bin/sh diff --git a/src/libs/testing-library b/src/libs/testing-library deleted file mode 100755 index a1cf3c1..0000000 --- a/src/libs/testing-library +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/sh - -function description { - echo "# DESCRIPTION: $1" -} - -# $1 is the number of total tests -function plan { - TESTS=$1; FAILED=0; CUR_TEST=0 - echo "1..$TESTS" -} - -function conclude { - PERC=$(echo "scale=2;100 * $FAILED / $TESTS" | bc) - echo "# CONCLUSION: ($PERC%) $FAILED out of $TESTS tests failed" - - if [ $TESTS -ne $CUR_TEST ]; then - echo -e "# expected $TESTS, but got $CUR_TEST tests" - fi - - if [ $FAILED -ne 0 ]; then - exit 1 - fi -} - -# $1 is the value to be tested -# $2 description -function ok { - CUR_TEST=$((CUR_TEST + 1)) - - if [ $1 -eq 0 ]; then - echo "ok $CUR_TEST - $2" - else - echo "not ok $CUR_TEST - $2" - echo -e " returned '$1'" - FAILED=$((FAILED + 1)) - fi -} - -# $1 is the expected value -# $2 is the actual value -# $3 description -function is { - CUR_TEST=$((CUR_TEST + 1)) - - if [ "$1" = "$2" ]; then - echo "ok $CUR_TEST - $3" - else - echo "not ok $CUR_TEST - $3" - echo -e " exptected '$2', but got '$1'" - FAILED=$((FAILED + 1)) - fi -} diff --git a/src/tests/test1.sh b/src/tests/test1.sh index a432e5f..5ac52d8 100755 --- a/src/tests/test1.sh +++ b/src/tests/test1.sh @@ -1,5 +1,5 @@ #!/bin/sh -. libs/testing-library +. include/test-util.sh description "Test _test_module" plan 5 @@ -7,5 +7,5 @@ is "$(cat /proc/_test_module)" "You have no previous messages" "No message read" ok $(echo kek > /proc/_test_module; echo "$?") "Successful write" is "$(cat /proc/_test_module)" "Your last message was: kek" "Message read" ok $(echo something > /proc/_test_module; echo "$?") "Successful write" -is "$(cat /proc/_test_module)" "Your last message was: kek" "Message read" +is "$(cat /proc/_test_module)" "Your last message was: something" "Message read" conclude diff --git a/src/tests/test2.c b/src/tests/test2.c new file mode 100644 index 0000000..dc24f28 --- /dev/null +++ b/src/tests/test2.c @@ -0,0 +1,13 @@ +#include "test-util.h" + +int main(void) +{ + description("Test _test_module in c"); + plan(3); + + ok(0, "test something"); + is_s("kek", "kek", "test kek"); + is_i(1, 1, "test 1"); + + return conclude(); +} diff --git a/vm.sh b/vm.sh index 9972464..cd0d725 100755 --- a/vm.sh +++ b/vm.sh @@ -2,7 +2,8 @@ function run { - st qemu-system-x86_64 \ + # st \ + qemu-system-x86_64 \ -kernel "$BIND/$KERNEL_NAME" \ -initrd "$BIND/$INITFS_NAME" \ -append "console=ttyS0" -nographic @@ -12,7 +13,6 @@ function initramfs { IMG="$(pwd)/$1" DIR="${IMG%.*}" - mkdir -p $DIR # get busybox things @@ -20,15 +20,13 @@ function initramfs ln -sf busybox $DIR/bin/sh ln -sf busybox $DIR/bin/dmesg ln -sf busybox $DIR/bin/mount - # ln -sf busybox $DIR/bin/bash # copy the things - mkdir -p $DIR/usr/ - cp -r $BIND/* $DIR/usr/ - - rm -f $DIR/usr/kernel* # no need for the kernel - rm -f $DIR/usr/*.img # no need for the initramfs - rm -rf $DIR/usr/initramfs # no need for the initramfs + mkdir -p "$DIR/usr" + find "$BIND" -maxdepth 1 -mindepth 1 \ + ! -name "*initramfs*" \ + ! -name "*kernel*" \ + -exec cp -r {} "$DIR/usr" \; # copy the script and the bin cp $2 $DIR/init -- cgit v1.2.3