xref: /freebsd/lib/libsbuf/tests/sbuf_core_test.c (revision 4543ef516683042d46f3bd3bb8a4f3f746e00499)
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 #define	TEST_STRING_CHOP_COUNT	5
41 _Static_assert(nitems(test_string) > TEST_STRING_CHOP_COUNT,
42     "test_string is too short");
43 
44 ATF_TC_WITHOUT_HEAD(sbuf_clear_test);
45 ATF_TC_BODY(sbuf_clear_test, tc)
46 {
47 	struct sbuf *sb;
48 	ssize_t buf_len;
49 	pid_t child_proc;
50 
51 	sb = sbuf_new_auto();
52 	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
53 	    strerror(errno));
54 
55 	ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
56 
57 	/*
58 	 * Cheat so we can get the contents of the buffer before calling
59 	 * sbuf_finish(3) below, making additional sbuf changes impossible.
60 	 */
61 	child_proc = atf_utils_fork();
62 	if (child_proc == 0) {
63 		ATF_REQUIRE_EQ_MSG(0, sbuf_finish(sb), "sbuf_finish failed: %s",
64 		    strerror(errno));
65 
66 		sbuf_putbuf(sb);
67 		exit(0);
68 	}
69 	atf_utils_wait(child_proc, 0, test_string, "");
70 
71 	sbuf_clear(sb);
72 
73 	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
74 	    strerror(errno));
75 
76 	buf_len = sbuf_len(sb);
77 	ATF_REQUIRE_MSG(buf_len == 0, "sbuf_len (%zd) != 0", buf_len);
78 	ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), "",
79 	    "sbuf (\"%s\") was not empty", sbuf_data(sb));
80 
81 	sbuf_delete(sb);
82 }
83 
84 ATF_TC_WITHOUT_HEAD(sbuf_done_and_sbuf_finish_test);
85 ATF_TC_BODY(sbuf_done_and_sbuf_finish_test, tc)
86 {
87 	struct sbuf *sb;
88 
89 	sb = sbuf_new_auto();
90 	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
91 	    strerror(errno));
92 
93 	ATF_CHECK(sbuf_done(sb) == 0);
94 
95 	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
96 	    strerror(errno));
97 
98 	ATF_CHECK(sbuf_done(sb) != 0);
99 
100 	sbuf_delete(sb);
101 }
102 
103 static int
104 drain_ret0(void *arg, const char *data, int len)
105 {
106 
107 	(void)arg;
108 	(void)data;
109 	(void)len;
110 
111 	return (0);
112 }
113 
114 ATF_TC_WITHOUT_HEAD(sbuf_drain_ret0_test);
115 ATF_TC_BODY(sbuf_drain_ret0_test, tc)
116 {
117 	struct sbuf *sb;
118 
119 	sb = sbuf_new_auto();
120 
121 	sbuf_set_drain(sb, drain_ret0, NULL);
122 
123 	sbuf_cat(sb, test_string);
124 
125 	ATF_CHECK_EQ_MSG(-1, sbuf_finish(sb),
126 	    "required to return error when drain func returns 0");
127 	ATF_CHECK_EQ_MSG(EDEADLK, errno,
128 	    "errno required to be EDEADLK when drain func returns 0");
129 }
130 
131 ATF_TC_WITHOUT_HEAD(sbuf_len_test);
132 ATF_TC_BODY(sbuf_len_test, tc)
133 {
134 	struct sbuf *sb;
135 	ssize_t buf_len, test_string_len;
136 	int i;
137 
138 	sb = sbuf_new_auto();
139 	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
140 	    strerror(errno));
141 
142 	test_string_len = strlen(test_string);
143 	for (i = 0; i < 20; i++) {
144 		buf_len = sbuf_len(sb);
145 		ATF_REQUIRE_MSG(buf_len == (ssize_t)(i * test_string_len),
146 		    "sbuf_len (%zd) != %zu", buf_len, i * test_string_len);
147 		ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
148 	}
149 
150 #ifdef	HAVE_SBUF_SET_FLAGS
151 	sbuf_set_flags(sb, SBUF_INCLUDENUL);
152 	ATF_REQUIRE_MSG((ssize_t)(i * test_string_len + 1) == sbuf_len(sb),
153 	    "sbuf_len(..) didn't report the NUL char");
154 #endif
155 
156 	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
157 	    strerror(errno));
158 
159 	sbuf_delete(sb);
160 }
161 
162 ATF_TC_WITHOUT_HEAD(sbuf_new_fixedlen);
163 ATF_TC_BODY(sbuf_new_fixedlen, tc)
164 {
165 	char buf[strlen(test_string) + 1];
166 	struct sbuf sb;
167 	pid_t child_proc;
168 
169 	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
170 
171 	sbuf_cat(&sb, test_string);
172 
173 	child_proc = atf_utils_fork();
174 	if (child_proc == 0) {
175 		ATF_REQUIRE_EQ_MSG(0, sbuf_finish(&sb), "sbuf_finish failed: %s",
176 		    strerror(errno));
177 
178 		sbuf_putbuf(&sb);
179 		exit(0);
180 	}
181 	atf_utils_wait(child_proc, 0, test_string, "");
182 
183 	sbuf_putc(&sb, ' ');
184 
185 	ATF_CHECK_EQ_MSG(-1, sbuf_finish(&sb), "failed to return error on overflow");
186 
187 	sbuf_delete(&sb);
188 }
189 
190 ATF_TC_WITHOUT_HEAD(sbuf_setpos_test);
191 ATF_TC_BODY(sbuf_setpos_test, tc)
192 {
193 	struct sbuf *sb;
194 	size_t test_string_chopped_len, test_string_len;
195 	ssize_t buf_len;
196 
197 	sb = sbuf_new_auto();
198 	ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
199 	    strerror(errno));
200 
201 	/*
202 	 * An obvious sanity check -- if sbuf_len(..) lies, these invariants
203 	 * are impossible to test.
204 	 */
205 	ATF_REQUIRE(sbuf_len(sb) == 0);
206 
207 	ATF_CHECK(sbuf_setpos(sb, -1) == -1);
208 	ATF_CHECK(sbuf_setpos(sb, 0) == 0);
209 	ATF_CHECK(sbuf_setpos(sb, 1) == -1);
210 
211 	ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
212 
213 	buf_len = sbuf_len(sb);
214 	test_string_len = strlen(test_string);
215 	test_string_chopped_len = test_string_len - TEST_STRING_CHOP_COUNT;
216 	ATF_REQUIRE_MSG(buf_len == (ssize_t)test_string_len,
217 	    "sbuf length (%zd) != test_string length (%zu)", buf_len,
218 	    test_string_len);
219 
220 	/* Out of bounds (under length) */
221 	ATF_CHECK(sbuf_setpos(sb, -1) == -1);
222 	/*
223 	 * Out of bounds (over length)
224 	 *
225 	 * Note: SBUF_INCLUDENUL not set, so take '\0' into account.
226 	 */
227 	ATF_CHECK(sbuf_setpos(sb, test_string_len + 2) == -1);
228 	/* Within bounds */
229 	ATF_CHECK(sbuf_setpos(sb, test_string_chopped_len) == 0);
230 
231 	ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
232 	    strerror(errno));
233 
234 	buf_len = sbuf_len(sb);
235 	ATF_REQUIRE_MSG(buf_len == (ssize_t)test_string_chopped_len,
236 	    "sbuf_setpos didn't truncate string as expected");
237 	ATF_REQUIRE_MSG(strncmp(sbuf_data(sb), test_string, buf_len) == 0,
238 	    "sbuf (\"%s\") != test string (\"%s\") for [0,%zd]", sbuf_data(sb),
239 	    test_string, buf_len);
240 
241 	sbuf_delete(sb);
242 }
243 
244 ATF_TP_ADD_TCS(tp)
245 {
246 
247 	ATF_TP_ADD_TC(tp, sbuf_clear_test);
248 	ATF_TP_ADD_TC(tp, sbuf_done_and_sbuf_finish_test);
249 	ATF_TP_ADD_TC(tp, sbuf_drain_ret0_test);
250 	ATF_TP_ADD_TC(tp, sbuf_len_test);
251 	ATF_TP_ADD_TC(tp, sbuf_new_fixedlen);
252 #if 0
253 	/* TODO */
254 #ifdef	HAVE_SBUF_CLEAR_FLAGS
255 	ATF_TP_ADD_TC(tp, sbuf_clear_flags_test);
256 #endif
257 #ifdef	HAVE_SBUF_GET_FLAGS
258 	ATF_TP_ADD_TC(tp, sbuf_get_flags_test);
259 #endif
260 	ATF_TP_ADD_TC(tp, sbuf_new_positive_test);
261 	ATF_TP_ADD_TC(tp, sbuf_new_negative_test);
262 #ifdef	HAVE_SBUF_SET_FLAGS
263 	ATF_TP_ADD_TC(tp, sbuf_set_flags_test);
264 #endif
265 #endif
266 	ATF_TP_ADD_TC(tp, sbuf_setpos_test);
267 
268 	return (atf_no_error());
269 }
270