Skip to content

Commit

Permalink
[nrf fromtree] tests: drivers: flash: Move negative tests to a separa…
Browse files Browse the repository at this point in the history
…te project

Move negative tests for flash driver to a separate test suite.
Run negative tests only on platforms that are aligned.

Signed-off-by: Sebastian Głąb <[email protected]>
(cherry picked from commit ffd065f)
  • Loading branch information
nordic-segl committed Sep 16, 2024
1 parent 3a403a4 commit 5b67768
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 162 deletions.
162 changes: 0 additions & 162 deletions tests/drivers/flash/common/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,6 @@
#error "Unsupported configuraiton"
#endif

#define TEST_FLASH_START (DT_REG_ADDR(DT_CHOSEN(zephyr_flash)))
#if (DT_PROP(DT_CHOSEN(zephyr_flash), size))
/* When flash is defined in DTS as:
* size = < 0x4000000 >;
* reg = < 0x0 >;
*/
#define TEST_FLASH_SIZE (DT_PROP(DT_CHOSEN(zephyr_flash), size))
#else
/* When flash is defined in DTS as:
* reg = < 0xe000000 0x200000 >;
*/
#define TEST_FLASH_SIZE (DT_REG_SIZE(DT_CHOSEN(zephyr_flash)))
#endif /* #if (DT_PROP(DT_CHOSEN(zephyr_flash), size)) */

#define EXPECTED_SIZE 512

static const struct device *const flash_dev = TEST_AREA_DEVICE;
Expand Down Expand Up @@ -217,154 +203,6 @@ ZTEST(flash_driver, test_flash_erase)
zassert_not_equal(expected[0], erase_value, "These values shall be different");
}

ZTEST(flash_driver, test_negative_flash_erase)
{
int rc;

#if !defined(TEST_AREA)
/* Flash memory boundaries are correctly calculated
* only for storage_partition.
*/
ztest_test_skip();
#endif

TC_PRINT("TEST_FLASH_START = 0x%lx\n", (unsigned long)TEST_FLASH_START);
TC_PRINT("TEST_FLASH_SIZE = 0x%lx\n", (unsigned long)TEST_FLASH_SIZE);

/* Check error returned when erasing memory at wrong address (too low) */
rc = flash_erase(flash_dev, (TEST_FLASH_START - 1), EXPECTED_SIZE);
zassert_true(rc < 0, "Invalid use of flash_erase returned %d", rc);

/* Check error returned when erasing memory at wrong address (too high) */
rc = flash_erase(flash_dev, (TEST_FLASH_START + TEST_FLASH_SIZE), EXPECTED_SIZE);
zassert_true(rc < 0, "Invalid use of flash_erase returned %d", rc);

/* Check error returned when erasing to large chunk of memory */
rc = flash_erase(flash_dev, TEST_AREA_OFFSET, (TEST_FLASH_SIZE - TEST_AREA_OFFSET + 1));
zassert_true(rc < 0, "Invalid use of flash_erase returned %d", rc);

/* Erasing 0 bytes shall succeed */
rc = flash_erase(flash_dev, TEST_AREA_OFFSET, 0);
zassert_true(rc == 0, "flash_erase 0 bytes returned %d", rc);
}

ZTEST(flash_driver, test_negative_flash_fill)
{
int rc;
uint8_t fill_val = 0xA; /* Dummy value */

#if !defined(TEST_AREA)
/* Flash memory boundaries are correctly calculated
* only for storage_partition.
*/
ztest_test_skip();
#endif

/* Check error returned when filling memory at wrong address (too low) */
rc = flash_fill(flash_dev, fill_val, (TEST_FLASH_START - 1), EXPECTED_SIZE);
zassert_true(rc < 0, "Invalid use of flash_fill returned %d", rc);

/* Check error returned when filling memory at wrong address (too high) */
rc = flash_fill(flash_dev, fill_val, (TEST_FLASH_START + TEST_FLASH_SIZE), EXPECTED_SIZE);
zassert_true(rc < 0, "Invalid use of flash_fill returned %d", rc);

/* Check error returned when filling to large chunk of memory */
rc = flash_fill(flash_dev, fill_val, TEST_AREA_OFFSET,
(TEST_FLASH_SIZE - TEST_AREA_OFFSET + 1));
zassert_true(rc < 0, "Invalid use of flash_fill returned %d", rc);

/* Filling 0 bytes shall succeed */
rc = flash_fill(flash_dev, fill_val, TEST_AREA_OFFSET, 0);
zassert_true(rc == 0, "flash_fill 0 bytes returned %d", rc);
}

