|
| 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 | +} |
0 commit comments