Skip to content

Commit

Permalink
tests: net: lib: nrf_cloud: client_id: Add unit test
Browse files Browse the repository at this point in the history
Unit test for client id. Must run on an nRF91xxDK.

Add missing error codes to nrf_cloud.h for client id API.

Jira: IRIS-4787

Signed-off-by: Pete Skeggs <peter.skeggs@nordicsemi.no>
  • Loading branch information
plskeggs committed Oct 18, 2024
1 parent 32e6fa0 commit 7fb2241
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/net/nrf_cloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ int nrf_cloud_modem_fota_completed(const bool fota_success);
* @param[in] id_buf_sz Size of buffer, maximum size is NRF_CLOUD_CLIENT_ID_MAX_LEN + 1.
*
* @retval 0 If successful.
* @retval -EINVAL The id_buf parameter is NULL and/or the id_buf_sz parameter is 0.
* @retval -EMSGSIZE The provided buffer is too small.
* @retval -EIO The client ID could not be initialized.
* @retval -ENXIO The Kconfig option @kconfig{CONFIG_NRF_CLOUD_CLIENT_ID_SRC_RUNTIME} is enabled
Expand All @@ -965,6 +966,8 @@ int nrf_cloud_client_id_get(char *id_buf, size_t id_buf_sz);
* Max string length is NRF_CLOUD_CLIENT_ID_MAX_LEN.
*
* @retval 0 If successful.
* @retval -EINVAL The length of the client_id provided exceeds the maximum allowed.

Check warning on line 969 in include/net/nrf_cloud.h

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

SPACE_BEFORE_TAB

include/net/nrf_cloud.h:969 please, no space before tabs
* @retval -ENODATA The client_id cannot be empty.
* @return A negative value indicates an error.
*/
int nrf_cloud_client_id_runtime_set(const char *const client_id);
Expand Down
55 changes: 55 additions & 0 deletions tests/subsys/net/lib/nrf_cloud/client_id/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#
# Copyright (c) 2024 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(nrf_cloud_client_id)
set(NRF_SDK_DIR ${ZEPHYR_BASE}/../nrf)
set(NRFXLIB_MODEM_DIR ${ZEPHYR_BASE}/../nrfxlib/nrf_modem)
cmake_path(NORMAL_PATH NRF_SDK_DIR)
cmake_path(NORMAL_PATH NRFXLIB_MODEM_DIR)

FILE(GLOB app_sources src/main.c)

target_sources(app PRIVATE ${app_sources})

target_sources(app
PRIVATE
${ZEPHYR_NRF_MODULE_DIR}/subsys/net/lib/nrf_cloud/src/nrf_cloud_client_id.c
)

target_include_directories(app
PRIVATE
src
${NRF_SDK_DIR}/subsys/net/lib/nrf_cloud/include
${ZEPHYR_BASE}/subsys/testsuite/include
)

if (CONFIG_NRF_MODEM_LIB)

set_source_files_properties(
${NRF_SDK_DIR}/lib/nrf_modem_lib/nrf_modem_lib.c
PROPERTIES HEADER_FILE_ONLY ON
)

set_source_files_properties(
${NRFXLIB_MODEM_DIR}/include/nrf_modem_at.h
PROPERTIES HEADER_FILE_ONLY ON
)

set_source_files_properties(
${NRF_SDK_DIR}/lib/modem_jwt/modem_jwt.c
PROPERTIES HEADER_FILE_ONLY ON
)

set_source_files_properties(
${NRF_SDK_DIR}/lib/hw_id/hw_id.c
PROPERTIES HEADER_FILE_ONLY ON
)


endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# Copyright (c) 2023 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

# Disable networking
CONFIG_NETWORKING=n

# For the unit test to run in qemu_cortex_m3,
# we need the following KConfig values to be set.
# See https://github.com/zephyrproject-rtos/zephyr/issues/15565
CONFIG_ENTROPY_GENERATOR=y
CONFIG_TEST_RANDOM_GENERATOR=y
27 changes: 27 additions & 0 deletions tests/subsys/net/lib/nrf_cloud/client_id/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#
# Copyright (c) 2024 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

# ZTEST with new API
CONFIG_ZTEST=y

# Networking
CONFIG_NETWORKING=y
CONFIG_NET_NATIVE=n
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_POSIX_API=y

# Modem library
CONFIG_NRF_MODEM_LIB=y

# AT host library
CONFIG_AT_HOST_LIBRARY=y
CONFIG_UART_INTERRUPT_DRIVEN=y

# Stacks and heaps
CONFIG_MAIN_STACK_SIZE=3072
CONFIG_HEAP_MEM_POOL_SIZE=16384
CONFIG_AT_MONITOR_HEAP_SIZE=512
61 changes: 61 additions & 0 deletions tests/subsys/net/lib/nrf_cloud/client_id/src/fakes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <zephyr/fff.h>
#include <zephyr/ztest.h>
#include <modem/modem_jwt.h>

