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