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 comparison by check boundary conditions.
13 */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include "tst_test.h"
20
21 #define BSIZE 4096
22 #define LEN 100
23
24 char buf[BSIZE];
25
26 static struct test_case {
27 char *p;
28 char *q;
29 int len;
30 } tcases[] = {
31 {&buf[100], &buf[800], LEN},
32 {&buf[800], &buf[100], LEN},
33 };
34
fill(char * str,int len)35 static void fill(char *str, int len)
36 {
37 register int i;
38 for (i = 0; i < len; i++)
39 *str++ = 'a';
40 }
41
setup(void)42 static void setup(void)
43 {
44 register int i;
45
46 for (i = 0; i < BSIZE; i++)
47 buf[i] = 0;
48
49 return;
50 }
51
verify_memcmp(char * p,char * q,int len)52 static void verify_memcmp(char *p, char *q, int len)
53 {
54 fill(p, len);
55 fill(q, len);
56
57 if (memcmp(p, q, len)) {
58 tst_res(TFAIL, "memcmp fails - should have succeeded.");
59 goto out;
60 }
61
62 p[len - 1] = 0;
63
64 if (memcmp(p, q, len) >= 0) {
65 tst_res(TFAIL, "memcmp succeeded - should have failed.");
66 goto out;
67 };
68
69 p[len - 1] = 'a';
70 p[0] = 0;
71
72 if (memcmp(p, q, len) >= 0) {
73 tst_res(TFAIL, "memcmp succeeded - should have failed.");
74 goto out;
75 };
76
77 p[0] = 'a';
78 q[len - 1] = 0;
79
80 if (memcmp(p, q, len) <= 0) {
81 tst_res(TFAIL, "memcmp succeeded - should have failed.");
82 goto out;
83 };
84
85 q[len - 1] = 'a';
86 q[0] = 0;
87
88 if (memcmp(p, q, len) <= 0) {
89 tst_res(TFAIL, "memcmp succeeded - should have failed.");
90 goto out;
91 };
92
93 q[0] = 'a';
94
95 if (memcmp(p, q, len)) {
96 tst_res(TFAIL, "memcmp fails - should have succeeded.");
97 goto out;
98 }
99
100 tst_res(TPASS, "Test passed");
101 out:
102 return;
103 }
104
run_test(unsigned int nr)105 static void run_test(unsigned int nr)
106 {
107 struct test_case *tc = &tcases[nr];
108
109 verify_memcmp(tc->p, tc->q, tc->len);
110
111 return;
112 }
113
114 static struct tst_test test = {
115 .tcnt = ARRAY_SIZE(tcases),
116 .setup = setup,
117 .test = run_test,
118 };
119