xref: /freebsd/lib/libc/tests/string/memcmp_test.c (revision 61ba55bcf70f2340f9c943c9571113b3fd8eda69)
1 /*-
2  * Copyright (c) 2016 Jilles Tjoelker <jilles@FreeBSD.org>
3  * Copyright (c) 2023 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Robert Clausecker
7  * <fuz@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #include <assert.h>
33 #include <dlfcn.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 #include <atf-c.h>
39 
40 #ifndef MEMCMP
41 #define MEMCMP memcmp
42 #endif
43 
44 /*
45  * On FreeBSD we demand that memcmp returns the difference between the
46  * characters at the first site of mismatch.  However, ISO/IEC 9899:1990
47  * only specifies that a number greater than, equal to, or less than
48  * zero shall be returned.  If a unit test for this less strict
49  * behaviour is desired, define RES(x) to be (((x) > 0) - ((x) < 0)).
50  */
51 #ifndef RES
52 #define RES(x) (x)
53 #endif
54 
55 static int (*memcmp_fn)(const void *, const void *, size_t);
56 
57 static void
58 check_memcmp(const char *a, const char *b, size_t len, int expected)
59 {
60 	int got;
61 
62 	got = memcmp_fn(a, b, len);
63 	ATF_CHECK_EQ_MSG(RES(expected), RES(got),
64 	    "%s(%p, %p, %zu) gave %d, but wanted %d",
65 	    __XSTRING(MEMCMP), a, b, len, got, expected);
66 }
67 
68 ATF_TC_WITHOUT_HEAD(zero);
69 ATF_TC_BODY(zero, tc)
70 {
71 
72 	check_memcmp("a", "b", 0, 0);
73 	check_memcmp("", "", 0, 0);
74 }
75 
76 ATF_TC_WITHOUT_HEAD(eq);
77 ATF_TC_BODY(eq, tc)
78 {
79 	unsigned char data1[256], data2[256];
80 	int i;
81 
82 	for (i = 0; i < 256; i++)
83 		data1[i] = data2[i] = i ^ 0x55;
84 	for (i = 1; i < 256; i++)
85 		check_memcmp(data1, data2, i, 0);
86 	for (i = 1; i < 256; i++)
87 		check_memcmp(data1 + i, data2 + i, 256 - i, 0);
88 }
89 
90 ATF_TC_WITHOUT_HEAD(neq);
91 ATF_TC_BODY(neq, tc)
92 {
93 	unsigned char data1[256], data2[256];
94 	int i;
95 
96 	for (i = 0; i < 256; i++) {
97 		data1[i] = i;
98 		data2[i] = i ^ 0x55;
99 	}
100 	for (i = 1; i < 256; i++)
101 		check_memcmp(data1, data2, i, -0x55);
102 	for (i = 1; i < 256; i++)
103 		check_memcmp(data1 + i, data2 + i, 256 - i, i - (i ^ 0x55));
104 }
105 
106 ATF_TC_WITHOUT_HEAD(diff);
107 ATF_TC_BODY(diff, tc)
108 {
109 	unsigned char data1[256], data2[256];
110 	int i;
111 
112 	memset(data1, 'a', sizeof(data1));
113 	memset(data2, 'a', sizeof(data2));
114 	data1[128] = 255;
115 	data2[128] = 0;
116 	for (i = 1; i < 66; i++) {
117 		check_memcmp(data1 + 128, data2 + 128, i, 255);
118 		check_memcmp(data2 + 128, data1 + 128, i, -255);
119 		check_memcmp(data1 + 129 - i, data2 + 129 - i, i, 255);
120 		check_memcmp(data2 + 129 - i, data1 + 129 - i, i, -255);
121 		check_memcmp(data1 + 129 - i, data2 + 129 - i, i * 2, 255);
122 		check_memcmp(data2 + 129 - i, data1 + 129 - i, i * 2, -255);
123 	}
124 	data1[128] = 'c';
125 	data2[128] = 'e';
126 	for (i = 1; i < 66; i++) {
127 		check_memcmp(data1 + 128, data2 + 128, i, -2);
128 		check_memcmp(data2 + 128, data1 + 128, i, 2);
129 		check_memcmp(data1 + 129 - i, data2 + 129 - i, i, -2);
130 		check_memcmp(data2 + 129 - i, data1 + 129 - i, i, 2);
131 		check_memcmp(data1 + 129 - i, data2 + 129 - i, i * 2, -2);
132 		check_memcmp(data2 + 129 - i, data1 + 129 - i, i * 2, 2);
133 	}
134 	memset(data1 + 129, 'A', sizeof(data1) - 129);
135 	memset(data2 + 129, 'Z', sizeof(data2) - 129);
136 	for (i = 1; i < 66; i++) {
137 		check_memcmp(data1 + 128, data2 + 128, i, -2);
138 		check_memcmp(data2 + 128, data1 + 128, i, 2);
139 		check_memcmp(data1 + 129 - i, data2 + 129 - i, i, -2);
140 		check_memcmp(data2 + 129 - i, data1 + 129 - i, i, 2);
141 		check_memcmp(data1 + 129 - i, data2 + 129 - i, i * 2, -2);
142 		check_memcmp(data2 + 129 - i, data1 + 129 - i, i * 2, 2);
143 	}
144 }
145 
146 ATF_TP_ADD_TCS(tp)
147 {
148 	void *dl_handle;
149 
150 	dl_handle = dlopen(NULL, RTLD_LAZY);
151 	memcmp_fn = dlsym(dl_handle, "test_" __XSTRING(MEMCMP));
152 	if (memcmp_fn == NULL)
153 		memcmp_fn = MEMCMP;
154 
155 	ATF_TP_ADD_TC(tp, zero);
156 	ATF_TP_ADD_TC(tp, eq);
157 	ATF_TP_ADD_TC(tp, neq);
158 	ATF_TP_ADD_TC(tp, diff);
159 
160 	return (atf_no_error());
161 }
162