X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Frbuf%2Ftest%2Frun-term-eof.c;h=86d7c18d43a13832aed17a3f87177caaf2377430;hp=097dcbbda351492786cb79f0f4c11d4cb89f193c;hb=e4142e580c36230a1e5137f8a5f5a5cc01a57a1e;hpb=44b37ee8aeb185f6837378fef17d887bda6ff89b diff --git a/ccan/rbuf/test/run-term-eof.c b/ccan/rbuf/test/run-term-eof.c index 097dcbbd..86d7c18d 100644 --- a/ccan/rbuf/test/run-term-eof.c +++ b/ccan/rbuf/test/run-term-eof.c @@ -7,6 +7,14 @@ #include #include +static bool test_realloc_fail; +static void *test_realloc(struct membuf *mb, void *buf, size_t n) +{ + if (test_realloc_fail) + return NULL; + return realloc(buf, n); +} + int main(void) { struct rbuf in; @@ -14,7 +22,7 @@ int main(void) int fd = open("test/run-term-eof.c", O_RDONLY), len; /* This is how many tests you plan to run */ - plan_tests(6); + plan_tests(10); /* Grab ourselves for comparison. */ len = read(fd, buf, sizeof(buf)); @@ -22,23 +30,35 @@ int main(void) lseek(fd, SEEK_SET, 0); /* We have exact-size buffer, which causes problems adding term. */ - rbuf_init(&in, fd, malloc(len), len); - p = rbuf_read_str(&in, 64, NULL); /* At symbol does not appear. */ + rbuf_init(&in, fd, malloc(len), len, test_realloc); + test_realloc_fail = true; + p = rbuf_read_str(&in, 64); /* At symbol does not appear. */ ok1(errno == ENOMEM); ok1(!p); /* This should succeed... */ - p = rbuf_read_str(&in, 64, realloc); + test_realloc_fail = false; + p = rbuf_read_str(&in, 64); ok1(p); ok1(strcmp(p, buf) == 0); - free(in.buf); + ok1(rbuf_start(&in) == p + strlen(p)); + free(rbuf_cleanup(&in)); /* Try again. */ lseek(fd, SEEK_SET, 0); - rbuf_init(&in, fd, malloc(len), len); - p = rbuf_read_str(&in, 64, realloc); + rbuf_init(&in, fd, malloc(len), len, test_realloc); + p = rbuf_read_str(&in, 64); ok1(p); ok1(strcmp(p, buf) == 0); - free(in.buf); + ok1(rbuf_start(&in) == p + strlen(p)); + free(rbuf_cleanup(&in)); + + /* Normal case, we get rbuf_start after nul */ + lseek(fd, SEEK_SET, 0); + rbuf_init(&in, fd, NULL, 0, test_realloc); + p = rbuf_read_str(&in, '^'); + ok1(p); + ok1(rbuf_start(&in) == p + strlen(p) + 1); + free(rbuf_cleanup(&in)); return exit_status(); }