xref: /freebsd/lib/libc/tests/string/memcmp_test.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
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 <assert.h>
32 #include <dlfcn.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include <atf-c.h>
38 
39 #ifndef MEMCMP
40 #define MEMCMP memcmp
41 #endif
42 
43 /*
44  * On FreeBSD we demand that memcmp returns the difference between the
45  * characters at the first site of mismatch.  However, ISO/IEC 9899:1990
46  * only specifies that a number greater than, equal to, or less than
47  * zero shall be returned.  If a unit test for this less strict
48  * behaviour is desired, define RES(x) to be (((x) > 0) - ((x) < 0)).
49  */
50 #ifndef RES
51 #define RES(x) (x)
52 #endif
53 
54 static int (*memcmp_fn)(const void *, const void *, size_t);
55 
56 static void
check_memcmp(const char * a,const char * b,size_t len,int expected)57 check_memcmp(const char *a, const char *b, size_t len, int expected)
58 {
59 	int got;
60 
61 	got = memcmp_fn(a, b, len);
62 	ATF_CHECK_EQ_MSG(RES(expected), RES(got),
63 	    "%s(%p, %p, %zu) gave %d, but wanted %d",
64 	    __XSTRING(MEMCMP), a, b, len, got, expected);
65 }
66 
67 ATF_TC_WITHOUT_HEAD(zero);
ATF_TC_BODY(zero,tc)68 ATF_TC_BODY(zero, tc)
69 {
70 
71 	check_memcmp("a", "b", 0, 0);
72 	check_memcmp("", "", 0, 0);
73 }
74 
75 ATF_TC_WITHOUT_HEAD(eq);
ATF_TC_BODY(eq,tc)76 ATF_TC_BODY(eq, tc)
77 {
78 	unsigned char data1[256], data2[256];
79 	int i;
80 
81 	for (i = 0; i < 256; i++)
82 		data1[i] = data2[i] = i ^ 0x55;
83 	for (i = 1; i < 256; i++)
84 		check_memcmp(data1, data2, i, 0);
85 	for (i = 1; i < 256; i++)
86 		check_memcmp(data1 + i, data2 + i, 256 - i, 0);
87 }
88 
89 ATF_TC_WITHOUT_HEAD(neq);
ATF_TC_BODY(neq,tc)90 ATF_TC_BODY(neq, tc)
91 {
92 	unsigned char data1[256], data2[256];
93 	int i;
94 
95 	for (i = 0; i < 256; i++) {
96 		data1[i] = i;
97 		data2[i] = i ^ 0x55;
98 	}
99 	for (i = 1; i < 256; i++)
100 		check_memcmp(data1, data2, i, -0x55);
101 	for (i = 1; i < 256; i++)
102 		check_memcmp(data1 + i, data2 + i, 256 - i, i - (i ^ 0x55));
103 }
104 
105 ATF_TC_WITHOUT_HEAD(diff);
ATF_TC_BODY(diff,tc)106 ATF_TC_BODY(diff, tc)
107 {
108 	unsigned char data1[256], data2[256];
109 	int i;
110 
111 	memset(data1, 'a', sizeof(data1));
112 	memset(data2, 'a', sizeof(data2));
113 	data1[128] = 255;
114 	data2[128] = 0;
115 	for (i = 1; i < 66; i++) {
116 		check_memcmp(data1 + 128, data2 + 128, i, 255);
117 		check_memcmp(data2 + 128, data1 + 128, i, -255);
118 		check_memcmp(data1 + 129 - i, data2 + 129 - i, i, 255);
119 		check_memcmp(data2 + 129 - i, data1 + 129 - i, i, -255);
120 		check_memcmp(data1 + 129 - i, data2 + 129 - i, i * 2, 255);
121 		check_memcmp(data2 + 129 - i, data1 + 129 - i, i * 2, -255);
122 	}
123 	data1[128] = 'c';
124 	data2[128] = 'e';
125 	for (i = 1; i < 66; i++) {
126 		check_memcmp(data1 + 128, data2 + 128, i, -2);
127 		check_memcmp(data2 + 128, data1 + 128, i, 2);
128 		check_memcmp(data1 + 129 - i, data2 + 129 - i, i, -2);
129 		check_memcmp(data2 + 129 - i, data1 + 129 - i, i, 2);
130 		check_memcmp(data1 + 129 - i, data2 + 129 - i, i * 2, -2);
131 		check_memcmp(data2 + 129 - i, data1 + 129 - i, i * 2, 2);
132 	}
133 	memset(data1 + 129, 'A', sizeof(data1) - 129);
134 	memset(data2 + 129, 'Z', sizeof(data2) - 129);
135 	for (i = 1; i < 66; i++) {
136 		check_memcmp(data1 + 128, data2 + 128, i, -2);
137 		check_memcmp(data2 + 128, data1 + 128, i, 2);
138 		check_memcmp(data1 + 129 - i, data2 + 129 - i, i, -2);
139 		check_memcmp(data2 + 129 - i, data1 + 129 - i, i, 2);
140 		check_memcmp(data1 + 129 - i, data2 + 129 - i, i * 2, -2);
141 		check_memcmp(data2 + 129 - i, data1 + 129 - i, i * 2, 2);
142 	}
143 }
144 
ATF_TP_ADD_TCS(tp)145 ATF_TP_ADD_TCS(tp)
146 {
147 	void *dl_handle;
148 
149 	dl_handle = dlopen(NULL, RTLD_LAZY);
150 	memcmp_fn = dlsym(dl_handle, "test_" __XSTRING(MEMCMP));
151 	if (memcmp_fn == NULL)
152 		memcmp_fn = MEMCMP;
153 
154 	ATF_TP_ADD_TC(tp, zero);
155 	ATF_TP_ADD_TC(tp, eq);
156 	ATF_TP_ADD_TC(tp, neq);
157 	ATF_TP_ADD_TC(tp, diff);
158 
159 	return (atf_no_error());
160 }
161