test/lib: add process tests
[petitboot] / test / lib / test-process-async.c
1
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5
6 #include <process/process.h>
7 #include <waiter/waiter.h>
8 #include <talloc/talloc.h>
9
10 static int do_child(void)
11 {
12         return 42;
13 }
14
15 static bool exited;
16 static int exit_status;
17
18 static void exit_cb(struct process *process)
19 {
20         exited = true;
21         exit_status = process->exit_status;
22 }
23
24 int main(int argc, char **argv)
25 {
26         struct waitset *waitset;
27         struct process *process;
28         const char *child_argv[3];
29         void *ctx;
30
31         if (argc == 2 && !strcmp(argv[1], "child"))
32                 return do_child();
33
34         ctx = talloc_new(NULL);
35
36         waitset = waitset_create(ctx);
37
38         process_init(ctx, waitset);
39
40         child_argv[0] = argv[0];
41         child_argv[1] = "child";
42         child_argv[2] = NULL;
43
44         process = process_create(ctx);
45         process->path = child_argv[0];
46         process->argv = child_argv;
47         process->exit_cb = exit_cb;
48
49         exited = false;
50
51         process_run_async(process);
52
53         for (;;) {
54                 waiter_poll(waitset);
55
56                 if (exited)
57                         break;
58         }
59
60         assert(WIFEXITED(exit_status));
61         assert(WEXITSTATUS(exit_status) == 42);
62
63         talloc_free(ctx);
64
65         return EXIT_SUCCESS;
66 }