1*11be35a1SLionel Sambuc /* $NetBSD: t_strchr.c,v 1.1 2011/07/07 08:59:33 jruoho Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*
4*11be35a1SLionel Sambuc * Written by J.T. Conklin <jtc@acorntoolworks.com>
5*11be35a1SLionel Sambuc * Public domain.
6*11be35a1SLionel Sambuc */
7*11be35a1SLionel Sambuc
8*11be35a1SLionel Sambuc #include <atf-c.h>
9*11be35a1SLionel Sambuc #include <string.h>
10*11be35a1SLionel Sambuc #include <unistd.h>
11*11be35a1SLionel Sambuc #include <stdio.h>
12*11be35a1SLionel Sambuc #include <stdlib.h>
13*11be35a1SLionel Sambuc #include <dlfcn.h>
14*11be35a1SLionel Sambuc
15*11be35a1SLionel Sambuc static char *slow_strchr(char *, int);
16*11be35a1SLionel Sambuc static void verify_strchr(char *, int, unsigned int, unsigned int);
17*11be35a1SLionel Sambuc
18*11be35a1SLionel Sambuc char * (*volatile strchr_fn)(const char *, int);
19*11be35a1SLionel Sambuc
20*11be35a1SLionel Sambuc static char *
slow_strchr(char * buf,int ch)21*11be35a1SLionel Sambuc slow_strchr(char *buf, int ch)
22*11be35a1SLionel Sambuc {
23*11be35a1SLionel Sambuc unsigned char c = 1;
24*11be35a1SLionel Sambuc
25*11be35a1SLionel Sambuc ch &= 0xff;
26*11be35a1SLionel Sambuc
27*11be35a1SLionel Sambuc for (; c != 0; buf++) {
28*11be35a1SLionel Sambuc c = *buf;
29*11be35a1SLionel Sambuc if (c == ch)
30*11be35a1SLionel Sambuc return buf;
31*11be35a1SLionel Sambuc }
32*11be35a1SLionel Sambuc return 0;
33*11be35a1SLionel Sambuc }
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc static void
verify_strchr(char * buf,int ch,unsigned int t,unsigned int a)36*11be35a1SLionel Sambuc verify_strchr(char *buf, int ch, unsigned int t, unsigned int a)
37*11be35a1SLionel Sambuc {
38*11be35a1SLionel Sambuc const char *off, *ok_off;
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc off = strchr_fn(buf, ch);
41*11be35a1SLionel Sambuc ok_off = slow_strchr(buf, ch);
42*11be35a1SLionel Sambuc if (off == ok_off)
43*11be35a1SLionel Sambuc return;
44*11be35a1SLionel Sambuc
45*11be35a1SLionel Sambuc fprintf(stderr, "test_strchr(\"%s\", %#x) gave %zd not %zd (test %d, "
46*11be35a1SLionel Sambuc "alignment %d)\n",
47*11be35a1SLionel Sambuc buf, ch, off ? off - buf : -1, ok_off ? ok_off - buf : -1, t, a);
48*11be35a1SLionel Sambuc
49*11be35a1SLionel Sambuc atf_tc_fail("Check stderr for details");
50*11be35a1SLionel Sambuc }
51*11be35a1SLionel Sambuc
52*11be35a1SLionel Sambuc ATF_TC(strchr_basic);
ATF_TC_HEAD(strchr_basic,tc)53*11be35a1SLionel Sambuc ATF_TC_HEAD(strchr_basic, tc)
54*11be35a1SLionel Sambuc {
55*11be35a1SLionel Sambuc
56*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test strchr(3) results");
57*11be35a1SLionel Sambuc }
58*11be35a1SLionel Sambuc
ATF_TC_BODY(strchr_basic,tc)59*11be35a1SLionel Sambuc ATF_TC_BODY(strchr_basic, tc)
60*11be35a1SLionel Sambuc {
61*11be35a1SLionel Sambuc unsigned int t, a;
62*11be35a1SLionel Sambuc char *off;
63*11be35a1SLionel Sambuc char buf[32];
64*11be35a1SLionel Sambuc
65*11be35a1SLionel Sambuc const char *tab[] = {
66*11be35a1SLionel Sambuc "",
67*11be35a1SLionel Sambuc "a",
68*11be35a1SLionel Sambuc "aa",
69*11be35a1SLionel Sambuc "abc",
70*11be35a1SLionel Sambuc "abcd",
71*11be35a1SLionel Sambuc "abcde",
72*11be35a1SLionel Sambuc "abcdef",
73*11be35a1SLionel Sambuc "abcdefg",
74*11be35a1SLionel Sambuc "abcdefgh",
75*11be35a1SLionel Sambuc
76*11be35a1SLionel Sambuc "/",
77*11be35a1SLionel Sambuc "//",
78*11be35a1SLionel Sambuc "/a",
79*11be35a1SLionel Sambuc "/a/",
80*11be35a1SLionel Sambuc "/ab",
81*11be35a1SLionel Sambuc "/ab/",
82*11be35a1SLionel Sambuc "/abc",
83*11be35a1SLionel Sambuc "/abc/",
84*11be35a1SLionel Sambuc "/abcd",
85*11be35a1SLionel Sambuc "/abcd/",
86*11be35a1SLionel Sambuc "/abcde",
87*11be35a1SLionel Sambuc "/abcde/",
88*11be35a1SLionel Sambuc "/abcdef",
89*11be35a1SLionel Sambuc "/abcdef/",
90*11be35a1SLionel Sambuc "/abcdefg",
91*11be35a1SLionel Sambuc "/abcdefg/",
92*11be35a1SLionel Sambuc "/abcdefgh",
93*11be35a1SLionel Sambuc "/abcdefgh/",
94*11be35a1SLionel Sambuc
95*11be35a1SLionel Sambuc "a/",
96*11be35a1SLionel Sambuc "a//",
97*11be35a1SLionel Sambuc "a/a",
98*11be35a1SLionel Sambuc "a/a/",
99*11be35a1SLionel Sambuc "a/ab",
100*11be35a1SLionel Sambuc "a/ab/",
101*11be35a1SLionel Sambuc "a/abc",
102*11be35a1SLionel Sambuc "a/abc/",
103*11be35a1SLionel Sambuc "a/abcd",
104*11be35a1SLionel Sambuc "a/abcd/",
105*11be35a1SLionel Sambuc "a/abcde",
106*11be35a1SLionel Sambuc "a/abcde/",
107*11be35a1SLionel Sambuc "a/abcdef",
108*11be35a1SLionel Sambuc "a/abcdef/",
109*11be35a1SLionel Sambuc "a/abcdefg",
110*11be35a1SLionel Sambuc "a/abcdefg/",
111*11be35a1SLionel Sambuc "a/abcdefgh",
112*11be35a1SLionel Sambuc "a/abcdefgh/",
113*11be35a1SLionel Sambuc
114*11be35a1SLionel Sambuc "ab/",
115*11be35a1SLionel Sambuc "ab//",
116*11be35a1SLionel Sambuc "ab/a",
117*11be35a1SLionel Sambuc "ab/a/",
118*11be35a1SLionel Sambuc "ab/ab",
119*11be35a1SLionel Sambuc "ab/ab/",
120*11be35a1SLionel Sambuc "ab/abc",
121*11be35a1SLionel Sambuc "ab/abc/",
122*11be35a1SLionel Sambuc "ab/abcd",
123*11be35a1SLionel Sambuc "ab/abcd/",
124*11be35a1SLionel Sambuc "ab/abcde",
125*11be35a1SLionel Sambuc "ab/abcde/",
126*11be35a1SLionel Sambuc "ab/abcdef",
127*11be35a1SLionel Sambuc "ab/abcdef/",
128*11be35a1SLionel Sambuc "ab/abcdefg",
129*11be35a1SLionel Sambuc "ab/abcdefg/",
130*11be35a1SLionel Sambuc "ab/abcdefgh",
131*11be35a1SLionel Sambuc "ab/abcdefgh/",
132*11be35a1SLionel Sambuc
133*11be35a1SLionel Sambuc "abc/",
134*11be35a1SLionel Sambuc "abc//",
135*11be35a1SLionel Sambuc "abc/a",
136*11be35a1SLionel Sambuc "abc/a/",
137*11be35a1SLionel Sambuc "abc/ab",
138*11be35a1SLionel Sambuc "abc/ab/",
139*11be35a1SLionel Sambuc "abc/abc",
140*11be35a1SLionel Sambuc "abc/abc/",
141*11be35a1SLionel Sambuc "abc/abcd",
142*11be35a1SLionel Sambuc "abc/abcd/",
143*11be35a1SLionel Sambuc "abc/abcde",
144*11be35a1SLionel Sambuc "abc/abcde/",
145*11be35a1SLionel Sambuc "abc/abcdef",
146*11be35a1SLionel Sambuc "abc/abcdef/",
147*11be35a1SLionel Sambuc "abc/abcdefg",
148*11be35a1SLionel Sambuc "abc/abcdefg/",
149*11be35a1SLionel Sambuc "abc/abcdefgh",
150*11be35a1SLionel Sambuc "abc/abcdefgh/",
151*11be35a1SLionel Sambuc
152*11be35a1SLionel Sambuc "abcd/",
153*11be35a1SLionel Sambuc "abcd//",
154*11be35a1SLionel Sambuc "abcd/a",
155*11be35a1SLionel Sambuc "abcd/a/",
156*11be35a1SLionel Sambuc "abcd/ab",
157*11be35a1SLionel Sambuc "abcd/ab/",
158*11be35a1SLionel Sambuc "abcd/abc",
159*11be35a1SLionel Sambuc "abcd/abc/",
160*11be35a1SLionel Sambuc "abcd/abcd",
161*11be35a1SLionel Sambuc "abcd/abcd/",
162*11be35a1SLionel Sambuc "abcd/abcde",
163*11be35a1SLionel Sambuc "abcd/abcde/",
164*11be35a1SLionel Sambuc "abcd/abcdef",
165*11be35a1SLionel Sambuc "abcd/abcdef/",
166*11be35a1SLionel Sambuc "abcd/abcdefg",
167*11be35a1SLionel Sambuc "abcd/abcdefg/",
168*11be35a1SLionel Sambuc "abcd/abcdefgh",
169*11be35a1SLionel Sambuc "abcd/abcdefgh/",
170*11be35a1SLionel Sambuc
171*11be35a1SLionel Sambuc "abcde/",
172*11be35a1SLionel Sambuc "abcde//",
173*11be35a1SLionel Sambuc "abcde/a",
174*11be35a1SLionel Sambuc "abcde/a/",
175*11be35a1SLionel Sambuc "abcde/ab",
176*11be35a1SLionel Sambuc "abcde/ab/",
177*11be35a1SLionel Sambuc "abcde/abc",
178*11be35a1SLionel Sambuc "abcde/abc/",
179*11be35a1SLionel Sambuc "abcde/abcd",
180*11be35a1SLionel Sambuc "abcde/abcd/",
181*11be35a1SLionel Sambuc "abcde/abcde",
182*11be35a1SLionel Sambuc "abcde/abcde/",
183*11be35a1SLionel Sambuc "abcde/abcdef",
184*11be35a1SLionel Sambuc "abcde/abcdef/",
185*11be35a1SLionel Sambuc "abcde/abcdefg",
186*11be35a1SLionel Sambuc "abcde/abcdefg/",
187*11be35a1SLionel Sambuc "abcde/abcdefgh",
188*11be35a1SLionel Sambuc "abcde/abcdefgh/",
189*11be35a1SLionel Sambuc
190*11be35a1SLionel Sambuc "abcdef/",
191*11be35a1SLionel Sambuc "abcdef//",
192*11be35a1SLionel Sambuc "abcdef/a",
193*11be35a1SLionel Sambuc "abcdef/a/",
194*11be35a1SLionel Sambuc "abcdef/ab",
195*11be35a1SLionel Sambuc "abcdef/ab/",
196*11be35a1SLionel Sambuc "abcdef/abc",
197*11be35a1SLionel Sambuc "abcdef/abc/",
198*11be35a1SLionel Sambuc "abcdef/abcd",
199*11be35a1SLionel Sambuc "abcdef/abcd/",
200*11be35a1SLionel Sambuc "abcdef/abcde",
201*11be35a1SLionel Sambuc "abcdef/abcde/",
202*11be35a1SLionel Sambuc "abcdef/abcdef",
203*11be35a1SLionel Sambuc "abcdef/abcdef/",
204*11be35a1SLionel Sambuc "abcdef/abcdefg",
205*11be35a1SLionel Sambuc "abcdef/abcdefg/",
206*11be35a1SLionel Sambuc "abcdef/abcdefgh",
207*11be35a1SLionel Sambuc "abcdef/abcdefgh/",
208*11be35a1SLionel Sambuc
209*11be35a1SLionel Sambuc "abcdefg/",
210*11be35a1SLionel Sambuc "abcdefg//",
211*11be35a1SLionel Sambuc "abcdefg/a",
212*11be35a1SLionel Sambuc "abcdefg/a/",
213*11be35a1SLionel Sambuc "abcdefg/ab",
214*11be35a1SLionel Sambuc "abcdefg/ab/",
215*11be35a1SLionel Sambuc "abcdefg/abc",
216*11be35a1SLionel Sambuc "abcdefg/abc/",
217*11be35a1SLionel Sambuc "abcdefg/abcd",
218*11be35a1SLionel Sambuc "abcdefg/abcd/",
219*11be35a1SLionel Sambuc "abcdefg/abcde",
220*11be35a1SLionel Sambuc "abcdefg/abcde/",
221*11be35a1SLionel Sambuc "abcdefg/abcdef",
222*11be35a1SLionel Sambuc "abcdefg/abcdef/",
223*11be35a1SLionel Sambuc "abcdefg/abcdefg",
224*11be35a1SLionel Sambuc "abcdefg/abcdefg/",
225*11be35a1SLionel Sambuc "abcdefg/abcdefgh",
226*11be35a1SLionel Sambuc "abcdefg/abcdefgh/",
227*11be35a1SLionel Sambuc
228*11be35a1SLionel Sambuc "abcdefgh/",
229*11be35a1SLionel Sambuc "abcdefgh//",
230*11be35a1SLionel Sambuc "abcdefgh/a",
231*11be35a1SLionel Sambuc "abcdefgh/a/",
232*11be35a1SLionel Sambuc "abcdefgh/ab",
233*11be35a1SLionel Sambuc "abcdefgh/ab/",
234*11be35a1SLionel Sambuc "abcdefgh/abc",
235*11be35a1SLionel Sambuc "abcdefgh/abc/",
236*11be35a1SLionel Sambuc "abcdefgh/abcd",
237*11be35a1SLionel Sambuc "abcdefgh/abcd/",
238*11be35a1SLionel Sambuc "abcdefgh/abcde",
239*11be35a1SLionel Sambuc "abcdefgh/abcde/",
240*11be35a1SLionel Sambuc "abcdefgh/abcdef",
241*11be35a1SLionel Sambuc "abcdefgh/abcdef/",
242*11be35a1SLionel Sambuc "abcdefgh/abcdefg",
243*11be35a1SLionel Sambuc "abcdefgh/abcdefg/",
244*11be35a1SLionel Sambuc "abcdefgh/abcdefgh",
245*11be35a1SLionel Sambuc "abcdefgh/abcdefgh/",
246*11be35a1SLionel Sambuc };
247*11be35a1SLionel Sambuc
248*11be35a1SLionel Sambuc
249*11be35a1SLionel Sambuc strchr_fn = dlsym(dlopen(0, RTLD_LAZY), "test_strchr");
250*11be35a1SLionel Sambuc if (!strchr_fn)
251*11be35a1SLionel Sambuc strchr_fn = strchr;
252*11be35a1SLionel Sambuc
253*11be35a1SLionel Sambuc for (a = 3; a < 3 + sizeof(long); ++a) {
254*11be35a1SLionel Sambuc /* Put char and a \0 before the buffer */
255*11be35a1SLionel Sambuc buf[a-1] = '/';
256*11be35a1SLionel Sambuc buf[a-2] = '0';
257*11be35a1SLionel Sambuc buf[a-3] = 0xff;
258*11be35a1SLionel Sambuc for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
259*11be35a1SLionel Sambuc int len = strlen(tab[t]) + 1;
260*11be35a1SLionel Sambuc memcpy(&buf[a], tab[t], len);
261*11be35a1SLionel Sambuc
262*11be35a1SLionel Sambuc /* Put the char we are looking for after the \0 */
263*11be35a1SLionel Sambuc buf[a + len] = '/';
264*11be35a1SLionel Sambuc
265*11be35a1SLionel Sambuc /* Check search for NUL at end of string */
266*11be35a1SLionel Sambuc verify_strchr(buf + a, 0, t, a);
267*11be35a1SLionel Sambuc
268*11be35a1SLionel Sambuc /* Then for the '/' in the strings */
269*11be35a1SLionel Sambuc verify_strchr(buf + a, '/', t, a);
270*11be35a1SLionel Sambuc
271*11be35a1SLionel Sambuc /* check zero extension of char arg */
272*11be35a1SLionel Sambuc verify_strchr(buf + a, 0xffffff00 | '/', t, a);
273*11be35a1SLionel Sambuc
274*11be35a1SLionel Sambuc /* Replace all the '/' with 0xff */
275*11be35a1SLionel Sambuc while ((off = slow_strchr(buf + a, '/')) != NULL)
276*11be35a1SLionel Sambuc *off = 0xff;
277*11be35a1SLionel Sambuc
278*11be35a1SLionel Sambuc buf[a + len] = 0xff;
279*11be35a1SLionel Sambuc
280*11be35a1SLionel Sambuc /* Check we can search for 0xff as well as '/' */
281*11be35a1SLionel Sambuc verify_strchr(buf + a, 0xff, t, a);
282*11be35a1SLionel Sambuc }
283*11be35a1SLionel Sambuc }
284*11be35a1SLionel Sambuc }
285*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)286*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
287*11be35a1SLionel Sambuc {
288*11be35a1SLionel Sambuc
289*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, strchr_basic);
290*11be35a1SLionel Sambuc
291*11be35a1SLionel Sambuc return atf_no_error();
292*11be35a1SLionel Sambuc }
293