discover: Recognise and open LUKS encrypted partitions
[petitboot] / test / lib / test-process-stderr-stdout.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         fprintf(stderr, "forty ");
13         fflush(stderr);
14         fprintf(stdout, "two\n");
15         fflush(stdout);
16         return 42;
17 }
18
19 int main(int argc, char **argv)
20 {
21         struct waitset *waitset;
22         struct process *process;
23         const char *child_argv[3];
24         void *ctx;
25
26         if (argc == 2 && !strcmp(argv[1], "child"))
27                 return do_child();
28
29         ctx = talloc_new(NULL);
30
31         waitset = waitset_create(ctx);
32
33         process_init(ctx, waitset, false);
34
35         child_argv[0] = argv[0];
36         child_argv[1] = "child";
37         child_argv[2] = NULL;
38
39         process = process_create(ctx);
40         process->path = child_argv[0];
41         process->argv = child_argv;
42         process->keep_stdout = true;
43         process->add_stderr = true;
44
45         process_run_sync(process);
46
47         assert(WIFEXITED(process->exit_status));
48         assert(WEXITSTATUS(process->exit_status) == 42);
49
50         assert(process->stdout_len == strlen("forty two\n"));
51         assert(!memcmp(process->stdout_buf, "forty two\n",
52                                 process->stdout_len));
53
54         talloc_free(ctx);
55
56         return EXIT_SUCCESS;
57 }