summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordzwdz2021-10-15 19:23:23 +0200
committerdzwdz2021-10-15 19:23:23 +0200
commitc0903edc3ee1fe4dd9c9921f4b7712a94271f238 (patch)
tree9530e5a3a75db98057d5904e1e416cfe9639b16f /src
parent710ddbffea55f06f91cb0325b900f0ffda77795f (diff)
init/tests: write some actual tests - test_await and test_faults
Diffstat (limited to 'src')
-rw-r--r--src/init/tests/main.c39
1 files changed, 31 insertions, 8 deletions
diff --git a/src/init/tests/main.c b/src/init/tests/main.c
index 7890ca3..239ed3e 100644
--- a/src/init/tests/main.c
+++ b/src/init/tests/main.c
@@ -4,6 +4,13 @@
#define argify(str) str, sizeof(str) - 1
+#define test_fail() do { \
+ printf("\033[31m" "TEST FAILED: %s:%xh\n" "\033[0m", __func__, __LINE__); \
+ return; \
+} while (0)
+
+#define assert(cond) if (!(cond)) test_fail();
+
static void run_forked(void (*fn)()) {
if (!_syscall_fork()) {
fn();
@@ -14,13 +21,30 @@ static void run_forked(void (*fn)()) {
static void test_await(void) {
+ /* creates 16 child processes, each returning a different value. then checks
+ * if await() returns every value exactly once */
int ret;
+ int counts[16] = {0};
+
+ for (int i = 0; i < 16; i++)
+ if (!_syscall_fork())
+ _syscall_exit(i);
+
+ while ((ret = _syscall_await()) != ~0) {
+ assert(0 <= ret && ret < 16);
+ counts[ret]++;
+ }
+
+ for (int i = 0; i < 16; i++)
+ assert(counts[i] == 1);
+}
- // regular exit()s
- if (!_syscall_fork()) _syscall_exit(69);
- if (!_syscall_fork()) _syscall_exit(420);
+static void test_faults(void) {
+ /* tests what happens when child processes fault.
+ * expected behavior: parent processes still manages to finish, and it can
+ * reap all its children */
+ int await_cnt = 0;
- // faults
if (!_syscall_fork()) { // invalid memory access
asm volatile("movb $69, 0" ::: "memory");
printf("this shouldn't happen");
@@ -32,13 +56,12 @@ static void test_await(void) {
_syscall_exit(-1);
}
- while ((ret = _syscall_await()) != ~0)
- printf("await returned: %x\n", ret);
- printf("await: no more children\n");
+ while (_syscall_await() != ~0) await_cnt++;
+ assert(await_cnt == 2);
}
-
void test_all(void) {
run_forked(test_await);
+ run_forked(test_faults);
}