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)
27 TEST_SIZE_LARGE = 0xe0,
29 TEST_ADDR = CONFIG_BLOBLIST_ADDR,
30 TEST_BLOBLIST_SIZE = 0x100,
33 static struct bloblist_hdr *clear_bloblist(void)
35 struct bloblist_hdr *hdr;
38 * Clear out any existing bloblist so we have a clean slate. Zero the
39 * header so that existing records are removed, but set everything else
40 * to 0xff for testing purposes.
42 hdr = map_sysmem(CONFIG_BLOBLIST_ADDR, TEST_BLOBLIST_SIZE);
43 memset(hdr, '\xff', TEST_BLOBLIST_SIZE);
44 memset(hdr, '\0', sizeof(*hdr));
49 static int check_zero(void *data, int size)
54 for (ptr = data, i = 0; i < size; i++, ptr++) {
62 static int bloblist_test_init(struct unit_test_state *uts)
64 struct bloblist_hdr *hdr;
66 hdr = clear_bloblist();
67 ut_asserteq(-ENOENT, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
68 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
70 ut_asserteq(-EPROTONOSUPPORT, bloblist_check(TEST_ADDR,
73 ut_asserteq(-ENOSPC, bloblist_new(TEST_ADDR, 0x10, 0));
74 ut_asserteq(-EFAULT, bloblist_new(1, TEST_BLOBLIST_SIZE, 0));
75 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
77 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
78 ut_assertok(bloblist_finish());
79 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
81 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
85 BLOBLIST_TEST(bloblist_test_init, 0);
87 static int bloblist_test_blob(struct unit_test_state *uts)
89 struct bloblist_hdr *hdr;
90 struct bloblist_rec *rec, *rec2;
93 /* At the start there should be no records */
94 hdr = clear_bloblist();
95 ut_assertnull(bloblist_find(TEST_TAG, TEST_BLOBLIST_SIZE));
96 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
98 /* Add a record and check that we can find it */
99 data = bloblist_add(TEST_TAG, TEST_SIZE);
100 rec = (void *)(hdr + 1);
101 ut_asserteq_ptr(rec + 1, data);
102 data = bloblist_find(TEST_TAG, TEST_SIZE);
103 ut_asserteq_ptr(rec + 1, data);
105 /* Check the data is zeroed */
106 ut_assertok(check_zero(data, TEST_SIZE));
108 /* Check the 'ensure' method */
109 ut_asserteq_ptr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
110 ut_assertnull(bloblist_ensure(TEST_TAG, TEST_SIZE2));
111 rec2 = (struct bloblist_rec *)(data + ALIGN(TEST_SIZE, BLOBLIST_ALIGN));
112 ut_assertok(check_zero(data, TEST_SIZE));
114 /* Check for a non-existent record */
115 ut_asserteq_ptr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
116 ut_asserteq_ptr(rec2 + 1, bloblist_ensure(TEST_TAG2, TEST_SIZE2));
117 ut_assertnull(bloblist_find(TEST_TAG_MISSING, 0));
121 BLOBLIST_TEST(bloblist_test_blob, 0);
123 /* Check bloblist_ensure_size_ret() */
124 static int bloblist_test_blob_ensure(struct unit_test_state *uts)
129 /* At the start there should be no records */
131 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
133 /* Test with an empty bloblist */
135 ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data));
136 ut_asserteq(TEST_SIZE, size);
137 ut_assertok(check_zero(data, TEST_SIZE));
139 /* Check that we get the same thing again */
140 ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data2));
141 ut_asserteq(TEST_SIZE, size);
142 ut_asserteq_ptr(data, data2);
144 /* Check that the size remains the same */
146 ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data));
147 ut_asserteq(TEST_SIZE, size);
149 /* Check running out of space */
150 size = TEST_SIZE_LARGE;
151 ut_asserteq(-ENOSPC, bloblist_ensure_size_ret(TEST_TAG2, &size, &data));
155 BLOBLIST_TEST(bloblist_test_blob_ensure, 0);
157 static int bloblist_test_bad_blob(struct unit_test_state *uts)
159 struct bloblist_hdr *hdr;
162 hdr = clear_bloblist();
163 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
165 data += sizeof(struct bloblist_rec);
166 ut_asserteq_ptr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
167 ut_asserteq_ptr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
171 BLOBLIST_TEST(bloblist_test_bad_blob, 0);
173 static int bloblist_test_checksum(struct unit_test_state *uts)
175 struct bloblist_hdr *hdr;
178 hdr = clear_bloblist();
179 ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
180 ut_assertok(bloblist_finish());
181 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
184 * Now change things amd make sure that the checksum notices. We cannot
185 * change the size or alloced fields, since that will crash the code.
186 * It has to rely on these being correct.
189 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
193 ut_asserteq(-EFBIG, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
197 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
201 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
204 /* Make sure the checksum changes when we add blobs */
205 data = bloblist_add(TEST_TAG, TEST_SIZE);
206 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
208 data2 = bloblist_add(TEST_TAG2, TEST_SIZE2);
209 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
210 ut_assertok(bloblist_finish());
212 /* It should also change if we change the data */
213 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
215 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
218 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
220 ut_asserteq(-EIO, bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
224 * Changing data outside the range of valid data should not affect
227 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
230 ut_assertok(bloblist_check(TEST_ADDR, TEST_BLOBLIST_SIZE));
235 BLOBLIST_TEST(bloblist_test_checksum, 0);
237 int do_ut_bloblist(struct cmd_tbl *cmdtp, int flag, int argc,
240 struct unit_test *tests = ll_entry_start(struct unit_test,
242 const int n_ents = ll_entry_count(struct unit_test, bloblist_test);
244 return cmd_ut_category("bloblist", "bloblist_test_",
245 tests, n_ents, argc, argv);