xref: /freebsd/contrib/netbsd-tests/lib/libc/string/t_strcat.c (revision e7437ae907c89bf85a99c5cbb7ddd194a1ff1354)
1 /* $NetBSD: t_strcat.c,v 1.2 2011/07/14 05:46:04 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(strcat_basic);
15 ATF_TC_HEAD(strcat_basic, tc)
16 {
17         atf_tc_set_md_var(tc, "descr", "Test strcat(3) results");
18 }
19 
20 ATF_TC_BODY(strcat_basic, tc)
21 {
22 	/* try to trick the compiler */
23 	char * (*f)(char *, const char *s) = strcat;
24 
25 	unsigned int a0, a1, t0, t1;
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 (t0 = 0; t0 < __arraycount(tab); ++t0) {
89 				for (t1 = 0; t1 < __arraycount(tab); ++t1) {
90 
91 					memcpy(&buf0[a0], tab[t0].val,
92 					    tab[t0].len + 1);
93 					memcpy(&buf1[a1], tab[t1].val,
94 					    tab[t1].len + 1);
95 
96 					ret = f(&buf0[a0], &buf1[a1]);
97 
98 					/*
99 					 * verify strcat returns address
100 					 * of first parameter
101 					 */
102 					if (&buf0[a0] != ret) {
103 						fprintf(stderr, "a0 %d, a1 %d, "
104 						    "t0 %d, t1 %d\n",
105 						    a0, a1, t0, t1);
106 						atf_tc_fail("strcat did not "
107 						    "return its first arg");
108 					}
109 
110 					/* verify string copied correctly */
111 					if (memcmp(&buf0[a0] + tab[t0].len,
112 						   &buf1[a1],
113 						   tab[t1].len + 1) != 0) {
114 						fprintf(stderr, "a0 %d, a1 %d, "
115 						    "t0 %d, t1 %d\n",
116 						    a0, a1, t0, t1);
117 						atf_tc_fail("string not copied "
118 						    "correctly");
119 					}
120 				}
121 			}
122 		}
123 	}
124 }
125 
126 ATF_TC(strncat_simple);
127 ATF_TC_HEAD(strncat_simple, tc)
128 {
129         atf_tc_set_md_var(tc, "descr", "Test strncat(3) results");
130 }
131 
132 ATF_TC_BODY(strncat_simple, tc)
133 {
134 	char buf[100] = "abcdefg";
135 
136 	ATF_CHECK(strncat(buf, "xxx", 0) == buf);
137 	ATF_CHECK(strcmp(buf, "abcdefg") == 0);
138 	ATF_CHECK(strncat(buf, "xxx", 1) == buf);
139 	ATF_CHECK(strcmp(buf, "abcdefgx") == 0);
140 	ATF_CHECK(strncat(buf, "xxx", 2) == buf);
141 	ATF_CHECK(strcmp(buf, "abcdefgxxx") == 0);
142 	ATF_CHECK(strncat(buf, "\0", 1) == buf);
143 	ATF_CHECK(strcmp(buf, "abcdefgxxx") == 0);
144 }
145 
146 ATF_TP_ADD_TCS(tp)
147 {
148 
149 	ATF_TP_ADD_TC(tp, strcat_basic);
150 	ATF_TP_ADD_TC(tp, strncat_simple);
151 
152 	return atf_no_error();
153 }
154