aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2023-04-28 20:39:42 +0300
committerkartofen <mladenovnasko0@gmail.com>2023-04-28 20:39:42 +0300
commita78c52265d755a2294a743e186ad5a6b5456d9f1 (patch)
tree3606129e28526dca6c2726712c755700b912895d
parent7395f6ec5385cd4895755c0c48e878a01214ef1c (diff)
testing structure done
-rw-r--r--Makefile37
-rw-r--r--src/initramfs-init.sh5
-rwxr-xr-xsrc/libs/testing-library53
-rwxr-xr-xsrc/tests/framework.sh41
-rwxr-xr-xsrc/tests/run-tests4
-rwxr-xr-xsrc/tests/test1.sh19
6 files changed, 85 insertions, 74 deletions
diff --git a/Makefile b/Makefile
index 4278311..544ff31 100644
--- a/Makefile
+++ b/Makefile
@@ -5,14 +5,13 @@ SRCD := src
OBJD := obj
BIND := bin
-SRCS = $(shell find $(SRCD)/$(SUBD) -type f)
-OBJS = $(SRCS:$(SRCD)/$(SUBD)/%.c=$(OBJD)/$(SUBD)/%.o)
+FILES = $(shell find $(SRCD)/$(SUBD) -type f)
-DAEMON_TARGET_NAME := daemon
-CLIENT_TARGET_NAME := client
-MODULE_TARGET_NAME := keylogger.ko
-TESTS_TARGET_NAME := tests
+CSRCS = $(filter %.c, $(FILES))
+HSRCS = $(filter %.h, $(FILES))
+COBJS = $(CSRCS:$(SRCD)/%.c=$(OBJD)/%.o)
+MODULE_TARGET := keylogger.ko
KERNEL_NAME := kernel-$(shell uname -r)
INITFS_NAME := initramfs.img
@@ -20,34 +19,38 @@ export
all: daemon client module tests vm
-daemon client module tests:
- $(MAKE) $(BIND)/$($(shell echo $@ | tr '[:lower:]' '[:upper:]')_TARGET_NAME) SUBD=$@
-
clean:
rm -rf $(BIND)
rm -rf $(OBJD)
-# generic build
-$(BIND)/%: $(OBJS)
+daemon client module tests libs:
+ $(eval TARGET := $($(shell echo $@ | tr 'a-z' 'A-Z')_TARGET))
+ $(MAKE) $(BIND)/$(if $(TARGET),$(TARGET),$@) SUBD=$@
+
+# generic link
+$(BIND)/%: $(COBJS) $(HSRCS)
mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(SFLAGS) $^ -o $@
-$(OBJD)/$(SUBD)/%.o: $(SRCD)/$(SUBD)/%.c
+# generic bulid
+$(OBJD)/%.o: $(SRCD)/%.c
mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(SFLAGS) -c $^ -o $@
+
# build a module
-$(BIND)/%.ko: $(SRCS)
+$(BIND)/%.ko: $(CSRCS) $(HSRCS)
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 the test files
-$(BIND)/tests: $(SRCS)
- mkdir -p $(BIND)
- cp -r $(SRCD)/$(SUBD) $(BIND)
+# 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
diff --git a/src/initramfs-init.sh b/src/initramfs-init.sh
index c7c212d..07272c0 100644
--- a/src/initramfs-init.sh
+++ b/src/initramfs-init.sh
@@ -5,7 +5,8 @@ mkdir /proc
mount -t proc proc /proc
insmod /usr/keylogger.ko
-cd /usr/tests
-./run-tests
+
+# test the module
+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
new file mode 100755
index 0000000..a1cf3c1
--- /dev/null
+++ b/src/libs/testing-library
@@ -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 * $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/framework.sh b/src/tests/framework.sh
deleted file mode 100755
index d6b7854..0000000
--- a/src/tests/framework.sh
+++ /dev/null
@@ -1,41 +0,0 @@
-#!/bin/sh
-
-# $1 is the number of total tests
-function plan {
- TESTS=$1
- FAILED=0
- CUR_TEST=1
-
- echo "1..$TESTS"
-}
-
-function conclude {
- if [ $FAILED -ne 0 ]; then
- exit 1
- fi
-}
-
-# $1 is the value to be tested
-function ok {
- if [ $1 -eq 0 ]; then
- echo "ok $CUR_TEST"
- else
- echo "not ok $CUR_TEST"
- FAILED=$((FAILED + 1))
- fi
-
- CUR_TEST=$((CUR_TEST + 1))
-}
-
-# $1 is the expected value
-# $2 is the actual value
-function is {
- if [ "$1" = "$2" ]; then
- echo "ok $CUR_TEST"
- else
- echo "not ok $CUR_TEST - expected '$1', but got '$2'"
- FAILED=$((FAILED + 1))
- fi
-
- CUR_TEST=$((CUR_TEST + 1))
-}
diff --git a/src/tests/run-tests b/src/tests/run-tests
deleted file mode 100755
index f109b8c..0000000
--- a/src/tests/run-tests
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/bin/sh
-
-echo "Test 1"
-./test1.sh
diff --git a/src/tests/test1.sh b/src/tests/test1.sh
index 52c2ba3..a432e5f 100755
--- a/src/tests/test1.sh
+++ b/src/tests/test1.sh
@@ -1,12 +1,11 @@
#!/bin/sh
-
-. ./framework.sh
-
-plan 2
-
-is "$(cat /proc/_test_module)" "You have no previous messages"
-
-echo kek > /proc/_test_module
-is "$(cat /proc/_test_module)" "Your last message was: kek"
-
+. libs/testing-library
+
+description "Test _test_module"
+plan 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"
conclude