From patchwork Fri Nov 13 17:17:37 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dario Binacchi X-Patchwork-Id: 5023 Delivered-To: michael@amarulasolutions.com Received: by 2002:a05:7301:4187:b0:36a:3487:1c26 with SMTP id ph7csp545606dyb; Fri, 13 Nov 2026 09:17:55 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErHZtTTye9xIJiWu/yGSA8KgNmZtatk7Mn+7RRbRO9VLq7FMLnQNyt+H2N4QGDLjEY/4M7LHxwoL@amarulasolutions.com X-Received: by 2002:a5d:588f:0:b0:48b:a4b:5a2c with SMTP id ffacd0b85a97d-49c6d4538fbmr7655424f8f.3.1794590275494; Fri, 13 Nov 2026 09:17:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1794590275; cv=none; d=google.com; s=arc-20260327; b=bnwN2jihyTghYe+M1jOThdTXRFsDikT34dr6WC54EevvICRUUFqrH5/TqGnLK8Tsof 00ZaHtDfXbEiJa61/RSR4a6GopC7oJCjWWkPewmCktZPdehDw20GCeebXWdBk02snxIz s/TAq4hum02w68MgpURB2dvx86UfI2fnxZLzZx6QHlUAPiAQdxdwbx2v5+ByEVBIQg5X YEVOspHuU1SNXXrB1aojfq0rSYBwoVWUxL4A25APTY8oQra0jaUSR3kOAN91l+X5C1PP xjCpdzz3YXWpxFkow6Der+WuXXCfUg19OZ9ELP+uFfEbdkbz5tvzQbioVtvGtX3+4/yf pPNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=26uD7/k5/Vrbtl5anzsBXCbZv9YwvPKrjfQ99TZO2yc=; fh=3yyuI94qjLlNFmDCyqr+sNPB+siKcd/QJOK0HpfaOFY=; b=B+f/RMFTeJ6ETiqjRIVQcWxJOltpI3b6RqDWIT60pL+rKjLIMb+Qb8QD5DZy2xWU36 7ASRB52tdmoKW2MTdmlA0m01p5qm4uXVrOIzV+mZ7M/E9qw7tzy3sZCn5dRk7D0g/ZVa mVfgo7lcdgH13LK7qYrwAQcZU+9MrKaZsDnDh0/16b/a4TgzixwHcSSedbAgVTdrlYK1 dSwIDhKWZk/DaohgtzDAAHAyTVhvniA3R/XycTNIyV1QSzYldWHFyD3FnRfY9J/LPfCe Op+JmPQR7VT1eY7T3KxYT36c1aihKA0nbVfEfLsld6HNTAPWqne8mRPLMuV5+imDwfcM yeWQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=msLIObFv; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com; dara=neutral header.i=@amarulasolutions.com Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id ffacd0b85a97d-49c60f8e3e8sor612228f8f.8.2026.11.13.09.17.55 for (Google Transport Security); Fri, 13 Nov 2026 09:17:55 -0800 (PST) Received-SPF: pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@amarulasolutions.com header.s=google header.b=msLIObFv; spf=pass (google.com: domain of dario.binacchi@amarulasolutions.com designates 209.85.220.41 as permitted sender) smtp.mailfrom=dario.binacchi@amarulasolutions.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=amarulasolutions.com; dara=neutral header.i=@amarulasolutions.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amarulasolutions.com; s=google; t=1794590275; x=1795195075; darn=amarulasolutions.com; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=26uD7/k5/Vrbtl5anzsBXCbZv9YwvPKrjfQ99TZO2yc=; b=msLIObFvJbHCOxrUMRVDdFqzTfheKbuxRAT+defjPb7W5n9ygp3FCu+tLKG7UZvcye x6dZ2rVeyLA8BA+6xY1F9h+oK588WTbpibuwQZQVeLdcrplzxbrVQ/tr0j5gJS1R5o5F AkO2xe17FMhGuhtT2AZsnyM8g5JZi04+To644= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794590275; x=1795195075; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=26uD7/k5/Vrbtl5anzsBXCbZv9YwvPKrjfQ99TZO2yc=; b=VoTINn+3YBPsU4+nBeCd0JxBVDurP9iUzQu6b6agbSIWCO14+6iyy9NWWq3+ZQwuzE yJdagtgYWwBj37vT0dVCTOzHmgU0XniQbl69s2Oiv2kVyVC92RTyTZ1kCdDfraWxdsjf ThHjRo5RtcaQFUKoKiq/alaI0PIkMsbGCCziArfsFM3H1XTvO2qiFrxEzJMYBhVKRUIa RolUHZwIlqLtoZDd4XD6UphUcmpGVTXWE7jTlMBn0VYYK/deG0NGcUhnidCyJ5PSTkoE 76frAAk+CCemn5CA+6uFIsssjEKQeihwvM0zPJmoTYa0ZiGHYD8EIdInLKeWKAm4MIxB 79yA== X-Forwarded-Encrypted: i=1; AL+hErEBJd4RYuDgU/O8KD1XYKX9Jh0H23q1iYDEmbZqP6aI/tGQ6DNwHP2AtIb48BjYYIXVgq4K0FNi@amarulasolutions.com X-Gm-Message-State: APuU4Z9fV2zP0fZOWOnnoMGYXeXG8PZhhfngjC4vTA5tecyL2MKFA7DW +WiGoPcgd21IlTcmhQS7qnWBvekkVopXJ0k+evbMhiJiTQQgPyEJyRedTzh1vAVkLB7F7w== X-Gm-Gg: AYBFou3K8RVMf5G3hnocKYgC65WkfgricYRU2yLGY8+RYGyxDcM7QbAEkrukEKyoWnq ez/0pCvgvfZEdAZhsYsNo7vuDIJiTjhDikLX4h3+/xSnPI4AJS6j+jQ3iZnl3OSdK4DgdYu67W3 dNssR/xhpRiKnVfAvMRSFxCk+jwtmjjmyyELAd2gr4F0wxKKbG3q6cJufDUUqYWBjkwB2X/Vffv KwJejh+zQ3LE/7AMspXXPV+HbH1tZuFcN2m/eCxbOMlgq6OdJdRIrVVtcqpoKzOnajeP/jXBOSi 8KgK4942V/0Wh0wO9fQK7/RolTWybIumgcHkjGMfiKBc1KCjch/gebu13msZK+Xaa741yd/zqKN z3yK4hftro4Ugo/lAiObuHA99i7jJdoNvhGOT6dqEbVBkzCAdySubRXYWQipJaVZ2yniupF03TU 1S8+Fo4CtTGLW/ekDEDapM0x1y6miy2W9/iBgwne1ap3UiO2NhE5BuCaUtgqo9cOfmq6qGKIozO i3kciXR0OD/1NZYObbHqcIvP9eNRM7wIcjJnHHlVFo8MNX1yzFslHTd+sXeOGN2F0riLR+ZtwD3 5p8aeqFDmq+3lyBaRF26Xkp4Ir0lC9y51L7JDg+loC3D7xCNBiPIOsaFP6ikod0lyK4LBO0BAes W5wQp3t79788G6a1Jb1hIX7S0l1Ac7tY6zhRI7hz1efgh0Fd1C1hkLZkbyqJENU/AExq95oozEs gM+YUq3eQlzzIDN+TdW13XiiJeUg== X-Received: by 2002:a05:600c:3111:b0:4a3:2f35:38b4 with SMTP id 5b1f17b1804b1-4bb55a4490fmr67398935e9.10.1794590274875; Fri, 13 Nov 2026 09:17:54 -0800 (PST) Return-Path: Received: from dario-ThinkPad-P14s-Gen-5.homenet.telecomitalia.it (host-79-14-29-58.business.telecomitalia.it. [79.14.29.58]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb55ac0e1esm68545935e9.3.2026.11.13.09.17.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Nov 2026 09:17:54 -0800 (PST) From: Dario Binacchi To: swupdate@googlegroups.com Cc: stefano.babic@swupdate.org, linux-amarula@amarulasolutions.com, michael@amarulasolutions.com, Dario Binacchi Subject: [libubootenv][PATCH v2 8/8] tests: add unit tests for the FWU metadata format Date: Fri, 13 Nov 2026 18:17:37 +0100 Message-ID: <20261113171737.470894-9-dario.binacchi@amarulasolutions.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261113171737.470894-1-dario.binacchi@amarulasolutions.com> References: <20261113171737.470894-1-dario.binacchi@amarulasolutions.com> MIME-Version: 1.0 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 --- 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 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 + * + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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); +}