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 29 #include <atf-c.h> 30 #include <errno.h> 31 #include <stdarg.h> 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <string.h> 35 #include <unistd.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), "", "sbuf (\"%s\") was not empty", 79 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 (void)arg; 107 (void)data; 108 (void)len; 109 110 return (0); 111 } 112 113 ATF_TC_WITHOUT_HEAD(sbuf_drain_ret0_test); 114 ATF_TC_BODY(sbuf_drain_ret0_test, tc) 115 { 116 struct sbuf *sb; 117 118 sb = sbuf_new_auto(); 119 120 sbuf_set_drain(sb, drain_ret0, NULL); 121 122 sbuf_cat(sb, test_string); 123 124 ATF_CHECK_EQ_MSG(-1, sbuf_finish(sb), 125 "required to return error when drain func returns 0"); 126 ATF_CHECK_EQ_MSG(EDEADLK, errno, 127 "errno required to be EDEADLK when drain func returns 0"); 128 } 129 130 ATF_TC_WITHOUT_HEAD(sbuf_len_test); 131 ATF_TC_BODY(sbuf_len_test, tc) 132 { 133 struct sbuf *sb; 134 ssize_t buf_len, test_string_len; 135 int i; 136 137 sb = sbuf_new_auto(); 138 ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", 139 strerror(errno)); 140 141 test_string_len = strlen(test_string); 142 for (i = 0; i < 20; i++) { 143 buf_len = sbuf_len(sb); 144 ATF_REQUIRE_MSG(buf_len == (ssize_t)(i * test_string_len), 145 "sbuf_len (%zd) != %zu", buf_len, i * test_string_len); 146 ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, 147 "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), 176 "sbuf_finish failed: %s", 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), 186 "failed to return error on overflow"); 187 188 sbuf_delete(&sb); 189 } 190 191 ATF_TC_WITHOUT_HEAD(sbuf_setpos_test); 192 ATF_TC_BODY(sbuf_setpos_test, tc) 193 { 194 struct sbuf *sb; 195 size_t test_string_chopped_len, test_string_len; 196 ssize_t buf_len; 197 198 sb = sbuf_new_auto(); 199 ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", 200 strerror(errno)); 201 202 /* 203 * An obvious sanity check -- if sbuf_len(..) lies, these invariants 204 * are impossible to test. 205 */ 206 ATF_REQUIRE(sbuf_len(sb) == 0); 207 208 ATF_CHECK(sbuf_setpos(sb, -1) == -1); 209 ATF_CHECK(sbuf_setpos(sb, 0) == 0); 210 ATF_CHECK(sbuf_setpos(sb, 1) == -1); 211 212 ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); 213 214 buf_len = sbuf_len(sb); 215 test_string_len = strlen(test_string); 216 test_string_chopped_len = test_string_len - TEST_STRING_CHOP_COUNT; 217 ATF_REQUIRE_MSG(buf_len == (ssize_t)test_string_len, 218 "sbuf length (%zd) != test_string length (%zu)", buf_len, 219 test_string_len); 220 221 /* Out of bounds (under length) */ 222 ATF_CHECK(sbuf_setpos(sb, -1) == -1); 223 /* 224 * Out of bounds (over length) 225 * 226 * Note: SBUF_INCLUDENUL not set, so take '\0' into account. 227 */ 228 ATF_CHECK(sbuf_setpos(sb, test_string_len + 2) == -1); 229 /* Within bounds */ 230 ATF_CHECK(sbuf_setpos(sb, test_string_chopped_len) == 0); 231 232 ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", 233 strerror(errno)); 234 235 buf_len = sbuf_len(sb); 236 ATF_REQUIRE_MSG(buf_len == (ssize_t)test_string_chopped_len, 237 "sbuf_setpos didn't truncate string as expected"); 238 ATF_REQUIRE_MSG(strncmp(sbuf_data(sb), test_string, buf_len) == 0, 239 "sbuf (\"%s\") != test string (\"%s\") for [0,%zd]", sbuf_data(sb), 240 test_string, buf_len); 241 242 sbuf_delete(sb); 243 } 244 245 ATF_TP_ADD_TCS(tp) 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