1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2019 Heinrich Schuchardt <xypron.glpk@gmx.de>
4  *
5  * Unit tests for memory functions
6  *
7  * The architecture dependent implementations run through different lines of
8  * code depending on the alignment and length of memory regions copied or set.
9  * This has to be considered in testing.
10  */
11 
12 #include <command.h>
13 #include <log.h>
14 #include <string.h>
15 #include <test/lib.h>
16 #include <test/test.h>
17 #include <test/ut.h>
18 
19 /* Xor mask used for marking memory regions */
20 #define MASK 0xA5
21 /* Number of different alignment values */
22 #define SWEEP 16
23 /* Allow for copying up to 32 bytes */
24 #define BUFLEN (SWEEP + 33)
25 
26 #define TEST_STR	"hello"
27 
28 /**
29  * init_buffer() - initialize buffer
30  *
31  * The buffer is filled with incrementing values xor'ed with the mask.
32  *
33  * @buf:	buffer
34  * @mask:	xor mask
35  */
init_buffer(u8 buf[],u8 mask)36 static void init_buffer(u8 buf[], u8 mask)
37 {
38 	int i;
39 
40 	for (i = 0; i < BUFLEN; ++i)
41 		buf[i] = i ^ mask;
42 }
43 
44 /**
45  * test_memset() - test result of memset()
46  *
47  * @uts:	unit test state
48  * @buf:	buffer
49  * @mask:	value set by memset()
50  * @offset:	relative start of region changed by memset() in buffer
51  * @len:	length of region changed by memset()
52  * Return:	0 = success, 1 = failure
53  */
test_memset(struct unit_test_state * uts,u8 buf[],u8 mask,int offset,int len)54 static int test_memset(struct unit_test_state *uts, u8 buf[], u8 mask,
55 		       int offset, int len)
56 {
57 	int i;
58 
59 	for (i = 0; i < BUFLEN; ++i) {
60 		if (i < offset || i >= offset + len) {
61 			ut_asserteq(i, buf[i]);
62 		} else {
63 			ut_asserteq(mask, buf[i]);
64 		}
65 	}
66 	return 0;
67 }
68 
69 /**
70  * lib_memset() - unit test for memset()
71  *
72  * Test memset() with varied alignment and length of the changed buffer.
73  *
74  * @uts:	unit test state
75  * Return:	0 = success, 1 = failure
76  */
lib_memset(struct unit_test_state * uts)77 static int lib_memset(struct unit_test_state *uts)
78 {
79 	u8 buf[BUFLEN];
80 	int offset, len;
81 	void *ptr;
82 
83 	for (offset = 0; offset <= SWEEP; ++offset) {
84 		for (len = 1; len < BUFLEN - SWEEP; ++len) {
85 			init_buffer(buf, 0);
86 			ptr = memset(buf + offset, MASK, len);
87 			ut_asserteq_ptr(buf + offset, (u8 *)ptr);
88 			if (test_memset(uts, buf, MASK, offset, len)) {
89 				debug("%s: failure %d, %d\n",
90 				      __func__, offset, len);
91 				return CMD_RET_FAILURE;
92 			}
93 		}
94 	}
95 	return 0;
96 }
97 LIB_TEST(lib_memset, 0);
98 
99 /**
100  * test_memmove() - test result of memcpy() or memmove()
101  *
102  * @uts:	unit test state
103  * @buf:	buffer
104  * @mask:	xor mask used to initialize source buffer
105  * @offset1:	relative start of copied region in source buffer
106  * @offset2:	relative start of copied region in destination buffer
107  * @len:	length of region changed by memset()
108  * Return:	0 = success, 1 = failure
109  */
test_memmove(struct unit_test_state * uts,u8 buf[],u8 mask,int offset1,int offset2,int len)110 static int test_memmove(struct unit_test_state *uts, u8 buf[], u8 mask,
111 			int offset1, int offset2, int len)
112 {
113 	int i;
114 
115 	for (i = 0; i < BUFLEN; ++i) {
116 		if (i < offset2 || i >= offset2 + len) {
117 			ut_asserteq(i, buf[i]);
118 		} else {
119 			ut_asserteq((i + offset1 - offset2) ^ mask, buf[i]);
120 		}
121 	}
122 	return 0;
123 }
124 
125 /**
126  * lib_memcpy() - unit test for memcpy()
127  *
128  * Test memcpy() with varied alignment and length of the copied buffer.
129  *
130  * @uts:	unit test state
131  * Return:	0 = success, 1 = failure
132  */
lib_memcpy(struct unit_test_state * uts)133 static int lib_memcpy(struct unit_test_state *uts)
134 {
135 	u8 buf1[BUFLEN];
136 	u8 buf2[BUFLEN];
137 	int offset1, offset2, len;
138 	void *ptr;
139 
140 	init_buffer(buf1, MASK);
141 
142 	for (offset1 = 0; offset1 <= SWEEP; ++offset1) {
143 		for (offset2 = 0; offset2 <= SWEEP; ++offset2) {
144 			for (len = 1; len < BUFLEN - SWEEP; ++len) {
145 				init_buffer(buf2, 0);
146 				ptr = memcpy(buf2 + offset2, buf1 + offset1,
147 					     len);
148 				ut_asserteq_ptr(buf2 + offset2, (u8 *)ptr);
149 				if (test_memmove(uts, buf2, MASK, offset1,
150 						 offset2, len)) {
151 					debug("%s: failure %d, %d, %d\n",
152 					      __func__, offset1, offset2, len);
153 					return CMD_RET_FAILURE;
154 				}
155 			}
156 		}
157 	}
158 	return 0;
159 }
160 LIB_TEST(lib_memcpy, 0);
161 
162 /**
163  * lib_memmove() - unit test for memmove()
164  *
165  * Test memmove() with varied alignment and length of the copied buffer.
166  *
167  * @uts:	unit test state
168  * Return:	0 = success, 1 = failure
169  */
lib_memmove(struct unit_test_state * uts)170 static int lib_memmove(struct unit_test_state *uts)
171 {
172 	u8 buf[BUFLEN];
173 	int offset1, offset2, len;
174 	void *ptr;
175 
176 	for (offset1 = 0; offset1 <= SWEEP; ++offset1) {
177 		for (offset2 = 0; offset2 <= SWEEP; ++offset2) {
178 			for (len = 1; len < BUFLEN - SWEEP; ++len) {
179 				init_buffer(buf, 0);
180 				ptr = memmove(buf + offset2, buf + offset1,
181 					      len);
182 				ut_asserteq_ptr(buf + offset2, (u8 *)ptr);
183 				if (test_memmove(uts, buf, 0, offset1, offset2,
184 						 len)) {
185 					debug("%s: failure %d, %d, %d\n",
186 					      __func__, offset1, offset2, len);
187 					return CMD_RET_FAILURE;
188 				}
189 			}
190 		}
191 	}
192 	return 0;
193 }
194 LIB_TEST(lib_memmove, 0);
195 
196 /** lib_memdup() - unit test for memdup() */
lib_memdup(struct unit_test_state * uts)197 static int lib_memdup(struct unit_test_state *uts)
198 {
199 	char buf[BUFLEN];
200 	size_t len;
201 	char *p, *q;
202 
203 	/* Zero size should do nothing */
204 	p = memdup(NULL, 0);
205 	ut_assertnonnull(p);
206 	free(p);
207 
208 	p = memdup(buf, 0);
209 	ut_assertnonnull(p);
210 	free(p);
211 
212 	strcpy(buf, TEST_STR);
213 	len = sizeof(TEST_STR);
214 	p = memdup(buf, len);
215 	ut_asserteq_mem(p, buf, len);
216 
217 	q = memdup(p, len);
218 	ut_asserteq_mem(q, buf, len);
219 	free(q);
220 	free(p);
221 
222 	return 0;
223 }
224 LIB_TEST(lib_memdup, 0);
225 
226 /** lib_strnstr() - unit test for strnstr() */
lib_strnstr(struct unit_test_state * uts)227 static int lib_strnstr(struct unit_test_state *uts)
228 {
229 	const char *s1 = "Itsy Bitsy Teenie Weenie";
230 	const char *s2 = "eenie";
231 	const char *s3 = "eery";
232 
233 	ut_asserteq_ptr(&s1[12], strnstr(s1, s2, SIZE_MAX));
234 	ut_asserteq_ptr(&s1[12], strnstr(s1, s2, 17));
235 	ut_assertnull(strnstr(s1, s2, 16));
236 	ut_assertnull(strnstr(s1, s2, 0));
237 	ut_asserteq_ptr(&s1[13], strnstr(&s1[3], &s2[1], SIZE_MAX));
238 	ut_asserteq_ptr(&s1[13], strnstr(&s1[3], &s2[1], 14));
239 	ut_assertnull(strnstr(&s1[3], &s2[1], 13));
240 	ut_assertnull(strnstr(&s1[3], &s2[1], 0));
241 	ut_assertnull(strnstr(s1, s3, SIZE_MAX));
242 	ut_assertnull(strnstr(s1, s3, 0));
243 
244 	return 0;
245 }
246 LIB_TEST(lib_strnstr, 0);
247 
248 /** lib_strstr() - unit test for strstr() */
lib_strstr(struct unit_test_state * uts)249 static int lib_strstr(struct unit_test_state *uts)
250 {
251 	const char *s1 = "Itsy Bitsy Teenie Weenie";
252 	const char *s2 = "eenie";
253 	const char *s3 = "easy";
254 
255 	ut_asserteq_ptr(&s1[12], strstr(s1, s2));
256 	ut_asserteq_ptr(&s1[13], strstr(&s1[3], &s2[1]));
257 	ut_assertnull(strstr(s1, s3));
258 	ut_asserteq_ptr(&s1[2], strstr(s1, &s3[2]));
259 	ut_asserteq_ptr(&s1[8], strstr(&s1[5], &s3[2]));
260 
261 	return 0;
262 }
263 LIB_TEST(lib_strstr, 0);
264 
lib_strim(struct unit_test_state * uts)265 static int lib_strim(struct unit_test_state *uts)
266 {
267 	char buf[BUFLEN], *p;
268 
269 	strcpy(buf, "abc");
270 	ut_asserteq_str("abc", strim(buf));
271 
272 	/* leading space */
273 	strcpy(buf, " abc");
274 	ut_asserteq_str("abc", strim(buf));
275 
276 	/* multiple leading spaces */
277 	strcpy(buf, "  abc");
278 	ut_asserteq_str("abc", strim(buf));
279 
280 	/* multiple internal spaces */
281 	strcpy(buf, "  a   bc");
282 	ut_asserteq_str("a   bc", strim(buf));
283 
284 	/* with trailing space */
285 	strcpy(buf, "  a   bc ");
286 	ut_asserteq_str("a   bc", strim(buf));
287 
288 	/* with multiple trailing spaces */
289 	strcpy(buf, "  a   bc   ");
290 	ut_asserteq_str("a   bc", strim(buf));
291 
292 	/* with only spaces */
293 	strcpy(buf, "     ");
294 	p = strim(buf);
295 	ut_asserteq_ptr(p, buf);
296 	ut_asserteq_str("", p);
297 
298 	return 0;
299 }
300 LIB_TEST(lib_strim, 0);
301