1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include <pthread.h>
4#include <spawn.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <sys/stat.h>
9#include <sys/wait.h>
10
11struct
12{
13 const char *name;
14 const char *executable;
15} const tests[] = {
16 { "fork", "/initrd/tests/fork-test" }, //
17 { "rpc", "/initrd/tests/rpc-test" }, //
18 { "libc", "/initrd/tests/libc-test" }, //
19 { "c++", "/initrd/tests/libstdc++-test" }, //
20 { "rust", "/initrd/tests/rust-test" }, //
21 { "pipe", "/initrd/tests/pipe-test" }, //
22 { "signal", "/initrd/tests/signal" }, //
23 { "syslog", "/initrd/tests/syslog-test" }, //
24 { "memfd", "/initrd/tests/memfd-test" }, //
25 { 0 },
26};
27
28int main(int argc, char **argv)
29{
30 printf(format: "MOS Userspace Test Suite\n");
31 printf(format: "Invoked with %d arguments:\n", argc);
32 for (int i = 0; i < argc; i++)
33 printf(format: " %d: %s\n", i, argv[i]);
34
35 bool detached = false;
36 if (argc > 1 && strcmp(a: argv[1], b: "--detached") == 0)
37 {
38 detached = true;
39 printf(format: "Detached mode enabled\n");
40 }
41
42 printf(format: "\n");
43
44 for (int i = 0; tests[i].name; i++)
45 {
46 printf(format: "Running test %s (%s)... \n", tests[i].name, tests[i].executable);
47 const char *test_argv[] = { tests[i].name, NULL };
48 pid_t ret;
49 posix_spawn(pid: &ret, path: tests[i].executable,
50 NULL, // file_actions
51 NULL, // attrp
52 argv: (char *const *) test_argv, NULL);
53
54 if (ret < 0)
55 {
56 printf(format: "FAILED: cannot spawn: %d\n", ret);
57 continue;
58 }
59
60 if (!detached)
61 {
62 int status = 0;
63 waitpid(pid: ret, status: &status, flags: 0);
64 printf(format: "Test %s exited with status %d\n", tests[i].name, status);
65 printf(format: "OK\n");
66 }
67 }
68
69 return 0;
70}
71