1*11be35a1SLionel Sambuc /* $NetBSD: t_strcat.c,v 1.2 2011/07/14 05:46:04 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
14*11be35a1SLionel Sambuc ATF_TC(strcat_basic);
ATF_TC_HEAD(strcat_basic,tc)15*11be35a1SLionel Sambuc ATF_TC_HEAD(strcat_basic, tc)
16*11be35a1SLionel Sambuc {
17*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test strcat(3) results");
18*11be35a1SLionel Sambuc }
19*11be35a1SLionel Sambuc
ATF_TC_BODY(strcat_basic,tc)20*11be35a1SLionel Sambuc ATF_TC_BODY(strcat_basic, tc)
21*11be35a1SLionel Sambuc {
22*11be35a1SLionel Sambuc /* try to trick the compiler */
23*11be35a1SLionel Sambuc char * (*f)(char *, const char *s) = strcat;
24*11be35a1SLionel Sambuc
25*11be35a1SLionel Sambuc unsigned int a0, a1, t0, t1;
26*11be35a1SLionel Sambuc char buf0[64];
27*11be35a1SLionel Sambuc char buf1[64];
28*11be35a1SLionel Sambuc char *ret;
29*11be35a1SLionel Sambuc
30*11be35a1SLionel Sambuc struct tab {
31*11be35a1SLionel Sambuc const char* val;
32*11be35a1SLionel Sambuc size_t len;
33*11be35a1SLionel Sambuc };
34*11be35a1SLionel Sambuc
35*11be35a1SLionel Sambuc const struct tab tab[] = {
36*11be35a1SLionel Sambuc /*
37*11be35a1SLionel Sambuc * patterns that check for all combinations of leading and
38*11be35a1SLionel Sambuc * trailing unaligned characters (on a 64 bit processor)
39*11be35a1SLionel Sambuc */
40*11be35a1SLionel Sambuc
41*11be35a1SLionel Sambuc { "", 0 },
42*11be35a1SLionel Sambuc { "a", 1 },
43*11be35a1SLionel Sambuc { "ab", 2 },
44*11be35a1SLionel Sambuc { "abc", 3 },
45*11be35a1SLionel Sambuc { "abcd", 4 },
46*11be35a1SLionel Sambuc { "abcde", 5 },
47*11be35a1SLionel Sambuc { "abcdef", 6 },
48*11be35a1SLionel Sambuc { "abcdefg", 7 },
49*11be35a1SLionel Sambuc { "abcdefgh", 8 },
50*11be35a1SLionel Sambuc { "abcdefghi", 9 },
51*11be35a1SLionel Sambuc { "abcdefghij", 10 },
52*11be35a1SLionel Sambuc { "abcdefghijk", 11 },
53*11be35a1SLionel Sambuc { "abcdefghijkl", 12 },
54*11be35a1SLionel Sambuc { "abcdefghijklm", 13 },
55*11be35a1SLionel Sambuc { "abcdefghijklmn", 14 },
56*11be35a1SLionel Sambuc { "abcdefghijklmno", 15 },
57*11be35a1SLionel Sambuc { "abcdefghijklmnop", 16 },
58*11be35a1SLionel Sambuc { "abcdefghijklmnopq", 17 },
59*11be35a1SLionel Sambuc { "abcdefghijklmnopqr", 18 },
60*11be35a1SLionel Sambuc { "abcdefghijklmnopqrs", 19 },
61*11be35a1SLionel Sambuc { "abcdefghijklmnopqrst", 20 },
62*11be35a1SLionel Sambuc { "abcdefghijklmnopqrstu", 21 },
63*11be35a1SLionel Sambuc { "abcdefghijklmnopqrstuv", 22 },
64*11be35a1SLionel Sambuc { "abcdefghijklmnopqrstuvw", 23 },
65*11be35a1SLionel Sambuc
66*11be35a1SLionel Sambuc /*
67*11be35a1SLionel Sambuc * patterns that check for the cases where the expression:
68*11be35a1SLionel Sambuc *
69*11be35a1SLionel Sambuc * ((word - 0x7f7f..7f) & 0x8080..80)
70*11be35a1SLionel Sambuc *
71*11be35a1SLionel Sambuc * returns non-zero even though there are no zero bytes in
72*11be35a1SLionel Sambuc * the word.
73*11be35a1SLionel Sambuc */
74*11be35a1SLionel Sambuc
75*11be35a1SLionel Sambuc { "" "\xff\xff\xff\xff\xff\xff\xff\xff" "abcdefgh", 16 },
76*11be35a1SLionel Sambuc { "a" "\xff\xff\xff\xff\xff\xff\xff\xff" "bcdefgh", 16 },
77*11be35a1SLionel Sambuc { "ab" "\xff\xff\xff\xff\xff\xff\xff\xff" "cdefgh", 16 },
78*11be35a1SLionel Sambuc { "abc" "\xff\xff\xff\xff\xff\xff\xff\xff" "defgh", 16 },
79*11be35a1SLionel Sambuc { "abcd" "\xff\xff\xff\xff\xff\xff\xff\xff" "efgh", 16 },
80*11be35a1SLionel Sambuc { "abcde" "\xff\xff\xff\xff\xff\xff\xff\xff" "fgh", 16 },
81*11be35a1SLionel Sambuc { "abcdef" "\xff\xff\xff\xff\xff\xff\xff\xff" "gh", 16 },
82*11be35a1SLionel Sambuc { "abcdefg" "\xff\xff\xff\xff\xff\xff\xff\xff" "h", 16 },
83*11be35a1SLionel Sambuc { "abcdefgh" "\xff\xff\xff\xff\xff\xff\xff\xff" "", 16 },
84*11be35a1SLionel Sambuc };
85*11be35a1SLionel Sambuc
86*11be35a1SLionel Sambuc for (a0 = 0; a0 < sizeof(long); ++a0) {
87*11be35a1SLionel Sambuc for (a1 = 0; a1 < sizeof(long); ++a1) {
88*11be35a1SLionel Sambuc for (t0 = 0; t0 < __arraycount(tab); ++t0) {
89*11be35a1SLionel Sambuc for (t1 = 0; t1 < __arraycount(tab); ++t1) {
90*11be35a1SLionel Sambuc
91*11be35a1SLionel Sambuc memcpy(&buf0[a0], tab[t0].val,
92*11be35a1SLionel Sambuc tab[t0].len + 1);
93*11be35a1SLionel Sambuc memcpy(&buf1[a1], tab[t1].val,
94*11be35a1SLionel Sambuc tab[t1].len + 1);
95*11be35a1SLionel Sambuc
96*11be35a1SLionel Sambuc ret = f(&buf0[a0], &buf1[a1]);
97*11be35a1SLionel Sambuc
98*11be35a1SLionel Sambuc /*
99*11be35a1SLionel Sambuc * verify strcat returns address
100*11be35a1SLionel Sambuc * of first parameter
101*11be35a1SLionel Sambuc */
102*11be35a1SLionel Sambuc if (&buf0[a0] != ret) {
103*11be35a1SLionel Sambuc fprintf(stderr, "a0 %d, a1 %d, "
104*11be35a1SLionel Sambuc "t0 %d, t1 %d\n",
105*11be35a1SLionel Sambuc a0, a1, t0, t1);
106*11be35a1SLionel Sambuc atf_tc_fail("strcat did not "
107*11be35a1SLionel Sambuc "return its first arg");
108*11be35a1SLionel Sambuc }
109*11be35a1SLionel Sambuc
110*11be35a1SLionel Sambuc /* verify string copied correctly */
111*11be35a1SLionel Sambuc if (memcmp(&buf0[a0] + tab[t0].len,
112*11be35a1SLionel Sambuc &buf1[a1],
113*11be35a1SLionel Sambuc tab[t1].len + 1) != 0) {
114*11be35a1SLionel Sambuc fprintf(stderr, "a0 %d, a1 %d, "
115*11be35a1SLionel Sambuc "t0 %d, t1 %d\n",
116*11be35a1SLionel Sambuc a0, a1, t0, t1);
117*11be35a1SLionel Sambuc atf_tc_fail("string not copied "
118*11be35a1SLionel Sambuc "correctly");
119*11be35a1SLionel Sambuc }
120*11be35a1SLionel Sambuc }
121*11be35a1SLionel Sambuc }
122*11be35a1SLionel Sambuc }
123*11be35a1SLionel Sambuc }
124*11be35a1SLionel Sambuc }
125*11be35a1SLionel Sambuc
126*11be35a1SLionel Sambuc ATF_TC(strncat_simple);
ATF_TC_HEAD(strncat_simple,tc)127*11be35a1SLionel Sambuc ATF_TC_HEAD(strncat_simple, tc)
128*11be35a1SLionel Sambuc {
129*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test strncat(3) results");
130*11be35a1SLionel Sambuc }
131*11be35a1SLionel Sambuc
ATF_TC_BODY(strncat_simple,tc)132*11be35a1SLionel Sambuc ATF_TC_BODY(strncat_simple, tc)
133*11be35a1SLionel Sambuc {
134*11be35a1SLionel Sambuc char buf[100] = "abcdefg";
135*11be35a1SLionel Sambuc
136*11be35a1SLionel Sambuc ATF_CHECK(strncat(buf, "xxx", 0) == buf);
137*11be35a1SLionel Sambuc ATF_CHECK(strcmp(buf, "abcdefg") == 0);
138*11be35a1SLionel Sambuc ATF_CHECK(strncat(buf, "xxx", 1) == buf);
139*11be35a1SLionel Sambuc ATF_CHECK(strcmp(buf, "abcdefgx") == 0);
140*11be35a1SLionel Sambuc ATF_CHECK(strncat(buf, "xxx", 2) == buf);
141*11be35a1SLionel Sambuc ATF_CHECK(strcmp(buf, "abcdefgxxx") == 0);
142*11be35a1SLionel Sambuc ATF_CHECK(strncat(buf, "\0", 1) == buf);
143*11be35a1SLionel Sambuc ATF_CHECK(strcmp(buf, "abcdefgxxx") == 0);
144*11be35a1SLionel Sambuc }
145*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)146*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
147*11be35a1SLionel Sambuc {
148*11be35a1SLionel Sambuc
149*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, strcat_basic);
150*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, strncat_simple);
151*11be35a1SLionel Sambuc
152*11be35a1SLionel Sambuc return atf_no_error();
153*11be35a1SLionel Sambuc }
154