timer: handle time going backwards.
[ccan] / ccan / failtest / test / run-history.c
1 /* Include the C files directly. */
2 #include <ccan/failtest/failtest.c>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <stdarg.h>
6 #include <ccan/tap/tap.h>
7
8 int main(void)
9 {
10         struct failtest_call *call;
11         struct calloc_call calloc_call;
12         struct malloc_call malloc_call;
13         struct realloc_call realloc_call;
14         struct open_call open_call;
15         struct pipe_call pipe_call;
16         struct read_call read_call;
17         struct write_call write_call;
18         struct mmap_call mmap_call;
19         char buf[20];
20         unsigned int i;
21         char *path;
22
23         /* This is how many tests you plan to run */
24         plan_tests(69);
25
26         calloc_call.ret = calloc(1, 2);
27         calloc_call.nmemb = 1;
28         calloc_call.size = 2;
29         call = add_history(FAILTEST_CALLOC, true,
30                            "run-history.c", 1, &calloc_call);
31         /* Normally should_fail would set this. */
32         call->fail = false;
33         ok1(call->type == FAILTEST_CALLOC);
34         ok1(call->can_leak == true);
35         ok1(strcmp(call->file, "run-history.c") == 0);
36         ok1(call->line == 1);
37         ok1(call->u.calloc.ret == calloc_call.ret);
38         ok1(call->u.calloc.nmemb == calloc_call.nmemb);
39         ok1(call->u.calloc.size == calloc_call.size);
40
41         malloc_call.ret = malloc(2);
42         malloc_call.size = 2;
43         call = add_history(FAILTEST_MALLOC, true,
44                            "run-history.c", 2, &malloc_call);
45         /* Normally should_fail would set this. */
46         call->fail = false;
47         ok1(call->type == FAILTEST_MALLOC);
48         ok1(call->can_leak == true);
49         ok1(strcmp(call->file, "run-history.c") == 0);
50         ok1(call->line == 2);
51         ok1(call->u.malloc.ret == malloc_call.ret);
52         ok1(call->u.malloc.size == malloc_call.size);
53
54         realloc_call.ret = realloc(malloc_call.ret, 3);
55         realloc_call.ptr = malloc_call.ret;
56         realloc_call.size = 3;
57         call = add_history(FAILTEST_REALLOC, true, "run-history.c", 3,
58                            &realloc_call);
59         /* Normally should_fail would set this. */
60         call->fail = false;
61         ok1(call->type == FAILTEST_REALLOC);
62         ok1(call->can_leak == true);
63         ok1(strcmp(call->file, "run-history.c") == 0);
64         ok1(call->line == 3);
65         ok1(call->u.realloc.ret == realloc_call.ret);
66         ok1(call->u.realloc.ptr == realloc_call.ptr);
67         ok1(call->u.realloc.size == realloc_call.size);
68
69         open_call.ret = open("test/run-history.c", O_RDONLY);
70         open_call.pathname = "test/run-history.c";
71         open_call.flags = O_RDONLY;
72         open_call.mode = 0;
73         open_call.closed = false;
74         call = add_history(FAILTEST_OPEN, true, "run-history.c", 4, &open_call);
75         /* Normally should_fail would set this. */
76         call->fail = false;
77         ok1(call->type == FAILTEST_OPEN);
78         ok1(call->can_leak == true);
79         ok1(strcmp(call->file, "run-history.c") == 0);
80         ok1(call->line == 4);
81         ok1(call->u.open.ret == open_call.ret);
82         ok1(strcmp(call->u.open.pathname, open_call.pathname) == 0);
83         ok1(call->u.open.flags == open_call.flags);
84         ok1(call->u.open.mode == open_call.mode);
85
86         pipe_call.ret = pipe(pipe_call.fds);
87         call = add_history(FAILTEST_PIPE, true, "run-history.c", 5, &pipe_call);
88         /* Normally should_fail would set this. */
89         call->fail = false;
90         ok1(call->type == FAILTEST_PIPE);
91         ok1(strcmp(call->file, "run-history.c") == 0);
92         ok1(call->can_leak == true);
93         ok1(call->line == 5);
94         ok1(call->u.pipe.ret == pipe_call.ret);
95         ok1(call->u.pipe.fds[0] == pipe_call.fds[0]);
96         ok1(call->u.pipe.fds[1] == pipe_call.fds[1]);
97
98         read_call.ret = read(open_call.ret, buf, 20);
99         read_call.buf = buf;
100         read_call.fd = open_call.ret;
101         read_call.count = 20;
102         call = add_history(FAILTEST_READ, false, "run-history.c", 6, &read_call);
103         /* Normally should_fail would set this. */
104         call->fail = false;
105         ok1(call->type == FAILTEST_READ);
106         ok1(call->can_leak == false);
107         ok1(strcmp(call->file, "run-history.c") == 0);
108         ok1(call->line == 6);
109         ok1(call->u.read.ret == read_call.ret);
110         ok1(call->u.read.buf == read_call.buf);
111         ok1(call->u.read.fd == read_call.fd);
112         ok1(call->u.read.count == read_call.count);
113
114         write_call.ret = 20;
115         write_call.buf = buf;
116         write_call.fd = open_call.ret;
117         write_call.count = 20;
118         write_call.opener = NULL;
119         call = add_history(FAILTEST_WRITE, false, "run-history.c", 7,
120                            &write_call);
121         /* Normally should_fail would set this. */
122         call->fail = false;
123         ok1(call->type == FAILTEST_WRITE);
124         ok1(call->can_leak == false);
125         ok1(strcmp(call->file, "run-history.c") == 0);
126         ok1(call->line == 7);
127         ok1(call->u.write.ret == write_call.ret);
128         ok1(call->u.write.buf == write_call.buf);
129         ok1(call->u.write.fd == write_call.fd);
130         ok1(call->u.write.count == write_call.count);
131         ok1(call->u.write.opener == write_call.opener);
132
133         mmap_call.ret = &mmap_call;
134         mmap_call.addr = NULL;
135         mmap_call.length = 4096;
136         mmap_call.prot = PROT_READ;
137         mmap_call.flags = 0;
138         mmap_call.fd = open_call.ret;
139         mmap_call.offset = 0;
140         mmap_call.opener = opener_of(open_call.ret);
141         ok1(mmap_call.opener->type == FAILTEST_OPEN);
142         mmap_call.saved = NULL;
143
144         call = add_history(FAILTEST_MMAP, false, "run-history.c", 8,
145                            &mmap_call);
146         /* Normally should_fail would set this. */
147         call->fail = false;
148         ok1(call->type == FAILTEST_MMAP);
149         ok1(call->can_leak == false);
150         ok1(strcmp(call->file, "run-history.c") == 0);
151         ok1(call->line == 8);
152         ok1(call->u.mmap.ret == mmap_call.ret);
153         ok1(call->u.mmap.addr == mmap_call.addr);
154         ok1(call->u.mmap.length == mmap_call.length);
155         ok1(call->u.mmap.prot == mmap_call.prot);
156         ok1(call->u.mmap.flags == mmap_call.flags);
157         ok1(call->u.mmap.fd == mmap_call.fd);
158         ok1(call->u.mmap.offset == mmap_call.offset);
159         ok1(call->u.mmap.opener == mmap_call.opener);
160         ok1(call->u.mmap.saved == mmap_call.saved);
161
162         i = 0;
163         tlist_for_each(&history, call, list)
164                 i++;
165
166         ok1(i == 8);
167
168         tlist_for_each(&history, call, list)
169                 call->fail = false;
170
171         path = failpath_string();
172         ok1(streq(path, "cmeoprwa"));
173         free(path);
174
175         tlist_for_each(&history, call, list)
176                 call->fail = true;
177
178         path = failpath_string();
179         ok1(streq(path, "CMEOPRWA"));
180         free(path);
181
182         return exit_status();
183 }