blob: aa8ec4bad23f9754f662d081470cb8b1afe762d9 [file] [log] [blame]
robbiewc6219fd2003-01-03 20:33:32 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewc6219fd2003-01-03 20:33:32 +000018 */
19
20/* 01/02/2003 Port to LTP avenkat@us.ibm.com */
21/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
22
robbiewc6219fd2003-01-03 20:33:32 +000023/*
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_modakbdbaec52009-02-26 12:14:51 +000043#include <stdio.h>
robbiewc6219fd2003-01-03 20:33:32 +000044#include <string.h>
45#include <unistd.h>
46#include <string.h>
47#include <errno.h>
48
49/***** LTP Port *****/
50#include "test.h"
robbiewc6219fd2003-01-03 20:33:32 +000051
52char *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 *****/
61int local_flag = PASSED;
62int block_number;
robbiewc6219fd2003-01-03 20:33:32 +000063FILE *temp;
subrata_modak56207ce2009-03-23 13:35:39 +000064int TST_TOTAL = 1;
robbiewc6219fd2003-01-03 20:33:32 +000065/***** ** ** *****/
66char buf[BSIZE];
67
68/***** LTP Port *****/
robbiewc6219fd2003-01-03 20:33:32 +000069
70int anyfail();
71int blenter();
72int blexit();
73
74void setup();
75/***** ** ** *****/
76void clearit();
77void fill(char *str);
78int checkit(char *str);
79
80/*--------------------------------------------------------------*/
subrata_modak56207ce2009-03-23 13:35:39 +000081int main(int argc, char *argv[])
robbiewc6219fd2003-01-03 20:33:32 +000082{
83 char *p, *q;
84
subrata_modak56207ce2009-03-23 13:35:39 +000085 setup(); /* temp file is now open */
robbiewc6219fd2003-01-03 20:33:32 +000086/*--------------------------------------------------------------*/
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
robbiewc6219fd2003-01-03 20:33:32 +0000139 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_modak56207ce2009-03-23 13:35:39 +0000197 anyfail(); /* THIS CALL DOES NOT RETURN - EXITS!! */
Garrett Cooper2c282152010-12-16 00:55:50 -0800198 tst_exit();
robbiewc6219fd2003-01-03 20:33:32 +0000199}
subrata_modak56207ce2009-03-23 13:35:39 +0000200
robbiewc6219fd2003-01-03 20:33:32 +0000201/*--------------------------------------------------------------*/
202/* FUNCTIONS GO HERE */
203
Mike Frysingerc57fba52014-04-09 18:56:30 -0400204void clearit(void)
robbiewc6219fd2003-01-03 20:33:32 +0000205{
206 register int i;
207
subrata_modak56207ce2009-03-23 13:35:39 +0000208 for (i = 0; i < BSIZE; i++)
robbiewc6219fd2003-01-03 20:33:32 +0000209 buf[i] = 0;
210}
211
212void fill(char *str)
213{
214 register int i;
subrata_modak56207ce2009-03-23 13:35:39 +0000215 for (i = 0; i < LEN; i++)
robbiewc6219fd2003-01-03 20:33:32 +0000216 *str++ = 'a';
217}
218
219int checkit(char *str)
220{
221 register int i;
subrata_modak56207ce2009-03-23 13:35:39 +0000222 for (i = 0; i < LEN; i++)
robbiewc6219fd2003-01-03 20:33:32 +0000223 if (*str++ != 'a')
subrata_modak56207ce2009-03-23 13:35:39 +0000224 return (-1);
subrata_modakbdbaec52009-02-26 12:14:51 +0000225
robbiewc6219fd2003-01-03 20:33:32 +0000226 return (0);
227}
228
Mike Frysingerc57fba52014-04-09 18:56:30 -0400229int anyfail(void)
robbiewc6219fd2003-01-03 20:33:32 +0000230{
subrata_modak56207ce2009-03-23 13:35:39 +0000231 (local_flag == FAILED) ? tst_resm(TFAIL,
232 "Test failed") : tst_resm(TPASS,
233 "Test passed");
234 tst_exit();
robbiewc6219fd2003-01-03 20:33:32 +0000235}
236
Mike Frysingerc57fba52014-04-09 18:56:30 -0400237void setup(void)
robbiewc6219fd2003-01-03 20:33:32 +0000238{
subrata_modak56207ce2009-03-23 13:35:39 +0000239 temp = stderr;
robbiewc6219fd2003-01-03 20:33:32 +0000240}
241
Mike Frysingerc57fba52014-04-09 18:56:30 -0400242int blenter(void)
robbiewc6219fd2003-01-03 20:33:32 +0000243{
subrata_modak56207ce2009-03-23 13:35:39 +0000244 local_flag = PASSED;
245 return 0;
robbiewc6219fd2003-01-03 20:33:32 +0000246}
247
Mike Frysingerc57fba52014-04-09 18:56:30 -0400248int blexit(void)
robbiewc6219fd2003-01-03 20:33:32 +0000249{
subrata_modak56207ce2009-03-23 13:35:39 +0000250 (local_flag == FAILED) ? tst_resm(TFAIL,
251 "Test failed") : tst_resm(TPASS,
252 "Test passed");
253 return 0;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700254}