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