Merge branch '2020-05-18-reduce-size-of-common.h'
[oweals/u-boot.git] / test / bloblist.c
index 89bdb012e351e1bf23ecc91c33a61a29c8751ef8..4e537ee1b9c422d73af98e1ab67cda8a887cc943 100644 (file)
@@ -24,6 +24,7 @@ enum {
 
        TEST_SIZE               = 10,
        TEST_SIZE2              = 20,
+       TEST_SIZE_LARGE         = 0xe0,
 
        TEST_ADDR               = CONFIG_BLOBLIST_ADDR,
        TEST_BLOBLIST_SIZE      = 0x100,
@@ -33,13 +34,31 @@ static struct bloblist_hdr *clear_bloblist(void)
 {
        struct bloblist_hdr *hdr;
 
-       /* Clear out any existing bloblist so we have a clean slate */
+       /*
+        * Clear out any existing bloblist so we have a clean slate. Zero the
+        * header so that existing records are removed, but set everything else
+        * to 0xff for testing purposes.
+        */
        hdr = map_sysmem(CONFIG_BLOBLIST_ADDR, TEST_BLOBLIST_SIZE);
-       memset(hdr, '\0', TEST_BLOBLIST_SIZE);
+       memset(hdr, '\xff', TEST_BLOBLIST_SIZE);
+       memset(hdr, '\0', sizeof(*hdr));
 
        return hdr;
 }
 
+static int check_zero(void *data, int size)
+{
+       u8 *ptr;
+       int i;
+
+       for (ptr = data, i = 0; i < size; i++, ptr++) {
+               if (*ptr)
+                       return -EINVAL;
+       }
+
+       return 0;
+}
+
 static int bloblist_test_init(struct unit_test_state *uts)
 {
        struct bloblist_hdr *hdr;
@@ -83,10 +102,14 @@ static int bloblist_test_blob(struct unit_test_state *uts)
        data = bloblist_find(TEST_TAG, TEST_SIZE);
        ut_asserteq_ptr(rec + 1, data);
 
+       /* Check the data is zeroed */
+       ut_assertok(check_zero(data, TEST_SIZE));
+
        /* Check the 'ensure' method */
        ut_asserteq_ptr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
        ut_assertnull(bloblist_ensure(TEST_TAG, TEST_SIZE2));
        rec2 = (struct bloblist_rec *)(data + ALIGN(TEST_SIZE, BLOBLIST_ALIGN));
+       ut_assertok(check_zero(data, TEST_SIZE));
 
        /* Check for a non-existent record */
        ut_asserteq_ptr(data, bloblist_ensure(TEST_TAG, TEST_SIZE));
@@ -97,6 +120,40 @@ static int bloblist_test_blob(struct unit_test_state *uts)
 }
 BLOBLIST_TEST(bloblist_test_blob, 0);
 
+/* Check bloblist_ensure_size_ret() */
+static int bloblist_test_blob_ensure(struct unit_test_state *uts)
+{
+       void *data, *data2;
+       int size;
+
+       /* At the start there should be no records */
+       clear_bloblist();
+       ut_assertok(bloblist_new(TEST_ADDR, TEST_BLOBLIST_SIZE, 0));
+
+       /* Test with an empty bloblist */
+       size = TEST_SIZE;
+       ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data));
+       ut_asserteq(TEST_SIZE, size);
+       ut_assertok(check_zero(data, TEST_SIZE));
+
+       /* Check that we get the same thing again */
+       ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data2));
+       ut_asserteq(TEST_SIZE, size);
+       ut_asserteq_ptr(data, data2);
+
+       /* Check that the size remains the same */
+       size = TEST_SIZE2;
+       ut_assertok(bloblist_ensure_size_ret(TEST_TAG, &size, &data));
+       ut_asserteq(TEST_SIZE, size);
+
+       /* Check running out of space */
+       size = TEST_SIZE_LARGE;
+       ut_asserteq(-ENOSPC, bloblist_ensure_size_ret(TEST_TAG2, &size, &data));
+
+       return 0;
+}
+BLOBLIST_TEST(bloblist_test_blob_ensure, 0);
+
 static int bloblist_test_bad_blob(struct unit_test_state *uts)
 {
        struct bloblist_hdr *hdr;
@@ -177,11 +234,13 @@ static int bloblist_test_checksum(struct unit_test_state *uts)
 
 BLOBLIST_TEST(bloblist_test_checksum, 0);
 
-int do_ut_bloblist(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
+int do_ut_bloblist(struct cmd_tbl *cmdtp, int flag, int argc,
+                  char *const argv[])
 {
        struct unit_test *tests = ll_entry_start(struct unit_test,
                                                 bloblist_test);
        const int n_ents = ll_entry_count(struct unit_test, bloblist_test);
 
-       return cmd_ut_category("bloblist", tests, n_ents, argc, argv);
+       return cmd_ut_category("bloblist", "bloblist_test_",
+                              tests, n_ents, argc, argv);
 }