From 852c992da596e07698692dafc4546cff562a1700 Mon Sep 17 00:00:00 2001 From: Dominik Ermel Date: Wed, 23 Jun 2021 08:30:02 +0000 Subject: [PATCH] tests/drivers/flash_api: Flash API tests The commit adds test for flash_get_size. Signed-off-by: Dominik Ermel --- tests/drivers/flash_api/CMakeLists.txt | 12 ++++ tests/drivers/flash_api/prj.conf | 7 ++ tests/drivers/flash_api/src/main.c | 98 ++++++++++++++++++++++++++ tests/drivers/flash_api/testcase.yaml | 16 +++++ 4 files changed, 133 insertions(+) create mode 100644 tests/drivers/flash_api/CMakeLists.txt create mode 100644 tests/drivers/flash_api/prj.conf create mode 100644 tests/drivers/flash_api/src/main.c create mode 100644 tests/drivers/flash_api/testcase.yaml diff --git a/tests/drivers/flash_api/CMakeLists.txt b/tests/drivers/flash_api/CMakeLists.txt new file mode 100644 index 00000000000..469541ca5e1 --- /dev/null +++ b/tests/drivers/flash_api/CMakeLists.txt @@ -0,0 +1,12 @@ +# +# Copyright (c) 2024 Nordic Semiconductor ASA. +# +# SPDX-License-Identifier: Apache-2.0 +# + +cmake_minimum_required(VERSION 3.13.1) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(flash_api_tests) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/tests/drivers/flash_api/prj.conf b/tests/drivers/flash_api/prj.conf new file mode 100644 index 00000000000..244c013dd5c --- /dev/null +++ b/tests/drivers/flash_api/prj.conf @@ -0,0 +1,7 @@ +# +# Copyright (c) 2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: Apache-2.0 +# +CONFIG_ZTEST=y +CONFIG_FLASH=y diff --git a/tests/drivers/flash_api/src/main.c b/tests/drivers/flash_api/src/main.c new file mode 100644 index 00000000000..76cc6ade6d9 --- /dev/null +++ b/tests/drivers/flash_api/src/main.c @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +/** Test instrumentation **/ +/* + * The structure below gathers variables that may be used by the simulated API calls + * to mock behavior of a flash device; the variables may be used however it is desired, + * because it is up to the mocked functions and checks afterwards to relate these variables + * to results. + */ +static struct { + /* Set to value returned from any API call */ + int ret; + /* Some size */ + uint64_t size; +} simulated_values = { + .ret = 0, + .size = 0, +}; + +/*** Device definition atd == API Test Dev ***/ +static int some_get_size(const struct device *dev, uint64_t *size) +{ + __ASSERT_NO_MSG(dev != NULL); + + *size = simulated_values.size; + + return 0; +} + +static int enotsup_get_size(const struct device *dev, uint64_t *size) +{ + ARG_UNUSED(dev); + + return -ENOTSUP; +} + +/** Device objects **/ +/* The device state, just to make it "ready" device */ +static struct device_state some_dev_state = { + .init_res = 0, + .initialized = 1, +}; + +/* Device with get_size */ +const static struct flash_driver_api size_fun_api = { + .get_size = some_get_size, +}; +const static struct device size_fun_dev = { + "get_size", + NULL, + &size_fun_api, + &some_dev_state, +}; + +/* No functions device */ +const static struct flash_driver_api no_fun_api = {0}; +const static struct device no_fun_dev = { + "no_fun", + NULL, + &no_fun_api, + &some_dev_state, +}; + +/* Device with get_size implemented but returning -ENOTSUP */ +static struct flash_driver_api enotsup_fun_api = { + .get_size = enotsup_get_size, +}; +static struct device enotsup_fun_dev = { + "enotsup", + NULL, + &enotsup_fun_api, + &some_dev_state, +}; + +ZTEST(flash_api, test_get_size) +{ + uint64_t size = 0; + + simulated_values.size = 45; + zassert_ok(flash_get_size(&size_fun_dev, &size), "Expected success"); + zassert_equal(size, simulated_values.size, "Size mismatch"); + simulated_values.size = 46; + zassert_ok(flash_get_size(&size_fun_dev, &size), "Expected success"); + zassert_equal(size, simulated_values.size, "Size mismatch"); + zassert_equal(flash_get_size(&no_fun_dev, &size), -ENOSYS); + + zassert_equal(flash_get_size(&enotsup_fun_dev, &size), -ENOTSUP); +} + +ZTEST_SUITE(flash_api, NULL, NULL, NULL, NULL, NULL); diff --git a/tests/drivers/flash_api/testcase.yaml b/tests/drivers/flash_api/testcase.yaml new file mode 100644 index 00000000000..5c099e00f59 --- /dev/null +++ b/tests/drivers/flash_api/testcase.yaml @@ -0,0 +1,16 @@ +# +# Copyright (c) 2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: Apache-2.0 +# +common: + platform_allow: + - native_sim/native/64 + - native_sim +tests: + drivers.flash.api: + tags: drivers flash + drivers.flash.api.userspace: + tags: driver flash userspace + extra_configs: + - CONFIG_USERSPACE=y