1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2002
4 *
5 * 01/02/2003 Port to LTP avenkat@us.ibm.com
6 * 06/30/2001 Port to Linux nsharoff@us.ibm.com
7 */
8
9 /*\
10 * [Description]
11 *
12 * The testcase for buffer copy by check boundary conditions.
13 */
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <errno.h>
20
21 #include "tst_test.h"
22
23 #define BSIZE 4096
24 #define LEN 100
25
26 char buf[BSIZE];
27
clearit(void)28 static void clearit(void)
29 {
30 register int i;
31
32 for (i = 0; i < BSIZE; i++)
33 buf[i] = 0;
34 }
35
fill(char * str,int len)36 static void fill(char *str, int len)
37 {
38 register int i;
39 for (i = 0; i < len; i++)
40 *str++ = 'a';
41 }
42
checkit(char * str,int len)43 static int checkit(char *str, int len)
44 {
45 register int i;
46 for (i = 0; i < len; i++)
47 if (*str++ != 'a')
48 return (-1);
49
50 return 0;
51 }
52
53 static struct test_case {
54 char *p;
55 char *q;
56 int len;
57 } tcases[] = {
58 {&buf[100], &buf[800], LEN},
59 {&buf[800], &buf[100], LEN},
60 };
61
setup(void)62 static void setup(void)
63 {
64 clearit();
65
66 return;
67 }
68
verify_memcpy(char * p,char * q,int len)69 static void verify_memcpy(char *p, char *q, int len)
70 {
71 fill(p, len);
72 memcpy(q, p, LEN);
73
74 if (checkit(q, len)) {
75 tst_res(TFAIL, "copy failed - missed data");
76 goto out;
77 }
78
79 if (p[-1] || p[LEN]) {
80 tst_res(TFAIL, "copy failed - 'to' bounds");
81 goto out;
82 }
83
84 if (q[-1] || q[LEN]) {
85 tst_res(TFAIL, "copy failed - 'from' bounds");
86 goto out;
87 }
88
89 tst_res(TPASS, "Test passed");
90 out:
91 return;
92 }
93
run_test(unsigned int nr)94 static void run_test(unsigned int nr)
95 {
96 struct test_case *tc = &tcases[nr];
97
98 clearit();
99 verify_memcpy(tc->p, tc->q, tc->len);
100
101 return;
102 }
103
104 static struct tst_test test = {
105 .tcnt = ARRAY_SIZE(tcases),
106 .setup = setup,
107 .test = run_test,
108 };
109