2 * Copyright (c) 2017 Ngie Cooper <ngie@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
41 #include "sbuf_test_common.h"
43 static char test_string[] = "this is a test string";
44 #define TEST_STRING_CHOP_COUNT 5
45 _Static_assert(nitems(test_string) > TEST_STRING_CHOP_COUNT,
46 "test_string is too short");
48 ATF_TC_WITHOUT_HEAD(sbuf_clear_test);
49 ATF_TC_BODY(sbuf_clear_test, tc)
56 ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
59 ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
62 * Cheat so we can get the contents of the buffer before calling
63 * sbuf_finish(3) below, making additional sbuf changes impossible.
65 child_proc = atf_utils_fork();
66 if (child_proc == 0) {
70 atf_utils_wait(child_proc, 0, test_string, "");
74 ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
77 buf_len = sbuf_len(sb);
78 ATF_REQUIRE_MSG(buf_len == 0, "sbuf_len (%zd) != 0", buf_len);
79 ATF_REQUIRE_STREQ_MSG(sbuf_data(sb), "",
80 "sbuf (\"%s\") was not empty", sbuf_data(sb));
85 ATF_TC_WITHOUT_HEAD(sbuf_done_and_sbuf_finish_test);
86 ATF_TC_BODY(sbuf_done_and_sbuf_finish_test, tc)
91 ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
94 ATF_CHECK(sbuf_done(sb) == 0);
96 ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
99 ATF_CHECK(sbuf_done(sb) != 0);
104 ATF_TC_WITHOUT_HEAD(sbuf_len_test);
105 ATF_TC_BODY(sbuf_len_test, tc)
108 ssize_t buf_len, test_string_len;
111 sb = sbuf_new_auto();
112 ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
115 test_string_len = strlen(test_string);
116 for (i = 0; i < 20; i++) {
117 buf_len = sbuf_len(sb);
118 ATF_REQUIRE_MSG(buf_len == (ssize_t)(i * test_string_len),
119 "sbuf_len (%zd) != %zu", buf_len, i * test_string_len);
120 ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
123 #ifdef HAVE_SBUF_SET_FLAGS
124 sbuf_set_flags(sb, SBUF_INCLUDENUL);
125 ATF_REQUIRE_MSG((ssize_t)(i * test_string_len + 1) == sbuf_len(sb),
126 "sbuf_len(..) didn't report the NUL char");
129 ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
135 ATF_TC_WITHOUT_HEAD(sbuf_setpos_test);
136 ATF_TC_BODY(sbuf_setpos_test, tc)
139 size_t test_string_chopped_len, test_string_len;
142 sb = sbuf_new_auto();
143 ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s",
147 * An obvious sanity check -- if sbuf_len(..) lies, these invariants
148 * are impossible to test.
150 ATF_REQUIRE(sbuf_len(sb) == 0);
152 ATF_CHECK(sbuf_setpos(sb, -1) == -1);
153 ATF_CHECK(sbuf_setpos(sb, 0) == 0);
154 ATF_CHECK(sbuf_setpos(sb, 1) == -1);
156 ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed");
158 buf_len = sbuf_len(sb);
159 test_string_len = strlen(test_string);
160 test_string_chopped_len = test_string_len - TEST_STRING_CHOP_COUNT;
161 ATF_REQUIRE_MSG(buf_len == (ssize_t)test_string_len,
162 "sbuf length (%zd) != test_string length (%zu)", buf_len,
165 /* Out of bounds (under length) */
166 ATF_CHECK(sbuf_setpos(sb, -1) == -1);
168 * Out of bounds (over length)
170 * Note: SBUF_INCLUDENUL not set, so take '\0' into account.
172 ATF_CHECK(sbuf_setpos(sb, test_string_len + 2) == -1);
174 ATF_CHECK(sbuf_setpos(sb, test_string_chopped_len) == 0);
176 ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s",
179 buf_len = sbuf_len(sb);
180 ATF_REQUIRE_MSG(buf_len == (ssize_t)test_string_chopped_len,
181 "sbuf_setpos didn't truncate string as expected");
182 ATF_REQUIRE_MSG(strncmp(sbuf_data(sb), test_string, buf_len) == 0,
183 "sbuf (\"%s\") != test string (\"%s\") for [0,%zd]", sbuf_data(sb),
184 test_string, buf_len);
192 ATF_TP_ADD_TC(tp, sbuf_clear_test);
193 ATF_TP_ADD_TC(tp, sbuf_done_and_sbuf_finish_test);
194 ATF_TP_ADD_TC(tp, sbuf_len_test);
197 #ifdef HAVE_SBUF_CLEAR_FLAGS
198 ATF_TP_ADD_TC(tp, sbuf_clear_flags_test);
200 #ifdef HAVE_SBUF_GET_FLAGS
201 ATF_TP_ADD_TC(tp, sbuf_get_flags_test);
203 ATF_TP_ADD_TC(tp, sbuf_new_positive_test);
204 ATF_TP_ADD_TC(tp, sbuf_new_negative_test);
205 #ifdef HAVE_SBUF_SET_FLAGS
206 ATF_TP_ADD_TC(tp, sbuf_set_flags_test);
209 ATF_TP_ADD_TC(tp, sbuf_setpos_test);
211 return (atf_no_error());