DEFINE_FFF_GLOBALS;

#if !IS_ENABLED(CONFIG_NRF_MODEM_LIB)
FAKE_VALUE_FUNC(int, nrf_modem_lib_init);
FAKE_VALUE_FUNC(int, nrf_modem_lib_shutdown);
FAKE_VALUE_FUNC_VARARG(int, nrf_modem_at_cmd, void *, size_t, const char *, ...);
FAKE_VALUE_FUNC(int, modem_jwt_get_uuids, struct nrf_device_uuid *, struct nrf_modem_fw_uuid *);
FAKE_VALUE_FUNC(int, hw_id_get, char *, size_t);

int nrf_modem_lib_shutdown__succeeds(void)
{
return 0;
}

int nrf_modem_lib_shutdown__fails(void)
{
return -1;
}

int nrf_modem_at_cmd__fails(void *buf, size_t len, const char *fmt, ...)
{
return -1;
}

int nrf_modem_at_cmd__succeeds(void *buf, size_t len, const char *fmt, ...)
{
return 0;
}

int modem_jwt_get_uuids__fails(struct nrf_device_uuid *dev,
struct nrf_modem_fw_uuid *mfw)
{
return -1;
}

int modem_jwt_get_uuids__succeeds(struct nrf_device_uuid *dev,
struct nrf_modem_fw_uuid *mfw)
{
return 0;
}

int hw_id_get__fails(char *buf, size_t buf_len)
{
return -1;
}

int hw_id_get__succeeds(char *buf, size_t buf_len)
{
return 0;
}
#endif
120 changes: 120 additions & 0 deletions tests/subsys/net/lib/nrf_cloud/client_id/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <net/nrf_cloud.h>
#include <zephyr/fff.h>
#include <zephyr/ztest.h>
#if IS_ENABLED(CONFIG_NRF_MODEM)
#include <modem/nrf_modem_lib.h>
#endif
#include <modem/modem_jwt.h>
#include "fakes.h"
#include "string.h"

#define RUNTIME_ID "test"
#define UUID_LEN 36

/* This function runs before each test */
static void run_before(void *fixture)
{
ARG_UNUSED(fixture);

#if !IS_ENABLED(CONFIG_NRF_MODEM_LIB)
RESET_FAKE(nrf_modem_lib_init);
RESET_FAKE(nrf_modem_lib_shutdown);
RESET_FAKE(nrf_modem_at_cmd);
RESET_FAKE(modem_jwt_get_uuids);
RESET_FAKE(hw_id_get);
#endif
int err = nrf_modem_lib_init();

printk("MODEM LIB INIT: %d\n", err);

}

/* This function runs after each completed test */
static void run_after(void *fixture)
{
ARG_UNUSED(fixture);
}

ZTEST_SUITE(nrf_cloud_client_id_test, NULL, NULL, run_before, run_after, NULL);

