From 5db84d68d4704fc9a2d484c02741b0617c4cd2e0 Mon Sep 17 00:00:00 2001 From: Marek Pieta Date: Tue, 4 Jul 2023 14:28:15 +0200 Subject: [PATCH] tests: fast_pair: account_key_storage: Migrate to new ztest API Change migrates test to new ztest API. The old API is deprecated. Change also removes the test from quarantine. Jira: NCSDK-21698 Signed-off-by: Marek Pieta --- scripts/quarantine.yaml | 1 - .../include/test_corrupted_data.h | 35 --- .../storage/account_key_storage/prj.conf | 3 + .../storage/account_key_storage/src/main.c | 71 +++--- .../src/test_corrupted_data.c | 211 +++++++++--------- 5 files changed, 155 insertions(+), 166 deletions(-) delete mode 100644 tests/subsys/bluetooth/fast_pair/storage/account_key_storage/include/test_corrupted_data.h diff --git a/scripts/quarantine.yaml b/scripts/quarantine.yaml index b25255aac422..ae3d58e52909 100644 --- a/scripts/quarantine.yaml +++ b/scripts/quarantine.yaml @@ -15,7 +15,6 @@ comment: "Temporary disable till the issue is fixed" - scenarios: - - fast_pair.storage.account_key_storage.* - fast_pair.storage.factory_reset.* platforms: - all diff --git a/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/include/test_corrupted_data.h b/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/include/test_corrupted_data.h deleted file mode 100644 index dfb3a58c1ddd..000000000000 --- a/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/include/test_corrupted_data.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2022 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause - */ - -#ifndef _TEST_CORRUPTED_DATA_H_ -#define _TEST_CORRUPTED_DATA_H_ - -/** - * @defgroup fp_storage_test_corrupted_data Fast Pair storage corrupted data unit test - * @brief Unit test of handling corrupted data in the Fast Pair storage - * - * @{ - */ - -#ifdef __cplusplus -extern "C" { -#endif - -/** Start test suite of corrupted data handling in the Fast Pair storage. - * - * The test suite verifies handling of corrupted settings data in the Fast Pair storage module. - */ -void test_corrupted_data_run(void); - -#ifdef __cplusplus -} -#endif - -/** - * @} - */ - -#endif /* _TEST_CORRUPTED_DATA_H_ */ diff --git a/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/prj.conf b/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/prj.conf index 782b1202da3d..9bd442a539f4 100644 --- a/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/prj.conf +++ b/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/prj.conf @@ -5,6 +5,9 @@ # CONFIG_ZTEST=y +CONFIG_ZTEST_NEW_API=y +CONFIG_ZTEST_SHUFFLE=y + CONFIG_SETTINGS=y CONFIG_SETTINGS_CUSTOM=y CONFIG_HEAP_MEM_POOL_SIZE=1024 diff --git a/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/src/main.c b/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/src/main.c index 8177f2ddf0dd..fee1adfd03f7 100644 --- a/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/src/main.c +++ b/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/src/main.c @@ -13,7 +13,6 @@ #include "storage_mock.h" #include "common_utils.h" -#include "test_corrupted_data.h" #define ACCOUNT_KEY_MAX_CNT CONFIG_BT_FAST_PAIR_STORAGE_ACCOUNT_KEY_MAX @@ -29,8 +28,10 @@ static void reload_keys_from_storage(void) zassert_ok(err, "Failed to load settings"); } -static void setup_fn(void) +static void before_fn(void *f) { + ARG_UNUSED(f); + int err; cu_account_keys_validate_unloaded(); @@ -41,28 +42,48 @@ static void setup_fn(void) zassert_ok(err, "Settings load failed"); } -static void teardown_fn(void) +static void after_fn(void *f) { + ARG_UNUSED(f); + fp_storage_ak_ram_clear(); storage_mock_clear(); cu_account_keys_validate_unloaded(); } -static void test_unloaded(void) +static bool unloaded_ak_check_cb(const struct fp_account_key *account_key, void *context) +{ + zassert_unreachable("Callback should never be called"); + return false; +} + +ZTEST(suite_fast_pair_storage, test_unloaded) { static const uint8_t seed = 0; int err; struct fp_account_key account_key; + struct fp_account_key read_keys[ACCOUNT_KEY_MAX_CNT]; + size_t read_cnt = ACCOUNT_KEY_MAX_CNT; - cu_account_keys_validate_unloaded(); - cu_generate_account_key(seed, &account_key); + /* Check that Account Key storage operations fail when settings are unloaded. */ + after_fn(NULL); + cu_generate_account_key(seed, &account_key); err = fp_storage_ak_save(&account_key); zassert_equal(err, -ENODATA, "Expected error before settings load"); + + err = fp_storage_ak_count(); + zassert_equal(err, -ENODATA, "Expected error before settings load"); + + err = fp_storage_ak_get(read_keys, &read_cnt); + zassert_equal(err, -ENODATA, "Expected error before settings load"); + + err = fp_storage_ak_find(NULL, unloaded_ak_check_cb, NULL); + zassert_equal(err, -ENODATA, "Expected error before settings load"); } -static void test_one_key(void) +ZTEST(suite_fast_pair_storage, test_one_key) { static const uint8_t seed = 5; @@ -94,7 +115,7 @@ static void test_one_key(void) zassert_true(cu_check_account_key_seed(seed, &read_keys[0]), "Invalid key on read"); } -static void test_duplicate(void) +ZTEST(suite_fast_pair_storage, test_duplicate) { static const uint8_t seed = 3; @@ -130,7 +151,7 @@ static void test_duplicate(void) zassert_true(cu_check_account_key_seed(seed, &read_keys[0]), "Invalid key on read"); } -static void test_invalid_calls(void) +ZTEST(suite_fast_pair_storage, test_invalid_calls) { static const uint8_t first_seed = 3; static const uint8_t test_key_cnt = 3; @@ -173,7 +194,7 @@ static bool account_key_find_cb(const struct fp_account_key *account_key, void * return cu_check_account_key_seed(*seed, account_key); } -static void test_find(void) +ZTEST(suite_fast_pair_storage, test_find) { static const uint8_t first_seed = 0; static const size_t test_key_cnt = 3; @@ -207,12 +228,17 @@ static void test_find(void) zassert_equal(err, -ESRCH, "Expected error when key cannot be found"); } -static void test_loop(void) +ZTEST(suite_fast_pair_storage, test_loop) { static const uint8_t first_seed = 0; for (uint8_t i = 1; i < UCHAR_MAX; i++) { - setup_fn(); + /* Triggering before function twice in a row would lead to assertion failure. + * The function checks if settings are not loaded and then loads the settings. + */ + if (i > 1) { + before_fn(NULL); + } cu_account_keys_generate_and_store(first_seed, i); cu_account_keys_validate_loaded(first_seed, i); @@ -221,24 +247,9 @@ static void test_loop(void) reload_keys_from_storage(); cu_account_keys_validate_loaded(first_seed, i); - teardown_fn(); + after_fn(NULL); } } -void test_main(void) -{ - ztest_test_suite(fast_pair_storage_tests, - ztest_unit_test(test_unloaded), - ztest_unit_test_setup_teardown(test_one_key, setup_fn, teardown_fn), - ztest_unit_test_setup_teardown(test_duplicate, setup_fn, teardown_fn), - ztest_unit_test_setup_teardown(test_invalid_calls, setup_fn, teardown_fn), - ztest_unit_test_setup_teardown(test_find, setup_fn, teardown_fn), - ztest_unit_test(test_loop) - ); - - ztest_run_test_suite(fast_pair_storage_tests); - /* Test cases verifying that module properly handles settings data corruption. - * Corrupted data test suite relies on internal data representation. - */ - test_corrupted_data_run(); -} + +ZTEST_SUITE(suite_fast_pair_storage, NULL, NULL, before_fn, after_fn, NULL); diff --git a/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/src/test_corrupted_data.c b/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/src/test_corrupted_data.c index f9018a01d894..e0045abbf34a 100644 --- a/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/src/test_corrupted_data.c +++ b/tests/subsys/bluetooth/fast_pair/storage/account_key_storage/src/test_corrupted_data.c @@ -13,37 +13,116 @@ #include "storage_mock.h" #include "common_utils.h" -#include "test_corrupted_data.h" +static void store_key(uint8_t key_id) +{ + int err; + char settings_key[SETTINGS_AK_NAME_MAX_SIZE] = SETTINGS_AK_FULL_PREFIX; + struct account_key_data data; + zassert_true(('0' + account_key_id_to_idx(key_id)) <= '9', "Key index out of range"); + settings_key[ARRAY_SIZE(settings_key) - 2] = ('0' + account_key_id_to_idx(key_id)); + + data.account_key_id = key_id; + cu_generate_account_key(key_id, &data.account_key); + + err = settings_save_one(settings_key, &data, sizeof(data)); + zassert_ok(err, "Unexpected error in settings save"); +} + +static void self_test_after(void) +{ + fp_storage_ak_ram_clear(); + storage_mock_clear(); + cu_account_keys_validate_unloaded(); +} -static void setup_fn(void) +static void self_test(void) +{ + int err; + uint8_t key_id = ACCOUNT_KEY_MIN_ID; + uint8_t key_cnt = 0; + + store_key(key_id); + key_cnt++; + key_id = next_account_key_id(key_id); + + store_key(key_id); + key_cnt++; + key_id = next_account_key_id(key_id); + + store_key(key_id); + key_cnt++; + + err = settings_load(); + zassert_ok(err, "Unexpected error in settings load"); + + cu_account_keys_validate_loaded(ACCOUNT_KEY_MIN_ID, key_cnt); +} + +static void self_test_rollover(void) +{ + int err; + uint8_t key_id = ACCOUNT_KEY_MIN_ID; + uint8_t key_cnt = 0; + + for (size_t i = 0; i < ACCOUNT_KEY_CNT; i++) { + store_key(key_id); + key_cnt++; + key_id = next_account_key_id(key_id); + } + + store_key(key_id); + key_cnt++; + key_id = next_account_key_id(key_id); + + store_key(key_id); + key_cnt++; + + err = settings_load(); + zassert_ok(err, "Unexpected error in settings load"); + + cu_account_keys_validate_loaded(ACCOUNT_KEY_MIN_ID, key_cnt); +} + +static void *setup_fn(void) +{ + /* Perform simple self tests to ensure that the test method is valid. */ + self_test(); + self_test_after(); + + self_test_rollover(); + self_test_after(); + + return NULL; +} + +static void before_fn(void *f) { /* Make sure that settings are not yet loaded. Settings will be populated with data and * loaded by the unit test. */ + ARG_UNUSED(f); + cu_account_keys_validate_unloaded(); } -static void teardown_fn(void) +static void after_fn(void *f) { - int err = settings_load(); - - zassert_not_equal(err, 0, "Expected error in settings load"); - cu_account_keys_validate_unloaded(); + ARG_UNUSED(f); fp_storage_ak_ram_clear(); storage_mock_clear(); } -/* Self test is done to ensure that the test method is valid. */ -static void test_self_teardown_fn(void) +static void settings_load_error_validate(void) { - fp_storage_ak_ram_clear(); - storage_mock_clear(); + int err = settings_load(); + + zassert_not_equal(err, 0, "Expected error in settings load"); cu_account_keys_validate_unloaded(); } -static void test_wrong_settings_key(void) +ZTEST(suite_fast_pair_storage_corrupted, test_wrong_settings_key) { static const uint8_t key_id = ACCOUNT_KEY_MIN_ID; @@ -56,9 +135,11 @@ static void test_wrong_settings_key(void) err = settings_save_one(SETTINGS_AK_SUBTREE_NAME SETTINGS_NAME_SEPARATOR_STR "not_account_key", &data, sizeof(data)); zassert_ok(err, "Unexpected error in settings save"); + + settings_load_error_validate(); } -static void test_wrong_data_len(void) +ZTEST(suite_fast_pair_storage_corrupted, test_wrong_data_len) { static const uint8_t key_id = ACCOUNT_KEY_MIN_ID; @@ -74,9 +155,11 @@ static void test_wrong_data_len(void) err = settings_save_one(settings_key, &data, sizeof(data) - 1); zassert_ok(err, "Unexpected error in settings save"); + + settings_load_error_validate(); } -static void test_inconsistent_key_id(void) +ZTEST(suite_fast_pair_storage_corrupted, test_inconsistent_key_id) { static const uint8_t key_id = ACCOUNT_KEY_MIN_ID; @@ -92,78 +175,18 @@ static void test_inconsistent_key_id(void) err = settings_save_one(settings_key, &data, sizeof(data)); zassert_ok(err, "Unexpected error in settings save"); -} - -static void store_key(uint8_t key_id) -{ - int err; - char settings_key[SETTINGS_AK_NAME_MAX_SIZE] = SETTINGS_AK_FULL_PREFIX; - struct account_key_data data; - - zassert_true(('0' + account_key_id_to_idx(key_id)) <= '9', "Key index out of range"); - settings_key[ARRAY_SIZE(settings_key) - 2] = ('0' + account_key_id_to_idx(key_id)); - - data.account_key_id = key_id; - cu_generate_account_key(key_id, &data.account_key); - err = settings_save_one(settings_key, &data, sizeof(data)); - zassert_ok(err, "Unexpected error in settings save"); + settings_load_error_validate(); } -static void test_self(void) -{ - int err; - uint8_t key_id = ACCOUNT_KEY_MIN_ID; - uint8_t key_cnt = 0; - - store_key(key_id); - key_cnt++; - key_id = next_account_key_id(key_id); - - store_key(key_id); - key_cnt++; - key_id = next_account_key_id(key_id); - - store_key(key_id); - key_cnt++; - - err = settings_load(); - zassert_ok(err, "Unexpected error in settings load"); - - cu_account_keys_validate_loaded(ACCOUNT_KEY_MIN_ID, key_cnt); -} - -static void test_self_rollover(void) -{ - int err; - uint8_t key_id = ACCOUNT_KEY_MIN_ID; - uint8_t key_cnt = 0; - - for (size_t i = 0; i < ACCOUNT_KEY_CNT; i++) { - store_key(key_id); - key_cnt++; - key_id = next_account_key_id(key_id); - } - - store_key(key_id); - key_cnt++; - key_id = next_account_key_id(key_id); - - store_key(key_id); - key_cnt++; - - err = settings_load(); - zassert_ok(err, "Unexpected error in settings load"); - - cu_account_keys_validate_loaded(ACCOUNT_KEY_MIN_ID, key_cnt); -} - -static void test_drop_first_key(void) +ZTEST(suite_fast_pair_storage_corrupted, test_drop_first_key) { store_key(next_account_key_id(ACCOUNT_KEY_MIN_ID)); + + settings_load_error_validate(); } -static void test_drop_key(void) +ZTEST(suite_fast_pair_storage_corrupted, test_drop_key) { uint8_t key_id = ACCOUNT_KEY_MIN_ID; @@ -175,9 +198,11 @@ static void test_drop_key(void) key_id = next_account_key_id(key_id); key_id = next_account_key_id(key_id); store_key(key_id); + + settings_load_error_validate(); } -static void test_drop_keys(void) +ZTEST(suite_fast_pair_storage_corrupted, test_drop_keys) { uint8_t key_id = ACCOUNT_KEY_MIN_ID; @@ -190,9 +215,11 @@ static void test_drop_keys(void) zassert_equal(account_key_id_to_idx(ACCOUNT_KEY_MIN_ID), account_key_id_to_idx(key_id), "Test should write key exactly after rollover"); store_key(key_id); + + settings_load_error_validate(); } -static void test_drop_rollover(void) +ZTEST(suite_fast_pair_storage_corrupted, test_drop_rollover) { uint8_t key_id = ACCOUNT_KEY_MIN_ID; @@ -205,24 +232,8 @@ static void test_drop_rollover(void) "Test should drop key exactly after rollover"); key_id = next_account_key_id(key_id); store_key(key_id); -} -void test_corrupted_data_run(void) -{ - ztest_test_suite(fast_pair_storage_courrupted_data_tests, - ztest_unit_test_setup_teardown(test_self, setup_fn, test_self_teardown_fn), - ztest_unit_test_setup_teardown(test_self_rollover, setup_fn, - test_self_teardown_fn), - ztest_unit_test_setup_teardown(test_wrong_settings_key, setup_fn, - teardown_fn), - ztest_unit_test_setup_teardown(test_wrong_data_len, setup_fn, teardown_fn), - ztest_unit_test_setup_teardown(test_inconsistent_key_id, setup_fn, - teardown_fn), - ztest_unit_test_setup_teardown(test_drop_first_key, setup_fn, teardown_fn), - ztest_unit_test_setup_teardown(test_drop_key, setup_fn, teardown_fn), - ztest_unit_test_setup_teardown(test_drop_keys, setup_fn, teardown_fn), - ztest_unit_test_setup_teardown(test_drop_rollover, setup_fn, teardown_fn) - ); - - ztest_run_test_suite(fast_pair_storage_courrupted_data_tests); + settings_load_error_validate(); } + +ZTEST_SUITE(suite_fast_pair_storage_corrupted, NULL, setup_fn, before_fn, after_fn, NULL);