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