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