xref: /freebsd/lib/libc/tests/string/strncmp_test.c (revision 459ddefcc9dcc010f6f7445285e61e2b6780379c)
1 /*-
2  * Copyright (c) 2023 The FreeBSD Foundation
3  *
4  * This software was developed by Robert Clausecker <fuz@FreeBSD.org>
5  * under sponsorship from the FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ''AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE
27  */
28 
29 #include <sys/cdefs.h>
30 
31 #include <atf-c.h>
32 #include <dlfcn.h>
33 #include <string.h>
34 
35 int (*volatile strncmp_fn)(const char *, const char *, size_t);
36 
37 static void
alignment_testcase(char * a,char * b,int want,size_t len)38 alignment_testcase(char *a, char *b, int want, size_t len)
39 {
40 	int res;
41 
42 	res = strncmp_fn(a, b, len);
43 	ATF_CHECK_MSG(want == (res > 0) - (res < 0),
44 	    "strcmp(%p \"%s\", %p \"%s\", %zu) = %d != %d",
45 	    (void *)a, a, (void *)b, b, len, res, want);
46 }
47 
48 static void
check_strncmp_alignments(char a[],char b[],size_t a_off,size_t b_off,size_t len,size_t pos)49 check_strncmp_alignments(char a[], char b[],
50     size_t a_off, size_t b_off, size_t len, size_t pos)
51 {
52 	char *a_str, *b_str, a_orig, b_orig;
53 
54 	a[a_off] = '\0';
55 	b[b_off] = '\0';
56 
57 	a_str = a + a_off + 1;
58 	b_str = b + b_off + 1;
59 
60 	a_str[len] = '\0';
61 	b_str[len] = '\0';
62 	a_str[len+1] = 'A';
63 	b_str[len+1] = 'B';
64 
65 	a_orig = a_str[pos];
66 	b_orig = b_str[pos];
67 
68 	alignment_testcase(a_str, b_str, 0, len + 16);
69 	alignment_testcase(a_str, b_str, 0, len + 1);
70 	alignment_testcase(a_str, b_str, 0, len);
71 
72 	if (pos < len) {
73 		a_str[pos] = '\0';
74 		alignment_testcase(a_str, b_str, -1, len + 16);
75 		alignment_testcase(a_str, b_str, -1, len + 1);
76 		alignment_testcase(a_str, b_str, -1, len);
77 		alignment_testcase(a_str, b_str, -1, pos + 1);
78 		alignment_testcase(a_str, b_str, 0, pos);
79 		a_str[pos] = a_orig;
80 
81 		b_str[pos] = '\0';
82 		alignment_testcase(a_str, b_str, 1, len + 16);
83 		alignment_testcase(a_str, b_str, 1, len + 1);
84 		alignment_testcase(a_str, b_str, 1, len);
85 		alignment_testcase(a_str, b_str, 1, pos + 1);
86 		alignment_testcase(a_str, b_str, 0, pos);
87 		b_str[pos] = b_orig;
88 	}
89 
90 	a_str[pos] = 'X';
91 	alignment_testcase(a_str, b_str, 1, len + 16);
92 	alignment_testcase(a_str, b_str, 0, pos);
93 	alignment_testcase(a_str, b_str, 1, pos + 1);
94 	if (pos < len) {
95 		alignment_testcase(a_str, b_str, 1, len);
96 		alignment_testcase(a_str, b_str, 1, len + 1);
97 	}
98 	a_str[pos] = a_orig;
99 
100 	b_str[pos] = 'X';
101 	alignment_testcase(a_str, b_str, -1, len + 16);
102 	alignment_testcase(a_str, b_str, 0, pos);
103 	alignment_testcase(a_str, b_str, -1, pos + 1);
104 	if (pos < len) {
105 		alignment_testcase(a_str, b_str, -1, len);
106 		alignment_testcase(a_str, b_str, -1, len + 1);
107 	}
108 	b_str[pos] = b_orig;
109 
110 	a[a_off] = '-';
111 	b[b_off] = '-';
112 	a_str[len] = '-';
113 	b_str[len] = '-';
114 	a_str[len+1] = '-';
115 	b_str[len+1] = '-';
116 }
117 
118 ATF_TC(strncmp_alignments);
ATF_TC_HEAD(strncmp_alignments,tc)119 ATF_TC_HEAD(strncmp_alignments, tc)
120 {
121 	atf_tc_set_md_var(tc, "descr", "Test strncmp(3) with various alignments");
122 }
123 
ATF_TC_BODY(strncmp_alignments,tc)124 ATF_TC_BODY(strncmp_alignments, tc)
125 {
126 	size_t a_off, b_off, len, pos;
127 	char a[64+16+16+3], b[64+16+16+3];
128 
129 	memset(a, '-', sizeof(a));
130 	memset(b, '-', sizeof(b));
131 	a[sizeof(a) - 1] = '\0';
132 	b[sizeof(b) - 1] = '\0';
133 
134 	for (a_off = 0; a_off < 16; a_off++)
135 		for (b_off = 0; b_off < 16; b_off++)
136 			for (len = 1; len <= 64; len++)
137 				for (pos = 0; pos <= len; pos++)
138 					check_strncmp_alignments(a, b, a_off, b_off, len, pos);
139 }
140 
141 ATF_TC(strncmp_null);
ATF_TC_HEAD(strncmp_null,tc)142 ATF_TC_HEAD(strncmp_null, tc)
143 {
144 	atf_tc_set_md_var(tc, "descr", "Test strncmp(3) with null pointers");
145 }
146 
ATF_TC_BODY(strncmp_null,tc)147 ATF_TC_BODY(strncmp_null, tc)
148 {
149 	alignment_testcase(NULL, NULL, 0, 0);
150 }
151 
ATF_TP_ADD_TCS(tp)152 ATF_TP_ADD_TCS(tp)
153 {
154 	void *dl_handle;
155 
156 	dl_handle = dlopen(NULL, RTLD_LAZY);
157 	strncmp_fn = dlsym(dl_handle, "test_strncmp");
158 	if (strncmp_fn == NULL)
159 		strncmp_fn = strncmp;
160 
161 	ATF_TP_ADD_TC(tp, strncmp_alignments);
162 	ATF_TP_ADD_TC(tp, strncmp_null);
163 
164 	return atf_no_error();
165 }
166