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/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/param.h> 30 #include <sys/sbuf.h> 31 #include <errno.h> 32 #include <stdarg.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 38 #include <atf-c.h> 39 40 #include "sbuf_test_common.h" 41 42 static char test_string[] = "this is a test string"; 43 #define TEST_STRING_CHOP_COUNT 5 44 _Static_assert(nitems(test_string) > TEST_STRING_CHOP_COUNT, 45 "test_string is too short"); 46 47 ATF_TC_WITHOUT_HEAD(sbuf_clear_test); 48 ATF_TC_BODY(sbuf_clear_test, tc) 49 { 50 struct sbuf *sb; 51 ssize_t buf_len; 52 pid_t child_proc; 53 54 sb = sbuf_new_auto(); 55 ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", 56 strerror(errno)); 57 58 ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); 59 60 /* 61 * Cheat so we can get the contents of the buffer before calling 62 * sbuf_finish(3) below, making additional sbuf changes impossible. 63 */ 64 child_proc = atf_utils_fork(); 65 if (child_proc == 0) { 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 ATF_TC_WITHOUT_HEAD(sbuf_len_test); 104 ATF_TC_BODY(sbuf_len_test, tc) 105 { 106 struct sbuf *sb; 107 ssize_t buf_len, test_string_len; 108 int i; 109 110 sb = sbuf_new_auto(); 111 ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", 112 strerror(errno)); 113 114 test_string_len = strlen(test_string); 115 for (i = 0; i < 20; i++) { 116 buf_len = sbuf_len(sb); 117 ATF_REQUIRE_MSG(buf_len == (ssize_t)(i * test_string_len), 118 "sbuf_len (%zd) != %zu", buf_len, i * test_string_len); 119 ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); 120 } 121 122 #ifdef HAVE_SBUF_SET_FLAGS 123 sbuf_set_flags(sb, SBUF_INCLUDENUL); 124 ATF_REQUIRE_MSG((ssize_t)(i * test_string_len + 1) == sbuf_len(sb), 125 "sbuf_len(..) didn't report the NUL char"); 126 #endif 127 128 ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", 129 strerror(errno)); 130 131 sbuf_delete(sb); 132 } 133 134 ATF_TC_WITHOUT_HEAD(sbuf_setpos_test); 135 ATF_TC_BODY(sbuf_setpos_test, tc) 136 { 137 struct sbuf *sb; 138 size_t test_string_chopped_len, test_string_len; 139 ssize_t buf_len; 140 141 sb = sbuf_new_auto(); 142 ATF_REQUIRE_MSG(sb != NULL, "sbuf_new_auto failed: %s", 143 strerror(errno)); 144 145 /* 146 * An obvious sanity check -- if sbuf_len(..) lies, these invariants 147 * are impossible to test. 148 */ 149 ATF_REQUIRE(sbuf_len(sb) == 0); 150 151 ATF_CHECK(sbuf_setpos(sb, -1) == -1); 152 ATF_CHECK(sbuf_setpos(sb, 0) == 0); 153 ATF_CHECK(sbuf_setpos(sb, 1) == -1); 154 155 ATF_REQUIRE_MSG(sbuf_cat(sb, test_string) == 0, "sbuf_cat failed"); 156 157 buf_len = sbuf_len(sb); 158 test_string_len = strlen(test_string); 159 test_string_chopped_len = test_string_len - TEST_STRING_CHOP_COUNT; 160 ATF_REQUIRE_MSG(buf_len == (ssize_t)test_string_len, 161 "sbuf length (%zd) != test_string length (%zu)", buf_len, 162 test_string_len); 163 164 /* Out of bounds (under length) */ 165 ATF_CHECK(sbuf_setpos(sb, -1) == -1); 166 /* 167 * Out of bounds (over length) 168 * 169 * Note: SBUF_INCLUDENUL not set, so take '\0' into account. 170 */ 171 ATF_CHECK(sbuf_setpos(sb, test_string_len + 2) == -1); 172 /* Within bounds */ 173 ATF_CHECK(sbuf_setpos(sb, test_string_chopped_len) == 0); 174 175 ATF_REQUIRE_MSG(sbuf_finish(sb) == 0, "sbuf_finish failed: %s", 176 strerror(errno)); 177 178 buf_len = sbuf_len(sb); 179 ATF_REQUIRE_MSG(buf_len == (ssize_t)test_string_chopped_len, 180 "sbuf_setpos didn't truncate string as expected"); 181 ATF_REQUIRE_MSG(strncmp(sbuf_data(sb), test_string, buf_len) == 0, 182 "sbuf (\"%s\") != test string (\"%s\") for [0,%zd]", sbuf_data(sb), 183 test_string, buf_len); 184 185 sbuf_delete(sb); 186 } 187 188 ATF_TP_ADD_TCS(tp) 189 { 190 191 ATF_TP_ADD_TC(tp, sbuf_clear_test); 192 ATF_TP_ADD_TC(tp, sbuf_done_and_sbuf_finish_test); 193 ATF_TP_ADD_TC(tp, sbuf_len_test); 194 #if 0 195 /* TODO */ 196 #ifdef HAVE_SBUF_CLEAR_FLAGS 197 ATF_TP_ADD_TC(tp, sbuf_clear_flags_test); 198 #endif 199 #ifdef HAVE_SBUF_GET_FLAGS 200 ATF_TP_ADD_TC(tp, sbuf_get_flags_test); 201 #endif 202 ATF_TP_ADD_TC(tp, sbuf_new_positive_test); 203 ATF_TP_ADD_TC(tp, sbuf_new_negative_test); 204 #ifdef HAVE_SBUF_SET_FLAGS 205 ATF_TP_ADD_TC(tp, sbuf_set_flags_test); 206 #endif 207 #endif 208 ATF_TP_ADD_TC(tp, sbuf_setpos_test); 209 210 return (atf_no_error()); 211 } 212