xref: /netbsd-src/tests/lib/libc/string/t_strcpy.c (revision 54bb10746c4fb0dfb3bc10047511fe5a62c414f4)
1*54bb1074Sjruoho /* $NetBSD: t_strcpy.c,v 1.1 2011/07/07 08:59:33 jruoho Exp $ */
2*54bb1074Sjruoho 
3*54bb1074Sjruoho /*
4*54bb1074Sjruoho  * Written by J.T. Conklin <jtc@acorntoolworks.com>
5*54bb1074Sjruoho  * Public domain.
6*54bb1074Sjruoho  */
7*54bb1074Sjruoho 
8*54bb1074Sjruoho #include <atf-c.h>
9*54bb1074Sjruoho #include <string.h>
10*54bb1074Sjruoho #include <unistd.h>
11*54bb1074Sjruoho #include <stdio.h>
12*54bb1074Sjruoho #include <stdlib.h>
13*54bb1074Sjruoho 
14*54bb1074Sjruoho ATF_TC(strcpy_basic);
ATF_TC_HEAD(strcpy_basic,tc)15*54bb1074Sjruoho ATF_TC_HEAD(strcpy_basic, tc)
16*54bb1074Sjruoho {
17*54bb1074Sjruoho         atf_tc_set_md_var(tc, "descr", "Test strcpy(3) results");
18*54bb1074Sjruoho }
19*54bb1074Sjruoho 
ATF_TC_BODY(strcpy_basic,tc)20*54bb1074Sjruoho ATF_TC_BODY(strcpy_basic, tc)
21*54bb1074Sjruoho {
22*54bb1074Sjruoho 	/* try to trick the compiler */
23*54bb1074Sjruoho 	char * (*f)(char *, const char *s) = strcpy;
24*54bb1074Sjruoho 
25*54bb1074Sjruoho 	unsigned int a0, a1, t;
26*54bb1074Sjruoho 	char buf0[64];
27*54bb1074Sjruoho 	char buf1[64];
28*54bb1074Sjruoho 	char *ret;
29*54bb1074Sjruoho 
30*54bb1074Sjruoho 	struct tab {
31*54bb1074Sjruoho 		const char*	val;
32*54bb1074Sjruoho 		size_t		len;
33*54bb1074Sjruoho 	};
34*54bb1074Sjruoho 
35*54bb1074Sjruoho 	const struct tab tab[] = {
36*54bb1074Sjruoho 		/*
37*54bb1074Sjruoho 		 * patterns that check for all combinations of leading and
38*54bb1074Sjruoho 		 * trailing unaligned characters (on a 64 bit processor)
39*54bb1074Sjruoho 		 */
40*54bb1074Sjruoho 
41*54bb1074Sjruoho 		{ "",				0 },
42*54bb1074Sjruoho 		{ "a",				1 },
43*54bb1074Sjruoho 		{ "ab",				2 },
44*54bb1074Sjruoho 		{ "abc",			3 },
45*54bb1074Sjruoho 		{ "abcd",			4 },
46*54bb1074Sjruoho 		{ "abcde",			5 },
47*54bb1074Sjruoho 		{ "abcdef",			6 },
48*54bb1074Sjruoho 		{ "abcdefg",			7 },
49*54bb1074Sjruoho 		{ "abcdefgh",			8 },
50*54bb1074Sjruoho 		{ "abcdefghi",			9 },
51*54bb1074Sjruoho 		{ "abcdefghij",			10 },
52*54bb1074Sjruoho 		{ "abcdefghijk",		11 },
53*54bb1074Sjruoho 		{ "abcdefghijkl",		12 },
54*54bb1074Sjruoho 		{ "abcdefghijklm",		13 },
55*54bb1074Sjruoho 		{ "abcdefghijklmn",		14 },
56*54bb1074Sjruoho 		{ "abcdefghijklmno",		15 },
57*54bb1074Sjruoho 		{ "abcdefghijklmnop",		16 },
58*54bb1074Sjruoho 		{ "abcdefghijklmnopq",		17 },
59*54bb1074Sjruoho 		{ "abcdefghijklmnopqr",		18 },
60*54bb1074Sjruoho 		{ "abcdefghijklmnopqrs",	19 },
61*54bb1074Sjruoho 		{ "abcdefghijklmnopqrst",	20 },
62*54bb1074Sjruoho 		{ "abcdefghijklmnopqrstu",	21 },
63*54bb1074Sjruoho 		{ "abcdefghijklmnopqrstuv",	22 },
64*54bb1074Sjruoho 		{ "abcdefghijklmnopqrstuvw",	23 },
65*54bb1074Sjruoho 
66*54bb1074Sjruoho 		/*
67*54bb1074Sjruoho 		 * patterns that check for the cases where the expression:
68*54bb1074Sjruoho 		 *
69*54bb1074Sjruoho 		 *	((word - 0x7f7f..7f) & 0x8080..80)
70*54bb1074Sjruoho 		 *
71*54bb1074Sjruoho 		 * returns non-zero even though there are no zero bytes in
72*54bb1074Sjruoho 		 * the word.
73*54bb1074Sjruoho 		 */
74*54bb1074Sjruoho 
75*54bb1074Sjruoho 		{ "" "\xff\xff\xff\xff\xff\xff\xff\xff" "abcdefgh",	16 },
76*54bb1074Sjruoho 		{ "a" "\xff\xff\xff\xff\xff\xff\xff\xff" "bcdefgh",	16 },
77*54bb1074Sjruoho 		{ "ab" "\xff\xff\xff\xff\xff\xff\xff\xff" "cdefgh",	16 },
78*54bb1074Sjruoho 		{ "abc" "\xff\xff\xff\xff\xff\xff\xff\xff" "defgh",	16 },
79*54bb1074Sjruoho 		{ "abcd" "\xff\xff\xff\xff\xff\xff\xff\xff" "efgh",	16 },
80*54bb1074Sjruoho 		{ "abcde" "\xff\xff\xff\xff\xff\xff\xff\xff" "fgh",	16 },
81*54bb1074Sjruoho 		{ "abcdef" "\xff\xff\xff\xff\xff\xff\xff\xff" "gh",	16 },
82*54bb1074Sjruoho 		{ "abcdefg" "\xff\xff\xff\xff\xff\xff\xff\xff" "h",	16 },
83*54bb1074Sjruoho 		{ "abcdefgh" "\xff\xff\xff\xff\xff\xff\xff\xff" "",	16 },
84*54bb1074Sjruoho 	};
85*54bb1074Sjruoho 
86*54bb1074Sjruoho 	for (a0 = 0; a0 < sizeof(long); ++a0) {
87*54bb1074Sjruoho 		for (a1 = 0; a1 < sizeof(long); ++a1) {
88*54bb1074Sjruoho 			for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
89*54bb1074Sjruoho 
90*54bb1074Sjruoho 				memcpy(&buf1[a1], tab[t].val, tab[t].len + 1);
91*54bb1074Sjruoho 				ret = f(&buf0[a0], &buf1[a1]);
92*54bb1074Sjruoho 
93*54bb1074Sjruoho 				/*
94*54bb1074Sjruoho 				 * verify strcpy returns address of
95*54bb1074Sjruoho 				 * first parameter
96*54bb1074Sjruoho 				 */
97*54bb1074Sjruoho 			    	if (&buf0[a0] != ret) {
98*54bb1074Sjruoho 					fprintf(stderr, "a0 %d, a1 %d, t %d\n",
99*54bb1074Sjruoho 					    a0, a1, t);
100*54bb1074Sjruoho 					atf_tc_fail("strcpy did not return "
101*54bb1074Sjruoho 					    "its first arg");
102*54bb1074Sjruoho 				}
103*54bb1074Sjruoho 
104*54bb1074Sjruoho 				/*
105*54bb1074Sjruoho 				 * verify string was copied correctly
106*54bb1074Sjruoho 				 */
107*54bb1074Sjruoho 				if (memcmp(&buf0[a0], &buf1[a1],
108*54bb1074Sjruoho 					   tab[t].len + 1) != 0) {
109*54bb1074Sjruoho 					fprintf(stderr, "a0 %d, a1 %d, t %d\n",
110*54bb1074Sjruoho 					    a0, a1, t);
111*54bb1074Sjruoho 					atf_tc_fail("not correctly copied");
112*54bb1074Sjruoho 				}
113*54bb1074Sjruoho 			}
114*54bb1074Sjruoho 		}
115*54bb1074Sjruoho 	}
116*54bb1074Sjruoho }
117*54bb1074Sjruoho 
ATF_TP_ADD_TCS(tp)118*54bb1074Sjruoho ATF_TP_ADD_TCS(tp)
119*54bb1074Sjruoho {
120*54bb1074Sjruoho 
121*54bb1074Sjruoho 	ATF_TP_ADD_TC(tp, strcpy_basic);
122*54bb1074Sjruoho 
123*54bb1074Sjruoho 	return atf_no_error();
124*54bb1074Sjruoho }
125