xref: /minix3/tests/lib/libc/string/t_strcpy.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /* $NetBSD: t_strcpy.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 
14*11be35a1SLionel Sambuc ATF_TC(strcpy_basic);
ATF_TC_HEAD(strcpy_basic,tc)15*11be35a1SLionel Sambuc ATF_TC_HEAD(strcpy_basic, tc)
16*11be35a1SLionel Sambuc {
17*11be35a1SLionel Sambuc         atf_tc_set_md_var(tc, "descr", "Test strcpy(3) results");
18*11be35a1SLionel Sambuc }
19*11be35a1SLionel Sambuc 
ATF_TC_BODY(strcpy_basic,tc)20*11be35a1SLionel Sambuc ATF_TC_BODY(strcpy_basic, tc)
21*11be35a1SLionel Sambuc {
22*11be35a1SLionel Sambuc 	/* try to trick the compiler */
23*11be35a1SLionel Sambuc 	char * (*f)(char *, const char *s) = strcpy;
24*11be35a1SLionel Sambuc 
25*11be35a1SLionel Sambuc 	unsigned int a0, a1, t;
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 (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
89*11be35a1SLionel Sambuc 
90*11be35a1SLionel Sambuc 				memcpy(&buf1[a1], tab[t].val, tab[t].len + 1);
91*11be35a1SLionel Sambuc 				ret = f(&buf0[a0], &buf1[a1]);
92*11be35a1SLionel Sambuc 
93*11be35a1SLionel Sambuc 				/*
94*11be35a1SLionel Sambuc 				 * verify strcpy returns address of
95*11be35a1SLionel Sambuc 				 * first parameter
96*11be35a1SLionel Sambuc 				 */
97*11be35a1SLionel Sambuc 			    	if (&buf0[a0] != ret) {
98*11be35a1SLionel Sambuc 					fprintf(stderr, "a0 %d, a1 %d, t %d\n",
99*11be35a1SLionel Sambuc 					    a0, a1, t);
100*11be35a1SLionel Sambuc 					atf_tc_fail("strcpy did not return "
101*11be35a1SLionel Sambuc 					    "its first arg");
102*11be35a1SLionel Sambuc 				}
103*11be35a1SLionel Sambuc 
104*11be35a1SLionel Sambuc 				/*
105*11be35a1SLionel Sambuc 				 * verify string was copied correctly
106*11be35a1SLionel Sambuc 				 */
107*11be35a1SLionel Sambuc 				if (memcmp(&buf0[a0], &buf1[a1],
108*11be35a1SLionel Sambuc 					   tab[t].len + 1) != 0) {
109*11be35a1SLionel Sambuc 					fprintf(stderr, "a0 %d, a1 %d, t %d\n",
110*11be35a1SLionel Sambuc 					    a0, a1, t);
111*11be35a1SLionel Sambuc 					atf_tc_fail("not correctly copied");
112*11be35a1SLionel Sambuc 				}
113*11be35a1SLionel Sambuc 			}
114*11be35a1SLionel Sambuc 		}
115*11be35a1SLionel Sambuc 	}
116*11be35a1SLionel Sambuc }
117*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)118*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
119*11be35a1SLionel Sambuc {
120*11be35a1SLionel Sambuc 
121*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, strcpy_basic);
122*11be35a1SLionel Sambuc 
123*11be35a1SLionel Sambuc 	return atf_no_error();
124*11be35a1SLionel Sambuc }
125