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 handling of buffers that are too large */
130 static int lib_test_abuf_large(struct unit_test_state *uts)
139 * This crashes at present due to trying to allocate more memory than
140 * available, which breaks something on sandbox.
144 start = ut_check_free();
146 /* Try an impossible size */
148 ut_asserteq(false, abuf_realloc(&buf, CONFIG_SYS_MALLOC_LEN));
149 ut_assertnull(buf.data);
150 ut_asserteq(0, buf.size);
151 ut_asserteq(false, buf.alloced);
154 ut_assertnull(buf.data);
155 ut_asserteq(0, buf.size);
156 ut_asserteq(false, buf.alloced);
158 /* Start with a normal size then try to increase it, to check realloc */
159 ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
160 ut_assertnonnull(buf.data);
161 ut_asserteq(TEST_DATA_LEN, buf.size);
162 ut_asserteq(true, buf.alloced);
164 delta = ut_check_delta(start);
165 ut_assert(delta > 0);
167 /* try to increase it */
168 ut_asserteq(false, abuf_realloc(&buf, CONFIG_SYS_MALLOC_LEN));
169 ut_asserteq_ptr(ptr, buf.data);
170 ut_asserteq(TEST_DATA_LEN, buf.size);
171 ut_asserteq(true, buf.alloced);
172 ut_asserteq(delta, ut_check_delta(start));
174 /* Check for memory leaks */
176 ut_assertok(ut_check_delta(start));
178 /* Start with a huge unallocated buf and try to move it */
180 abuf_map_sysmem(&buf, 0, CONFIG_SYS_MALLOC_LEN);
181 ut_asserteq(CONFIG_SYS_MALLOC_LEN, buf.size);
182 ut_asserteq(false, buf.alloced);
183 ut_assertnull(abuf_uninit_move(&buf, &size));
185 /* Check for memory leaks */
187 ut_assertok(ut_check_delta(start));
191 LIB_TEST(lib_test_abuf_large, 0);
193 /* Test abuf_uninit_move() */
194 static int lib_test_abuf_uninit_move(struct unit_test_state *uts)
196 void *ptr, *orig_ptr;
202 start = ut_check_free();
205 * TODO: crashes on sandbox sometimes due to an apparent bug in
210 /* Move an empty buffer */
212 ut_assertnull(abuf_uninit_move(&buf, &size));
213 ut_asserteq(0, size);
214 ut_assertnull(abuf_uninit_move(&buf, NULL));
216 /* Move an unallocated buffer */
217 abuf_set(&buf, test_data, TEST_DATA_LEN);
218 ut_assertok(ut_check_delta(start));
219 ptr = abuf_uninit_move(&buf, &size);
220 ut_asserteq(TEST_DATA_LEN, size);
221 ut_asserteq_str(ptr, test_data);
222 ut_assertnonnull(ptr);
223 ut_assertnull(buf.data);
224 ut_asserteq(0, buf.size);
225 ut_asserteq(false, buf.alloced);
227 /* Check that freeing it frees the only allocation */
228 delta = ut_check_delta(start);
229 ut_assert(delta > 0);
231 ut_assertok(ut_check_delta(start));
233 /* Move an allocated buffer */
234 ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
236 strcpy(orig_ptr, test_data);
238 delta = ut_check_delta(start);
239 ut_assert(delta > 0);
240 ptr = abuf_uninit_move(&buf, &size);
241 ut_asserteq(TEST_DATA_LEN, size);
242 ut_assertnonnull(ptr);
243 ut_asserteq_ptr(ptr, orig_ptr);
244 ut_asserteq_str(ptr, test_data);
245 ut_assertnull(buf.data);
246 ut_asserteq(0, buf.size);
247 ut_asserteq(false, buf.alloced);
249 /* Check there was no new allocation */
250 ut_asserteq(delta, ut_check_delta(start));
252 /* Check that freeing it frees the only allocation */
254 ut_assertok(ut_check_delta(start));
256 /* Move an unallocated buffer, without the size */
257 abuf_set(&buf, test_data, TEST_DATA_LEN);
258 ut_assertok(ut_check_delta(start));
259 ptr = abuf_uninit_move(&buf, NULL);
260 ut_asserteq_str(ptr, test_data);
264 LIB_TEST(lib_test_abuf_uninit_move, 0);
266 /* Test abuf_uninit() */
267 static int lib_test_abuf_uninit(struct unit_test_state *uts)
271 /* Nothing in the buffer */
274 ut_assertnull(buf.data);
275 ut_asserteq(0, buf.size);
276 ut_asserteq(false, buf.alloced);
279 abuf_set(&buf, test_data, TEST_DATA_LEN);
281 ut_assertnull(buf.data);
282 ut_asserteq(0, buf.size);
283 ut_asserteq(false, buf.alloced);
287 LIB_TEST(lib_test_abuf_uninit, 0);
289 /* Test abuf_init_set() */
290 static int lib_test_abuf_init_set(struct unit_test_state *uts)
294 abuf_init_set(&buf, test_data, TEST_DATA_LEN);
295 ut_asserteq_ptr(test_data, buf.data);
296 ut_asserteq(TEST_DATA_LEN, buf.size);
297 ut_asserteq(false, buf.alloced);
301 LIB_TEST(lib_test_abuf_init_set, 0);
303 /* Test abuf_init_move() */
304 static int lib_test_abuf_init_move(struct unit_test_state *uts)
310 * TODO: crashes on sandbox sometimes due to an apparent bug in
315 ptr = strdup(test_data);
316 ut_assertnonnull(ptr);
320 abuf_init_move(&buf, ptr, TEST_DATA_LEN);
321 ut_asserteq_ptr(ptr, abuf_data(&buf));
322 ut_asserteq(TEST_DATA_LEN, abuf_size(&buf));
323 ut_asserteq(true, buf.alloced);
327 LIB_TEST(lib_test_abuf_init_move, 0);
329 /* Test abuf_init() */
330 static int lib_test_abuf_init(struct unit_test_state *uts)
338 ut_assertnull(buf.data);
339 ut_asserteq(0, buf.size);
340 ut_asserteq(false, buf.alloced);
344 LIB_TEST(lib_test_abuf_init, 0);