[libubootenv,v2,8/8] tests: add unit tests for the FWU metadata format

Message ID 20261113171737.470894-9-dario.binacchi@amarulasolutions.com
State New
Headers
Series Add the FWU metadata storage format |

Commit Message

Dario Binacchi Nov. 13, 2026, 5:17 p.m. UTC
Create version 1 and version 2 metadata and check:

- loading
- bank switching
- bank acceptance
- copies with a wrong CRC
- copies with a valid CRC but invalid content
- rejected values
- rejected variable addition/removal

The layout of the structures is checked against the specification at
build time.

Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
---
Changes in v2:
- New patch

 tests/CMakeLists.txt   |   8 +
 tests/test_fwu_mdata.c | 637 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 645 insertions(+)
 create mode 100644 tests/test_fwu_mdata.c

-- 
2.43.0
  

Patch

diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 67626a492436..93fc88f2646a 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -2,6 +2,10 @@ 
 #
 # SPDX-License-Identifier:     LGPL-2.1-or-later
 
+if(NO_YML_SUPPORT)
+  message(FATAL_ERROR "The tests need the YAML support")
+endif(NO_YML_SUPPORT)
+
 find_path(CMOCKA_INCLUDE_DIR cmocka.h)
 find_library(CMOCKA_LIBRARY cmocka)
 if(NOT CMOCKA_INCLUDE_DIR OR NOT CMOCKA_LIBRARY)
@@ -9,3 +13,7 @@  if(NOT CMOCKA_INCLUDE_DIR OR NOT CMOCKA_LIBRARY)
 endif()
 
 include_directories(${CMOCKA_INCLUDE_DIR})
