robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2002 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* 01/02/2003 Port to LTP avenkat@us.ibm.com */ |
| 21 | /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */ |
| 22 | |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 23 | /* |
| 24 | * NAME |
| 25 | * memcpy -- test memcpy |
| 26 | * |
| 27 | * CALLS |
| 28 | * memcpy1(3) |
| 29 | * |
| 30 | * ALGORITHM |
| 31 | * There are 4 cases for copies: S = Source, D = Destination |
| 32 | * |
| 33 | * 1 - S < D no overlap |
| 34 | * 2 - D < S no overlap |
| 35 | * 3 - S < D with overlap |
| 36 | * 4 - D < S with overlap |
| 37 | * |
| 38 | * We try all four cases. Check buffer boundaries. |
| 39 | * |
| 40 | * RESTRICTIONS |
| 41 | */ |
| 42 | |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 43 | #include <stdio.h> |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 44 | #include <string.h> |
| 45 | #include <unistd.h> |
| 46 | #include <string.h> |
| 47 | #include <errno.h> |
| 48 | |
| 49 | /***** LTP Port *****/ |
| 50 | #include "test.h" |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 51 | |
| 52 | char *TCID = "memcpy1"; |
| 53 | /***** ** ** *****/ |
| 54 | #undef BSIZE |
| 55 | #define BSIZE 4096 |
| 56 | #define LEN 100 |
| 57 | #define FAILED 0 |
| 58 | #define PASSED 1 |
| 59 | |
| 60 | /***** LTP Port *****/ |
| 61 | int local_flag = PASSED; |
| 62 | int block_number; |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 63 | FILE *temp; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 64 | int TST_TOTAL = 1; |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 65 | /***** ** ** *****/ |
| 66 | char buf[BSIZE]; |
| 67 | |
| 68 | /***** LTP Port *****/ |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 69 | |
| 70 | int anyfail(); |
| 71 | int blenter(); |
| 72 | int blexit(); |
| 73 | |
| 74 | void setup(); |
| 75 | /***** ** ** *****/ |
| 76 | void clearit(); |
| 77 | void fill(char *str); |
| 78 | int checkit(char *str); |
| 79 | |
| 80 | /*--------------------------------------------------------------*/ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | int main(int argc, char *argv[]) |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 82 | { |
| 83 | char *p, *q; |
| 84 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | setup(); /* temp file is now open */ |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 86 | /*--------------------------------------------------------------*/ |
| 87 | blenter(); |
| 88 | |
| 89 | clearit(); |
| 90 | |
| 91 | p = &buf[100]; |
| 92 | |
| 93 | fill(p); |
| 94 | q = &buf[800]; |
| 95 | memcpy(q, p, LEN); |
| 96 | |
| 97 | if (checkit(q)) { |
| 98 | fprintf(temp, "\tcopy failed - missed data\n"); |
| 99 | local_flag = FAILED; |
| 100 | } |
| 101 | |
| 102 | if (p[-1] || p[LEN]) { |
| 103 | fprintf(temp, "\tcopy failed - 'to' bounds\n"); |
| 104 | local_flag = FAILED; |
| 105 | } |
| 106 | |
| 107 | if (q[-1] || q[LEN]) { |
| 108 | fprintf(temp, "\tcopy failed - 'from' bounds\n"); |
| 109 | local_flag = FAILED; |
| 110 | } |
| 111 | |
| 112 | blexit(); |
| 113 | /*--------------------------------------------------------------*/ |
| 114 | blenter(); |
| 115 | |
| 116 | clearit(); |
| 117 | |
| 118 | p = &buf[800]; |
| 119 | |
| 120 | fill(p); |
| 121 | q = &buf[100]; |
| 122 | memcpy(q, p, LEN); |
| 123 | |
| 124 | if (checkit(q)) { |
| 125 | fprintf(temp, "\tcopy failed - missed data\n"); |
| 126 | local_flag = FAILED; |
| 127 | } |
| 128 | |
| 129 | if (p[-1] || p[LEN]) { |
| 130 | fprintf(temp, "\tcopy failed - 'to' bounds\n"); |
| 131 | local_flag = FAILED; |
| 132 | } |
| 133 | |
| 134 | if (q[-1] || q[LEN]) { |
| 135 | fprintf(temp, "\tcopy failed - 'from' bounds\n"); |
| 136 | local_flag = FAILED; |
| 137 | } |
| 138 | |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 139 | blexit(); |
| 140 | /*--------------------------------------------------------------*/ |
| 141 | blenter(); |
| 142 | |
| 143 | clearit(); |
| 144 | |
| 145 | p = &buf[800]; |
| 146 | |
| 147 | fill(p); |
| 148 | q = &buf[850]; |
| 149 | memcpy(q, p, LEN); |
| 150 | |
| 151 | if (checkit(q)) { |
| 152 | fprintf(temp, "\tcopy failed - missed data\n"); |
| 153 | local_flag = FAILED; |
| 154 | } |
| 155 | |
| 156 | if (p[-1]) { |
| 157 | fprintf(temp, "\tcopy failed - 'to' bounds\n"); |
| 158 | local_flag = FAILED; |
| 159 | } |
| 160 | |
| 161 | if (q[LEN]) { |
| 162 | fprintf(temp, "\tcopy failed - 'from' bounds\n"); |
| 163 | local_flag = FAILED; |
| 164 | } |
| 165 | |
| 166 | blexit(); |
| 167 | /*--------------------------------------------------------------*/ |
| 168 | blenter(); |
| 169 | |
| 170 | clearit(); |
| 171 | |
| 172 | p = &buf[850]; |
| 173 | |
| 174 | fill(p); |
| 175 | q = &buf[800]; |
| 176 | memcpy(q, p, LEN); |
| 177 | |
| 178 | if (checkit(q)) { |
| 179 | fprintf(temp, "\tcopy failed - missed data\n"); |
| 180 | local_flag = FAILED; |
| 181 | } |
| 182 | |
| 183 | if (p[LEN]) { |
| 184 | fprintf(temp, "\tcopy failed - 'to' bounds\n"); |
| 185 | local_flag = FAILED; |
| 186 | } |
| 187 | |
| 188 | if (q[-1]) { |
| 189 | fprintf(temp, "\tcopy failed - 'from' bounds\n"); |
| 190 | local_flag = FAILED; |
| 191 | } |
| 192 | |
| 193 | blexit(); |
| 194 | /*--------------------------------------------------------------*/ |
| 195 | /* Clean up any files created by test before call to anyfail. */ |
| 196 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 197 | anyfail(); /* THIS CALL DOES NOT RETURN - EXITS!! */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 198 | tst_exit(); |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 199 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 200 | |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 201 | /*--------------------------------------------------------------*/ |
| 202 | /* FUNCTIONS GO HERE */ |
| 203 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 204 | void clearit(void) |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 205 | { |
| 206 | register int i; |
| 207 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 208 | for (i = 0; i < BSIZE; i++) |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 209 | buf[i] = 0; |
| 210 | } |
| 211 | |
| 212 | void fill(char *str) |
| 213 | { |
| 214 | register int i; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 215 | for (i = 0; i < LEN; i++) |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 216 | *str++ = 'a'; |
| 217 | } |
| 218 | |
| 219 | int checkit(char *str) |
| 220 | { |
| 221 | register int i; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 222 | for (i = 0; i < LEN; i++) |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 223 | if (*str++ != 'a') |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 224 | return (-1); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 225 | |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 226 | return (0); |
| 227 | } |
| 228 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 229 | int anyfail(void) |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 230 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 231 | (local_flag == FAILED) ? tst_resm(TFAIL, |
| 232 | "Test failed") : tst_resm(TPASS, |
| 233 | "Test passed"); |
| 234 | tst_exit(); |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 237 | void setup(void) |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 238 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 239 | temp = stderr; |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 242 | int blenter(void) |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 243 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 244 | local_flag = PASSED; |
| 245 | return 0; |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 248 | int blexit(void) |
robbiew | c6219fd | 2003-01-03 20:33:32 +0000 | [diff] [blame] | 249 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 250 | (local_flag == FAILED) ? tst_resm(TFAIL, |
| 251 | "Test failed") : tst_resm(TPASS, |
| 252 | "Test passed"); |
| 253 | return 0; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 254 | } |