1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018, Google Inc. All rights reserved.
10 #include <test/suites.h>
11 #include <test/test.h>
14 DECLARE_GLOBAL_DATA_PTR;
16 /* Declare a new compression test */
17 #define BLOBLIST_TEST(_name, _flags) \
18 UNIT_TEST(_name, _flags, bloblist_test)
28 TEST_ADDR = CONFIG_BLOBLIST_ADDR,
29 TEST_BLOBLIST_SIZE = 0x100,
32 static struct bloblist_hdr *clear_bloblist(void)
34 struct bloblist_hdr *hdr;
36 /* Clear out any existing bloblist so we have a clean slate */
37 hdr = map_sysmem(CONFIG_BLOBLIST_ADDR, TEST_BLOBLIST_SIZE);
38 memset(hdr, '\0', TEST_BLOBLIST_SIZE);
43 static int bloblist_test_init(struct unit_test_state *uts)
45 struct bloblist_hdr *hdr;
47 hdr = clear_bloblist();
48 ut_asserteq(-ENOENT, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
49 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
51 ut_asserteq(-EPROTONOSUPPORT, bloblist_check(TEST_ADDR,
54 ut_asserteq(-ENOSPC, bloblist_new(TEST_ADDR, 0x10, 0));
55 ut_asserteq(-EFAULT, bloblist_new(1, TEST_BLOBLIST_SIZE, 0));
56 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
58 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
59 ut_assertok(bloblist_finish());
60 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
62 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
66 BLOBLIST_TEST(bloblist_test_init, 0);
68 static int bloblist_test_blob(struct unit_test_state *uts)
70 struct bloblist_hdr *hdr;
71 struct bloblist_rec *rec, *rec2;
74 /* At the start there should be no records */
75 hdr = clear_bloblist();
76 ut_assertnull(bloblist_find(TEST_TAG, TEST_BLOBLIST_SIZE));
77 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
79 /* Add a record and check that we can find it */
80 data = bloblist_add(TEST_TAG, TEST_SIZE);
81 rec = (void *)(hdr + 1);
82 ut_asserteq_ptr(rec + 1, data);
83 data = bloblist_find(TEST_TAG, TEST_SIZE);
84 ut_asserteq_ptr(rec + 1, data);
86 /* Check the 'ensure' method */
87 ut_asserteq_ptr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
88 ut_assertnull(bloblist_ensure(TEST_TAG, TEST_SIZE2));
89 rec2 = (struct bloblist_rec *)(data + ALIGN(TEST_SIZE, BLOBLIST_ALIGN));
91 /* Check for a non-existent record */
92 ut_asserteq_ptr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
93 ut_asserteq_ptr(rec2 + 1, bloblist_ensure(TEST_TAG2, TEST_SIZE2));
94 ut_assertnull(bloblist_find(TEST_TAG_MISSING, 0));
98 BLOBLIST_TEST(bloblist_test_blob, 0);
100 static int bloblist_test_bad_blob(struct unit_test_state *uts)
102 struct bloblist_hdr *hdr;
105 hdr = clear_bloblist();
106 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
108 data += sizeof(struct bloblist_rec);
109 ut_asserteq_ptr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
110 ut_asserteq_ptr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
114 BLOBLIST_TEST(bloblist_test_bad_blob, 0);
116 static int bloblist_test_checksum(struct unit_test_state *uts)
118 struct bloblist_hdr *hdr;
121 hdr = clear_bloblist();
122 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
123 ut_assertok(bloblist_finish());
124 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
127 * Now change things amd make sure that the checksum notices. We cannot
128 * change the size or alloced fields, since that will crash the code.
129 * It has to rely on these being correct.
132 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
136 ut_asserteq(-EFBIG, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
140 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
144 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
147 /* Make sure the checksum changes when we add blobs */
148 data = bloblist_add(TEST_TAG, TEST_SIZE);
149 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
151 data2 = bloblist_add(TEST_TAG2, TEST_SIZE2);
152 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
153 ut_assertok(bloblist_finish());
155 /* It should also change if we change the data */
156 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
158 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
161 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
163 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
167 * Changing data outside the range of valid data should not affect
170 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
173 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
178 BLOBLIST_TEST(bloblist_test_checksum, 0);
180 int do_ut_bloblist(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
182 struct unit_test *tests = ll_entry_start(struct unit_test,
184 const int n_ents = ll_entry_count(struct unit_test, bloblist_test);
186 return cmd_ut_category("bloblist", "bloblist_test_",
187 tests, n_ents, argc, argv);