/*

Check failure on line 46 in tests/subsys/net/lib/nrf_cloud/client_id/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

TRAILING_WHITESPACE

tests/subsys/net/lib/nrf_cloud/client_id/src/main.c:46 trailing whitespace
*

Check failure on line 47 in tests/subsys/net/lib/nrf_cloud/client_id/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

TRAILING_WHITESPACE

tests/subsys/net/lib/nrf_cloud/client_id/src/main.c:47 trailing whitespace
*/
ZTEST(nrf_cloud_client_id_test, test_nrf_cloud_client_id)
{
int ret;
char buf[NRF_CLOUD_CLIENT_ID_MAX_LEN + 2];
size_t len;

#if IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_IMEI)
printk("IMEI TEST\n");
#elif IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_COMPILE_TIME)
printk("COMPTIME TEST\n");
#elif IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_INTERNAL_UUID)
printk("UUID TEST\n");
#elif IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_HW_ID)
printk("HWID TEST\n");
#elif IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_RUNTIME)
printk("RUNTIME TEST\n");
#endif

ret = nrf_cloud_client_id_get(buf, 0);
zassert_true((ret < 0), "Zero len buffer returned a value >= 0.");
zassert_equal(ret, -EINVAL, "Zero len returned wrong error.");

len = NRF_CLOUD_CLIENT_ID_MAX_LEN;
ret = nrf_cloud_client_id_get(NULL, len);
zassert_true((ret < 0), "NULL pointer returned a value >= 0.");
zassert_equal(ret, -EINVAL, "NULL pointer returned wrong error.");

#if IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_RUNTIME)
len = NRF_CLOUD_CLIENT_ID_MAX_LEN;
ret = nrf_cloud_client_id_get(buf, len);
zassert_true((ret < 0), "Unset runtime ID returned a value >= 0.");
zassert_equal(ret, -ENXIO, "Wrong error returned when runtime ID not set.");

ret = nrf_cloud_client_id_runtime_set("");
zassert_equal(ret, -ENODATA, "Wrong error returned when empty runtime ID set.");

memset(buf, 'A', NRF_CLOUD_CLIENT_ID_MAX_LEN + 1);
buf[NRF_CLOUD_CLIENT_ID_MAX_LEN + 1] = '\0';
ret = nrf_cloud_client_id_runtime_set(buf);
zassert_equal(ret, -EINVAL, "Wrong error returned when too large runtime ID set.");

ret = nrf_cloud_client_id_runtime_set(RUNTIME_ID);
zassert_equal(ret, 0, "Unexpected error when setting runtime client id");
#endif

len = 1;
ret = nrf_cloud_client_id_get(buf, len);
zassert_true((ret < 0), "Too-small buffer returned a value >= 0.");
zassert_equal(ret, -EMSGSIZE, "Wrong error returned with too-small buffer.");

len = NRF_CLOUD_CLIENT_ID_MAX_LEN;
ret = nrf_cloud_client_id_get(buf, len);
printk("nrf_cloud_client_id_get: ret = %d, id: %.*s\n", ret, len, buf);
zassert_equal(ret, 0, "Unexpected error when getting client id");

#if IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_IMEI)
ret = strncmp(buf, CONFIG_NRF_CLOUD_CLIENT_ID_PREFIX,
strlen(CONFIG_NRF_CLOUD_CLIENT_ID_PREFIX));
zassert_equal(ret, 0, "Unexpected prefix on IMEI client id");
#elif IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_COMPILE_TIME)
ret = strncmp(buf, CONFIG_NRF_CLOUD_CLIENT_ID,
strlen(CONFIG_NRF_CLOUD_CLIENT_ID));
zassert_equal(ret, 0, "Unexpected miscompare on compile time client id");
#elif IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_INTERNAL_UUID)
zassert_equal(strlen(buf), UUID_LEN, "Unexpected length of UUID id");
#elif IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_HW_ID)
/* TODO: check length returned depending on CONFIG_HW_ID_LIBRARY_SOURCE */
#elif IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_RUNTIME)
ret = strncmp(buf, RUNTIME_ID, len);
zassert_equal(ret, 0, "Unexpected miscompare on runtime client id");
#endif
}
42 changes: 42 additions & 0 deletions tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
common:
platform_allow: nrf9160dk/nrf9160/ns nrf9161dk/nrf9161/ns nrf9151dk/nrf9151/ns
integration_platforms:
- nrf9160dk/nrf9160/ns nrf9161dk/nrf9161/ns nrf9151dk/nrf9151/ns
tags: ci_build nrf_cloud_test nrf_cloud_lib ci_tests_subsys_net
tests:
net.lib.nrf_cloud.client_id.imei:
sysbuild: true
timeout: 60
extra_args:
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_IMEI=y

Check warning on line 11 in tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

YAMLLint (hyphens)

tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml:11 too many spaces after hyphen
tags: sysbuild ci_tests_subsys_net
net.lib.nrf_cloud.client_id.runtime:
sysbuild: true
timeout: 60
extra_args:
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_IMEI=n

Check warning on line 17 in tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

YAMLLint (hyphens)

tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml:17 too many spaces after hyphen
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_RUNTIME=y

Check warning on line 18 in tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

YAMLLint (hyphens)

tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml:18 too many spaces after hyphen
tags: sysbuild ci_tests_subsys_net
net.lib.nrf_cloud.client_id.uuid:
sysbuild: true
timeout: 60
extra_args:
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_IMEI=n

Check warning on line 24 in tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

YAMLLint (hyphens)

tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml:24 too many spaces after hyphen
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_INTERNAL_UUID=y

Check warning on line 25 in tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

YAMLLint (hyphens)

tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml:25 too many spaces after hyphen
- CONFIG_MODEM_JWT=y

Check warning on line 26 in tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

YAMLLint (hyphens)

tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml:26 too many spaces after hyphen
tags: sysbuild ci_tests_subsys_net
net.lib.nrf_cloud.client_id.hwid:
sysbuild: true
timeout: 60
extra_args:
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_IMEI=n

Check warning on line 32 in tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

YAMLLint (hyphens)

tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml:32 too many spaces after hyphen
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_HW_ID=y

Check warning on line 33 in tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

YAMLLint (hyphens)

tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml:33 too many spaces after hyphen
- CONFIG_HW_ID_LIBRARY=y

Check warning on line 34 in tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

YAMLLint (hyphens)

tests/subsys/net/lib/nrf_cloud/client_id/testcase.yaml:34 too many spaces after hyphen
tags: sysbuild ci_tests_subsys_net
net.lib.nrf_cloud.client_id.comptime:
sysbuild: true
timeout: 60
extra_args:
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_IMEI=n
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_COMPILE_TIME=y
tags: sysbuild ci_tests_subsys_net

0 comments on commit 7fb2241

Please sign in to comment.