|
| 1 | +/* unit-psa_store.c |
| 2 | + * |
| 3 | + * Unit test for PSA storage module |
| 4 | + * |
| 5 | + * Copyright (C) 2026 wolfSSL Inc. |
| 6 | + * |
| 7 | + * This file is part of wolfBoot. |
| 8 | + * |
| 9 | + * wolfBoot is free software; you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU General Public License as published by |
| 11 | + * the Free Software Foundation; either version 3 of the License, or |
| 12 | + * (at your option) any later version. |
| 13 | + * |
| 14 | + * wolfBoot is distributed in the hope that it will be useful, |
| 15 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | + * GNU General Public License for more details. |
| 18 | + * |
| 19 | + * You should have received a copy of the GNU General Public License |
| 20 | + * along with this program; if not, write to the Free Software |
| 21 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 22 | + */ |
| 23 | + |
| 24 | +#define WOLFBOOT_HASH_SHA256 |
| 25 | +#define EXT_FLASH |
| 26 | +#define PART_UPDATE_EXT |
| 27 | +#define NVM_FLASH_WRITEONCE |
| 28 | + |
| 29 | +#define KEYSTORE_PUBKEY_SIZE KEYSTORE_PUBKEY_SIZE_ECC256 |
| 30 | + |
| 31 | +#include <check.h> |
| 32 | +#include <stdint.h> |
| 33 | +#include <stdlib.h> |
| 34 | +#include <string.h> |
| 35 | +#define XMALLOC_OVERRIDE |
| 36 | +#define XMALLOC(n,h,t) malloc(n) |
| 37 | +#define XFREE(p,h,t) free(p) |
| 38 | + |
| 39 | +#include "user_settings.h" |
| 40 | +#include "wolfssl/wolfcrypt/sha.h" |
| 41 | +#include "wolfssl/wolfcrypt/error-crypt.h" |
| 42 | +#include "wolfboot/wolfboot.h" |
| 43 | +#include "wolfpsa/psa_store.h" |
| 44 | +#include "hal.h" |
| 45 | +#include <fcntl.h> |
| 46 | +#include <unistd.h> |
| 47 | +#include <sys/mman.h> |
| 48 | + |
| 49 | +#define MOCK_ADDRESS 0xCF000000 |
| 50 | +uint8_t *vault_base = (uint8_t *)MOCK_ADDRESS; |
| 51 | +#include "psa_store.c" |
| 52 | +const uint32_t keyvault_size = KEYVAULT_OBJ_SIZE * KEYVAULT_MAX_ITEMS + 2 * WOLFBOOT_SECTOR_SIZE; |
| 53 | +#include "unit-mock-flash.c" |
| 54 | + |
| 55 | +START_TEST(test_cross_sector_write_preserves_length) |
| 56 | +{ |
| 57 | + enum { type = WOLFPSA_STORE_KEY }; |
| 58 | + const unsigned long id1 = 7; |
| 59 | + const unsigned long id2 = 9; |
| 60 | + void *store = NULL; |
| 61 | + unsigned char *payload; |
| 62 | + struct store_handle *handle; |
| 63 | + int ret; |
| 64 | + |
| 65 | + payload = malloc(WOLFBOOT_SECTOR_SIZE); |
| 66 | + ck_assert_ptr_nonnull(payload); |
| 67 | + |
| 68 | + for (ret = 0; ret < WOLFBOOT_SECTOR_SIZE; ret++) |
| 69 | + payload[ret] = (unsigned char)(ret & 0xFF); |
| 70 | + |
| 71 | + ret = mmap_file("/tmp/wolfboot-unit-psa-keyvault.bin", vault_base, |
| 72 | + keyvault_size, NULL); |
| 73 | + ck_assert_int_eq(ret, 0); |
| 74 | + memset(vault_base, 0xEE, keyvault_size); |
| 75 | + |
| 76 | + ret = wolfPSA_Store_Open(type, id1, id2, 0, &store); |
| 77 | + ck_assert_int_eq(ret, 0); |
| 78 | + ck_assert_ptr_nonnull(store); |
| 79 | + |
| 80 | + ret = wolfPSA_Store_Write(store, payload, WOLFBOOT_SECTOR_SIZE); |
| 81 | + ck_assert_int_eq(ret, WOLFBOOT_SECTOR_SIZE); |
| 82 | + handle = store; |
| 83 | + ck_assert_uint_eq(handle->in_buffer_offset, |
| 84 | + 2 * sizeof(uint32_t) + WOLFBOOT_SECTOR_SIZE); |
| 85 | + ck_assert_uint_eq(handle->hdr->size, |
| 86 | + 2 * sizeof(uint32_t) + WOLFBOOT_SECTOR_SIZE); |
| 87 | + wolfPSA_Store_Close(store); |
| 88 | + |
| 89 | + free(payload); |
| 90 | +} |
| 91 | +END_TEST |
| 92 | + |
| 93 | +Suite *wolfboot_suite(void) |
| 94 | +{ |
| 95 | + Suite *s = suite_create("wolfBoot-psa-store"); |
| 96 | + TCase *tcase_write = tcase_create("cross_sector_write"); |
| 97 | + |
| 98 | + tcase_add_test(tcase_write, test_cross_sector_write_preserves_length); |
| 99 | + suite_add_tcase(s, tcase_write); |
| 100 | + return s; |
| 101 | +} |
| 102 | + |
| 103 | +int main(void) |
| 104 | +{ |
| 105 | + int fails; |
| 106 | + Suite *s = wolfboot_suite(); |
| 107 | + SRunner *sr = srunner_create(s); |
| 108 | + |
| 109 | + srunner_run_all(sr, CK_NORMAL); |
| 110 | + fails = srunner_ntests_failed(sr); |
| 111 | + srunner_free(sr); |
| 112 | + return fails; |
| 113 | +} |
0 commit comments