+
+add_executable(test_fwu_mdata test_fwu_mdata.c)
+target_link_libraries(test_fwu_mdata ubootenv z ${CMOCKA_LIBRARY})
+add_test(NAME fwu_mdata COMMAND test_fwu_mdata)
diff --git a/tests/test_fwu_mdata.c b/tests/test_fwu_mdata.c
new file mode 100644
index 000000000000..bedaf47c20b6
--- /dev/null
+++ b/tests/test_fwu_mdata.c
@@ -0,0 +1,637 @@ 
+/*
+ * (C) Copyright 2026
+ * Amarula Solutions, Dario Binacchi <dario.binacchi@amarulasolutions.com>
+ *
+ * SPDX-License-Identifier:     LGPL-2.1-or-later
+ */
+
+#include <endian.h>
+#include <errno.h>
+#include <setjmp.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <cmocka.h>
+#include <zlib.h>
+
+#include "libuboot.h"
+#include "fwu_mdata.h"
+
+#define STORAGE_SIZE 0x1000
+#define NUM_BANKS 2
+#define NUM_IMAGES 3
+#define PATH_LEN 128
+
+/* Layout fixed by the specification */
+_Static_assert(sizeof(struct fwu_mdata) == 16, "header");
+_Static_assert(sizeof(struct fwu_mdata_ext) == 16, "header extension");
+_Static_assert(sizeof(struct fwu_mdata) + sizeof(struct fwu_mdata_ext) ==
+	       FWU_MDATA_DESC_OFFSET, "descriptor offset");
+_Static_assert(sizeof(struct fwu_mdata_fw_store_desc) == 8, "descriptor");
+_Static_assert(sizeof(struct fwu_mdata_image_entry) == 32, "image entry");
+_Static_assert(sizeof(struct fwu_mdata_image_bank_info) == 24, "bank info");
+_Static_assert(offsetof(struct fwu_mdata_ext, bank_state) == 8, "bank_state");
+
+#define IMG_ENTRY_SIZE (sizeof(struct fwu_mdata_image_entry) + \
+			NUM_BANKS * sizeof(struct fwu_mdata_image_bank_info))
+
+struct fwu_mdata_config {
+	char path[PATH_LEN];		/* the configuration file */
+	char lockfile[PATH_LEN];
+	char devices[2][PATH_LEN];	/* the two copies */
+};
+
+struct fwu_mdata_fixture {
+	unsigned int version;
+	char dir[64];
+	struct fwu_mdata_config config;
+	char script[PATH_LEN];
+	uint8_t mdata[STORAGE_SIZE];
+	struct uboot_ctx *ctx;
+};
+
+static struct fwu_mdata *to_hdr(uint8_t *buf)
+{
+	return (struct fwu_mdata *)buf;
+}
+
+static struct fwu_mdata_ext *to_ext(uint8_t *buf)
+{
+	return (struct fwu_mdata_ext *)(buf + sizeof(struct fwu_mdata));
+}
+
+static struct fwu_mdata_fw_store_desc *to_desc(uint8_t *buf)
+{
+	return (struct fwu_mdata_fw_store_desc *)(buf + FWU_MDATA_DESC_OFFSET);
+}
+
+static size_t images_offset(unsigned int version)
+{
+	if (version == 1)
+		return sizeof(struct fwu_mdata);
+
+	return FWU_MDATA_DESC_OFFSET + sizeof(struct fwu_mdata_fw_store_desc);
+}
+
+static size_t mdata_size(unsigned int version)
+{
+	return images_offset(version) + NUM_IMAGES * IMG_ENTRY_SIZE;
+}
+
+static struct fwu_mdata_image_entry *to_image(unsigned int version,
+					      uint8_t *buf, int image)
+{
+	size_t offset = images_offset(version) + image * IMG_ENTRY_SIZE;
+
+	return (struct fwu_mdata_image_entry *)(buf + offset);
+}
+
+static struct fwu_mdata_image_bank_info *
+to_bank_info(unsigned int version, uint8_t *buf, int image, int bank)
+{
+	struct fwu_mdata_image_entry *img = to_image(version, buf, image);
+
+	return &img->img_bank_info[bank];
+}
+
+static uint32_t mdata_crc(uint8_t *buf, size_t size)
+{
+	return crc32(0, buf + sizeof(uint32_t), size - sizeof(uint32_t));
+}
+
+static void mdata_update_crc(uint8_t *buf, size_t size)
+{
+	struct fwu_mdata *hdr = to_hdr(buf);
+
+	hdr->crc32 = htole32(mdata_crc(buf, size));
+}
+
+/* Active bank 0, previous bank 1, both banks accepted */
+static void mdata_build(uint8_t *buf, unsigned int version)
+{
+	struct fwu_mdata *hdr = to_hdr(buf);
+	size_t size = mdata_size(version);
+	int i, b;
+
+	memset(buf, 0, STORAGE_SIZE);
+
+	hdr->version = htole32(version);
+	hdr->active_index = htole32(0);
+	hdr->previous_active_index = htole32(1);
+
+	if (version == 2) {
+		struct fwu_mdata_ext *ext = to_ext(buf);
+		struct fwu_mdata_fw_store_desc *desc = to_desc(buf);
+
+		ext->metadata_size = htole32(size);
+		ext->desc_offset = htole16(FWU_MDATA_DESC_OFFSET);
+		memset(ext->bank_state, FWU_MDATA_BANK_INVALID,
+		       sizeof(ext->bank_state));
+		memset(ext->bank_state, FWU_MDATA_BANK_ACCEPTED, NUM_BANKS);
+		desc->num_banks = NUM_BANKS;
+		desc->num_images = htole16(NUM_IMAGES);
+		desc->img_entry_size = htole16(IMG_ENTRY_SIZE);
+		desc->bank_info_entry_size =
+			htole16(sizeof(struct fwu_mdata_image_bank_info));
+	}
+
+	/*
+	 * A different, non-zero pattern for each GUID, to check they are
+	 * preserved: the high nibble is the field, the low one the entry
+	 */
+	for (i = 0; i < NUM_IMAGES; i++) {
+		struct fwu_mdata_image_entry *img;
+
+		img = to_image(version, buf, i);
+		memset(&img->image_type_guid, 0x10 + i,
+		       sizeof(img->image_type_guid));
+		memset(&img->location_guid, 0x20 + i,
+		       sizeof(img->location_guid));
+		for (b = 0; b < NUM_BANKS; b++) {
+			struct fwu_mdata_image_bank_info *bank;
+
+			bank = to_bank_info(version, buf, i, b);
+			memset(&bank->image_guid, 0x30 + i * NUM_BANKS + b,
+			       sizeof(bank->image_guid));
+			bank->accepted = htole32(FWU_MDATA_IMAGE_ACCEPTED);
+		}
+	}
+
+	mdata_update_crc(buf, size);
+}
+
+static void write_file(const char *path, const void *buf, size_t size)
+{
+	FILE *fp = fopen(path, "w");
+
+	assert_non_null(fp);
+	assert_int_equal(fwrite(buf, 1, size, fp), size);
+	assert_int_equal(fclose(fp), 0);
+}
+
+static void write_copy(const char *path, const uint8_t *buf)
+{
+	write_file(path, buf, STORAGE_SIZE);
+}
+
+static void read_copy(const char *path, uint8_t *buf)
+{
+	FILE *fp = fopen(path, "r");
+
+	assert_non_null(fp);
+	assert_int_equal(fread(buf, 1, STORAGE_SIZE, fp), STORAGE_SIZE);
+	assert_int_equal(fclose(fp), 0);
+}
+
+static void write_config(const struct fwu_mdata_config *config, bool layout)
+{
+	FILE *fp = fopen(config->path, "w");
+
+	assert_non_null(fp);
+	fprintf(fp, "fwu:\n");
+	fprintf(fp, "  format : fwu-mdata\n");
+	fprintf(fp, "  size : 0x%x\n", STORAGE_SIZE);
+	if (layout) {
+		fprintf(fp, "  banks : %d\n", NUM_BANKS);
+		fprintf(fp, "  images : %d\n", NUM_IMAGES);
+	}
+	fprintf(fp, "  lockfile : %s\n", config->lockfile);
+	fprintf(fp, "  devices:\n");
+	fprintf(fp, "    - path : %s\n", config->devices[0]);
+	fprintf(fp, "    - path : %s\n", config->devices[1]);
+	assert_int_equal(fclose(fp), 0);
+}
+
+static int fwu_mdata_open(const char *config, struct uboot_ctx **ctx)
+{
+	struct uboot_ctx *ctxlist = NULL;
+	int ret;
+
+	ret = libuboot_read_config_ext(&ctxlist, config);
+	assert_int_equal(ret, 0);
+	*ctx = libuboot_get_namespace(ctxlist, "fwu");
+	assert_non_null(*ctx);
+
+	return libuboot_open(*ctx);
+}
+
+static void fwu_mdata_close(struct uboot_ctx **ctx)
+{
+	libuboot_close(*ctx);
+	libuboot_exit(*ctx);
+	*ctx = NULL;
+}
+
+static void assert_var(struct uboot_ctx *ctx, const char *name,
+		       const char *value)
+{
+	char *v = libuboot_get_env(ctx, name);
+
+	assert_non_null(v);
+	assert_string_equal(v, value);
+	free(v);
+}
+
+static void assert_bank_accepted(uint8_t *buf, unsigned int version,
+				 int bank, bool accepted)
+{
+	struct fwu_mdata_image_bank_info *info;
+	int i;
+
+	for (i = 0; i < NUM_IMAGES; i++) {
+		info = to_bank_info(version, buf, i, bank);
+		assert_int_equal(le32toh(info->accepted),
+				 accepted ? FWU_MDATA_IMAGE_ACCEPTED : 0);
+	}
+}
+
+static void assert_bank_state(uint8_t *buf, int bank, uint8_t state)
+{
+	struct fwu_mdata_ext *ext = to_ext(buf);
+
+	assert_int_equal(ext->bank_state[bank], state);
+}
+
+/* A valid copy, with the other fields preserved */
+static void assert_stored(uint8_t *stored, uint8_t *ref, uint32_t active,
+			  uint32_t previous)
+{
+	struct fwu_mdata *hdr = to_hdr(stored);
+	struct fwu_mdata *ref_hdr = to_hdr(ref);
+	unsigned int version = le32toh(ref_hdr->version);
+	size_t size = mdata_size(version);
+	int i, b;
+
+	assert_int_equal(le32toh(hdr->crc32), mdata_crc(stored, size));
+	assert_int_equal(hdr->version, ref_hdr->version);
+	assert_int_equal(le32toh(hdr->active_index), active);
+	assert_int_equal(le32toh(hdr->previous_active_index), previous);
+
+	if (version == 2) {
+		struct fwu_mdata_ext *ext = to_ext(stored);
+		struct fwu_mdata_ext *ref_ext = to_ext(ref);
+
+		assert_int_equal(ext->metadata_size, ref_ext->metadata_size);
+		assert_int_equal(ext->desc_offset, ref_ext->desc_offset);
+		assert_memory_equal(to_desc(stored), to_desc(ref),
+				    sizeof(struct fwu_mdata_fw_store_desc));
+	}
+
+	for (i = 0; i < NUM_IMAGES; i++) {
+		struct fwu_mdata_image_entry *img, *ref_img;
+
+		img = to_image(version, stored, i);
+		ref_img = to_image(version, ref, i);
+		assert_memory_equal(&img->image_type_guid,
+				    &ref_img->image_type_guid,
+				    sizeof(struct fwu_mdata_guid));
+		assert_memory_equal(&img->location_guid,
+				    &ref_img->location_guid,
+				    sizeof(struct fwu_mdata_guid));
+		for (b = 0; b < NUM_BANKS; b++) {
+			struct fwu_mdata_image_bank_info *bank, *ref_bank;
+
+			bank = &img->img_bank_info[b];
+			ref_bank = &ref_img->img_bank_info[b];
+			assert_memory_equal(&bank->image_guid,
+					    &ref_bank->image_guid,
+					    sizeof(struct fwu_mdata_guid));
+		}
+	}
+	assert_memory_equal(stored + size, ref + size, STORAGE_SIZE - size);
+}
+
+static int setup(void **state)
+{
+	struct fwu_mdata_fixture *f = calloc(1, sizeof(*f));
+	struct fwu_mdata_config *config;
+
+	assert_non_null(f);
+	f->version = *(unsigned int *)*state;
+	config = &f->config;
+
+	strcpy(f->dir, "fwu_mdata_XXXXXX");
+	assert_non_null(mkdtemp(f->dir));
+	snprintf(config->path, PATH_LEN, "%s/fw_env.config", f->dir);
+	snprintf(config->lockfile, PATH_LEN, "%s/lock", f->dir);
+	snprintf(config->devices[0], PATH_LEN, "%s/mdata1", f->dir);
+	snprintf(config->devices[1], PATH_LEN, "%s/mdata2", f->dir);
+	snprintf(f->script, PATH_LEN, "%s/script", f->dir);
+
+	mdata_build(f->mdata, f->version);
+	write_copy(config->devices[0], f->mdata);
+	write_copy(config->devices[1], f->mdata);
+	write_config(config, true);
+
+	*state = f;
+	return 0;
+}
+
+static int teardown(void **state)
+{
+	struct fwu_mdata_fixture *f = *state;
+
+	if (f->ctx)
+		fwu_mdata_close(&f->ctx);
+
+	unlink(f->config.path);
+	unlink(f->config.lockfile);
+	unlink(f->config.devices[0]);
+	unlink(f->config.devices[1]);
+	unlink(f->script);
+	rmdir(f->dir);
+	free(f);
+
+	return 0;
+}
+
+static void test_load(void **state)
+{
+	struct fwu_mdata_fixture *f = *state;
+	struct fwu_mdata_config *config = &f->config;
+	void *entry = NULL;
+	int count_vars = 0;
+	int num_vars = 2;
+	char name[16];
+	int bank;
+
+	assert_int_equal(fwu_mdata_open(config->path, &f->ctx), 0);
+	assert_var(f->ctx, "active_index", "0");
+	assert_var(f->ctx, "previous_active_index", "1");
+	for (bank = 0; bank < NUM_BANKS; bank++) {
+		snprintf(name, sizeof(name), "bank%d_state", bank);
+		assert_var(f->ctx, name, "accepted");
+		num_vars++;
+	}
+
+	while ((entry = libuboot_iterator(f->ctx, entry)))
+		count_vars++;
+	assert_int_equal(count_vars, num_vars);
+}
+
+static void test_switch(void **state)
+{
+	struct fwu_mdata_fixture *f = *state;
+	struct fwu_mdata_config *config = &f->config;
+	uint8_t copy0[STORAGE_SIZE];
+	uint8_t copy1[STORAGE_SIZE];
+
+	assert_int_equal(fwu_mdata_open(config->path, &f->ctx), 0);
+	assert_int_equal(libuboot_set_env(f->ctx, "active_index", "1"), 0);
+	assert_int_equal(libuboot_set_env(f->ctx, "previous_active_index", "0"),
+			 0);
+	assert_int_equal(libuboot_set_env(f->ctx, "bank1_state", "valid"), 0);
+	assert_int_equal(libuboot_env_store(f->ctx), 0);
+	fwu_mdata_close(&f->ctx);
+
+	read_copy(config->devices[0], copy0);
+	read_copy(config->devices[1], copy1);
+	assert_memory_equal(copy0, copy1, STORAGE_SIZE);
+	assert_stored(copy0, f->mdata, 1, 0);
+	assert_bank_accepted(copy0, f->version, 0, true);
+	assert_bank_accepted(copy0, f->version, 1, false);
+	if (f->version == 2) {
+		assert_bank_state(copy0, 0, FWU_MDATA_BANK_ACCEPTED);
+		assert_bank_state(copy0, 1, FWU_MDATA_BANK_VALID);
+	}
+
+	assert_int_equal(fwu_mdata_open(config->path, &f->ctx), 0);
+	assert_var(f->ctx, "active_index", "1");
+	assert_var(f->ctx, "previous_active_index", "0");
+	assert_var(f->ctx, "bank0_state", "accepted");
+	assert_var(f->ctx, "bank1_state", "valid");
+}
+
+static void test_switch_from_file(void **state)
+{
+	struct fwu_mdata_fixture *f = *state;
+	struct fwu_mdata_config *config = &f->config;
+	const char *script = "active_index=1\n"
+			     "previous_active_index=0\n"
+			     "bank1_state=valid\n";
+	uint8_t copy0[STORAGE_SIZE];
+	uint8_t copy1[STORAGE_SIZE];
+
+	write_file(f->script, script, strlen(script));
+
+	assert_int_equal(fwu_mdata_open(config->path, &f->ctx), 0);
+	assert_int_equal(libuboot_load_file(f->ctx, f->script), 0);
+	assert_int_equal(libuboot_env_store(f->ctx), 0);
+	fwu_mdata_close(&f->ctx);
+
+	read_copy(config->devices[0], copy0);
+	read_copy(config->devices[1], copy1);
+	assert_memory_equal(copy0, copy1, STORAGE_SIZE);
+	assert_stored(copy0, f->mdata, 1, 0);
+	assert_bank_accepted(copy0, f->version, 1, false);
+}
+
+static void test_accept(void **state)
+{
+	struct fwu_mdata_fixture *f = *state;
+	struct fwu_mdata_config *config = &f->config;
+	uint8_t copy0[STORAGE_SIZE];
+	uint8_t copy1[STORAGE_SIZE];
+
+	assert_int_equal(fwu_mdata_open(config->path, &f->ctx), 0);
+	assert_int_equal(libuboot_set_env(f->ctx, "bank1_state", "valid"), 0);
+	assert_int_equal(libuboot_env_store(f->ctx), 0);
+	assert_int_equal(libuboot_set_env(f->ctx, "bank1_state", "accepted"),
+			 0);
+	assert_int_equal(libuboot_env_store(f->ctx), 0);
+	fwu_mdata_close(&f->ctx);
+
+	read_copy(config->devices[0], copy0);
+	read_copy(config->devices[1], copy1);
+	assert_memory_equal(copy0, copy1, STORAGE_SIZE);
+	assert_stored(copy0, f->mdata, 0, 1);
+	assert_bank_accepted(copy0, f->version, 1, true);
+	if (f->version == 2)
+		assert_bank_state(copy0, 1, FWU_MDATA_BANK_ACCEPTED);
+}
+
+static void test_bank_invalid(void **state)
+{
+	struct fwu_mdata_fixture *f = *state;
+	struct fwu_mdata_config *config = &f->config;
+	uint8_t copy0[STORAGE_SIZE];
+	uint8_t copy1[STORAGE_SIZE];
+	int ret;
+
+	assert_int_equal(fwu_mdata_open(config->path, &f->ctx), 0);
+	ret = libuboot_set_env(f->ctx, "bank1_state", "invalid");
+	if (f->version == 1) {
+		assert_int_equal(ret, -ENOTSUP);
+		return;
+	}
+
+	assert_int_equal(ret, 0);
+	assert_int_equal(libuboot_env_store(f->ctx), 0);
+	fwu_mdata_close(&f->ctx);
+
+	read_copy(config->devices[0], copy0);
+	read_copy(config->devices[1], copy1);
+	assert_memory_equal(copy0, copy1, STORAGE_SIZE);
+	assert_stored(copy0, f->mdata, 0, 1);
+	assert_bank_accepted(copy0, f->version, 1, false);
+	assert_bank_state(copy0, 1, FWU_MDATA_BANK_INVALID);
+}
+
+static void test_rejected_values(void **state)
+{
+	struct fwu_mdata_fixture *f = *state;
+	struct fwu_mdata_config *config = &f->config;
+	uint8_t copy0[STORAGE_SIZE];
+
+	assert_int_equal(fwu_mdata_open(config->path, &f->ctx), 0);
+	assert_int_equal(libuboot_set_env(f->ctx, "active_index", "2"),
+			 -EINVAL);
+	assert_int_not_equal(libuboot_set_env(f->ctx, "active_index", "x"), 0);
+	assert_int_equal(libuboot_set_env(f->ctx, "previous_active_index", "7"),
+			 -EINVAL);
+	assert_int_equal(libuboot_set_env(f->ctx, "bank0_state", "bogus"),
+			 -EINVAL);
+	assert_var(f->ctx, "active_index", "0");
+	assert_var(f->ctx, "previous_active_index", "1");
+	assert_var(f->ctx, "bank0_state", "accepted");
+	fwu_mdata_close(&f->ctx);
+
+	read_copy(config->devices[0], copy0);
+	assert_memory_equal(copy0, f->mdata, STORAGE_SIZE);
+}
+
+static void test_fixed_varlist(void **state)
+{
+	struct fwu_mdata_fixture *f = *state;
+	struct fwu_mdata_config *config = &f->config;
+	const char *script = "active_index=1\n"
+			     "bank0_state=bogus\n";
+
+	write_file(f->script, script, strlen(script));
+
+	assert_int_equal(fwu_mdata_open(config->path, &f->ctx), 0);
+	assert_int_equal(libuboot_set_env(f->ctx, "foo", "1"), -EPERM);
+	assert_int_equal(libuboot_set_env(f->ctx, "bank2_state", "valid"),
+			 -EPERM);
+	assert_int_equal(libuboot_set_env(f->ctx, "active_index", NULL),
+			 -EPERM);
+	assert_null(libuboot_get_env(f->ctx, "foo"));
+	assert_var(f->ctx, "active_index", "0");
+	assert_int_not_equal(libuboot_load_file(f->ctx, f->script), 0);
+}
+
+static void test_corrupted_copy(void **state)
+{
+	struct fwu_mdata_fixture *f = *state;
+	struct fwu_mdata_config *config = &f->config;
+	uint8_t corrupted[STORAGE_SIZE];
+	struct fwu_mdata *hdr = to_hdr(corrupted);
+	uint8_t copy0[STORAGE_SIZE];
+	uint8_t copy1[STORAGE_SIZE];
+
+	memcpy(corrupted, f->mdata, STORAGE_SIZE);
+	hdr->active_index ^= htole32(1);
+	write_copy(config->devices[0], corrupted);
+	write_copy(config->devices[1], f->mdata);
+
+	assert_int_equal(fwu_mdata_open(config->path, &f->ctx), 0);
+	assert_var(f->ctx, "active_index", "0");
+	assert_int_equal(libuboot_set_env(f->ctx, "bank1_state", "valid"), 0);
+	assert_int_equal(libuboot_env_store(f->ctx), 0);
+	fwu_mdata_close(&f->ctx);
+
+	/* Both copies are written, so the corrupted one is repaired */
+	read_copy(config->devices[0], copy0);
+	read_copy(config->devices[1], copy1);
+	assert_memory_equal(copy0, copy1, STORAGE_SIZE);
+	assert_stored(copy0, f->mdata, 0, 1);
+}
+
+static void test_invalid_copy(void **state)
+{
+	struct fwu_mdata_fixture *f = *state;
+	struct fwu_mdata_config *config = &f->config;
+	uint8_t invalid[STORAGE_SIZE];
+	struct fwu_mdata *hdr = to_hdr(invalid);
+
+	/* A correct CRC, but an index out of range */
+	memcpy(invalid, f->mdata, STORAGE_SIZE);
+	hdr->active_index = htole32(NUM_BANKS);
+	mdata_update_crc(invalid, mdata_size(f->version));
+	write_copy(config->devices[0], invalid);
+	write_copy(config->devices[1], f->mdata);
+
+	assert_int_equal(fwu_mdata_open(config->path, &f->ctx), 0);
+	assert_var(f->ctx, "active_index", "0");
+}
+
+static void test_no_valid_copy(void **state)
+{
+	struct fwu_mdata_fixture *f = *state;
+	struct fwu_mdata_config *config = &f->config;
+	uint8_t corrupted[STORAGE_SIZE];
+	struct fwu_mdata *hdr = to_hdr(corrupted);
+
+	memcpy(corrupted, f->mdata, STORAGE_SIZE);
+	hdr->previous_active_index ^= htole32(1);
+	write_copy(config->devices[0], corrupted);
+	write_copy(config->devices[1], corrupted);
+
+	assert_int_not_equal(fwu_mdata_open(config->path, &f->ctx), 0);
+}
+
+static void test_missing_layout(void **state)
+{
+	struct fwu_mdata_fixture *f = *state;
+	struct fwu_mdata_config *config = &f->config;
+	int ret;
+
+	write_config(config, false);
+	ret = fwu_mdata_open(config->path, &f->ctx);
+
+	/* Only version 1 needs the layout in the configuration */
+	if (f->version == 1) {
+		assert_int_equal(ret, -EINVAL);
+	} else {
+		assert_int_equal(ret, 0);
+		assert_var(f->ctx, "bank1_state", "accepted");
+	}
+}
+
+static unsigned int v1 = 1;
+static unsigned int v2 = 2;
+
+#define FWU_TEST(test, version)					\
+	{							\
+		.name = #test " (" #version ")",		\
+		.test_func = test,				\
+		.setup_func = setup,				\
+		.teardown_func = teardown,			\
+		.initial_state = &version,			\
+	}
+
+#define FWU_TESTS(test) FWU_TEST(test, v1), FWU_TEST(test, v2)
+
+int main(void)
+{
+	const struct CMUnitTest tests[] = {
+		FWU_TESTS(test_load),
+		FWU_TESTS(test_switch),
+		FWU_TESTS(test_switch_from_file),
+		FWU_TESTS(test_accept),
+		FWU_TESTS(test_bank_invalid),
+		FWU_TESTS(test_rejected_values),
+		FWU_TESTS(test_fixed_varlist),
+		FWU_TESTS(test_corrupted_copy),
+		FWU_TESTS(test_invalid_copy),
+		FWU_TESTS(test_no_valid_copy),
+		FWU_TESTS(test_missing_layout),
+	};
+
+	return cmocka_run_group_tests(tests, NULL, NULL);
+}