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