aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2023-04-30 13:21:44 +0300
committerkartofen <mladenovnasko0@gmail.com>2023-04-30 13:21:44 +0300
commitd42853496fc976ef3d067af421a1a3811660033d (patch)
tree272ece830ae755e92f715b77f632e24b1adec2d8 /src
parenta78c52265d755a2294a743e186ad5a6b5456d9f1 (diff)
i am getting tired of the makefile
Diffstat (limited to 'src')
-rw-r--r--src/daemon/main.c2
-rw-r--r--src/include/test-util.h71
-rwxr-xr-xsrc/include/test-util.sh (renamed from src/libs/testing-library)2
-rw-r--r--src/initramfs-init.sh2
-rwxr-xr-xsrc/tests/test1.sh4
-rw-r--r--src/tests/test2.c13
6 files changed, 90 insertions, 4 deletions
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 <stdio.h>
+#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 <stdio.h>
+#include <string.h>
+
+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/libs/testing-library b/src/include/test-util.sh
index a1cf3c1..8af522f 100755
--- a/src/libs/testing-library
+++ b/src/include/test-util.sh
@@ -11,7 +11,7 @@ function plan {
}
function conclude {
- PERC=$(echo "scale=2;100 * $FAILED / $TESTS" | bc)
+ PERC=$(echo "scale=2;100 * (1 - ($FAILED / $TESTS))" | bc)
echo "# CONCLUSION: ($PERC%) $FAILED out of $TESTS tests failed"
if [ $TESTS -ne $CUR_TEST ]; then
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/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();
+}