]> git.ozlabs.org Git - ccan/blob - ccan/rbuf/test/run-term-eof.c
failtest, rbuf: fix up incorrect lseek arg order.
[ccan] / ccan / rbuf / test / run-term-eof.c
1 #include <ccan/rbuf/rbuf.h>
2 /* Include the C files directly. */
3 #include <ccan/rbuf/rbuf.c>
4 #include <ccan/tap/tap.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <stdlib.h>
9
10 static bool test_realloc_fail;
11 static void *test_realloc(struct membuf *mb, void *buf, size_t n)
12 {
13         if (test_realloc_fail)
14                 return NULL;
15         return realloc(buf, n);
16 }
17
18 int main(void)
19 {
20         struct rbuf in;
21         char buf[4096], *p;
22         int fd = open("test/run-term-eof.c", O_RDONLY), len;
23
24         /* This is how many tests you plan to run */
25         plan_tests(10);
26
27         /* Grab ourselves for comparison. */
28         len = read(fd, buf, sizeof(buf));
29         buf[len] = '\0';
30         lseek(fd, 0, SEEK_SET);
31
32         /* We have exact-size buffer, which causes problems adding term. */
33         rbuf_init(&in, fd, malloc(len), len, test_realloc);
34         test_realloc_fail = true;
35         p = rbuf_read_str(&in, 64); /* At symbol does not appear. */
36         ok1(errno == ENOMEM);
37         ok1(!p);
38         /* This should succeed... */
39         test_realloc_fail = false;
40         p = rbuf_read_str(&in, 64);
41         ok1(p);
42         ok1(strcmp(p, buf) == 0);
43         ok1(rbuf_start(&in) == p + strlen(p));
44         free(rbuf_cleanup(&in));
45
46         /* Try again. */
47         lseek(fd, 0, SEEK_SET);
48         rbuf_init(&in, fd, malloc(len), len, test_realloc);
49         p = rbuf_read_str(&in, 64);
50         ok1(p);
51         ok1(strcmp(p, buf) == 0);
52         ok1(rbuf_start(&in) == p + strlen(p));
53         free(rbuf_cleanup(&in));
54
55         /* Normal case, we get rbuf_start after nul */
56         lseek(fd, 0, SEEK_SET);
57         rbuf_init(&in, fd, NULL, 0, test_realloc);
58         p = rbuf_read_str(&in, '^');
59         ok1(p);
60         ok1(rbuf_start(&in) == p + strlen(p) + 1);
61         free(rbuf_cleanup(&in));
62
63         return exit_status();
64 }