ZTEST(flash_driver, test_negative_flash_flatten)
{
int rc;

#if !defined(TEST_AREA)
/* Flash memory boundaries are correctly calculated
* only for storage_partition.
*/
ztest_test_skip();
#endif

/* Check error returned when flatten memory at wrong address (too low) */
rc = flash_flatten(flash_dev, (TEST_FLASH_START - 1), EXPECTED_SIZE);
zassert_true(rc < 0, "Invalid use of flash_flatten returned %d", rc);

/* Check error returned when flatten memory at wrong address (too high) */
rc = flash_flatten(flash_dev, (TEST_FLASH_START + TEST_FLASH_SIZE), EXPECTED_SIZE);
zassert_true(rc < 0, "Invalid use of flash_flatten returned %d", rc);

/* Check error returned when flatten to large chunk of memory */
rc = flash_flatten(flash_dev, TEST_AREA_OFFSET, (TEST_FLASH_SIZE - TEST_AREA_OFFSET + 1));
zassert_true(rc < 0, "Invalid use of flash_flatten returned %d", rc);

/* Flatten 0 bytes shall succeed */
rc = flash_flatten(flash_dev, TEST_AREA_OFFSET, 0);
zassert_true(rc == 0, "flash_flatten 0 bytes returned %d", rc);
}

ZTEST(flash_driver, test_negative_flash_read)
{
int rc;
uint8_t read_buf[EXPECTED_SIZE];

#if !defined(TEST_AREA)
/* Flash memory boundaries are correctly calculated
* only for storage_partition.
*/
ztest_test_skip();
#endif

/* Check error returned when reading from a wrong address (too low) */
rc = flash_read(flash_dev, (TEST_FLASH_START - 1), read_buf, EXPECTED_SIZE);
zassert_true(rc < 0, "Invalid use of flash_read returned %d", rc);

/* Check error returned when reading from a wrong address (too high) */
rc = flash_read(flash_dev, (TEST_FLASH_START + TEST_FLASH_SIZE), read_buf, EXPECTED_SIZE);
zassert_true(rc < 0, "Invalid use of flash_read returned %d", rc);

/* Check error returned when reading too many data */
rc = flash_read(flash_dev, TEST_AREA_OFFSET, read_buf,
(TEST_FLASH_SIZE - TEST_AREA_OFFSET + 1));
zassert_true(rc < 0, "Invalid use of flash_read returned %d", rc);

/* Reading 0 bytes shall succeed */
rc = flash_read(flash_dev, TEST_AREA_OFFSET, read_buf, 0);
zassert_true(rc == 0, "flash_read 0 bytes returned %d", rc);
}

ZTEST(flash_driver, test_negative_flash_write)
{
int rc;

#if !defined(TEST_AREA)
/* Flash memory boundaries are correctly calculated
* only for storage_partition.
*/
ztest_test_skip();
#endif

/* Check error returned when writing to a wrong address (too low) */
rc = flash_write(flash_dev, (TEST_FLASH_START - 1), expected, EXPECTED_SIZE);
zassert_true(rc < 0, "Invalid use of flash_write returned %d", rc);

/* Check error returned when writing to a wrong address (too high) */
rc = flash_write(flash_dev, (TEST_FLASH_START + TEST_FLASH_SIZE), expected, EXPECTED_SIZE);
zassert_true(rc < 0, "Invalid use of flash_write returned %d", rc);

/* Check error returned when writing too many data */
rc = flash_write(flash_dev, TEST_AREA_OFFSET, expected,
(TEST_FLASH_SIZE - TEST_AREA_OFFSET + 1));
zassert_true(rc < 0, "Invalid use of flash_write returned %d", rc);

/* Writing 0 bytes shall succeed */
rc = flash_write(flash_dev, TEST_AREA_OFFSET, expected, 0);
zassert_true(rc == 0, "flash_write 0 bytes returned %d", rc);
}

struct test_cb_data_type {
uint32_t page_counter; /* used to count how many pages was iterated */
uint32_t exit_page; /* terminate iteration when this page is reached */
Expand Down
8 changes: 8 additions & 0 deletions tests/drivers/flash/negative_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(flash_negative_tests)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
5 changes: 5 additions & 0 deletions tests/drivers/flash/negative_tests/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CONFIG_TEST=y
CONFIG_ZTEST=y
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_MAIN_STACK_SIZE=2048
Loading

0 comments on commit 5b67768

Please sign in to comment.