1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2021 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
11 #include <test/test.h>
14 static char test_data[] = "1234";
15 #define TEST_DATA_LEN sizeof(test_data)
18 static int lib_test_abuf_set(struct unit_test_state *uts)
23 start = ut_check_free();
26 abuf_set(&buf, test_data, TEST_DATA_LEN);
27 ut_asserteq_ptr(test_data, buf.data);
28 ut_asserteq(TEST_DATA_LEN, buf.size);
29 ut_asserteq(false, buf.alloced);
31 /* Force it to allocate */
32 ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN + 1));
33 ut_assertnonnull(buf.data);
34 ut_asserteq(TEST_DATA_LEN + 1, buf.size);
35 ut_asserteq(true, buf.alloced);
37 /* Now set it again, to force it to free */
38 abuf_set(&buf, test_data, TEST_DATA_LEN);
39 ut_asserteq_ptr(test_data, buf.data);
40 ut_asserteq(TEST_DATA_LEN, buf.size);
41 ut_asserteq(false, buf.alloced);
43 /* Check for memory leaks */
44 ut_assertok(ut_check_delta(start));
48 LIB_TEST(lib_test_abuf_set, 0);
50 /* Test abuf_map_sysmem() */
51 static int lib_test_abuf_map_sysmem(struct unit_test_state *uts)
58 abuf_map_sysmem(&buf, addr, TEST_DATA_LEN);
60 ut_asserteq_ptr(map_sysmem(0x100, 0), buf.data);
61 ut_asserteq(TEST_DATA_LEN, buf.size);
62 ut_asserteq(false, buf.alloced);
66 LIB_TEST(lib_test_abuf_map_sysmem, 0);
68 /* Test abuf_realloc() */
69 static int lib_test_abuf_realloc(struct unit_test_state *uts)
76 * TODO: crashes on sandbox sometimes due to an apparent bug in
81 start = ut_check_free();
85 /* Allocate an empty buffer */
86 ut_asserteq(true, abuf_realloc(&buf, 0));
87 ut_assertnull(buf.data);
88 ut_asserteq(0, buf.size);
89 ut_asserteq(false, buf.alloced);
91 /* Allocate a non-empty abuf */
92 ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
93 ut_assertnonnull(buf.data);
94 ut_asserteq(TEST_DATA_LEN, buf.size);
95 ut_asserteq(true, buf.alloced);
99 * Make it smaller; the pointer should remain the same. Note this relies
100 * on knowledge of how U-Boot's realloc() works
102 ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN - 1));
103 ut_asserteq(TEST_DATA_LEN - 1, buf.size);
104 ut_asserteq(true, buf.alloced);
105 ut_asserteq_ptr(ptr, buf.data);
108 * Make it larger, forcing reallocation. Note this relies on knowledge
109 * of how U-Boot's realloc() works
111 ut_asserteq(true, abuf_realloc(&buf, 0x1000));
112 ut_assert(buf.data != ptr);
113 ut_asserteq(0x1000, buf.size);
114 ut_asserteq(true, buf.alloced);
117 ut_asserteq(true, abuf_realloc(&buf, 0));
118 ut_assertnull(buf.data);
119 ut_asserteq(0, buf.size);
120 ut_asserteq(false, buf.alloced);
122 /* Check for memory leaks */
123 ut_assertok(ut_check_delta(start));
127 LIB_TEST(lib_test_abuf_realloc, 0);
129 /* Test abuf_realloc() on an non-allocated buffer of zero size */
130 static int lib_test_abuf_realloc_size(struct unit_test_state *uts)
135 start = ut_check_free();
139 /* Allocate some space */
140 ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
141 ut_assertnonnull(buf.data);
142 ut_asserteq(TEST_DATA_LEN, buf.size);
143 ut_asserteq(true, buf.alloced);
146 ut_asserteq(true, abuf_realloc(&buf, 0));
147 ut_assertnull(buf.data);
148 ut_asserteq(0, buf.size);
149 ut_asserteq(false, buf.alloced);
151 /* Check for memory leaks */
152 ut_assertok(ut_check_delta(start));
156 LIB_TEST(lib_test_abuf_realloc_size, 0);
158 /* Test handling of buffers that are too large */
159 static int lib_test_abuf_large(struct unit_test_state *uts)
168 * This crashes at present due to trying to allocate more memory than
169 * available, which breaks something on sandbox.
173 start = ut_check_free();
175 /* Try an impossible size */
177 ut_asserteq(false, abuf_realloc(&buf, CONFIG_SYS_MALLOC_LEN));
178 ut_assertnull(buf.data);
179 ut_asserteq(0, buf.size);
180 ut_asserteq(false, buf.alloced);
183 ut_assertnull(buf.data);
184 ut_asserteq(0, buf.size);
185 ut_asserteq(false, buf.alloced);
187 /* Start with a normal size then try to increase it, to check realloc */
188 ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
189 ut_assertnonnull(buf.data);
190 ut_asserteq(TEST_DATA_LEN, buf.size);
191 ut_asserteq(true, buf.alloced);
193 delta = ut_check_delta(start);
194 ut_assert(delta > 0);
196 /* try to increase it */
197 ut_asserteq(false, abuf_realloc(&buf, CONFIG_SYS_MALLOC_LEN));
198 ut_asserteq_ptr(ptr, buf.data);
199 ut_asserteq(TEST_DATA_LEN, buf.size);
200 ut_asserteq(true, buf.alloced);
201 ut_asserteq(delta, ut_check_delta(start));
203 /* Check for memory leaks */
205 ut_assertok(ut_check_delta(start));
207 /* Start with a huge unallocated buf and try to move it */
209 abuf_map_sysmem(&buf, 0, CONFIG_SYS_MALLOC_LEN);
210 ut_asserteq(CONFIG_SYS_MALLOC_LEN, buf.size);
211 ut_asserteq(false, buf.alloced);
212 ut_assertnull(abuf_uninit_move(&buf, &size));
214 /* Check for memory leaks */
216 ut_assertok(ut_check_delta(start));
220 LIB_TEST(lib_test_abuf_large, 0);
222 /* Test abuf_uninit_move() */
223 static int lib_test_abuf_uninit_move(struct unit_test_state *uts)
225 void *ptr, *orig_ptr;
231 start = ut_check_free();
234 * TODO: crashes on sandbox sometimes due to an apparent bug in
239 /* Move an empty buffer */
241 ut_assertnull(abuf_uninit_move(&buf, &size));
242 ut_asserteq(0, size);
243 ut_assertnull(abuf_uninit_move(&buf, NULL));
245 /* Move an unallocated buffer */
246 abuf_set(&buf, test_data, TEST_DATA_LEN);
247 ut_assertok(ut_check_delta(start));
248 ptr = abuf_uninit_move(&buf, &size);
249 ut_asserteq(TEST_DATA_LEN, size);
250 ut_asserteq_str(ptr, test_data);
251 ut_assertnonnull(ptr);
252 ut_assertnull(buf.data);
253 ut_asserteq(0, buf.size);
254 ut_asserteq(false, buf.alloced);
256 /* Check that freeing it frees the only allocation */
257 delta = ut_check_delta(start);
258 ut_assert(delta > 0);
260 ut_assertok(ut_check_delta(start));
262 /* Move an allocated buffer */
263 ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
265 strcpy(orig_ptr, test_data);
267 delta = ut_check_delta(start);
268 ut_assert(delta > 0);
269 ptr = abuf_uninit_move(&buf, &size);
270 ut_asserteq(TEST_DATA_LEN, size);
271 ut_assertnonnull(ptr);
272 ut_asserteq_ptr(ptr, orig_ptr);
273 ut_asserteq_str(ptr, test_data);
274 ut_assertnull(buf.data);
275 ut_asserteq(0, buf.size);
276 ut_asserteq(false, buf.alloced);
278 /* Check there was no new allocation */
279 ut_asserteq(delta, ut_check_delta(start));
281 /* Check that freeing it frees the only allocation */
283 ut_assertok(ut_check_delta(start));
285 /* Move an unallocated buffer, without the size */
286 abuf_set(&buf, test_data, TEST_DATA_LEN);
287 ut_assertok(ut_check_delta(start));
288 ptr = abuf_uninit_move(&buf, NULL);
289 ut_asserteq_str(ptr, test_data);
293 LIB_TEST(lib_test_abuf_uninit_move, 0);
295 /* Test abuf_uninit() */
296 static int lib_test_abuf_uninit(struct unit_test_state *uts)
300 /* Nothing in the buffer */
303 ut_assertnull(buf.data);
304 ut_asserteq(0, buf.size);
305 ut_asserteq(false, buf.alloced);
308 abuf_set(&buf, test_data, TEST_DATA_LEN);
310 ut_assertnull(buf.data);
311 ut_asserteq(0, buf.size);
312 ut_asserteq(false, buf.alloced);
316 LIB_TEST(lib_test_abuf_uninit, 0);
318 /* Test abuf_init_set() */
319 static int lib_test_abuf_init_set(struct unit_test_state *uts)
323 abuf_init_set(&buf, test_data, TEST_DATA_LEN);
324 ut_asserteq_ptr(test_data, buf.data);
325 ut_asserteq(TEST_DATA_LEN, buf.size);
326 ut_asserteq(false, buf.alloced);
330 LIB_TEST(lib_test_abuf_init_set, 0);
332 /* Test abuf_init_move() */
333 static int lib_test_abuf_init_move(struct unit_test_state *uts)
339 * TODO: crashes on sandbox sometimes due to an apparent bug in
344 ptr = strdup(test_data);
345 ut_assertnonnull(ptr);
349 abuf_init_move(&buf, ptr, TEST_DATA_LEN);
350 ut_asserteq_ptr(ptr, abuf_data(&buf));
351 ut_asserteq(TEST_DATA_LEN, abuf_size(&buf));
352 ut_asserteq(true, buf.alloced);
356 LIB_TEST(lib_test_abuf_init_move, 0);
358 /* Test abuf_init() */
359 static int lib_test_abuf_init(struct unit_test_state *uts)
367 ut_assertnull(buf.data);
368 ut_asserteq(0, buf.size);
369 ut_asserteq(false, buf.alloced);
373 LIB_TEST(lib_test_abuf_init, 0);