]> Git Repo - u-boot.git/blob - test/common/bloblist.c
Merge branch 'master' of https://source.denx.de/u-boot/custodians/u-boot-sh
[u-boot.git] / test / common / bloblist.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2018, Google Inc. All rights reserved.
4  */
5
6 #include <bloblist.h>
7 #include <log.h>
8 #include <mapmem.h>
9 #include <test/suites.h>
10 #include <test/test.h>
11 #include <test/ut.h>
12
13 /* Declare a new bloblist test */
14 #define BLOBLIST_TEST(_name, _flags) \
15                 UNIT_TEST(_name, _flags, bloblist)
16
17 enum {
18         TEST_TAG                = BLOBLISTT_U_BOOT_SPL_HANDOFF,
19         TEST_TAG2               = BLOBLISTT_VBOOT_CTX,
20         TEST_TAG_MISSING        = 0x10000,
21
22         TEST_SIZE               = 10,
23         TEST_SIZE2              = 20,
24         TEST_SIZE_LARGE         = 0x3e0,
25
26         TEST_ADDR               = CONFIG_BLOBLIST_ADDR,
27         TEST_BLOBLIST_SIZE      = 0x400,
28
29         ERASE_BYTE              = '\xff',
30 };
31
32 static const char test1_str[] = "the eyes are open";
33 static const char test2_str[] = "the mouth moves";
34
35 static struct bloblist_hdr *clear_bloblist(void)
36 {
37         struct bloblist_hdr *hdr;
38
39         /*
40          * Clear out any existing bloblist so we have a clean slate. Zero the
41          * header so that existing records are removed, but set everything else
42          * to 0xff for testing purposes.
43          */
44         hdr = map_sysmem(CONFIG_BLOBLIST_ADDR, TEST_BLOBLIST_SIZE);
45         memset(hdr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
46         memset(hdr, '\0', sizeof(*hdr));
47
48         return hdr;
49 }
50
51 static int check_zero(void *data, int size)
52 {
53         u8 *ptr;
54         int i;
55
56         for (ptr = data, i = 0; i < size; i++, ptr++) {
57                 if (*ptr)
58                         return -EINVAL;
59         }
60
61         return 0;
62 }
63
64 static int bloblist_test_init(struct unit_test_state *uts)
65 {
66         struct bloblist_hdr *hdr;
67
68         hdr = clear_bloblist();
69         ut_asserteq(-ENOENT, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
70         ut_asserteq_ptr(NULL, bloblist_check_magic(TEST_ADDR));
71         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
72         ut_asserteq_ptr(hdr, bloblist_check_magic(TEST_ADDR));
73         hdr->version++;
74         ut_asserteq(-EPROTONOSUPPORT, bloblist_check(TEST_ADDR,
75                                                      TEST_BLOBLIST_SIZE));
76
77         ut_asserteq(-ENOSPC, bloblist_new(TEST_ADDR, 0xc, 0, 0));
78         ut_asserteq(-EFAULT, bloblist_new(1, TEST_BLOBLIST_SIZE, 0, 0));
79         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
80
81         ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
82         ut_assertok(bloblist_finish());
83         ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
84
85         hdr->magic++;
86         ut_asserteq_ptr(NULL, bloblist_check_magic(TEST_ADDR));
87         hdr->magic--;
88
89         hdr->flags++;
90         ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
91
92         return 1;
93 }
94 BLOBLIST_TEST(bloblist_test_init, UFT_BLOBLIST);
95
96 static int bloblist_test_blob(struct unit_test_state *uts)
97 {
98         struct bloblist_hdr *hdr;
99         struct bloblist_rec *rec, *rec2;
100         char *data;
101         int size = 0;
102
103         /* At the start there should be no records */
104         hdr = clear_bloblist();
105         ut_assertnull(bloblist_find(TEST_TAG, TEST_BLOBLIST_SIZE));
106         ut_assertnull(bloblist_get_blob(TEST_TAG, &size));
107         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
108         ut_asserteq(sizeof(struct bloblist_hdr), bloblist_get_size());
109         ut_asserteq(TEST_BLOBLIST_SIZE, bloblist_get_total_size());
110         ut_asserteq(TEST_ADDR, bloblist_get_base());
111         ut_asserteq(map_to_sysmem(hdr), TEST_ADDR);
112
113         /* Add a record and check that we can find it */
114         data = bloblist_add(TEST_TAG, TEST_SIZE, 0);
115         rec = (void *)(hdr + 1);
116         ut_asserteq_addr(rec + 1, data);
117         data = bloblist_find(TEST_TAG, TEST_SIZE);
118         ut_asserteq_addr(rec + 1, data);
119         ut_asserteq_addr(bloblist_get_blob(TEST_TAG, &size), data);
120         ut_asserteq(size, TEST_SIZE);
121
122         /* Check the data is zeroed */
123         ut_assertok(check_zero(data, TEST_SIZE));
124
125         /* Check the 'ensure' method */
126         ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
127         ut_assertnull(bloblist_ensure(TEST_TAG, TEST_SIZE2));
128         rec2 = (struct bloblist_rec *)(data + ALIGN(TEST_SIZE, BLOBLIST_ALIGN));
129         ut_assertok(check_zero(data, TEST_SIZE));
130
131         /* Check for a non-existent record */
132         ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
133         ut_asserteq_addr(rec2 + 1, bloblist_ensure(TEST_TAG2, TEST_SIZE2));
134         ut_assertnull(bloblist_find(TEST_TAG_MISSING, 0));
135
136         return 0;
137 }
138 BLOBLIST_TEST(bloblist_test_blob, UFT_BLOBLIST);
139
140 /* Check bloblist_ensure_size_ret() */
141 static int bloblist_test_blob_ensure(struct unit_test_state *uts)
142 {
143         void *data, *data2;
144         int size;
145
146         /* At the start there should be no records */
147         clear_bloblist();
148         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
149
150         /* Test with an empty bloblist */
151         size = TEST_SIZE;
152         ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data));
153         ut_asserteq(TEST_SIZE, size);
154         ut_assertok(check_zero(data, TEST_SIZE));
155
156         /* Check that we get the same thing again */
157         ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data2));
158         ut_asserteq(TEST_SIZE, size);
159         ut_asserteq_addr(data, data2);
160
161         /* Check that the size remains the same */
162         size = TEST_SIZE2;
163         ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data));
164         ut_asserteq(TEST_SIZE, size);
165
166         /* Check running out of space */
167         size = TEST_SIZE_LARGE;
168         ut_asserteq(-ENOSPC, bloblist_ensure_size_ret(TEST_TAG2, &size, &data));
169
170         return 0;
171 }
172 BLOBLIST_TEST(bloblist_test_blob_ensure, UFT_BLOBLIST);
173
174 static int bloblist_test_bad_blob(struct unit_test_state *uts)
175 {
176         struct bloblist_hdr *hdr;
177         void *data;
178
179         hdr = clear_bloblist();
180         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
181         data = hdr + 1;
182         data += sizeof(struct bloblist_rec);
183         ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
184         ut_asserteq_addr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
185
186         return 0;
187 }
188 BLOBLIST_TEST(bloblist_test_bad_blob, UFT_BLOBLIST);
189
190 static int bloblist_test_checksum(struct unit_test_state *uts)
191 {
192         struct bloblist_hdr *hdr;
193         char *data, *data2;
194
195         hdr = clear_bloblist();
196         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
197         ut_assertok(bloblist_finish());
198         ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
199
200         /*
201          * Now change things amd make sure that the checksum notices. We cannot
202          * change the size or alloced fields, since that will crash the code.
203          * It has to rely on these being correct.
204          */
205         hdr->flags--;
206         ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
207         hdr->flags++;
208
209         hdr->total_size--;
210         ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
211         hdr->total_size++;
212
213         hdr->spare++;
214         ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
215         hdr->spare--;
216
217         hdr->chksum++;
218         ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
219         hdr->chksum--;
220
221         hdr->align_log2++;
222         ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
223         hdr->align_log2--;
224
225         /* Make sure the checksum changes when we add blobs */
226         data = bloblist_add(TEST_TAG, TEST_SIZE, 0);
227         ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
228
229         data2 = bloblist_add(TEST_TAG2, TEST_SIZE2, 0);
230         ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
231         ut_assertok(bloblist_finish());
232
233         /* It should also change if we change the data */
234         ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
235         *data += 1;
236         ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
237         *data -= 1;
238
239         ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
240         *data2 += 1;
241         ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
242         *data2 -= 1;
243
244         /*
245          * Changing data outside the range of valid data should affect the
246          * checksum.
247          */
248         ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
249         data[TEST_SIZE]++;
250         ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
251         data[TEST_SIZE]--;
252         ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
253
254         data2[TEST_SIZE2]++;
255         ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
256         data[TEST_SIZE]--;
257         ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
258
259         return 0;
260 }
261 BLOBLIST_TEST(bloblist_test_checksum, UFT_BLOBLIST);
262
263 /* Test the 'bloblist info' command */
264 static int bloblist_test_cmd_info(struct unit_test_state *uts)
265 {
266         struct bloblist_hdr *hdr;
267         char *data, *data2;
268
269         hdr = clear_bloblist();
270         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
271         data = bloblist_ensure(TEST_TAG, TEST_SIZE);
272         data2 = bloblist_ensure(TEST_TAG2, TEST_SIZE2);
273
274         run_command("bloblist info", 0);
275         ut_assert_nextline("base:       %lx", (ulong)map_to_sysmem(hdr));
276         ut_assert_nextline("total size: 400    1 KiB");
277         ut_assert_nextline("used size:  50     80 Bytes");
278         ut_assert_nextline("free:       3b0    944 Bytes");
279
280         return 0;
281 }
282 BLOBLIST_TEST(bloblist_test_cmd_info, UFT_BLOBLIST | UTF_CONSOLE);
283
284 /* Test the 'bloblist list' command */
285 static int bloblist_test_cmd_list(struct unit_test_state *uts)
286 {
287         struct bloblist_hdr *hdr;
288         char *data, *data2;
289
290         hdr = clear_bloblist();
291         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
292         data = bloblist_ensure(TEST_TAG, TEST_SIZE);
293         data2 = bloblist_ensure(TEST_TAG2, TEST_SIZE2);
294
295         run_command("bloblist list", 0);
296         ut_assert_nextline("Address       Size   Tag Name");
297         ut_assert_nextline("%08lx  %8x  fff000 SPL hand-off",
298                            (ulong)map_to_sysmem(data), TEST_SIZE);
299         ut_assert_nextline("%08lx  %8x   202 Chrome OS vboot context",
300                            (ulong)map_to_sysmem(data2), TEST_SIZE2);
301
302         return 0;
303 }
304 BLOBLIST_TEST(bloblist_test_cmd_list, UFT_BLOBLIST | UTF_CONSOLE);
305
306 /* Test alignment of bloblist blobs */
307 static int bloblist_test_align(struct unit_test_state *uts)
308 {
309         struct bloblist_hdr *hdr;
310         ulong addr;
311         char *data;
312         int i;
313
314         /* At the start there should be no records */
315         hdr = clear_bloblist();
316         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
317         ut_assertnull(bloblist_find(TEST_TAG, TEST_BLOBLIST_SIZE));
318
319         /* Check the default alignment */
320         for (i = 0; i < 3; i++) {
321                 int size = i * 3;
322                 ulong addr;
323                 char *data;
324                 int j;
325
326                 data = bloblist_add(i, size, 0);
327                 ut_assertnonnull(data);
328                 addr = map_to_sysmem(data);
329                 ut_asserteq(0, addr & (BLOBLIST_BLOB_ALIGN - 1));
330
331                 /* Only the bytes in the blob data should be zeroed */
332                 for (j = 0; j < size; j++)
333                         ut_asserteq(0, data[j]);
334                 for (; j < BLOBLIST_BLOB_ALIGN; j++)
335                         ut_asserteq(ERASE_BYTE, data[j]);
336         }
337
338         /* Check larger alignment */
339         for (i = 0; i < 3; i++) {
340                 int align = 5 - i;
341
342                 data = bloblist_add(3 + i, i * 4, align);
343                 ut_assertnonnull(data);
344                 addr = map_to_sysmem(data);
345                 ut_asserteq(0, addr & (align - 1));
346         }
347
348         /* Check alignment with an bloblist starting on a smaller alignment */
349         hdr = map_sysmem(TEST_ADDR + BLOBLIST_BLOB_ALIGN, TEST_BLOBLIST_SIZE);
350         memset(hdr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
351         memset(hdr, '\0', sizeof(*hdr));
352         ut_assertok(bloblist_new(TEST_ADDR + BLOBLIST_ALIGN, TEST_BLOBLIST_SIZE,
353                                  0, 0));
354
355         data = bloblist_add(1, 5, BLOBLIST_ALIGN_LOG2 + 1);
356         ut_assertnonnull(data);
357         addr = map_to_sysmem(data);
358         ut_asserteq(0, addr & (BLOBLIST_BLOB_ALIGN * 2 - 1));
359
360         return 0;
361 }
362 BLOBLIST_TEST(bloblist_test_align, UFT_BLOBLIST);
363
364 /* Test relocation of a bloblist */
365 static int bloblist_test_reloc(struct unit_test_state *uts)
366 {
367         const uint large_size = TEST_BLOBLIST_SIZE;
368         const uint small_size = 0x20;
369         void *new_ptr;
370         void *blob1, *blob2;
371         ulong new_addr;
372         ulong new_size;
373
374         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
375
376         /* Add one blob and then one that won't fit */
377         blob1 = bloblist_add(TEST_TAG, small_size, 0);
378         ut_assertnonnull(blob1);
379         blob2 = bloblist_add(TEST_TAG2, large_size, 0);
380         ut_assertnull(blob2);
381
382         /* Relocate the bloblist somewhere else, a bit larger */
383         new_addr = TEST_ADDR + TEST_BLOBLIST_SIZE;
384         new_size = TEST_BLOBLIST_SIZE + 0x100;
385         new_ptr = map_sysmem(new_addr, TEST_BLOBLIST_SIZE);
386         ut_assertok(bloblist_reloc(new_ptr, new_size));
387
388         /* Check the old blob is there and that we can now add the bigger one */
389         ut_assertnonnull(bloblist_find(TEST_TAG, small_size));
390         ut_assertnull(bloblist_find(TEST_TAG2, small_size));
391         blob2 = bloblist_add(TEST_TAG2, large_size, 0);
392         ut_assertnonnull(blob2);
393
394         return 0;
395 }
396 BLOBLIST_TEST(bloblist_test_reloc, UFT_BLOBLIST);
397
398 /* Test expansion of a blob */
399 static int bloblist_test_grow(struct unit_test_state *uts)
400 {
401         const uint small_size = 0x20;
402         void *blob1, *blob2, *blob1_new;
403         struct bloblist_hdr *hdr;
404         void *ptr;
405
406         ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
407         hdr = ptr;
408         memset(hdr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
409
410         /* Create two blobs */
411         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
412         blob1 = bloblist_add(TEST_TAG, small_size, 0);
413         ut_assertnonnull(blob1);
414         ut_assertok(check_zero(blob1, small_size));
415         strcpy(blob1, test1_str);
416
417         blob2 = bloblist_add(TEST_TAG2, small_size, 0);
418         ut_assertnonnull(blob2);
419         strcpy(blob2, test2_str);
420
421         ut_asserteq(sizeof(struct bloblist_hdr) +
422                     sizeof(struct bloblist_rec) * 2 + small_size * 2,
423                     hdr->used_size);
424
425         /* Resize the first one */
426         ut_assertok(bloblist_resize(TEST_TAG, small_size + 4));
427
428         /* The first one should not have moved, just got larger */
429         blob1_new = bloblist_find(TEST_TAG, small_size + 4);
430         ut_asserteq_ptr(blob1, blob1_new);
431
432         /* The new space should be zeroed */
433         ut_assertok(check_zero(blob1 + small_size, 4));
434
435         /* The second one should have moved */
436         blob2 = bloblist_find(TEST_TAG2, small_size);
437         ut_assertnonnull(blob2);
438         ut_asserteq_str(test2_str, blob2);
439
440         /* The header should have more bytes in use */
441         hdr = ptr;
442         ut_asserteq(sizeof(struct bloblist_hdr) +
443                     sizeof(struct bloblist_rec) * 2 + small_size * 2 +
444                     BLOBLIST_BLOB_ALIGN,
445                     hdr->used_size);
446
447         return 0;
448 }
449 BLOBLIST_TEST(bloblist_test_grow, UFT_BLOBLIST);
450
451 /* Test shrinking of a blob */
452 static int bloblist_test_shrink(struct unit_test_state *uts)
453 {
454         const uint small_size = 0x20;
455         void *blob1, *blob2, *blob1_new;
456         struct bloblist_hdr *hdr;
457         int new_size;
458         void *ptr;
459
460         ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
461
462         /* Create two blobs */
463         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
464         blob1 = bloblist_add(TEST_TAG, small_size, 0);
465         ut_assertnonnull(blob1);
466         strcpy(blob1, test1_str);
467
468         blob2 = bloblist_add(TEST_TAG2, small_size, 0);
469         ut_assertnonnull(blob2);
470         strcpy(blob2, test2_str);
471
472         hdr = ptr;
473         ut_asserteq(sizeof(struct bloblist_hdr) +
474                     sizeof(struct bloblist_rec) * 2 + small_size * 2,
475                     hdr->used_size);
476
477         /* Resize the first one */
478         new_size = small_size - BLOBLIST_ALIGN - 4;
479         ut_assertok(bloblist_resize(TEST_TAG, new_size));
480
481         /* The first one should not have moved, just got smaller */
482         blob1_new = bloblist_find(TEST_TAG, new_size);
483         ut_asserteq_ptr(blob1, blob1_new);
484
485         /* The second one should have moved */
486         blob2 = bloblist_find(TEST_TAG2, small_size);
487         ut_assertnonnull(blob2);
488         ut_asserteq_str(test2_str, blob2);
489
490         /* The header should have fewer bytes in use */
491         hdr = ptr;
492         ut_asserteq(sizeof(struct bloblist_hdr) +
493                     sizeof(struct bloblist_rec) * 2 + small_size * 2 -
494                     BLOBLIST_ALIGN,
495                     hdr->used_size);
496
497         return 0;
498 }
499 BLOBLIST_TEST(bloblist_test_shrink, UFT_BLOBLIST);
500
501 /* Test failing to adjust a blob size */
502 static int bloblist_test_resize_fail(struct unit_test_state *uts)
503 {
504         const uint small_size = 0x20;
505         struct bloblist_hdr *hdr;
506         void *blob1, *blob2;
507         int new_size;
508         void *ptr;
509
510         ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
511
512         /* Create two blobs */
513         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
514         blob1 = bloblist_add(TEST_TAG, small_size, 0);
515         ut_assertnonnull(blob1);
516
517         blob2 = bloblist_add(TEST_TAG2, small_size, 0);
518         ut_assertnonnull(blob2);
519
520         hdr = ptr;
521         ut_asserteq(sizeof(struct bloblist_hdr) +
522                     sizeof(struct bloblist_rec) * 2 + small_size * 2,
523                     hdr->used_size);
524
525         /* Resize the first one, to check the boundary conditions */
526         ut_asserteq(-EINVAL, bloblist_resize(TEST_TAG, -1));
527
528         new_size = small_size + (hdr->total_size - hdr->used_size);
529         ut_asserteq(-ENOSPC, bloblist_resize(TEST_TAG, new_size + 1));
530         ut_assertok(bloblist_resize(TEST_TAG, new_size));
531
532         return 0;
533 }
534 BLOBLIST_TEST(bloblist_test_resize_fail, UFT_BLOBLIST);
535
536 /* Test expanding the last blob in a bloblist */
537 static int bloblist_test_resize_last(struct unit_test_state *uts)
538 {
539         const uint small_size = 0x20;
540         struct bloblist_hdr *hdr;
541         void *blob1, *blob2, *blob2_new;
542         int alloced_val;
543         void *ptr;
544
545         ptr = map_sysmem(TEST_ADDR, TEST_BLOBLIST_SIZE);
546         memset(ptr, ERASE_BYTE, TEST_BLOBLIST_SIZE);
547         hdr = ptr;
548
549         /* Create two blobs */
550         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
551         blob1 = bloblist_add(TEST_TAG, small_size, 0);
552         ut_assertnonnull(blob1);
553
554         blob2 = bloblist_add(TEST_TAG2, small_size, 0);
555         ut_assertnonnull(blob2);
556
557         /* Check the byte after the last blob */
558         alloced_val = sizeof(struct bloblist_hdr) +
559                     sizeof(struct bloblist_rec) * 2 + small_size * 2;
560         ut_asserteq(alloced_val, hdr->used_size);
561         ut_asserteq_ptr((void *)hdr + alloced_val, blob2 + small_size);
562         ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + hdr->used_size));
563
564         /* Resize the second one, checking nothing changes */
565         ut_asserteq(0, bloblist_resize(TEST_TAG2, small_size + 4));
566
567         blob2_new = bloblist_find(TEST_TAG2, small_size + 4);
568         ut_asserteq_ptr(blob2, blob2_new);
569
570         /*
571          * the new blob should encompass the byte we checked now, so it should
572          * be zeroed. This zeroing should affect only the four new bytes added
573          * to the blob.
574          */
575         ut_asserteq(0, *((u8 *)hdr + alloced_val));
576         ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + alloced_val + 4));
577
578         /* Check that the new top of the allocated blobs has not been touched */
579         alloced_val += BLOBLIST_BLOB_ALIGN;
580         ut_asserteq(alloced_val, hdr->used_size);
581         ut_asserteq((u8)ERASE_BYTE, *((u8 *)hdr + hdr->used_size));
582
583         return 0;
584 }
585 BLOBLIST_TEST(bloblist_test_resize_last, UFT_BLOBLIST);
586
587 /* Check a completely full bloblist */
588 static int bloblist_test_blob_maxsize(struct unit_test_state *uts)
589 {
590         void *ptr;
591         int size;
592
593         /* At the start there should be no records */
594         clear_bloblist();
595         ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0, 0));
596
597         /* Add a blob that takes up all space */
598         size = TEST_BLOBLIST_SIZE - sizeof(struct bloblist_hdr) -
599                 sizeof(struct bloblist_rec);
600         ptr = bloblist_add(TEST_TAG, size, 0);
601         ut_assertnonnull(ptr);
602
603         ptr = bloblist_add(TEST_TAG, size + 1, 0);
604         ut_assertnull(ptr);
605
606         return 0;
607 }
608 BLOBLIST_TEST(bloblist_test_blob_maxsize, UFT_BLOBLIST);
This page took 0.104704 seconds and 4 git commands to generate.