xref: /freebsd/lib/libc/tests/string/stpncpy_test.c (revision f126890a)
1 /*-
2  * Copyright (c) 2009 David Schultz <das@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/param.h>
32 #include <sys/mman.h>
33 #include <assert.h>
34 #include <dlfcn.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 
39 #include <atf-c.h>
40 
41 static char *(*stpncpy_fn)(char *restrict, const char *restrict, size_t);
42 
43 static char *
44 makebuf(size_t len, int guard_at_end)
45 {
46 	char *buf;
47 	size_t alloc_size, page_size;
48 
49 	page_size = getpagesize();
50 	alloc_size = roundup2(len, page_size) + page_size;
51 
52 	buf = mmap(NULL, alloc_size, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
53 	assert(buf);
54 	if (guard_at_end) {
55 		assert(munmap(buf + alloc_size - page_size, page_size) == 0);
56 		return (buf + alloc_size - page_size - len);
57 	} else {
58 		assert(munmap(buf, page_size) == 0);
59 		return (buf + page_size);
60 	}
61 }
62 
63 static void
64 test_stpncpy(const char *s)
65 {
66 	char *src, *dst;
67 	size_t size, len, bufsize, x;
68 	int i, j;
69 
70 	size = strlen(s) + 1;
71 	for (i = 0; i <= 1; i++) {
72 		for (j = 0; j <= 1; j++) {
73 			for (bufsize = 0; bufsize <= size + 10; bufsize++) {
74 				src = makebuf(size, i);
75 				memcpy(src, s, size);
76 				dst = makebuf(bufsize, j);
77 				memset(dst, 'X', bufsize);
78 				len = (bufsize < size) ? bufsize : size - 1;
79 				assert(stpncpy_fn(dst, src, bufsize) == dst+len);
80 				assert(memcmp(src, dst, len) == 0);
81 				for (x = len; x < bufsize; x++)
82 					assert(dst[x] == '\0');
83 			}
84 		}
85 	}
86 }
87 
88 static void
89 test_sentinel(char *dest, char *src, size_t destlen, size_t srclen)
90 {
91 	size_t i;
92 	const char *res, *wantres;
93 	const char *fail = NULL;
94 
95 	for (i = 0; i < srclen; i++)
96 		/* src will never include (){} */
97 		src[i] = '0' + i;
98 	src[srclen] = '\0';
99 
100 	/* source sentinels: not to be copied */
101 	src[-1] = '(';
102 	src[srclen+1] = ')';
103 
104 	memset(dest, 0xee, destlen);
105 
106 	/* destination sentinels: not to be touched */
107 	dest[-1] = '{';
108 	dest[destlen] = '}';
109 
110 	wantres = dest + (srclen > destlen ? destlen : srclen);
111 	res = stpncpy_fn(dest, src, destlen);
112 
113 	if (dest[-1] != '{')
114 		fail = "start sentinel overwritten";
115 	else if (dest[destlen] != '}')
116 		fail = "end sentinel overwritten";
117 	else if (strncmp(src, dest, destlen) != 0)
118 		fail = "string not copied correctly";
119 	else if (res != wantres)
120 		fail = "incorrect return value";
121 	else for (i = srclen; i < destlen; i++)
122 		if (dest[i] != '\0') {
123 			fail = "incomplete NUL padding";
124 			break;
125 		}
126 
127 	if (fail)
128 		atf_tc_fail_nonfatal("%s\n"
129 		    "stpncpy(%p \"%s\", %p \"%s\", %zu) = %p (want %p)\n",
130 		    fail, dest, dest, src, src, destlen, res, wantres);
131 }
132 
133 ATF_TC_WITHOUT_HEAD(null);
134 ATF_TC_BODY(null, tc)
135 {
136 	ATF_CHECK_EQ(stpncpy_fn(NULL, NULL, 0), NULL);
137 }
138 
139 ATF_TC_WITHOUT_HEAD(bounds);
140 ATF_TC_BODY(bounds, tc)
141 {
142 	size_t i;
143 	char buf[64+1];
144 
145 	for (i = 0; i < sizeof(buf) - 1; i++) {
146 		buf[i] = ' ' + i;
147 		buf[i+1] = '\0';
148 		test_stpncpy(buf);
149 	}
150 }
151 
152 ATF_TC_WITHOUT_HEAD(alignments);
153 ATF_TC_BODY(alignments, tc)
154 {
155 	size_t srcalign, destalign, srclen, destlen;
156 	char src[15+3+64]; /* 15 offsets + 64 max length + NUL + sentinels */
157 	char dest[15+2+64]; /* 15 offsets + 64 max length + sentinels */
158 
159 	for (srcalign = 0; srcalign < 16; srcalign++)
160 		for (destalign = 0; destalign < 16; destalign++)
161 			for (srclen = 0; srclen < 64; srclen++)
162 				for (destlen = 0; destlen < 64; destlen++)
163 					test_sentinel(dest+destalign+1,
164 					    src+srcalign+1, destlen, srclen);
165 }
166 
167 ATF_TP_ADD_TCS(tp)
168 {
169 	void *dl_handle;
170 
171 	dl_handle = dlopen(NULL, RTLD_LAZY);
172 	stpncpy_fn = dlsym(dl_handle, "test_stpncpy");
173 	if (stpncpy_fn == NULL)
174 		stpncpy_fn = stpncpy;
175 
176 	ATF_TP_ADD_TC(tp, null);
177 	ATF_TP_ADD_TC(tp, bounds);
178 	ATF_TP_ADD_TC(tp, alignments);
179 
180 	return (atf_no_error());
181 }
182