1 /* $NetBSD: t_strlen.c,v 1.5 2011/07/14 07:33:20 jruoho 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
)
43 /* try to trick the compiler */
44 size_t (*strlen_fn
)(const char *);
55 const struct tab tab
[] = {
57 * patterns that check for all combinations of leading and
58 * trailing unaligned characters (on a 64 bit processor)
72 { "abcdefghijk", 11 },
73 { "abcdefghijkl", 12 },
74 { "abcdefghijklm", 13 },
75 { "abcdefghijklmn", 14 },
76 { "abcdefghijklmno", 15 },
77 { "abcdefghijklmnop", 16 },
78 { "abcdefghijklmnopq", 17 },
79 { "abcdefghijklmnopqr", 18 },
80 { "abcdefghijklmnopqrs", 19 },
81 { "abcdefghijklmnopqrst", 20 },
82 { "abcdefghijklmnopqrstu", 21 },
83 { "abcdefghijklmnopqrstuv", 22 },
84 { "abcdefghijklmnopqrstuvw", 23 },
87 * patterns that check for the cases where the expression:
89 * ((word - 0x7f7f..7f) & 0x8080..80)
91 * returns non-zero even though there are no zero bytes in
95 { "" "\xff\xff\xff\xff\xff\xff\xff\xff" "abcdefgh", 16 },
96 { "a" "\xff\xff\xff\xff\xff\xff\xff\xff" "bcdefgh", 16 },
97 { "ab" "\xff\xff\xff\xff\xff\xff\xff\xff" "cdefgh", 16 },
98 { "abc" "\xff\xff\xff\xff\xff\xff\xff\xff" "defgh", 16 },
99 { "abcd" "\xff\xff\xff\xff\xff\xff\xff\xff" "efgh", 16 },
100 { "abcde" "\xff\xff\xff\xff\xff\xff\xff\xff" "fgh", 16 },
101 { "abcdef" "\xff\xff\xff\xff\xff\xff\xff\xff" "gh", 16 },
102 { "abcdefg" "\xff\xff\xff\xff\xff\xff\xff\xff" "h", 16 },
103 { "abcdefgh" "\xff\xff\xff\xff\xff\xff\xff\xff" "", 16 },
107 * During testing it is useful have the rest of the program
108 * use a known good version!
110 strlen_fn
= dlsym(dlopen(NULL
, RTLD_LAZY
), "test_strlen");
114 for (a
= 0; a
< sizeof(long); ++a
) {
115 for (t
= 0; t
< (sizeof(tab
) / sizeof(tab
[0])); ++t
) {
117 memcpy(&buf
[a
], tab
[t
].val
, tab
[t
].len
+ 1);
118 len
= strlen_fn(&buf
[a
]);
120 if (len
!= tab
[t
].len
) {
121 /* Write error without using printf / strlen */
122 write(2, "alignment ", 10);
124 write(2, ", test ", 7);
126 write(2, ", got len ", 10);
128 write(2, ", not ", 6);
129 write_num(tab
[t
].len
);
130 write(2, ", for '", 7);
131 write(2, tab
[t
].val
, tab
[t
].len
);
133 atf_tc_fail("See stderr for details");
140 ATF_TC_HEAD(strlen_huge
, tc
)
142 atf_tc_set_md_var(tc
, "descr", "Test strlen(3) with huge strings");
145 ATF_TC_BODY(strlen_huge
, tc
)
151 page
= sysconf(_SC_PAGESIZE
);
152 ATF_REQUIRE(page
>= 0);
154 for (i
= 1; i
< 1000; i
= i
+ 100) {
156 str
= malloc(i
* page
+ 1);
161 (void)memset(str
, 'x', i
* page
);
162 str
[i
* page
] = '\0';
164 ATF_REQUIRE(strlen(str
) == i
* page
);
169 ATF_TC(strnlen_basic
);
170 ATF_TC_HEAD(strnlen_basic
, tc
)
172 atf_tc_set_md_var(tc
, "descr", "A naive test of strnlen(3)");
175 ATF_TC_BODY(strnlen_basic
, tc
)
181 ATF_CHECK(strnlen(buf
, 000) == 0);
182 ATF_CHECK(strnlen(buf
, 111) == 0);
184 ATF_CHECK(strnlen("xxx", 0) == 0);
185 ATF_CHECK(strnlen("xxx", 1) == 1);
186 ATF_CHECK(strnlen("xxx", 2) == 2);
187 ATF_CHECK(strnlen("xxx", 3) == 3);
188 ATF_CHECK(strnlen("xxx", 9) == 3);
194 ATF_TP_ADD_TC(tp
, strlen_basic
);
195 ATF_TP_ADD_TC(tp
, strlen_huge
);
196 ATF_TP_ADD_TC(tp
, strnlen_basic
);
198 return atf_no_error();