Skip to content

Commit 90ac4ed

Browse files
plskeggsrlubos
authored andcommitted
tests: net: lib: nrf_cloud: client_id: Add unit test
Unit test for client id. Must run on an nRF9160DK. Add missing error codes to nrf_cloud.h for client id API. Jira: IRIS-4787 Signed-off-by: Pete Skeggs <[email protected]>
1 parent dda18fe commit 90ac4ed

File tree

6 files changed

+348
-0
lines changed

6 files changed

+348
-0
lines changed

include/net/nrf_cloud.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@ int nrf_cloud_modem_fota_completed(const bool fota_success);
946946
* @param[in] id_buf_sz Size of buffer, maximum size is NRF_CLOUD_CLIENT_ID_MAX_LEN + 1.
947947
*
948948
* @retval 0 If successful.
949+
* @retval -EINVAL The id_buf parameter is NULL and/or the id_buf_sz parameter is 0.
949950
* @retval -EMSGSIZE The provided buffer is too small.
950951
* @retval -EIO The client ID could not be initialized.
951952
* @retval -ENXIO The Kconfig option @kconfig{CONFIG_NRF_CLOUD_CLIENT_ID_SRC_RUNTIME} is enabled
@@ -965,6 +966,8 @@ int nrf_cloud_client_id_get(char *id_buf, size_t id_buf_sz);
965966
* Max string length is NRF_CLOUD_CLIENT_ID_MAX_LEN.
966967
*
967968
* @retval 0 If successful.
969+
* @retval -EINVAL The length of the client_id provided exceeds the maximum allowed.
970+
* @retval -ENODATA The client_id cannot be empty.
968971
* @return A negative value indicates an error.
969972
*/
970973
int nrf_cloud_client_id_runtime_set(const char *const client_id);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor
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+
project(nrf_cloud_client_id)
11+
set(NRFXLIB_MODEM_DIR ${ZEPHYR_NRFXLIB_MODULE_DIR}/nrf_modem)
12+
13+
FILE(GLOB app_sources src/main.c)
14+
15+
target_sources(app PRIVATE ${app_sources})
16+
17+
target_sources(app
18+
PRIVATE
19+
${ZEPHYR_NRF_MODULE_DIR}/subsys/net/lib/nrf_cloud/src/nrf_cloud_client_id.c
20+
)
21+
22+
target_include_directories(app
23+
PRIVATE
24+
src
25+
${ZEPHYR_NRF_MODULE_DIR}/subsys/net/lib/nrf_cloud/include
26+
${ZEPHYR_BASE}/subsys/testsuite/include
27+
)
28+
29+
if (CONFIG_NRF_MODEM_LIB)
30+
31+
set_source_files_properties(
32+
${ZEPHYR_NRF_MODULE_DIR}/lib/nrf_modem_lib/nrf_modem_lib.c
33+
PROPERTIES HEADER_FILE_ONLY ON
34+
)
35+
36+
set_source_files_properties(
37+
${NRFXLIB_MODEM_DIR}/include/nrf_modem_at.h
38+
PROPERTIES HEADER_FILE_ONLY ON
39+
)
40+
41+
set_source_files_properties(
42+
${ZEPHYR_NRF_MODULE_DIR}/lib/modem_jwt/modem_jwt.c
43+
PROPERTIES HEADER_FILE_ONLY ON
44+
)
45+
46+
set_source_files_properties(
47+
${ZEPHYR_NRF_MODULE_DIR}/lib/hw_id/hw_id.c
48+
PROPERTIES HEADER_FILE_ONLY ON
49+
)
50+
51+
52+
endif()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
# ZTEST with new API
8+
CONFIG_ZTEST=y
9+
10+
# Networking
11+
CONFIG_NETWORKING=y
12+
CONFIG_NET_NATIVE=n
13+
CONFIG_NET_SOCKETS=y
14+
CONFIG_NET_SOCKETS_OFFLOAD=y
15+
CONFIG_POSIX_API=y
16+
17+
# Modem library
18+
CONFIG_NRF_MODEM_LIB=y
19+
20+
# AT host library
21+
CONFIG_AT_HOST_LIBRARY=y
22+
CONFIG_UART_INTERRUPT_DRIVEN=y
23+
24+
# Stacks and heaps
25+
CONFIG_MAIN_STACK_SIZE=3072
26+
CONFIG_HEAP_MEM_POOL_SIZE=16384
27+
CONFIG_AT_MONITOR_HEAP_SIZE=512
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <zephyr/fff.h>
8+
#include <zephyr/ztest.h>
9+
#include <modem/modem_jwt.h>
10+
11+
DEFINE_FFF_GLOBALS;
12+
13+
#if !IS_ENABLED(CONFIG_NRF_MODEM_LIB)
14+
FAKE_VALUE_FUNC(int, nrf_modem_lib_init);
15+
FAKE_VALUE_FUNC(int, nrf_modem_lib_shutdown);
16+
FAKE_VALUE_FUNC_VARARG(int, nrf_modem_at_cmd, void *, size_t, const char *, ...);
17+
FAKE_VALUE_FUNC(int, modem_jwt_get_uuids, struct nrf_device_uuid *, struct nrf_modem_fw_uuid *);
18+
FAKE_VALUE_FUNC(int, hw_id_get, char *, size_t);
19+
20+
int nrf_modem_lib_shutdown__succeeds(void)
21+
{
22+
return 0;
23+
}
24+
25+
int nrf_modem_lib_shutdown__fails(void)
26+
{
27+
return -1;
28+
}
29+
30+
int nrf_modem_at_cmd__fails(void *buf, size_t len, const char *fmt, ...)
31+
{
32+
return -1;
33+
}
34+
35+
int nrf_modem_at_cmd__succeeds(void *buf, size_t len, const char *fmt, ...)
36+
{
37+
return 0;
38+
}
39+
40+
int modem_jwt_get_uuids__fails(struct nrf_device_uuid *dev,
41+
struct nrf_modem_fw_uuid *mfw)
42+
{
43+
return -1;
44+
}
45+
46+
int modem_jwt_get_uuids__succeeds(struct nrf_device_uuid *dev,
47+
struct nrf_modem_fw_uuid *mfw)
48+
{
49+
return 0;
50+
}
51+
52+
int hw_id_get__fails(char *buf, size_t buf_len)
53+
{
54+
return -1;
55+
}
56+
57+
int hw_id_get__succeeds(char *buf, size_t buf_len)
58+
{
59+
return 0;
60+
}
61+
#endif
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <net/nrf_cloud.h>
8+
#include <zephyr/fff.h>
9+
#include <zephyr/ztest.h>
10+
#if IS_ENABLED(CONFIG_NRF_MODEM)
11+
#include <modem/nrf_modem_lib.h>
12+
#endif
13+
#include <modem/modem_jwt.h>
14+
#include "fakes.h"
15+
#include "string.h"
16+
17+
#define RUNTIME_ID "test"
18+
#define UUID_LEN 36
19+
20+
static void *setup(void)
21+
{
22+
int err = nrf_modem_lib_init();
23+
24+
printk("MODEM LIB INIT: %d\n", err);
25+
26+
return NULL;
27+
}
28+
29+
/* This function runs before each test */
30+
static void run_before(void *fixture)
31+
{
32+
ARG_UNUSED(fixture);
33+
34+
#if !IS_ENABLED(CONFIG_NRF_MODEM_LIB)
35+
RESET_FAKE(nrf_modem_lib_init);
36+
RESET_FAKE(nrf_modem_lib_shutdown);
37+
RESET_FAKE(nrf_modem_at_cmd);
38+
RESET_FAKE(modem_jwt_get_uuids);
39+
RESET_FAKE(hw_id_get);
40+
#endif
41+
}
42+
43+
/* This function runs after each completed test */
44+
static void run_after(void *fixture)
45+
{
46+
ARG_UNUSED(fixture);
47+
}
48+
49+
ZTEST_SUITE(nrf_cloud_client_id_test, NULL, setup, run_before, run_after, NULL);
50+
51+
/*
52+
*
53+
*/
54+
ZTEST(nrf_cloud_client_id_test, test_01_nrf_cloud_client_id_get_len_zero)
55+
{
56+
int ret;
57+
char buf[NRF_CLOUD_CLIENT_ID_MAX_LEN + 2];
58+
59+
ret = nrf_cloud_client_id_get(buf, 0);
60+
zassert_true((ret < 0), "Zero len buffer returned a value >= 0.");
61+
zassert_equal(ret, -EINVAL, "Zero len returned wrong error.");
62+
}
63+
64+
65+
ZTEST(nrf_cloud_client_id_test, test_02_nrf_cloud_client_id_get_null_buf)
66+
{
67+
int ret;
68+
size_t len;
69+
70+
len = NRF_CLOUD_CLIENT_ID_MAX_LEN;
71+
ret = nrf_cloud_client_id_get(NULL, len);
72+
zassert_true((ret < 0), "NULL pointer returned a value >= 0.");
73+
zassert_equal(ret, -EINVAL, "NULL pointer returned wrong error.");
74+
}
75+
76+
#if IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_RUNTIME)
77+
78+
ZTEST(nrf_cloud_client_id_test, test_03_nrf_cloud_client_id_get_rt_not_set)
79+
{
80+
int ret;
81+
char buf[NRF_CLOUD_CLIENT_ID_MAX_LEN + 2];
82+
size_t len;
83+
84+
len = NRF_CLOUD_CLIENT_ID_MAX_LEN;
85+
ret = nrf_cloud_client_id_get(buf, len);
86+
zassert_true((ret < 0), "Unset runtime ID returned a value >= 0.");
87+
zassert_equal(ret, -ENXIO, "Wrong error returned when runtime ID not set.");
88+
}
89+
90+
ZTEST(nrf_cloud_client_id_test, test_04_nrf_cloud_client_id_set_rt_len_zero)
91+
{
92+
int ret;
93+
94+
ret = nrf_cloud_client_id_runtime_set("");
95+
zassert_equal(ret, -ENODATA, "Wrong error returned when empty runtime ID set.");
96+
}
97+
98+
ZTEST(nrf_cloud_client_id_test, test_05_nrf_cloud_client_id_set_rt_too_big)
99+
{
100+
int ret;
101+
char buf[NRF_CLOUD_CLIENT_ID_MAX_LEN + 2];
102+
103+
memset(buf, 'A', NRF_CLOUD_CLIENT_ID_MAX_LEN + 1);
104+
buf[NRF_CLOUD_CLIENT_ID_MAX_LEN + 1] = '\0';
105+
ret = nrf_cloud_client_id_runtime_set(buf);
106+
zassert_equal(ret, -EINVAL, "Wrong error returned when too large runtime ID set.");
107+
}
108+
109+
ZTEST(nrf_cloud_client_id_test, test_06_nrf_cloud_client_id_set_rt_good)
110+
{
111+
int ret;
112+
113+
ret = nrf_cloud_client_id_runtime_set(RUNTIME_ID);
114+
zassert_equal(ret, 0, "Unexpected error when setting runtime client id");
115+
}
116+
117+
#endif /* CONFIG_NRF_CLOUD_CLIENT_ID_SRC_RUNTIME */
118+
119+
ZTEST(nrf_cloud_client_id_test, test_07_nrf_cloud_client_id_get_too_small)
120+
{
121+
int ret;
122+
char buf[NRF_CLOUD_CLIENT_ID_MAX_LEN + 2];
123+
size_t len;
124+
125+
len = 1;
126+
ret = nrf_cloud_client_id_get(buf, len);
127+
zassert_true((ret < 0), "Too-small buffer returned a value >= 0.");
128+
zassert_equal(ret, -EMSGSIZE, "Wrong error returned with too-small buffer.");
129+
}
130+
131+
ZTEST(nrf_cloud_client_id_test, test_08_nrf_cloud_client_id_get_good)
132+
{
133+
int ret;
134+
char buf[NRF_CLOUD_CLIENT_ID_MAX_LEN + 2];
135+
size_t len;
136+
137+
#if IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_RUNTIME)
138+
ret = nrf_cloud_client_id_runtime_set(RUNTIME_ID);
139+
zassert_equal(ret, 0, "Unexpected error when setting runtime client id");
140+
#endif
141+
142+
len = NRF_CLOUD_CLIENT_ID_MAX_LEN;
143+
ret = nrf_cloud_client_id_get(buf, len);
144+
printk("nrf_cloud_client_id_get: ret = %d, id: %.*s\n", ret, len, buf);
145+
zassert_equal(ret, 0, "Unexpected error when getting client id");
146+
147+
#if IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_IMEI)
148+
ret = strncmp(buf, CONFIG_NRF_CLOUD_CLIENT_ID_PREFIX,
149+
strlen(CONFIG_NRF_CLOUD_CLIENT_ID_PREFIX));
150+
zassert_equal(ret, 0, "Unexpected prefix on IMEI client id");
151+
#elif IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_COMPILE_TIME)
152+
ret = strncmp(buf, CONFIG_NRF_CLOUD_CLIENT_ID,
153+
strlen(CONFIG_NRF_CLOUD_CLIENT_ID));
154+
zassert_equal(ret, 0, "Unexpected miscompare on compile time client id");
155+
#elif IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_INTERNAL_UUID)
156+
zassert_equal(strlen(buf), UUID_LEN, "Unexpected length of UUID id");
157+
#elif IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_HW_ID)
158+
/* TODO: check length returned depending on CONFIG_HW_ID_LIBRARY_SOURCE */
159+
#elif IS_ENABLED(CONFIG_NRF_CLOUD_CLIENT_ID_SRC_RUNTIME)
160+
ret = strncmp(buf, RUNTIME_ID, len);
161+
zassert_equal(ret, 0, "Unexpected miscompare on runtime client id");
162+
#endif
163+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
common:
2+
platform_allow: nrf9160dk/nrf9160/ns
3+
integration_platforms:
4+
- nrf9160dk/nrf9160/ns
5+
tags: ci_build nrf_cloud_test nrf_cloud_lib ci_tests_subsys_net
6+
tests:
7+
net.lib.nrf_cloud.client_id.imei:
8+
sysbuild: true
9+
timeout: 60
10+
extra_args:
11+
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_IMEI=y
12+
tags: sysbuild ci_tests_subsys_net
13+
net.lib.nrf_cloud.client_id.runtime:
14+
sysbuild: true
15+
timeout: 60
16+
extra_args:
17+
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_IMEI=n
18+
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_RUNTIME=y
19+
tags: sysbuild ci_tests_subsys_net
20+
net.lib.nrf_cloud.client_id.uuid:
21+
sysbuild: true
22+
timeout: 60
23+
extra_args:
24+
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_IMEI=n
25+
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_INTERNAL_UUID=y
26+
- CONFIG_MODEM_JWT=y
27+
tags: sysbuild ci_tests_subsys_net
28+
net.lib.nrf_cloud.client_id.hwid:
29+
sysbuild: true
30+
timeout: 60
31+
extra_args:
32+
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_IMEI=n
33+
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_HW_ID=y
34+
- CONFIG_HW_ID_LIBRARY=y
35+
tags: sysbuild ci_tests_subsys_net
36+
net.lib.nrf_cloud.client_id.comptime:
37+
sysbuild: true
38+
timeout: 60
39+
extra_args:
40+
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_IMEI=n
41+
- CONFIG_NRF_CLOUD_CLIENT_ID_SRC_COMPILE_TIME=y
42+
tags: sysbuild ci_tests_subsys_net

0 commit comments

Comments
 (0)