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_last_block.c

292 lines
7.9 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>
uint32_t find_last_block(struct ftl_map *map, uint32_t first);
static void test_middle(void **state)
{
struct ftl_map map;
struct ftl_page_group group;
uint32_t ret;
(void)state;
memcpy(group.magic, "FTL", 3);
group.epoch = 0;
map.nblocks = 64;
map.epoch = 0;
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 31);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 31);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 32);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 0);
will_return(__wrap_find_block, -1);
ret = find_last_block(&map, 0);
assert_int_equal(ret, 31);
}
static void test_first(void **state)
{
struct ftl_map map;
struct ftl_page_group group;
uint32_t ret;
(void)state;
memcpy(group.magic, "FTL", 3);
group.epoch = 0;
map.nblocks = 64;
map.epoch = 0;
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 31);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 0);
will_return(__wrap_find_block, -1);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 15);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 0);
will_return(__wrap_find_block, -1);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 7);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 0);
will_return(__wrap_find_block, -1);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 3);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 0);
will_return(__wrap_find_block, -1);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 1);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 0);
will_return(__wrap_find_block, -1);
ret = find_last_block(&map, 0);
assert_int_equal(ret, 0);
}
static void test_last(void **state)
{
struct ftl_map map;
struct ftl_page_group group;
uint32_t ret;
(void)state;
memcpy(group.magic, "FTL", 3);
group.epoch = 0;
map.nblocks = 64;
map.epoch = 0;
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 31);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 31);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 32);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 32);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 47);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 47);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 48);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 48);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 55);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 55);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 56);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 56);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 59);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 59);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 60);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 60);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 61);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 61);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 62);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 62);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 62);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 62);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 63);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 63);
will_return(__wrap_find_block, 0);
ret = find_last_block(&map, 0);
assert_int_equal(ret, 63);
}
static void test_left_right(void **state)
{
struct ftl_map map;
struct ftl_page_group group;
uint32_t ret;
(void)state;
memcpy(group.magic, "FTL", 3);
group.epoch = 0;
map.nblocks = 64;
map.epoch = 0;
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 31);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 31);
will_return(__wrap_find_block, -1);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 15);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 15);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 16);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 16);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 23);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 23);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 24);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 24);
will_return(__wrap_find_block, -1);
ret = find_last_block(&map, 0);
assert_int_equal(ret, 23);
}
static void test_right_left(void **state)
{
struct ftl_map map;
struct ftl_page_group group;
uint32_t ret;
(void)state;
memcpy(group.magic, "FTL", 3);
group.epoch = 0;
map.nblocks = 64;
map.epoch = 0;
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 31);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 31);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 32);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 32);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 47);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 47);
will_return(__wrap_find_block, -1);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 39);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 39);
will_return(__wrap_find_block, 0);
expect_value(__wrap_find_block, map, &map);
expect_value(__wrap_find_block, block, 40);
will_return(__wrap_find_block, &group);
will_return(__wrap_find_block, 40);
will_return(__wrap_find_block, -1);
ret = find_last_block(&map, 0);
assert_int_equal(ret, 39);
}
int test_find_last_block(void)
{
const struct CMUnitTest tests[] = {
{ "find_last_block: middle", test_middle, NULL, NULL, NULL },
{ "find_last_block: first", test_first, NULL, NULL, NULL },
{ "find_last_block: last", test_last, NULL, NULL, NULL },
{ "find_last_block: left right", test_left_right, NULL, NULL, NULL },
{ "find_last_block: right left", test_right_left, NULL, NULL, NULL },
};
return cmocka_run_group_tests_name("find_last_block", tests, NULL, NULL);
}