xref: /illumos-gate/usr/src/test/libc-tests/tests/stdio/fmemopentest.c (revision e86372a01d2d16a5dd4a64e144ed978ba17fe7dd)
1 /*
2  * Copyright (c) 2011 Martin Pieuchot <mpi@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <err.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 
23 const char *
24 _umem_debug_init(void)
25 {
26 	return ("default,verbose");
27 }
28 
29 const char *
30 _umem_logging_init(void)
31 {
32 	return ("fail,contents");
33 }
34 
35 int
36 simpletest(void)
37 {
38 	FILE	*s1, *s2;
39 	char	 string[] = "fmemopen test string!";
40 	char	 buffer[1024], *buf = NULL;
41 	size_t	 len;
42 	int	 c, failures = 0;
43 
44 	s1 = fmemopen(string, strlen(string) + 1, "r");
45 	if (s1 == NULL) {
46 		warn("unable to open a stream s1");
47 		return (1);
48 	}
49 
50 	s2 = fmemopen(buf, 22, "w+");
51 	if (s2 == NULL) {
52 		warn("unable to create a stream s2");
53 		(void) fclose(s1);
54 		return (1);
55 	}
56 
57 	while ((c = fgetc(s1)) != EOF)
58 		(void) fputc(c, s2);
59 
60 	if (ftell(s2) != strlen(string) + 1) {
61 		warnx("failed copy test (1)");
62 		failures++;
63 	}
64 	(void) fclose(s1);
65 
66 	(void) fseek(s2, 0, SEEK_SET);
67 	if (ftell(s2) != 0) {
68 		warnx("failed seek test (2)");
69 		failures++;
70 	}
71 
72 	len = fread(buffer, 1, sizeof(buffer) - 1, s2);
73 	if (len != strlen(string) + 1) {
74 		warnx("failed read test (3) %zu != %zu",
75 		    len, strlen(string) + 1);
76 		failures++;
77 	}
78 
79 	return (failures);
80 }
81 
82 int
83 updatetest(void)
84 {
85 	FILE	*s1;
86 	char	 string[] = "hello\0 test number 2";
87 	char	 buffer[256];
88 	size_t	 len;
89 	int	 failures = 0;
90 
91 	s1 = fmemopen(string, 19, "a+");
92 	if (s1 == NULL)
93 		return (1);
94 
95 	len = fwrite(" world", 1, 6, s1);
96 	if (len != 6) {
97 		warnx("failed write test (4)");
98 		failures++;
99 	}
100 
101 	(void) fseek(s1, 0, SEEK_SET);
102 	if (ftell(s1) != 0) {
103 		warnx("failed seek test (5)");
104 		failures++;
105 	}
106 
107 	len = fread(buffer, 1, sizeof(buffer) - 1, s1);
108 	if (strncmp(string, buffer, len)) {
109 		warnx("failed compare test (6)");
110 		failures++;
111 	}
112 
113 	if (strcmp(string, "hello world")) {
114 		warnx("failed compare test (7)");
115 		failures++;
116 	}
117 
118 	if (strcmp(string + strlen(string) + 1, "number 2")) {
119 		warnx("failed compare test (8)");
120 		failures++;
121 	}
122 
123 	return (failures);
124 }
125 
126 int
127 writetest(void)
128 {
129 	FILE	*s1;
130 	char	 string[] = "super test number 3";
131 	char	 buffer[256];
132 	size_t	 len, slen;
133 	int	 failures = 0;
134 
135 	slen = strlen(string) + 1;
136 
137 	s1 = fmemopen(string, slen, "w");
138 	if (s1 == NULL)
139 		return (1);
140 
141 	len = fwrite("short", 1, 5, s1);
142 	if (len != strlen("short")) {
143 		warnx("failed write test (9)");
144 		failures++;
145 	}
146 	(void) fclose(s1);
147 
148 	s1 = fmemopen(string, slen, "r");
149 	if (s1 == NULL) {
150 		warnx("failed open test (10)");
151 		failures++;
152 	}
153 
154 	len = fread(buffer, 1, sizeof(buffer) - 1, s1);
155 	if (strncmp(string, buffer, len)) {
156 		warnx("failed compare test (11)");
157 		failures++;
158 	}
159 
160 	if (strcmp(string, "short")) {
161 		warnx("failed compare test (12)");
162 		failures++;
163 	}
164 
165 	if (strcmp(string + strlen(string) + 1, "test number 3")) {
166 		warnx("failed compare test (13)");
167 		failures++;
168 	}
169 
170 	return (failures);
171 }
172 
173 int
174 seektest(void)
175 {
176 	FILE	*s1;
177 	char	 string[] = "long string for testing seek";
178 	size_t	 slen;
179 	int	 failures = 0;
180 
181 	slen = strlen(string) + 1;
182 
183 	s1 = fmemopen(string, slen, "r");
184 	if (s1 == NULL)
185 		return (1);
186 
187 	if (fseek(s1, 8, SEEK_SET) != 0) {
188 		warnx("failed to fseek. (14)");
189 		failures++;
190 	}
191 
192 	if (ftell(s1) != 8) {
193 		warnx("failed seek test. (15)");
194 		failures++;
195 	}
196 
197 	/* Try to seek backward */
198 	if (fseek(s1, -1, SEEK_CUR) != 0) {
199 		warnx("failed to fseek. (16)");
200 		failures++;
201 	}
202 
203 	if (ftell(s1) != 7) {
204 		warnx("failed seeking backward. (17)");
205 		failures++;
206 	}
207 
208 	return (failures);
209 }
210 
211 int
212 main(void)
213 {
214 	int	 failures = 0;
215 
216 	failures += simpletest();
217 	failures += updatetest();
218 	failures += writetest();
219 	failures += seektest();
220 
221 	return (failures);
222 }
223