1a99ba75eSjustin /*
2a99ba75eSjustin * Based on the OpenBSD test
3a99ba75eSjustin * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org>
4a99ba75eSjustin *
5a99ba75eSjustin * Permission to use, copy, modify, and distribute this software for any
6a99ba75eSjustin * purpose with or without fee is hereby granted, provided that the above
7a99ba75eSjustin * copyright notice and this permission notice appear in all copies.
8a99ba75eSjustin *
9a99ba75eSjustin * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10a99ba75eSjustin * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11a99ba75eSjustin * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12a99ba75eSjustin * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13a99ba75eSjustin * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14a99ba75eSjustin * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15a99ba75eSjustin * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16a99ba75eSjustin */
17a99ba75eSjustin
18a99ba75eSjustin #include <sys/cdefs.h>
19*bdb5d51cSrillig __RCSID("$NetBSD: t_open_memstream.c,v 1.3 2021/09/11 18:18:28 rillig Exp $");
20a99ba75eSjustin
21a99ba75eSjustin #include <atf-c.h>
22a99ba75eSjustin #include <err.h>
23a99ba75eSjustin #include <stdio.h>
24a99ba75eSjustin #include <stdlib.h>
25a99ba75eSjustin #include <string.h>
26a99ba75eSjustin #include <unistd.h>
27a99ba75eSjustin
28a99ba75eSjustin ATF_TC(test_open_memstream);
ATF_TC_HEAD(test_open_memstream,tc)29a99ba75eSjustin ATF_TC_HEAD(test_open_memstream, tc)
30a99ba75eSjustin {
31a99ba75eSjustin atf_tc_set_md_var(tc, "descr", "Test open_memstream functionality");
32a99ba75eSjustin }
33a99ba75eSjustin
34a99ba75eSjustin #define OFFSET 16384
35a99ba75eSjustin
36a99ba75eSjustin const char start[] = "start";
37a99ba75eSjustin const char hello[] = "hello";
38a99ba75eSjustin
ATF_TC_BODY(test_open_memstream,tc)39a99ba75eSjustin ATF_TC_BODY(test_open_memstream, tc)
40a99ba75eSjustin {
41a99ba75eSjustin FILE *fp;
42a99ba75eSjustin char *buf = (char *)0xff;
43a99ba75eSjustin size_t size = 0;
44a99ba75eSjustin off_t off;
45a99ba75eSjustin int i;
46a99ba75eSjustin
47a99ba75eSjustin fp = open_memstream(&buf, &size);
48a99ba75eSjustin ATF_REQUIRE(fp != NULL);
49a99ba75eSjustin
50a99ba75eSjustin off = ftello(fp);
51a99ba75eSjustin ATF_CHECK(off == 0);
52a99ba75eSjustin
53a99ba75eSjustin ATF_CHECK(fflush(fp) == 0);
54a99ba75eSjustin ATF_CHECK(size == 0);
55a99ba75eSjustin ATF_CHECK(buf != (char *)0xff);
567cbb46f7Sjustin ATF_CHECK(fseek(fp, -6, SEEK_SET) == -1);
57a99ba75eSjustin ATF_CHECK(fseek(fp, OFFSET, SEEK_SET) == 0);
58a99ba75eSjustin ATF_CHECK(fprintf(fp, hello) != EOF);
59a99ba75eSjustin ATF_CHECK(fflush(fp) != EOF);
60a99ba75eSjustin ATF_CHECK(size == OFFSET + sizeof(hello)-1);
61a99ba75eSjustin ATF_CHECK(fseek(fp, 0, SEEK_SET) == 0);
62a99ba75eSjustin ATF_CHECK(fprintf(fp, start) != EOF);
63a99ba75eSjustin ATF_CHECK(fflush(fp) != EOF);
64a99ba75eSjustin ATF_CHECK(size == sizeof(start)-1);
65a99ba75eSjustin
66a99ba75eSjustin /* Needed for sparse files */
67a99ba75eSjustin ATF_CHECK(strncmp(buf, start, sizeof(start)-1) == 0);
68a99ba75eSjustin for (i = sizeof(start)-1; i < OFFSET; i++)
69a99ba75eSjustin ATF_CHECK(buf[i] == '\0');
70a99ba75eSjustin
71a99ba75eSjustin ATF_CHECK(memcmp(buf + OFFSET, hello, sizeof(hello)-1) == 0);
72a99ba75eSjustin
73a99ba75eSjustin /* verify that simply seeking past the end doesn't increase the size */
74a99ba75eSjustin ATF_CHECK(fseek(fp, 100, SEEK_END) == 0);
75a99ba75eSjustin ATF_CHECK(fflush(fp) != EOF);
76a99ba75eSjustin ATF_CHECK(size == OFFSET + sizeof(hello)-1);
77a99ba75eSjustin ATF_CHECK(fseek(fp, 8, SEEK_SET) == 0);
78a99ba75eSjustin ATF_CHECK(ftell(fp) == 8);
79a99ba75eSjustin
80a99ba75eSjustin /* Try to seek backward */
81a99ba75eSjustin ATF_CHECK(fseek(fp, -1, SEEK_CUR) == 0);
82a99ba75eSjustin ATF_CHECK(ftell(fp) == 7);
83a99ba75eSjustin ATF_CHECK(fseek(fp, 5, SEEK_CUR) == 0);
84a99ba75eSjustin ATF_CHECK(fclose(fp) != EOF);
85a99ba75eSjustin ATF_CHECK(size == 12);
86a99ba75eSjustin
87a99ba75eSjustin free(buf);
88a99ba75eSjustin }
89a99ba75eSjustin
ATF_TP_ADD_TCS(tp)90a99ba75eSjustin ATF_TP_ADD_TCS(tp)
91a99ba75eSjustin {
92a99ba75eSjustin
93a99ba75eSjustin ATF_TP_ADD_TC(tp, test_open_memstream);
94a99ba75eSjustin
95a99ba75eSjustin return atf_no_error();
96a99ba75eSjustin }
97