summaryrefslogtreecommitdiff
path: root/src/init
diff options
context:
space:
mode:
authordzwdz2021-10-15 18:40:57 +0200
committerdzwdz2021-10-15 18:40:57 +0200
commit42bf65918f96d43e9ce8a7287a9508f44fe670d5 (patch)
tree2bb2dec665848c4f36752852df3e570d482e547e /src/init
parent6f9626fbd235f5e66ba61affa064e3b44ffc012a (diff)
init: move the tests to their own directory
Diffstat (limited to 'src/init')
-rw-r--r--src/init/main.c76
-rw-r--r--src/init/tests/main.c75
-rw-r--r--src/init/tests/main.h4
3 files changed, 82 insertions, 73 deletions
diff --git a/src/init/main.c b/src/init/main.c
index 5e40496..c922a99 100644
--- a/src/init/main.c
+++ b/src/init/main.c
@@ -1,10 +1,12 @@
#include <init/shell.h>
#include <init/stdlib.h>
#include <init/tar.h>
+#include <init/tests/main.h>
#include <shared/flags.h>
#include <shared/syscalls.h>
#include <stdint.h>
+// TODO move to shared header file
#define argify(str) str, sizeof(str) - 1
extern char _bss_start; // provided by the linker
@@ -13,8 +15,6 @@ extern char _initrd;
void read_file(const char *path, size_t len);
void fs_prep(void);
-void fs_test(void);
-void test_await(void);
__attribute__((section(".text.startup")))
int main(void) {
@@ -26,31 +26,13 @@ int main(void) {
_syscall_exit(1);
fs_prep();
- fs_test();
+ test_fs();
test_await();
shell_loop();
_syscall_exit(0);
}
-void read_file(const char *path, size_t len) {
- int fd = _syscall_open(path, len);
- static char buf[64];
- int buf_len = 64;
-
- _syscall_write(__tty_fd, path, len, 0);
- printf(": ");
- if (fd < 0) {
- printf("couldn't open.\n");
- return;
- }
-
- buf_len = _syscall_read(fd, buf, buf_len, 0);
- _syscall_write(__tty_fd, buf, buf_len, 0);
-
- _syscall_close(fd);
-}
-
void fs_prep(void) {
handle_t front, back;
front = _syscall_fs_create(&back);
@@ -64,55 +46,3 @@ void fs_prep(void) {
* TODO actually write tests */
_syscall_mount(front, argify("/init/"));
}
-
-void fs_test(void) {
- if (!_syscall_fork()) {
- /* run the "test" in a child process to not affect the fs view of the
- * main process */
- read_file(argify("/init/fake.txt"));
- read_file(argify("/init/1.txt"));
- read_file(argify("/init/2.txt"));
- read_file(argify("/init/dir/3.txt"));
-
- printf("\nshadowing /init/dir...\n");
- _syscall_mount(-1, argify("/init/dir"));
- read_file(argify("/init/fake.txt"));
- read_file(argify("/init/1.txt"));
- read_file(argify("/init/2.txt"));
- read_file(argify("/init/dir/3.txt"));
-
- printf("\n");
- _syscall_exit(0);
- } else _syscall_await();
-}
-
-void test_await(void) {
- int ret;
-
- if (!_syscall_fork()) {
- /* this "test" runs in a child process, because otherwise it would be
- * stuck waiting for e.g. the tar_driver process to exit */
-
- // regular exit()s
- if (!_syscall_fork()) _syscall_exit(69);
- if (!_syscall_fork()) _syscall_exit(420);
-
- // faults
- if (!_syscall_fork()) { // invalid memory access
- asm volatile("movb $69, 0" ::: "memory");
- printf("this shouldn't happen");
- _syscall_exit(-1);
- }
- if (!_syscall_fork()) { // #GP
- asm volatile("hlt" ::: "memory");
- printf("this shouldn't happen");
- _syscall_exit(-1);
- }
-
- while ((ret = _syscall_await()) != ~0)
- printf("await returned: %x\n", ret);
- printf("await: no more children\n");
-
- _syscall_exit(0);
- } else _syscall_await();
-}
diff --git a/src/init/tests/main.c b/src/init/tests/main.c
new file mode 100644
index 0000000..ed02e5f
--- /dev/null
+++ b/src/init/tests/main.c
@@ -0,0 +1,75 @@
+#include <init/stdlib.h>
+#include <init/tests/main.h>
+#include <shared/syscalls.h>
+
+#define argify(str) str, sizeof(str) - 1
+
+static void read_file(const char *path, size_t len) {
+ int fd = _syscall_open(path, len);
+ static char buf[64];
+ int buf_len = 64;
+
+ _syscall_write(__tty_fd, path, len, 0);
+ printf(": ");
+ if (fd < 0) {
+ printf("couldn't open.\n");
+ return;
+ }
+
+ buf_len = _syscall_read(fd, buf, buf_len, 0);
+ _syscall_write(__tty_fd, buf, buf_len, 0);
+
+ _syscall_close(fd);
+}
+
+void test_fs(void) {
+ if (!_syscall_fork()) {
+ /* run the "test" in a child process to not affect the fs view of the
+ * main process */
+ read_file(argify("/init/fake.txt"));
+ read_file(argify("/init/1.txt"));
+ read_file(argify("/init/2.txt"));
+ read_file(argify("/init/dir/3.txt"));
+
+ printf("\nshadowing /init/dir...\n");
+ _syscall_mount(-1, argify("/init/dir"));
+ read_file(argify("/init/fake.txt"));
+ read_file(argify("/init/1.txt"));
+ read_file(argify("/init/2.txt"));
+ read_file(argify("/init/dir/3.txt"));
+
+ printf("\n");
+ _syscall_exit(0);
+ } else _syscall_await();
+}
+
+void test_await(void) {
+ int ret;
+
+ if (!_syscall_fork()) {
+ /* this "test" runs in a child process, because otherwise it would be
+ * stuck waiting for e.g. the tar_driver process to exit */
+
+ // regular exit()s
+ if (!_syscall_fork()) _syscall_exit(69);
+ if (!_syscall_fork()) _syscall_exit(420);
+
+ // faults
+ if (!_syscall_fork()) { // invalid memory access
+ asm volatile("movb $69, 0" ::: "memory");
+ printf("this shouldn't happen");
+ _syscall_exit(-1);
+ }
+ if (!_syscall_fork()) { // #GP
+ asm volatile("hlt" ::: "memory");
+ printf("this shouldn't happen");
+ _syscall_exit(-1);
+ }
+
+ while ((ret = _syscall_await()) != ~0)
+ printf("await returned: %x\n", ret);
+ printf("await: no more children\n");
+
+ _syscall_exit(0);
+ } else _syscall_await();
+}
diff --git a/src/init/tests/main.h b/src/init/tests/main.h
new file mode 100644
index 0000000..0c24e03
--- /dev/null
+++ b/src/init/tests/main.h
@@ -0,0 +1,4 @@
+#pragma once
+
+void test_fs(void);
+void test_await(void);