xref: /freebsd/lib/libsbuf/tests/sbuf_stdio_test.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * Copyright (c) 2017 Enji Cooper <ngie@freebsd.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/param.h>
27 #include <sys/sbuf.h>
28 #include <errno.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 
35 #include <atf-c.h>
36 
37 #include "sbuf_test_common.h"
38 
39 static char	test_string[] = "this is a test string";
40 
41 #define	MESSAGE_FORMAT	"message: %s\n"
42 #define	MESSAGE_SEPARATOR	';'
43 
44 static int
45 sbuf_vprintf_helper(struct sbuf *sb, const char * restrict format, ...)
46 {
47 	va_list ap;
48 	int rc;
49 
50 	va_start(ap, format);
51 
52 	rc = sbuf_vprintf(sb, format, ap);
53 
54 	va_end(ap);
55 
56 	return (rc);
57 }
58 
59 ATF_TC_WITHOUT_HEAD(sbuf_printf_drain_null_test);
60 ATF_TC_BODY(sbuf_printf_drain_null_test, tc)
61 {
62 	struct sbuf *sb;
63 	char buf[2];
64 	pid_t child_proc;
65 
66 	sb = sbuf_new(NULL, buf, sizeof(buf), SBUF_FIXEDLEN);
67 	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
68 	    strerror(errno));
69 
70 	child_proc = atf_utils_fork();
71 	if (child_proc == 0) {
72 		sbuf_set_drain(sb, sbuf_printf_drain, NULL);
73 
74 		ATF_REQUIRE_EQ_MSG(0, sbuf_cat(sb, test_string),
75 		    "sbuf_cat failed");
76 
77 		ATF_CHECK_EQ(0, sbuf_finish(sb));
78 		exit(0);
79 	}
80 	atf_utils_wait(child_proc, 0, test_string, "");
81 
82 	sbuf_delete(sb);
83 }
84 
85 ATF_TC_WITHOUT_HEAD(sbuf_printf_drain_test);
86 ATF_TC_BODY(sbuf_printf_drain_test, tc)
87 {
88 	struct sbuf *sb;
89 	char buf[2];
90 	pid_t child_proc;
91 	size_t cnt = 0;
92 
93 	sb = sbuf_new(NULL, buf, sizeof(buf), SBUF_FIXEDLEN);
94 	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
95 	    strerror(errno));
96 
97 	child_proc = atf_utils_fork();
98 	if (child_proc == 0) {
99 		sbuf_set_drain(sb, sbuf_printf_drain, &cnt);
100 
101 		ATF_REQUIRE_EQ_MSG(0, sbuf_cat(sb, test_string),
102 		    "sbuf_cat failed");
103 
104 		ATF_CHECK_EQ(0, sbuf_finish(sb));
105 		ATF_CHECK_EQ(strlen(test_string), cnt);
106 		exit(0);
107 	}
108 	atf_utils_wait(child_proc, 0, test_string, "");
109 
110 	sbuf_delete(sb);
111 }
112 
113 ATF_TC_WITHOUT_HEAD(sbuf_printf_test);
114 ATF_TC_BODY(sbuf_printf_test, tc)
115 {
116 	struct sbuf *sb;
117 	char *test_string_tmp;
118 
119 	asprintf(&test_string_tmp, "%s%c" MESSAGE_FORMAT,
120 	    test_string, MESSAGE_SEPARATOR, test_string);
121 	ATF_REQUIRE_MSG(test_string_tmp != NULL, "asprintf failed");
122 
123 	sb = sbuf_new_auto();
124 	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
125 	    strerror(errno));
126 
127 	ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
128 	ATF_REQUIRE_MSG(sbuf_putc(sb, MESSAGE_SEPARATOR) == 0,
129 	    "sbuf_putc failed");
130 
131 	ATF_REQUIRE_MSG(sbuf_printf(sb, MESSAGE_FORMAT, test_string) == 0,
132 	    "sbuf_printf failed");
133 
134 	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
135 	    strerror(errno));
136 
137 	ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), test_string_tmp,
138 	    "sbuf (\"%s\") != test string (\"%s\")", sbuf_data(sb),
139 	    test_string_tmp);
140 
141 	sbuf_delete(sb);
142 
143 	free(test_string_tmp);
144 }
145 
146 ATF_TC_WITHOUT_HEAD(sbuf_putbuf_test);
147 ATF_TC_BODY(sbuf_putbuf_test, tc)
148 {
149 	struct sbuf *sb;
150 	pid_t child_proc;
151 
152 	sb = sbuf_new_auto();
153 	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
154 	    strerror(errno));
155 
156 	ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
157 
158 	child_proc = atf_utils_fork();
159 	if (child_proc == 0) {
160 		ATF_CHECK_EQ(0, sbuf_finish(sb));
161 		sbuf_putbuf(sb);
162 		exit(0);
163 	}
164 	atf_utils_wait(child_proc, 0, test_string, "");
165 
166 	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
167 	    strerror(errno));
168 
169 	sbuf_delete(sb);
170 }
171 
172 ATF_TC_WITHOUT_HEAD(sbuf_vprintf_test);
173 ATF_TC_BODY(sbuf_vprintf_test, tc)
174 {
175 	struct sbuf *sb;
176 	char *test_string_tmp;
177 	int rc;
178 
179 	asprintf(&test_string_tmp, "%s%c" MESSAGE_FORMAT,
180 	    test_string, MESSAGE_SEPARATOR, test_string);
181 	ATF_REQUIRE_MSG(test_string_tmp != NULL, "asprintf failed");
182 
183 	sb = sbuf_new_auto();
184 	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
185 	    strerror(errno));
186 
187 	ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
188 	ATF_REQUIRE_MSG(sbuf_putc(sb, MESSAGE_SEPARATOR) == 0,
189 	    "sbuf_putc failed");
190 
191 	rc = sbuf_vprintf_helper(sb, MESSAGE_FORMAT, test_string);
192 	ATF_REQUIRE_MSG(rc == 0, "sbuf_vprintf failed");
193 
194 	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
195 	    strerror(errno));
196 
197 	ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), test_string_tmp,
198 	    "sbuf (\"%s\") != test string (\"%s\")", sbuf_data(sb),
199 	    test_string_tmp);
200 
201 	sbuf_delete(sb);
202 }
203 
204 ATF_TP_ADD_TCS(tp)
205 {
206 
207 	ATF_TP_ADD_TC(tp, sbuf_printf_drain_null_test);
208 	ATF_TP_ADD_TC(tp, sbuf_printf_drain_test);
209 	ATF_TP_ADD_TC(tp, sbuf_printf_test);
210 	ATF_TP_ADD_TC(tp, sbuf_putbuf_test);
211 	ATF_TP_ADD_TC(tp, sbuf_vprintf_test);
212 
213 	return (atf_no_error());
214 }
215