Skip to content

Commit

Permalink
flash_partitions: add tests
Browse files Browse the repository at this point in the history
Added tests for flash partitions

Signed-off-by: Laczen JMS <laczenjms@gmail.com>
  • Loading branch information
Laczen committed Apr 5, 2024
1 parent 509b9e1 commit 18110d7
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/subsys/storage/flash_partitions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2024 Laczen
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(flash_partitions_test)

target_sources(app PRIVATE src/main.c)
24 changes: 24 additions & 0 deletions tests/subsys/storage/flash_partitions/boards/native_sim.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 Laczen
* SPDX-License-Identifier: Apache-2.0
*/

&flash0 {
compatible = "soc-nv-flash", "zephyr,flash-no-erase";

flash_partitions {
compatible = "zephyr,flash-partitions";
#address-cells = <1>;
#size-cells = <1>;

flash0_partition0: partition@0 {
reg = <0x00000000 0x00002000>;
erase-block-size = <DT_SIZE_K(8)>;
read-only;
};

flash0_partition1: partition@2000 {
reg = <0x00002000 0x00002000>;
};
};
};
6 changes: 6 additions & 0 deletions tests/subsys/storage/flash_partitions/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) 2024 Laczen
# SPDX-License-Identifier: Apache-2.0
CONFIG_FLASH_PARTITIONS=y
CONFIG_FLASH_PARTITIONS_RUNTIME_VERIFY=y
CONFIG_USERSPACE=n
CONFIG_ZTEST=y
72 changes: 72 additions & 0 deletions tests/subsys/storage/flash_partitions/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2024 Laczen.
* SPDX-License-Identifier: Apache-2.0
*/

#include <string.h>
#include <zephyr/kernel.h>
#include <zephyr/ztest.h>
#include <zephyr/storage/flash_partitions.h>

#define FLASH_PARTITION0 flash0_partition0
#define FLASH_PARTITION1 flash0_partition1

ZTEST_BMEM static const struct flash_partition *fp;

static void *flash_partition_api_setup(void)
{
return NULL;
}

ZTEST_USER(flash_partition_api, test_read_write_erase)
{
uint8_t *wr = "/a9/9a/a9/9a";
uint8_t rd[sizeof(wr)];
int rc = 0;

memset(rd, 0, sizeof(rd));

rc = flash_partition_open(fp);
zassert_equal(rc, 0, "open returned [%d]", rc);

TC_PRINT("flash partition: size %d, offset %d, erase-block-size %d\n",
fp->size, fp->offset, fp->erase_block_size);

rc = flash_partition_read(fp, 0, rd, sizeof(rd));
zassert_equal(rc, 0, "read returned [%d]", rc);

if (fp->write != NULL) {
rc = flash_partition_write(fp, 0, wr, sizeof(wr));
zassert_equal(rc, 0, "write returned [%d]", rc);

rc = flash_partition_read(fp, 0, rd, sizeof(rd));
zassert_equal(rc, 0, "read returned [%d]", rc);

zassert_equal(memcmp(wr, rd, sizeof(rd)), 0,
"read/write data differ");
}

if (fp->erase != NULL) {
rc = flash_partition_erase(fp, 0, fp->erase_block_size);
zassert_equal(rc, 0, "erase returned [%d]", rc);
} else {
TC_PRINT("no erase\n");
}
}

ZTEST_SUITE(flash_partition_api, NULL, flash_partition_api_setup, NULL, NULL,
NULL);

/* Run all of our tests on the given flash partition */
static void run_tests_on_partition(const struct flash_partition *partition)
{
fp = partition;
ztest_run_all(NULL, false, 1, 1);
}

void test_main(void)
{
run_tests_on_partition(FLASH_PARTITION_GET(FLASH_PARTITION0));
run_tests_on_partition(FLASH_PARTITION_GET(FLASH_PARTITION1));
ztest_verify_all_test_suites_ran();
}
12 changes: 12 additions & 0 deletions tests/subsys/storage/flash_partitions/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
common:
depends_on:
- flash_partitions
- flash
tags: flash_partitions
tests:
storage.flash_partitions.native_sim.api:
platform_allow:
- native_sim
integration_platforms:
- native_sim
timeout: 60

0 comments on commit 18110d7

Please sign in to comment.