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