tests: lwm2m: Add more tests to RD client

Add more test to RD client state machine.

Signed-off-by: Seppo Takalo <seppo.takalo@nordicsemi.no>
This commit is contained in:
Seppo Takalo 2023-04-11 16:13:22 +03:00 committed by Carles Cufí
parent 62bbdd59de
commit 49857cdae4
3 changed files with 178 additions and 9 deletions

View file

@ -25,6 +25,8 @@ add_compile_definitions(CONFIG_LWM2M_COAP_BLOCK_SIZE=256)
add_compile_definitions(CONFIG_LWM2M_COAP_MAX_MSG_SIZE=512)
add_compile_definitions(CONFIG_LWM2M_ENGINE_DEFAULT_LIFETIME=60)
add_compile_definitions(CONFIG_LWM2M_SECURITY_INSTANCE_COUNT=1)
add_compile_definitions(CONFIG_LWM2M_SECONDS_TO_UPDATE_EARLY=30)
add_compile_definitions(CONFIG_LWM2M_QUEUE_MODE_UPTIME=30)
add_compile_definitions(CONFIG_LWM2M_SECONDS_TO_UPDATE_EARLY=10)
add_compile_definitions(CONFIG_LWM2M_QUEUE_MODE_UPTIME=10)
add_compile_definitions(CONFIG_LWM2M_LOG_LEVEL=4)
add_compile_definitions(CONFIG_LWM2M_QUEUE_MODE_ENABLED=1)
add_compile_definitions(CONFIG_LWM2M_TLS_SESSION_CACHING=1)

View file

