Skip to content

Commit

Permalink
tests: fast_pair: account_key_storage: Migrate to new ztest API
Browse files Browse the repository at this point in the history
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 <Marek.Pieta@nordicsemi.no>
  • Loading branch information
MarekPieta authored and nordicjm committed Jul 6, 2023
1 parent 481fa68 commit 3f38c8a
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 166 deletions.
1 change: 0 additions & 1 deletion scripts/quarantine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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();
Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Loading

0 comments on commit 3f38c8a

Please sign in to comment.