Source code for the Trusted Boot Module.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tbm-mcu/source/tests/ftl/find_block.c

171 lines
4.7 KiB

#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include <cmocka.h>
#include <bitops.h>
#include <flash.h>
#include <ftl.h>
#include <macros.h>
int __real_find_block(struct ftl_map *map, struct ftl_page_group *group,
uint32_t *where, uint32_t block);
static void test_block0(void **state)
{
struct ftl_map map;
struct ftl_page_group group, ret_group;
int ret;
uint32_t where;
(void)state;
memcpy(group.magic, "FTL", 3);
map.log2_block_size = ilog2(64 * KIB);
map.log2_page_size = ilog2(1 * KIB);
map.log2_pages_per_group = ilog2(8);
map.nblocks = 64;
expect_value(__wrap_flash_read, addr, (8 - 1) * 1 * KIB);
expect_value(__wrap_flash_read, len, sizeof group);
will_return(__wrap_flash_read, sizeof group);
will_return(__wrap_flash_read, &group);
ret = __real_find_block(&map, &ret_group, &where, 0);
assert_int_equal(ret, 0);
assert_int_equal(where, 0);
}
static void test_block3(void **state)
{
struct ftl_map map;
struct ftl_page_group group, ret_group;
int ret;
uint32_t where;
(void)state;
memcpy(group.magic, "FTL", 3);
map.log2_block_size = ilog2(64 * KIB);
map.log2_page_size = ilog2(1 * KIB);
map.log2_pages_per_group = ilog2(8);
map.nblocks = 64;
expect_value(__wrap_flash_read, addr, (8 - 1) * 1 * KIB);
expect_value(__wrap_flash_read, len, sizeof group);
will_return(__wrap_flash_read, 0);
will_return(__wrap_flash_read, NULL);
expect_value(__wrap_flash_read, addr, (1 * 64 + 8 - 1) * 1 * KIB);
expect_value(__wrap_flash_read, len, sizeof group);
will_return(__wrap_flash_read, 0);
will_return(__wrap_flash_read, NULL);
expect_value(__wrap_flash_read, addr, (2 * 64 + 8 - 1) * 1 * KIB);
expect_value(__wrap_flash_read, len, sizeof group);
will_return(__wrap_flash_read, 0);
will_return(__wrap_flash_read, NULL);
expect_value(__wrap_flash_read, addr, (3 * 64 + 8 - 1) * 1 * KIB);
expect_value(__wrap_flash_read, len, sizeof group);
will_return(__wrap_flash_read, sizeof group);
will_return(__wrap_flash_read, &group);
ret = __real_find_block(&map, &ret_group, &where, 0);
assert_int_equal(ret, 0);
assert_int_equal(where, 3);
}
static void test_block8_offset5(void **state)
{
struct ftl_map map;
struct ftl_page_group group, ret_group;
int ret;
uint32_t where;
(void)state;
memcpy(group.magic, "FTL", 3);
map.log2_block_size = ilog2(64 * KIB);
map.log2_page_size = ilog2(1 * KIB);
map.log2_pages_per_group = ilog2(8);
map.nblocks = 64;
expect_value(__wrap_flash_read, addr, (5 * 64 + 8 - 1) * 1 * KIB);
expect_value(__wrap_flash_read, len, sizeof group);
will_return(__wrap_flash_read, 0);
will_return(__wrap_flash_read, NULL);
expect_value(__wrap_flash_read, addr, (6 * 64 + 8 - 1) * 1 * KIB);
expect_value(__wrap_flash_read, len, sizeof group);
will_return(__wrap_flash_read, 0);
will_return(__wrap_flash_read, NULL);
expect_value(__wrap_flash_read, addr, (7 * 64 + 8 - 1) * 1 * KIB);
expect_value(__wrap_flash_read, len, sizeof group);
will_return(__wrap_flash_read, 0);
will_return(__wrap_flash_read, NULL);
expect_value(__wrap_flash_read, addr, (8 * 64 + 8 - 1) * 1 * KIB);
expect_value(__wrap_flash_read, len, sizeof group);
will_return(__wrap_flash_read, sizeof group);
will_return(__wrap_flash_read, &group);
ret = __real_find_block(&map, &ret_group, &where, 5);
assert_int_equal(ret, 0);
assert_int_equal(where, 8);
}
static void test_no_block(void **state)
{
struct ftl_map map;
struct ftl_page_group group, ret_group;
int ret;
uint32_t where;
(void)state;
memcpy(group.magic, "FTL", 3);
map.log2_block_size = ilog2(64 * KIB);
map.log2_page_size = ilog2(1 * KIB);
map.log2_pages_per_group = ilog2(8);
map.nblocks = 64;
expect_value(__wrap_flash_read, addr, (61 * 64 + 8 - 1) * 1 * KIB);
expect_value(__wrap_flash_read, len, sizeof group);
will_return(__wrap_flash_read, 0);
will_return(__wrap_flash_read, NULL);
expect_value(__wrap_flash_read, addr, (62 * 64 + 8 - 1) * 1 * KIB);
expect_value(__wrap_flash_read, len, sizeof group);
will_return(__wrap_flash_read, 0);
will_return(__wrap_flash_read, NULL);
expect_value(__wrap_flash_read, addr, (63 * 64 + 8 - 1) * 1 * KIB);
expect_value(__wrap_flash_read, len, sizeof group);
will_return(__wrap_flash_read, 0);
will_return(__wrap_flash_read, NULL);
ret = __real_find_block(&map, &ret_group, &where, 61);
assert_int_equal(ret, -1);
}
int test_find_block(void)
{
const struct CMUnitTest tests[] = {
{ "find_block: block=0, offset=0", test_block0, NULL, NULL, NULL },
{ "find_block: block=3, offset=0", test_block3, NULL, NULL, NULL },
{ "find_block: block=8, offset=5", test_block8_offset5, NULL, NULL, NULL },
{ "find_block: no block, offset=61", test_no_block, NULL, NULL, NULL },
};
return cmocka_run_group_tests_name("find_block", tests, NULL, NULL);
}