Skip to content

Commit d217d33

Browse files
committed
tests: subsys: fs: add bm_zms_unity tests
Adds `bm_zms` Unity unit tests. Signed-off-by: Mirko Covizzi <[email protected]>
1 parent 8d73d5a commit d217d33

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

subsys/fs/bm_zms/bm_zms.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#define NRFX_CRITICAL_SECTION_EXIT(...)
2828
#endif
2929

30+
#if CONFIG_UNITY
31+
#define __ALIGN(x) __aligned(x)
32+
#endif
33+
3034
LOG_MODULE_REGISTER(bm_zms, CONFIG_BM_ZMS_LOG_LEVEL);
3135

3236
static zms_op_t cur_op; /* Current bm_zms operation. */
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
cmake_minimum_required(VERSION 3.20.0)
8+
9+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
10+
11+
project(bm_zms_unity)
12+
13+
cmock_handle(${ZEPHYR_BASE}/include/zephyr/sys/crc.h)
14+
cmock_handle(${ZEPHYR_NRF_BM_MODULE_DIR}/include/bm/storage/bm_storage.h)
15+
16+
target_include_directories(app PRIVATE ${ZEPHYR_NRF_BM_MODULE_DIR}/include)
17+
18+
target_compile_definitions(app PRIVATE
19+
CONFIG_BM_ZMS_MAX_USERS=8
20+
CONFIG_BM_ZMS_OP_QUEUE_SIZE=16
21+
)
22+
23+
# Generate and add test file
24+
test_runner_generate(src/unity_test.c)
25+
target_sources(app PRIVATE src/unity_test.c)
26+
27+
# Unit under test
28+
target_sources(app PRIVATE ${ZEPHYR_NRF_BM_MODULE_DIR}/subsys/fs/bm_zms/bm_zms.c)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
CONFIG_UNITY=y
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
#include <unity.h>
7+
#include <errno.h>
8+
9+
#include <bm/fs/bm_zms.h>
10+
11+
#include "cmock_bm_storage.h"
12+
#include "cmock_crc.h"
13+
14+
static struct bm_storage_info info = {
15+
.erase_unit = 4,
16+
.erase_value = 0,
17+
.program_unit = 4,
18+
.no_explicit_erase = true
19+
};
20+
21+
void bm_zms_callback(bm_zms_evt_t const *p_evt)
22+
{
23+
}
24+
25+
void test_bm_zms_register_efault(void)
26+
{
27+
int err;
28+
29+
struct bm_zms_fs fs = { 0 };
30+
31+
err = bm_zms_register(NULL, NULL);
32+
TEST_ASSERT_EQUAL(-EFAULT, err);
33+
34+
err = bm_zms_register(&fs, NULL);
35+
TEST_ASSERT_EQUAL(-EFAULT, err);
36+
37+
err = bm_zms_register(NULL, bm_zms_callback);
38+
TEST_ASSERT_EQUAL(-EFAULT, err);
39+
}
40+
41+
void test_bm_zms_register(void)
42+
{
43+
int err;
44+
45+
struct bm_zms_fs fs = { 0 };
46+
47+
err = bm_zms_register(&fs, bm_zms_callback);
48+
TEST_ASSERT_EQUAL(0, err);
49+
}
50+
51+
void test_bm_zms_mount_efault(void)
52+
{
53+
int err;
54+
55+
struct bm_zms_fs fs = { 0 };
56+
struct bm_zms_fs_config config = {
57+
.offset = 0,
58+
.sector_size = 1024,
59+
.sector_count = 4,
60+
};
61+
62+
err = bm_zms_mount(NULL, NULL);
63+
TEST_ASSERT_EQUAL(-EFAULT, err);
64+
65+
err = bm_zms_mount(&fs, NULL);
66+
TEST_ASSERT_EQUAL(-EFAULT, err);
67+
68+
err = bm_zms_mount(NULL, &config);
69+
TEST_ASSERT_EQUAL(-EFAULT, err);
70+
}
71+
72+
void test_bm_zms_mount(void)
73+
{
74+
int err;
75+
76+
struct bm_zms_fs fs = { 0 };
77+
struct bm_zms_fs_config config = {
78+
.offset = 0,
79+
.sector_size = 1024,
80+
.sector_count = 4,
81+
};
82+
83+
fs.zms_bm_storage.nvm_info = &info;
84+
85+
__cmock_bm_storage_init_ExpectAndReturn(&fs.zms_bm_storage, 0);
86+
87+
for (int i = 0; i < 13; i++) {
88+
__cmock_bm_storage_read_ExpectAndReturn(&fs.zms_bm_storage, 0, NULL, 0, 0);
89+
__cmock_bm_storage_read_IgnoreArg_src();
90+
__cmock_bm_storage_read_IgnoreArg_dest();
91+
__cmock_bm_storage_read_IgnoreArg_len();
92+
}
93+
94+
__cmock_crc8_ccitt_IgnoreAndReturn(0);
95+
96+
__cmock_bm_storage_write_ExpectAndReturn(&fs.zms_bm_storage, 0, NULL, 0, NULL, 0);
97+
__cmock_bm_storage_write_IgnoreArg_dest();
98+
__cmock_bm_storage_write_IgnoreArg_src();
99+
__cmock_bm_storage_write_IgnoreArg_len();
100+
__cmock_bm_storage_write_IgnoreArg_ctx();
101+
102+
err = bm_zms_mount(&fs, &config);
103+
TEST_ASSERT_EQUAL(0, err);
104+
}
105+
106+
void setUp(void)
107+
{
108+
}
109+
110+
void tearDown(void)
111+
{
112+
}
113+
114+
extern int unity_main(void);
115+
116+
int main(void)
117+
{
118+
return unity_main();
119+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
tests:
2+
lib.bm_zms_unity:
3+
platform_allow: native_sim
4+
tags: unittest

0 commit comments

Comments
 (0)