1 /* $NetBSD: t_strlen.c,v 1.6 2017/01/14 20:49:24 christos Exp $ */
4 * Written by J.T. Conklin <jtc@acorntoolworks.com>
16 static void write_num(int);
24 for (i = sizeof buf; --i >= 0;) {
25 buf[i] = '0' + val % 10;
28 write(2, buf + i, sizeof buf - i);
32 write(2, "overflow", 8);
36 ATF_TC_HEAD(strlen_basic, tc)
38 atf_tc_set_md_var(tc, "descr", "Test strlen(3) results");
41 ATF_TC_BODY(strlen_basic, tc)
44 /* try to trick the compiler */
45 size_t (*strlen_fn)(const char *);
56 const struct tab tab[] = {
58 * patterns that check for all combinations of leading and
59 * trailing unaligned characters (on a 64 bit processor)
73 { "abcdefghijk", 11 },
74 { "abcdefghijkl", 12 },
75 { "abcdefghijklm", 13 },
76 { "abcdefghijklmn", 14 },
77 { "abcdefghijklmno", 15 },
78 { "abcdefghijklmnop", 16 },
79 { "abcdefghijklmnopq", 17 },
80 { "abcdefghijklmnopqr", 18 },
81 { "abcdefghijklmnopqrs", 19 },
82 { "abcdefghijklmnopqrst", 20 },
83 { "abcdefghijklmnopqrstu", 21 },
84 { "abcdefghijklmnopqrstuv", 22 },
85 { "abcdefghijklmnopqrstuvw", 23 },
88 * patterns that check for the cases where the expression:
90 * ((word - 0x7f7f..7f) & 0x8080..80)
92 * returns non-zero even though there are no zero bytes in
96 { "" "\xff\xff\xff\xff\xff\xff\xff\xff" "abcdefgh", 16 },
97 { "a" "\xff\xff\xff\xff\xff\xff\xff\xff" "bcdefgh", 16 },
98 { "ab" "\xff\xff\xff\xff\xff\xff\xff\xff" "cdefgh", 16 },
99 { "abc" "\xff\xff\xff\xff\xff\xff\xff\xff" "defgh", 16 },
100 { "abcd" "\xff\xff\xff\xff\xff\xff\xff\xff" "efgh", 16 },
101 { "abcde" "\xff\xff\xff\xff\xff\xff\xff\xff" "fgh", 16 },
102 { "abcdef" "\xff\xff\xff\xff\xff\xff\xff\xff" "gh", 16 },
103 { "abcdefg" "\xff\xff\xff\xff\xff\xff\xff\xff" "h", 16 },
104 { "abcdefgh" "\xff\xff\xff\xff\xff\xff\xff\xff" "", 16 },
108 * During testing it is useful have the rest of the program
109 * use a known good version!
111 dl_handle = dlopen(NULL, RTLD_LAZY);
112 strlen_fn = dlsym(dl_handle, "test_strlen");
116 for (a = 0; a < sizeof(long); ++a) {
117 for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
119 memcpy(&buf[a], tab[t].val, tab[t].len + 1);
120 len = strlen_fn(&buf[a]);
122 if (len != tab[t].len) {
123 /* Write error without using printf / strlen */
124 write(2, "alignment ", 10);
126 write(2, ", test ", 7);
128 write(2, ", got len ", 10);
130 write(2, ", not ", 6);
131 write_num(tab[t].len);
132 write(2, ", for '", 7);
133 write(2, tab[t].val, tab[t].len);
135 atf_tc_fail("See stderr for details");
139 (void)dlclose(dl_handle);
143 ATF_TC_HEAD(strlen_huge, tc)
145 atf_tc_set_md_var(tc, "descr", "Test strlen(3) with huge strings");
148 ATF_TC_BODY(strlen_huge, tc)
154 page = sysconf(_SC_PAGESIZE);
155 ATF_REQUIRE(page >= 0);
157 for (i = 1; i < 1000; i = i + 100) {
159 str = malloc(i * page + 1);
164 (void)memset(str, 'x', i * page);
165 str[i * page] = '\0';
167 ATF_REQUIRE(strlen(str) == i * page);
172 ATF_TC(strnlen_basic);
173 ATF_TC_HEAD(strnlen_basic, tc)
175 atf_tc_set_md_var(tc, "descr", "A naive test of strnlen(3)");
178 ATF_TC_BODY(strnlen_basic, tc)
184 ATF_CHECK(strnlen(buf, 000) == 0);
185 ATF_CHECK(strnlen(buf, 111) == 0);
187 ATF_CHECK(strnlen("xxx", 0) == 0);
188 ATF_CHECK(strnlen("xxx", 1) == 1);
189 ATF_CHECK(strnlen("xxx", 2) == 2);
190 ATF_CHECK(strnlen("xxx", 3) == 3);
191 ATF_CHECK(strnlen("xxx", 9) == 3);
197 ATF_TP_ADD_TC(tp, strlen_basic);
198 ATF_TP_ADD_TC(tp, strlen_huge);
199 ATF_TP_ADD_TC(tp, strnlen_basic);
201 return atf_no_error();