t_strlen.c revision 1.1 1 1.1 jruoho /* $NetBSD: t_strlen.c,v 1.1 2011/07/07 08:59:33 jruoho Exp $ */
2 1.1 jruoho
3 1.1 jruoho /*
4 1.1 jruoho * Written by J.T. Conklin <jtc (at) acorntoolworks.com>
5 1.1 jruoho * Public domain.
6 1.1 jruoho */
7 1.1 jruoho
8 1.1 jruoho #include <atf-c.h>
9 1.1 jruoho #include <string.h>
10 1.1 jruoho #include <unistd.h>
11 1.1 jruoho #include <stdio.h>
12 1.1 jruoho #include <stdlib.h>
13 1.1 jruoho #include <dlfcn.h>
14 1.1 jruoho
15 1.1 jruoho static void write_num(int);
16 1.1 jruoho
17 1.1 jruoho static void
18 1.1 jruoho write_num(int val)
19 1.1 jruoho {
20 1.1 jruoho char buf[20];
21 1.1 jruoho int i;
22 1.1 jruoho
23 1.1 jruoho for (i = sizeof buf; --i >= 0;) {
24 1.1 jruoho buf[i] = '0' + val % 10;
25 1.1 jruoho val /= 10;
26 1.1 jruoho if (val == 0) {
27 1.1 jruoho write(2, buf + i, sizeof buf - i);
28 1.1 jruoho return;
29 1.1 jruoho }
30 1.1 jruoho }
31 1.1 jruoho write(2, "overflow", 8);
32 1.1 jruoho }
33 1.1 jruoho
34 1.1 jruoho ATF_TC(strlen_basic);
35 1.1 jruoho ATF_TC_HEAD(strlen_basic, tc)
36 1.1 jruoho {
37 1.1 jruoho atf_tc_set_md_var(tc, "descr", "Test strlen(3) results");
38 1.1 jruoho }
39 1.1 jruoho
40 1.1 jruoho ATF_TC_BODY(strlen_basic, tc)
41 1.1 jruoho {
42 1.1 jruoho /* try to trick the compiler */
43 1.1 jruoho size_t (*strlen_fn)(const char *);
44 1.1 jruoho
45 1.1 jruoho unsigned int a, t;
46 1.1 jruoho size_t len;
47 1.1 jruoho char buf[64];
48 1.1 jruoho
49 1.1 jruoho struct tab {
50 1.1 jruoho const char* val;
51 1.1 jruoho size_t len;
52 1.1 jruoho };
53 1.1 jruoho
54 1.1 jruoho const struct tab tab[] = {
55 1.1 jruoho /*
56 1.1 jruoho * patterns that check for all combinations of leading and
57 1.1 jruoho * trailing unaligned characters (on a 64 bit processor)
58 1.1 jruoho */
59 1.1 jruoho
60 1.1 jruoho { "", 0 },
61 1.1 jruoho { "a", 1 },
62 1.1 jruoho { "ab", 2 },
63 1.1 jruoho { "abc", 3 },
64 1.1 jruoho { "abcd", 4 },
65 1.1 jruoho { "abcde", 5 },
66 1.1 jruoho { "abcdef", 6 },
67 1.1 jruoho { "abcdefg", 7 },
68 1.1 jruoho { "abcdefgh", 8 },
69 1.1 jruoho { "abcdefghi", 9 },
70 1.1 jruoho { "abcdefghij", 10 },
71 1.1 jruoho { "abcdefghijk", 11 },
72 1.1 jruoho { "abcdefghijkl", 12 },
73 1.1 jruoho { "abcdefghijklm", 13 },
74 1.1 jruoho { "abcdefghijklmn", 14 },
75 1.1 jruoho { "abcdefghijklmno", 15 },
76 1.1 jruoho { "abcdefghijklmnop", 16 },
77 1.1 jruoho { "abcdefghijklmnopq", 17 },
78 1.1 jruoho { "abcdefghijklmnopqr", 18 },
79 1.1 jruoho { "abcdefghijklmnopqrs", 19 },
80 1.1 jruoho { "abcdefghijklmnopqrst", 20 },
81 1.1 jruoho { "abcdefghijklmnopqrstu", 21 },
82 1.1 jruoho { "abcdefghijklmnopqrstuv", 22 },
83 1.1 jruoho { "abcdefghijklmnopqrstuvw", 23 },
84 1.1 jruoho
85 1.1 jruoho /*
86 1.1 jruoho * patterns that check for the cases where the expression:
87 1.1 jruoho *
88 1.1 jruoho * ((word - 0x7f7f..7f) & 0x8080..80)
89 1.1 jruoho *
90 1.1 jruoho * returns non-zero even though there are no zero bytes in
91 1.1 jruoho * the word.
92 1.1 jruoho */
93 1.1 jruoho
94 1.1 jruoho { "" "\xff\xff\xff\xff\xff\xff\xff\xff" "abcdefgh", 16 },
95 1.1 jruoho { "a" "\xff\xff\xff\xff\xff\xff\xff\xff" "bcdefgh", 16 },
96 1.1 jruoho { "ab" "\xff\xff\xff\xff\xff\xff\xff\xff" "cdefgh", 16 },
97 1.1 jruoho { "abc" "\xff\xff\xff\xff\xff\xff\xff\xff" "defgh", 16 },
98 1.1 jruoho { "abcd" "\xff\xff\xff\xff\xff\xff\xff\xff" "efgh", 16 },
99 1.1 jruoho { "abcde" "\xff\xff\xff\xff\xff\xff\xff\xff" "fgh", 16 },
100 1.1 jruoho { "abcdef" "\xff\xff\xff\xff\xff\xff\xff\xff" "gh", 16 },
101 1.1 jruoho { "abcdefg" "\xff\xff\xff\xff\xff\xff\xff\xff" "h", 16 },
102 1.1 jruoho { "abcdefgh" "\xff\xff\xff\xff\xff\xff\xff\xff" "", 16 },
103 1.1 jruoho };
104 1.1 jruoho
105 1.1 jruoho /*
106 1.1 jruoho * During testing it is useful have the rest of the program
107 1.1 jruoho * use a known good version!
108 1.1 jruoho */
109 1.1 jruoho strlen_fn = dlsym(dlopen(NULL, RTLD_LAZY), "test_strlen");
110 1.1 jruoho if (!strlen_fn)
111 1.1 jruoho strlen_fn = strlen;
112 1.1 jruoho
113 1.1 jruoho for (a = 0; a < sizeof(long); ++a) {
114 1.1 jruoho for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
115 1.1 jruoho
116 1.1 jruoho memcpy(&buf[a], tab[t].val, tab[t].len + 1);
117 1.1 jruoho len = strlen_fn(&buf[a]);
118 1.1 jruoho
119 1.1 jruoho if (len != tab[t].len) {
120 1.1 jruoho /* Write error without using printf / strlen */
121 1.1 jruoho write(2, "alignment ", 10);
122 1.1 jruoho write_num(a);
123 1.1 jruoho write(2, ", test ", 7);
124 1.1 jruoho write_num(t);
125 1.1 jruoho write(2, ", got len ", 10);
126 1.1 jruoho write_num(len);
127 1.1 jruoho write(2, ", not ", 6);
128 1.1 jruoho write_num(tab[t].len);
129 1.1 jruoho write(2, ", for '", 7);
130 1.1 jruoho write(2, tab[t].val, tab[t].len);
131 1.1 jruoho write(2, "'\n", 2);
132 1.1 jruoho atf_tc_fail("See stderr for details");
133 1.1 jruoho }
134 1.1 jruoho }
135 1.1 jruoho }
136 1.1 jruoho }
137 1.1 jruoho
138 1.1 jruoho ATF_TP_ADD_TCS(tp)
139 1.1 jruoho {
140 1.1 jruoho
141 1.1 jruoho ATF_TP_ADD_TC(tp, strlen_basic);
142 1.1 jruoho
143 1.1 jruoho return atf_no_error();
144 1.1 jruoho }
145