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/write_upage.c

131 lines
3.0 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 write_upage(struct ftl_map *map, const void *page,
struct ftl_page_desc *page_desc);
static void test_prepare_head(void **state)
{
struct ftl_map map;
struct ftl_page_desc page_desc;
int ret;
(void)state;
map.log2_page_size = ilog2(4 * KIB);
map.head = 0;
will_return(__wrap_prepare_head, -1);
ret = write_upage(&map, NULL, &page_desc);
assert_int_equal(ret, -1);
}
static void test_null_page(void **state)
{
struct ftl_map map;
struct ftl_page_desc page_desc;
int ret;
(void)state;
map.log2_page_size = ilog2(4 * KIB);
map.head = 0;
will_return(__wrap_prepare_head, 0);
expect_value(__wrap_write_page_desc, map, &map);
expect_value(__wrap_write_page_desc, page_desc, &page_desc);
will_return(__wrap_write_page_desc, 0);
ret = write_upage(&map, NULL, &page_desc);
assert_int_equal(ret, 0);
}
static void test_page0(void **state)
{
struct ftl_map map;
char page[4 * KIB], ret_page[4 * KIB];
struct ftl_page_desc page_desc;
int ret;
(void)state;
memset(page, 0x5a, sizeof page);
memset(ret_page, 0, sizeof ret_page);
map.log2_page_size = ilog2(4 * KIB);
map.head = 0;
will_return(__wrap_prepare_head, 0);
expect_value(__wrap_flash_write, addr, 0);
expect_value(__wrap_flash_write, len, 4 * KIB);
will_return(__wrap_flash_write, 4 * KIB);
will_return(__wrap_flash_write, &ret_page);
expect_value(__wrap_write_page_desc, map, &map);
expect_value(__wrap_write_page_desc, page_desc, &page_desc);
will_return(__wrap_write_page_desc, 0);
ret = write_upage(&map, &page, &page_desc);
assert_int_equal(ret, 0);
assert_memory_equal(page, ret_page, sizeof page);
}
static void test_page42(void **state)
{
struct ftl_map map;
char page[4 * KIB], ret_page[4 * KIB];
struct ftl_page_desc page_desc;
int ret;
(void)state;
memset(page, 0x5a, sizeof page);
memset(ret_page, 0, sizeof ret_page);
map.log2_page_size = ilog2(4 * KIB);
map.head = 42;
will_return(__wrap_prepare_head, 0);
expect_value(__wrap_flash_write, addr, 42 * 4 * KIB);
expect_value(__wrap_flash_write, len, 4 * KIB);
will_return(__wrap_flash_write, 4 * KIB);
will_return(__wrap_flash_write, &ret_page);
expect_value(__wrap_write_page_desc, map, &map);
expect_value(__wrap_write_page_desc, page_desc, &page_desc);
will_return(__wrap_write_page_desc, 0);
ret = write_upage(&map, &page, &page_desc);
assert_int_equal(ret, 0);
assert_memory_equal(page, ret_page, sizeof page);
}
int test_write_upage(void)
{
const struct CMUnitTest tests[] = {
{ "write_upage: prepare_head() = -1", test_prepare_head, NULL, NULL, NULL },
{ "write_upage: null page", test_null_page, NULL, NULL, NULL },
{ "write_upage: head=0", test_page0, NULL, NULL, NULL },
{ "write_upage: head=42", test_page42, NULL, NULL, NULL },
};
return cmocka_run_group_tests_name("write_upage", tests, NULL, NULL);
}