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