@ -27,17 +27,26 @@ FAKE_VOID_FUNC(show_lwm2m_observe, enum lwm2m_observe_event);
bool check_lwm2m_rd_client_event(uint8_t expected_val, uint8_t arg_index)
{
int max_service_iterations = RD_CLIENT_MAX_LOOKUP_ITERATIONS;
bool match = false;
while (max_service_iterations > 0) {
if (show_lwm2m_event_fake.call_count > arg_index) {
return show_lwm2m_event_fake.arg0_history[arg_index] == expected_val;
match = show_lwm2m_event_fake.arg0_history[arg_index] == expected_val;
break;
}
wait_for_service(1);
max_service_iterations--;
}
return false;
if (!match) {
LOG_INF("Expecting %d at %d, events:", expected_val, arg_index);
for (int i = 0; i < show_lwm2m_event_fake.call_count; i++) {
LOG_INF("[%d] = %d", i, show_lwm2m_event_fake.arg0_history[i]);
}
}
return match;
}
bool check_lwm2m_observe_event(uint8_t expected_val, uint8_t arg_index)
@ -82,6 +91,8 @@ static void lwm2m_event_cb(struct lwm2m_ctx *client, enum lwm2m_rd_client_event
case LWM2M_RD_CLIENT_EVENT_NETWORK_ERROR:
LOG_INF("**** LWM2M_RD_CLIENT_EVENT_NETWORK_ERROR");
break;
case LWM2M_RD_CLIENT_EVENT_QUEUE_MODE_RX_OFF:
LOG_INF("**** LWM2M_RD_CLIENT_EVENT_QUEUE_MODE_RX_OFF");
default:
break;
}
@ -168,12 +179,45 @@ ZTEST(lwm2m_rd_client, test_start_registration_ok)
coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
NULL);
zassert(lwm2m_rd_client_ctx() == &ctx, "");
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE, 0),
NULL);
zassert_true(lwm2m_rd_client_is_registred(&ctx), NULL);
coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_deleted;
zassert_true(lwm2m_rd_client_stop(&ctx, lwm2m_event_cb, true) == 0, NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_DISCONNECT, 1), NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_DISCONNECT, 2), NULL);
zassert_true(!lwm2m_rd_client_is_registred(&ctx), NULL);
}
ZTEST(lwm2m_rd_client, test_timeout_resume_registration)
{
struct lwm2m_ctx ctx;
(void)memset(&ctx, 0x0, sizeof(ctx));
test_prepare_pending_message_cb(&message_reply_cb_default);
lwm2m_engine_add_service_fake.custom_fake = lwm2m_engine_add_service_fake_default;
lwm2m_rd_client_init();
test_lwm2m_engine_start_service();
wait_for_service(1);
lwm2m_get_bool_fake.custom_fake = lwm2m_get_bool_fake_default;
lwm2m_sprint_ip_addr_fake.custom_fake = lwm2m_sprint_ip_addr_fake_default;
lwm2m_init_message_fake.custom_fake = lwm2m_init_message_fake_default;
coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_created;
coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
NULL);
zassert(lwm2m_rd_client_ctx() == &ctx, "");
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE, 0),
NULL);
zassert(lwm2m_rd_client_timeout(&ctx) == 0, "");
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE, 2),
NULL);
}
ZTEST(lwm2m_rd_client, test_start_registration_timeout)
@ -244,7 +288,36 @@ ZTEST(lwm2m_rd_client, test_start_registration_update)
NULL);
lwm2m_rd_client_update();
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE, 2),
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE, 3),
NULL);
}
ZTEST(lwm2m_rd_client, test_rx_off)
{
struct lwm2m_ctx ctx;
(void)memset(&ctx, 0x0, sizeof(ctx));
test_prepare_pending_message_cb(&message_reply_cb_default);
lwm2m_engine_add_service_fake.custom_fake = lwm2m_engine_add_service_fake_default;
lwm2m_rd_client_init();
test_lwm2m_engine_start_service();
wait_for_service(1);
lwm2m_get_bool_fake.custom_fake = lwm2m_get_bool_fake_default;
lwm2m_sprint_ip_addr_fake.custom_fake = lwm2m_sprint_ip_addr_fake_default;
lwm2m_init_message_fake.custom_fake = lwm2m_init_message_fake_default;
coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_created;
coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE, 0),
NULL);
engine_update_tx_time();
k_sleep(K_SECONDS(15));
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_QUEUE_MODE_RX_OFF, 1),
NULL);
}
@ -274,10 +347,72 @@ ZTEST(lwm2m_rd_client, test_start_registration_update_fail)
RESET_FAKE(coap_header_get_code);
lwm2m_rd_client_update();
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE, 2),
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE, 3),
NULL);
}
ZTEST(lwm2m_rd_client, test_registration_update_timeout)
{
struct lwm2m_ctx ctx;
(void)memset(&ctx, 0x0, sizeof(ctx));
test_prepare_pending_message_cb(&message_reply_cb_default);
lwm2m_engine_add_service_fake.custom_fake = lwm2m_engine_add_service_fake_default;
lwm2m_rd_client_init();
test_lwm2m_engine_start_service();
wait_for_service(1);
lwm2m_get_bool_fake.custom_fake = lwm2m_get_bool_fake_default;
lwm2m_sprint_ip_addr_fake.custom_fake = lwm2m_sprint_ip_addr_fake_default;
lwm2m_init_message_fake.custom_fake = lwm2m_init_message_fake_default;
coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_created;
coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE, 0),
NULL);
test_prepare_pending_message_cb(&message_reply_timeout_cb_default);
lwm2m_rd_client_update();
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE, 2));
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_TIMEOUT, 3),
NULL);
test_prepare_pending_message_cb(&message_reply_cb_default);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE, 4),
NULL);
}
ZTEST(lwm2m_rd_client, test_deregistration_timeout)
{
struct lwm2m_ctx ctx;
(void)memset(&ctx, 0x0, sizeof(ctx));
test_prepare_pending_message_cb(&message_reply_cb_default);
lwm2m_engine_add_service_fake.custom_fake = lwm2m_engine_add_service_fake_default;
lwm2m_rd_client_init();
test_lwm2m_engine_start_service();
wait_for_service(1);
lwm2m_get_bool_fake.custom_fake = lwm2m_get_bool_fake_default;
lwm2m_sprint_ip_addr_fake.custom_fake = lwm2m_sprint_ip_addr_fake_default;
lwm2m_init_message_fake.custom_fake = lwm2m_init_message_fake_default;
coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_created;
coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE, 0),
NULL);
test_prepare_pending_message_cb(&message_reply_timeout_cb_default);
zassert_true(lwm2m_rd_client_stop(&ctx, lwm2m_event_cb, true) == 0, NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_DEREGISTER_FAILURE, 1));
}
ZTEST(lwm2m_rd_client, test_error_on_registration_update)
{
struct lwm2m_ctx ctx;
@ -351,11 +486,43 @@ ZTEST(lwm2m_rd_client, test_suspend_resume_registration)
NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE, 0),
NULL);
zassert_true(!lwm2m_rd_client_is_suspended(&ctx), NULL);
zassert_true(lwm2m_rd_client_pause() == 0, NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_ENGINE_SUSPENDED, 1), NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_ENGINE_SUSPENDED, 2), NULL);
zassert_true(lwm2m_rd_client_is_suspended(&ctx), NULL);
zassert_true(lwm2m_rd_client_resume() == 0, NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE, 4),
NULL);
zassert_true(!lwm2m_rd_client_is_suspended(&ctx), NULL);
}
ZTEST(lwm2m_rd_client, test_socket_error)
{
struct lwm2m_ctx ctx;
(void)memset(&ctx, 0x0, sizeof(ctx));
test_prepare_pending_message_cb(&message_reply_cb_default);
lwm2m_engine_add_service_fake.custom_fake = lwm2m_engine_add_service_fake_default;
lwm2m_rd_client_init();
test_lwm2m_engine_start_service();
wait_for_service(1);
lwm2m_get_bool_fake.custom_fake = lwm2m_get_bool_fake_default;
lwm2m_sprint_ip_addr_fake.custom_fake = lwm2m_sprint_ip_addr_fake_default;
lwm2m_init_message_fake.custom_fake = lwm2m_init_message_fake_default;
coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_created;
coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE, 0),
NULL);
ctx.fault_cb(EIO);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE, 2), NULL);
zassert_true(check_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE, 3),
NULL);
}

View file

@ -19,7 +19,7 @@
/* Number of iterations the state machine within the RDClient service
* is triggered
*/
static const uint8_t RD_CLIENT_MAX_SERVICE_ITERATIONS = 50;
static const uint16_t RD_CLIENT_MAX_SERVICE_ITERATIONS = UINT16_MAX;
/* zephyr/net/coap.h */
DECLARE_FAKE_VALUE_FUNC(uint8_t, coap_header_get_code, const struct coap_packet *);