aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2023-04-28 00:54:59 +0300
committerkartofen <mladenovnasko0@gmail.com>2023-04-28 00:54:59 +0300
commit7395f6ec5385cd4895755c0c48e878a01214ef1c (patch)
tree179d108430d6a6e580ab846f0ea0ea2af26921ae
parent2c0f30c29b4b70a45ba01a0c32ae31ac7f75625b (diff)
added testing framework
-rw-r--r--Makefile11
-rw-r--r--src/initramfs-init.sh5
-rwxr-xr-xsrc/tests/framework.sh41
-rwxr-xr-xsrc/tests/run-tests4
-rwxr-xr-xsrc/tests/test1.sh12
-rwxr-xr-xvm.sh10
6 files changed, 76 insertions, 7 deletions
diff --git a/Makefile b/Makefile
index a4a8911..4278311 100644
--- a/Makefile
+++ b/Makefile
@@ -11,15 +11,16 @@ OBJS = $(SRCS:$(SRCD)/$(SUBD)/%.c=$(OBJD)/$(SUBD)/%.o)
DAEMON_TARGET_NAME := daemon
CLIENT_TARGET_NAME := client
MODULE_TARGET_NAME := keylogger.ko
+TESTS_TARGET_NAME := tests
KERNEL_NAME := kernel-$(shell uname -r)
INITFS_NAME := initramfs.img
export
-all: daemon client module
+all: daemon client module tests vm
-daemon client module:
+daemon client module tests:
$(MAKE) $(BIND)/$($(shell echo $@ | tr '[:lower:]' '[:upper:]')_TARGET_NAME) SUBD=$@
clean:
@@ -43,6 +44,12 @@ $(BIND)/%.ko: $(SRCS)
mv $(SRCD)/$(SUBD)/*.ko $@
make -C $(SRCD)/$(SUBD) clean
+# copy the test files
+$(BIND)/tests: $(SRCS)
+ mkdir -p $(BIND)
+ cp -r $(SRCD)/$(SUBD) $(BIND)
+
+
# VM things
vm: vm.sh $(BIND)/$(INITFS_NAME) $(BIND)/$(KERNEL_NAME)
./vm.sh run
diff --git a/src/initramfs-init.sh b/src/initramfs-init.sh
index 7ce8606..c7c212d 100644
--- a/src/initramfs-init.sh
+++ b/src/initramfs-init.sh
@@ -4,7 +4,8 @@
mkdir /proc
mount -t proc proc /proc
-insmod /usr/bin/keylogger.ko
-dmesg
+insmod /usr/keylogger.ko
+cd /usr/tests
+./run-tests
exec /bin/sh
diff --git a/src/tests/framework.sh b/src/tests/framework.sh
new file mode 100755
index 0000000..d6b7854
--- /dev/null
+++ b/src/tests/framework.sh
@@ -0,0 +1,41 @@
+#!/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
new file mode 100755
index 0000000..f109b8c
--- /dev/null
+++ b/src/tests/run-tests
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+echo "Test 1"
+./test1.sh
diff --git a/src/tests/test1.sh b/src/tests/test1.sh
new file mode 100755
index 0000000..52c2ba3
--- /dev/null
+++ b/src/tests/test1.sh
@@ -0,0 +1,12 @@
+#!/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"
+
+conclude
diff --git a/vm.sh b/vm.sh
index 69b9bb5..9972464 100755
--- a/vm.sh
+++ b/vm.sh
@@ -22,9 +22,13 @@ function initramfs
ln -sf busybox $DIR/bin/mount
# ln -sf busybox $DIR/bin/bash
- # copy the compiled binaries
- mkdir -p $DIR/usr/bin
- cp $BIND/*.ko $DIR/usr/bin
+ # 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
# copy the script and the bin
cp $2 $DIR/init