1 /*
2 * Copyright (c) 2022 grandcentrix GmbH
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "stubs.h"
8
9 #include <zephyr/fff.h>
10 #include <zephyr/logging/log.h>
11 #include <zephyr/ztest.h>
12 #if defined(CONFIG_NATIVE_SIM_SLOWDOWN_TO_REAL_TIME)
13 #include "nsi_timer_model.h"
14 #endif
15 #include <lwm2m_rd_client.h>
16
17 LOG_MODULE_REGISTER(lwm2m_rd_client_test);
18
19 DEFINE_FFF_GLOBALS;
20
21 /* Maximum number of iterations within the state machine of RD Client
22 * service that is waited for until a possible event occurs
23 */
24 #define RD_CLIENT_MAX_LOOKUP_ITERATIONS 500
25
26 FAKE_VOID_FUNC(show_lwm2m_event, enum lwm2m_rd_client_event);
27 FAKE_VOID_FUNC(show_lwm2m_observe, enum lwm2m_observe_event);
28
29 static int next_event;
30
expect_lwm2m_rd_client_event(uint8_t expected_val)31 bool expect_lwm2m_rd_client_event(uint8_t expected_val)
32 {
33 int max_service_iterations = RD_CLIENT_MAX_LOOKUP_ITERATIONS;
34 bool match = false;
35
36 while (max_service_iterations > 0) {
37 for (int i = next_event; i < show_lwm2m_event_fake.call_count; i++) {
38 match = show_lwm2m_event_fake.arg0_history[i] == expected_val;
39 if (match) {
40 next_event = i + 1;
41 break;
42 }
43 }
44
45 if (match) {
46 break;
47 }
48 wait_for_service(1);
49 max_service_iterations--;
50 }
51
52 if (!match) {
53 LOG_INF("Expecting event %d, events:", expected_val);
54 for (int i = next_event; i < show_lwm2m_event_fake.call_count; i++) {
55 LOG_INF("[%d] = %d", i, show_lwm2m_event_fake.arg0_history[i]);
56 }
57 }
58
59 return match;
60 }
61
lwm2m_event_cb(struct lwm2m_ctx * client,enum lwm2m_rd_client_event client_event)62 static void lwm2m_event_cb(struct lwm2m_ctx *client, enum lwm2m_rd_client_event client_event)
63 {
64 ARG_UNUSED(client);
65
66 switch (client_event) {
67 case LWM2M_RD_CLIENT_EVENT_NONE:
68 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_NONE");
69 break;
70 case LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_REG_FAILURE:
71 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_REG_FAILURE");
72 break;
73 case LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_REG_COMPLETE:
74 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_REG_COMPLETE");
75 break;
76 case LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_TRANSFER_COMPLETE:
77 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_TRANSFER_COMPLETE");
78 break;
79 case LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE:
80 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE");
81 break;
82 case LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE:
83 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE");
84 break;
85 case LWM2M_RD_CLIENT_EVENT_REG_TIMEOUT:
86 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_REG_TIMEOUT");
87 break;
88 case LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE:
89 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE");
90 break;
91 case LWM2M_RD_CLIENT_EVENT_DEREGISTER_FAILURE:
92 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_DEREGISTER_FAILURE");
93 break;
94 case LWM2M_RD_CLIENT_EVENT_DISCONNECT:
95 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_DISCONNECT");
96 break;
97 case LWM2M_RD_CLIENT_EVENT_QUEUE_MODE_RX_OFF:
98 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_QUEUE_MODE_RX_OFF");
99 break;
100 case LWM2M_RD_CLIENT_EVENT_SERVER_DISABLED:
101 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_SERVER_DISABLED");
102 break;
103 case LWM2M_RD_CLIENT_EVENT_ENGINE_SUSPENDED:
104 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_ENGINE_SUSPENDED");
105 break;
106 case LWM2M_RD_CLIENT_EVENT_NETWORK_ERROR:
107 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_NETWORK_ERROR");
108 break;
109 case LWM2M_RD_CLIENT_EVENT_REG_UPDATE:
110 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_REG_UPDATE");
111 break;
112 case LWM2M_RD_CLIENT_EVENT_DEREGISTER:
113 LOG_INF("*** LWM2M_RD_CLIENT_EVENT_DEREGISTER");
114 break;
115 }
116
117 show_lwm2m_event(client_event);
118 }
119
lwm2m_observe_cb(enum lwm2m_observe_event event,struct lwm2m_obj_path * path,void * user_data)120 static void lwm2m_observe_cb(enum lwm2m_observe_event event, struct lwm2m_obj_path *path,
121 void *user_data)
122 {
123 switch (event) {
124 case LWM2M_OBSERVE_EVENT_OBSERVER_ADDED:
125 LOG_INF("**** LWM2M_OBSERVE_EVENT_OBSERVER_ADDED");
126 break;
127 case LWM2M_OBSERVE_EVENT_NOTIFY_TIMEOUT:
128 LOG_INF("**** LWM2M_OBSERVE_EVENT_NOTIFY_TIMEOUT");
129 break;
130 case LWM2M_OBSERVE_EVENT_OBSERVER_REMOVED:
131 LOG_INF("**** LWM2M_OBSERVE_EVENT_OBSERVER_REMOVED");
132 break;
133 case LWM2M_OBSERVE_EVENT_NOTIFY_ACK:
134 LOG_INF("**** LWM2M_OBSERVE_EVENT_NOTIFY_ACK");
135 break;
136 default:
137 break;
138 }
139
140 show_lwm2m_observe(event);
141 }
142
143 #define FFF_FAKES_LIST(FAKE)
144
my_suite_before(void * data)145 static void my_suite_before(void *data)
146 {
147 #if defined(CONFIG_NATIVE_SIM_SLOWDOWN_TO_REAL_TIME)
148 /* It is enough that some slow-down is happening on sleeps, it does not have to be
149 * real time
150 */
151 hwtimer_set_rt_ratio(100.0);
152 #endif
153 /* Register resets */
154 DO_FOREACH_FAKE(RESET_FAKE);
155
156 /* reset common FFF internal structures */
157 FFF_RESET_HISTORY();
158
159 RESET_FAKE(show_lwm2m_event);
160 RESET_FAKE(show_lwm2m_observe);
161 next_event = 0;
162
163 /* Common stubs for all tests */
164 get_u32_val = CONFIG_LWM2M_ENGINE_DEFAULT_LIFETIME;
165 lwm2m_get_u32_fake.custom_fake = lwm2m_get_u32_val;
166 lwm2m_get_bool_fake.custom_fake = lwm2m_get_bool_fake_default;
167 lwm2m_sprint_ip_addr_fake.custom_fake = lwm2m_sprint_ip_addr_fake_default;
168 lwm2m_init_message_fake.custom_fake = lwm2m_init_message_fake_default;
169 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_created;
170 coap_packet_append_option_fake.custom_fake = NULL;
171 stub_lwm2m_server_disable(false);
172 }
173
my_suite_after(void * data)174 static void my_suite_after(void *data)
175 {
176 test_lwm2m_engine_stop_service();
177 }
178
message_reply_cb_default(struct lwm2m_message * msg)179 void message_reply_cb_default(struct lwm2m_message *msg)
180 {
181 struct coap_packet response;
182 struct coap_reply reply;
183 struct sockaddr from;
184
185 memset(&response, 0, sizeof(struct coap_packet));
186 memset(&reply, 0, sizeof(struct coap_reply));
187 memset(&from, 0, sizeof(struct sockaddr));
188
189 msg->reply_cb(&response, &reply, &from);
190 }
191
message_reply_timeout_cb_default(struct lwm2m_message * msg)192 void message_reply_timeout_cb_default(struct lwm2m_message *msg)
193 {
194 msg->message_timeout_cb(msg);
195 }
196
197 ZTEST_SUITE(lwm2m_rd_client, NULL, NULL, my_suite_before, my_suite_after, NULL);
198
ZTEST(lwm2m_rd_client,test_start_registration_ok)199 ZTEST(lwm2m_rd_client, test_start_registration_ok)
200 {
201 struct lwm2m_ctx ctx;
202
203 (void)memset(&ctx, 0x0, sizeof(ctx));
204
205 lwm2m_rd_client_init();
206 test_lwm2m_engine_start_service();
207 wait_for_service(1);
208
209 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
210 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
211 NULL);
212 test_prepare_pending_message_cb(&message_reply_cb_default);
213 zassert(lwm2m_rd_client_ctx() == &ctx, "");
214 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
215 NULL);
216 zassert_true(lwm2m_rd_client_is_registred(&ctx), NULL);
217
218 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_deleted;
219 zassert_true(lwm2m_rd_client_stop(&ctx, lwm2m_event_cb, true) == 0, NULL);
220 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_DEREGISTER), NULL);
221 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_DISCONNECT), NULL);
222 zassert_true(!lwm2m_rd_client_is_registred(&ctx), NULL);
223 }
224
ZTEST(lwm2m_rd_client,test_register_update_too_small_lifetime_to_default)225 ZTEST(lwm2m_rd_client, test_register_update_too_small_lifetime_to_default)
226 {
227 struct lwm2m_ctx ctx;
228
229 get_u32_val = CONFIG_LWM2M_ENGINE_DEFAULT_LIFETIME / 2;
230 lwm2m_get_u32_fake.custom_fake = lwm2m_get_u32_val;
231
232 (void)memset(&ctx, 0x0, sizeof(ctx));
233
234 lwm2m_rd_client_init();
235 test_lwm2m_engine_start_service();
236 wait_for_service(1);
237
238 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
239 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
240 NULL);
241 test_prepare_pending_message_cb(&message_reply_cb_default);
242 zassert(lwm2m_rd_client_ctx() == &ctx, "");
243 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
244 NULL);
245 zassert_equal(lwm2m_set_u32_fake.arg1_val, CONFIG_LWM2M_ENGINE_DEFAULT_LIFETIME);
246 }
247
ZTEST(lwm2m_rd_client,test_timeout_resume_registration)248 ZTEST(lwm2m_rd_client, test_timeout_resume_registration)
249 {
250 struct lwm2m_ctx ctx;
251
252 (void)memset(&ctx, 0x0, sizeof(ctx));
253
254 lwm2m_rd_client_init();
255 test_lwm2m_engine_start_service();
256
257 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
258 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
259 NULL);
260 test_prepare_pending_message_cb(&message_reply_cb_default);
261 zassert(lwm2m_rd_client_ctx() == &ctx, "");
262 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
263 NULL);
264
265 zassert(lwm2m_rd_client_timeout(&ctx) == 0, "");
266 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
267 NULL);
268
269 }
270
ZTEST(lwm2m_rd_client,test_start_registration_timeout)271 ZTEST(lwm2m_rd_client, test_start_registration_timeout)
272 {
273 struct lwm2m_ctx ctx;
274
275 (void)memset(&ctx, 0x0, sizeof(ctx));
276
277 lwm2m_rd_client_init();
278 test_lwm2m_engine_start_service();
279 wait_for_service(1);
280
281 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
282 NULL);
283 test_prepare_pending_message_cb(&message_reply_timeout_cb_default);
284 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_TIMEOUT), NULL);
285 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_TIMEOUT), NULL);
286 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_TIMEOUT), NULL);
287 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_NETWORK_ERROR), NULL);
288 }
289
ZTEST(lwm2m_rd_client,test_start_registration_fail)290 ZTEST(lwm2m_rd_client, test_start_registration_fail)
291 {
292 struct lwm2m_ctx ctx;
293
294 (void)memset(&ctx, 0x0, sizeof(ctx));
295
296 lwm2m_rd_client_init();
297 test_lwm2m_engine_start_service();
298 wait_for_service(1);
299
300 lwm2m_get_bool_fake.custom_fake = lwm2m_get_bool_fake_default;
301 lwm2m_sprint_ip_addr_fake.custom_fake = lwm2m_sprint_ip_addr_fake_default;
302 lwm2m_init_message_fake.custom_fake = lwm2m_init_message_fake_default;
303 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
304 NULL);
305 test_prepare_pending_message_cb(&message_reply_cb_default);
306 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE),
307 NULL);
308 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE),
309 NULL);
310 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE),
311 NULL);
312 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_NETWORK_ERROR),
313 NULL);
314 }
315
ZTEST(lwm2m_rd_client,test_start_registration_update)316 ZTEST(lwm2m_rd_client, test_start_registration_update)
317 {
318 struct lwm2m_ctx ctx;
319
320 (void)memset(&ctx, 0x0, sizeof(ctx));
321
322 lwm2m_rd_client_init();
323 test_lwm2m_engine_start_service();
324 wait_for_service(1);
325
326 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
327 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
328 NULL);
329 test_prepare_pending_message_cb(&message_reply_cb_default);
330 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
331 NULL);
332
333 lwm2m_rd_client_update();
334 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE),
335 NULL);
336 }
337
ZTEST(lwm2m_rd_client,test_rx_off)338 ZTEST(lwm2m_rd_client, test_rx_off)
339 {
340 struct lwm2m_ctx ctx;
341
342 (void)memset(&ctx, 0x0, sizeof(ctx));
343
344 lwm2m_rd_client_init();
345 test_lwm2m_engine_start_service();
346 wait_for_service(1);
347
348 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
349 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
350 NULL);
351 test_prepare_pending_message_cb(&message_reply_cb_default);
352 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
353 NULL);
354
355 engine_update_tx_time();
356 k_sleep(K_SECONDS(15));
357 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_QUEUE_MODE_RX_OFF),
358 NULL);
359 }
360
ZTEST(lwm2m_rd_client,test_start_registration_update_fail)361 ZTEST(lwm2m_rd_client, test_start_registration_update_fail)
362 {
363 struct lwm2m_ctx ctx;
364
365 (void)memset(&ctx, 0x0, sizeof(ctx));
366
367 lwm2m_rd_client_init();
368 test_lwm2m_engine_start_service();
369 wait_for_service(1);
370
371 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
372 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
373 NULL);
374 test_prepare_pending_message_cb(&message_reply_cb_default);
375 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
376 NULL);
377
378 RESET_FAKE(coap_header_get_code);
379
380 lwm2m_rd_client_update();
381 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_FAILURE),
382 NULL);
383 }
384
ZTEST(lwm2m_rd_client,test_registration_update_timeout)385 ZTEST(lwm2m_rd_client, test_registration_update_timeout)
386 {
387 struct lwm2m_ctx ctx;
388
389 (void)memset(&ctx, 0x0, sizeof(ctx));
390
391 lwm2m_rd_client_init();
392 test_lwm2m_engine_start_service();
393 wait_for_service(1);
394
395 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
396 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
397 NULL);
398 test_prepare_pending_message_cb(&message_reply_cb_default);
399 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
400 NULL);
401 test_prepare_pending_message_cb(&message_reply_timeout_cb_default);
402 ctx.sock_fd = 100;
403 lwm2m_rd_client_update();
404 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE));
405 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_TIMEOUT),
406 NULL);
407 zassert_true(lwm2m_engine_stop_fake.call_count >= 1);
408 zassert_equal(lwm2m_engine_stop_fake.arg0_val, &ctx);
409
410 test_prepare_pending_message_cb(&message_reply_cb_default);
411 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
412 NULL);
413 }
414
ZTEST(lwm2m_rd_client,test_deregistration_timeout)415 ZTEST(lwm2m_rd_client, test_deregistration_timeout)
416 {
417 struct lwm2m_ctx ctx;
418
419 (void)memset(&ctx, 0x0, sizeof(ctx));
420
421 lwm2m_rd_client_init();
422 test_lwm2m_engine_start_service();
423 wait_for_service(1);
424
425 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
426 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
427 NULL);
428 test_prepare_pending_message_cb(&message_reply_cb_default);
429 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
430 NULL);
431
432 test_prepare_pending_message_cb(&message_reply_timeout_cb_default);
433 zassert_true(lwm2m_rd_client_stop(&ctx, lwm2m_event_cb, true) == 0, NULL);
434 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_DEREGISTER), NULL);
435 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_DEREGISTER_FAILURE));
436 }
437
ZTEST(lwm2m_rd_client,test_error_on_registration_update)438 ZTEST(lwm2m_rd_client, test_error_on_registration_update)
439 {
440 struct lwm2m_ctx ctx;
441
442 (void)memset(&ctx, 0x0, sizeof(ctx));
443
444 lwm2m_rd_client_init();
445 test_lwm2m_engine_start_service();
446 wait_for_service(1);
447
448 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
449 coap_packet_append_option_fake.custom_fake = NULL;
450 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
451 NULL);
452 test_prepare_pending_message_cb(&message_reply_cb_default);
453
454 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
455 NULL);
456
457 coap_packet_append_option_fake.custom_fake = coap_packet_append_option_fake_err;
458 lwm2m_rd_client_update();
459 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
460 NULL);
461 }
462
ZTEST(lwm2m_rd_client,test_network_error_on_registration)463 ZTEST(lwm2m_rd_client, test_network_error_on_registration)
464 {
465 struct lwm2m_ctx ctx;
466
467 (void)memset(&ctx, 0x0, sizeof(ctx));
468
469 lwm2m_rd_client_init();
470 test_lwm2m_engine_start_service();
471 wait_for_service(1);
472
473 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
474
475 coap_packet_append_option_fake.custom_fake = coap_packet_append_option_fake_err;
476 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
477 NULL);
478 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_NETWORK_ERROR), NULL);
479 }
480
ZTEST(lwm2m_rd_client,test_suspend_resume_registration)481 ZTEST(lwm2m_rd_client, test_suspend_resume_registration)
482 {
483 struct lwm2m_ctx ctx;
484
485 (void)memset(&ctx, 0x0, sizeof(ctx));
486
487 lwm2m_rd_client_init();
488 test_lwm2m_engine_start_service();
489 wait_for_service(1);
490
491 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
492 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
493 NULL);
494 test_prepare_pending_message_cb(&message_reply_cb_default);
495 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
496 NULL);
497 zassert_true(!lwm2m_rd_client_is_suspended(&ctx), NULL);
498
499 zassert_true(lwm2m_rd_client_pause() == 0, NULL);
500 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_ENGINE_SUSPENDED), NULL);
501 zassert_true(lwm2m_rd_client_is_suspended(&ctx), NULL);
502
503 zassert_true(lwm2m_rd_client_resume() == 0, NULL);
504 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE),
505 NULL);
506 zassert_true(!lwm2m_rd_client_is_suspended(&ctx), NULL);
507
508 zassert_true(lwm2m_rd_client_pause() == 0, NULL);
509 k_sleep(K_SECONDS(CONFIG_LWM2M_ENGINE_DEFAULT_LIFETIME));
510 zassert_true(lwm2m_rd_client_resume() == 0, NULL);
511 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
512 NULL);
513 }
514
ZTEST(lwm2m_rd_client,test_suspend_stop_resume)515 ZTEST(lwm2m_rd_client, test_suspend_stop_resume)
516 {
517 struct lwm2m_ctx ctx;
518
519 (void)memset(&ctx, 0x0, sizeof(ctx));
520
521 lwm2m_rd_client_init();
522 test_lwm2m_engine_start_service();
523 wait_for_service(1);
524
525 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_created;
526 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
527 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
528 NULL);
529 test_prepare_pending_message_cb(&message_reply_cb_default);
530 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
531 NULL);
532 zassert_true(lwm2m_rd_client_pause() == 0, NULL);
533 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_ENGINE_SUSPENDED), NULL);
534
535 zassert_equal(lwm2m_rd_client_stop(&ctx, lwm2m_event_cb, false), 0);
536 zassert_true(lwm2m_rd_client_resume() == 0, NULL);
537 zassert_false(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_DEREGISTER), NULL);
538 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_DISCONNECT), NULL);
539 }
540
ZTEST(lwm2m_rd_client,test_socket_error)541 ZTEST(lwm2m_rd_client, test_socket_error)
542 {
543 struct lwm2m_ctx ctx;
544
545 (void)memset(&ctx, 0x0, sizeof(ctx));
546
547 lwm2m_rd_client_init();
548 test_lwm2m_engine_start_service();
549 wait_for_service(1);
550
551 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_created;
552 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
553 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
554 NULL);
555 test_prepare_pending_message_cb(&message_reply_cb_default);
556 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
557 NULL);
558
559 ctx.fault_cb(EIO);
560 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE), NULL);
561 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_UPDATE_COMPLETE),
562 NULL);
563 }
564
ZTEST(lwm2m_rd_client,test_socket_error_on_stop)565 ZTEST(lwm2m_rd_client, test_socket_error_on_stop)
566 {
567 struct lwm2m_ctx ctx;
568
569 (void)memset(&ctx, 0x0, sizeof(ctx));
570
571 lwm2m_rd_client_init();
572 test_lwm2m_engine_start_service();
573 wait_for_service(1);
574
575 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_created;
576 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
577 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
578 NULL);
579 test_prepare_pending_message_cb(&message_reply_cb_default);
580 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
581 NULL);
582
583 test_prepare_pending_message_cb(NULL);
584 zassert_equal(lwm2m_rd_client_stop(&ctx, lwm2m_event_cb, true), 0);
585 k_msleep(1000);
586 ctx.fault_cb(EIO);
587 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_DEREGISTER_FAILURE),
588 NULL);
589 }
590
ZTEST(lwm2m_rd_client,test_no_context)591 ZTEST(lwm2m_rd_client, test_no_context)
592 {
593 struct lwm2m_ctx ctx;
594
595 lwm2m_rd_client_init();
596 zassert_equal(lwm2m_rd_client_stop(&ctx, NULL, false), -EPERM);
597 zassert_equal(lwm2m_rd_client_pause(), -EPERM);
598 zassert_equal(lwm2m_rd_client_resume(), -EPERM);
599 zassert_equal(lwm2m_rd_client_connection_resume(&ctx), -EPERM);
600 zassert_equal(lwm2m_rd_client_timeout(&ctx), -EPERM);
601 }
602
ZTEST(lwm2m_rd_client,test_engine_trigger_bootstrap)603 ZTEST(lwm2m_rd_client, test_engine_trigger_bootstrap)
604 {
605 struct lwm2m_ctx ctx;
606
607 (void)memset(&ctx, 0x0, sizeof(ctx));
608
609 lwm2m_rd_client_init();
610 test_lwm2m_engine_start_service();
611 wait_for_service(1);
612
613 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
614 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
615 NULL);
616 test_prepare_pending_message_cb(&message_reply_cb_default);
617 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
618 NULL);
619 lwm2m_get_bool_fake.custom_fake = lwm2m_get_bool_fake_true;
620 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_changed;
621 zassert_equal(engine_trigger_bootstrap(), 0);
622 zassert_equal(engine_trigger_bootstrap(), -EPERM);
623 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_REG_COMPLETE),
624 NULL);
625 engine_bootstrap_finish();
626 zassert_equal(
627 expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_TRANSFER_COMPLETE),
628 true);
629 }
630
ZTEST(lwm2m_rd_client,test_bootstrap_timeout)631 ZTEST(lwm2m_rd_client, test_bootstrap_timeout)
632 {
633
634 struct lwm2m_ctx ctx;
635
636 (void)memset(&ctx, 0x0, sizeof(ctx));
637
638 lwm2m_rd_client_init();
639 test_lwm2m_engine_start_service();
640 wait_for_service(1);
641
642 lwm2m_get_bool_fake.custom_fake = lwm2m_get_bool_fake_true;
643 lwm2m_sprint_ip_addr_fake.custom_fake = lwm2m_sprint_ip_addr_fake_default;
644 lwm2m_init_message_fake.custom_fake = lwm2m_init_message_fake_default;
645 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_created;
646 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
647 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 1, lwm2m_event_cb, lwm2m_observe_cb) == 0,
648 NULL);
649 test_prepare_pending_message_cb(&message_reply_timeout_cb_default);
650 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_REG_FAILURE),
651 NULL);
652 }
653
ZTEST(lwm2m_rd_client,test_bootstrap_fail)654 ZTEST(lwm2m_rd_client, test_bootstrap_fail)
655 {
656 struct lwm2m_ctx ctx;
657
658 (void)memset(&ctx, 0x0, sizeof(ctx));
659
660 lwm2m_rd_client_init();
661 test_lwm2m_engine_start_service();
662 wait_for_service(1);
663
664 lwm2m_get_bool_fake.custom_fake = lwm2m_get_bool_fake_true;
665 lwm2m_sprint_ip_addr_fake.custom_fake = lwm2m_sprint_ip_addr_fake_default;
666 lwm2m_init_message_fake.custom_fake = lwm2m_init_message_fake_default;
667 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_bad_request;
668 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
669 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 1, lwm2m_event_cb, lwm2m_observe_cb) == 0,
670 NULL);
671 test_prepare_pending_message_cb(&message_reply_cb_default);
672 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_REG_FAILURE),
673 NULL);
674
675 }
676
ZTEST(lwm2m_rd_client,test_bootstrap_no_srv)677 ZTEST(lwm2m_rd_client, test_bootstrap_no_srv)
678 {
679
680 struct lwm2m_ctx ctx;
681
682 (void)memset(&ctx, 0x0, sizeof(ctx));
683
684 lwm2m_rd_client_init();
685 test_lwm2m_engine_start_service();
686 wait_for_service(1);
687
688 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
689 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 1, lwm2m_event_cb, lwm2m_observe_cb) == 0,
690 NULL);
691 test_prepare_pending_message_cb(&message_reply_cb_default);
692 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_REG_FAILURE),
693 NULL);
694 }
695
ZTEST(lwm2m_rd_client,test_disable_server)696 ZTEST(lwm2m_rd_client, test_disable_server)
697 {
698 struct lwm2m_ctx ctx;
699
700 (void)memset(&ctx, 0x0, sizeof(ctx));
701
702 lwm2m_rd_client_init();
703 test_lwm2m_engine_start_service();
704 wait_for_service(1);
705
706 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
707 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
708 NULL);
709 test_prepare_pending_message_cb(&message_reply_cb_default);
710 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
711 NULL);
712 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_deleted;
713 stub_lwm2m_server_disable(true);
714 lwm2m_rd_client_server_disabled(0);
715 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_SERVER_DISABLED),
716 NULL);
717 }
718
ZTEST(lwm2m_rd_client,test_disable_server_stop)719 ZTEST(lwm2m_rd_client, test_disable_server_stop)
720 {
721 struct lwm2m_ctx ctx;
722
723 (void)memset(&ctx, 0x0, sizeof(ctx));
724
725 lwm2m_rd_client_init();
726 test_lwm2m_engine_start_service();
727 wait_for_service(1);
728
729 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
730 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
731 NULL);
732 test_prepare_pending_message_cb(&message_reply_cb_default);
733 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
734 NULL);
735 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_deleted;
736 stub_lwm2m_server_disable(true);
737 lwm2m_rd_client_server_disabled(0);
738 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_SERVER_DISABLED),
739 NULL);
740 wait_for_service(1);
741 zassert_true(lwm2m_rd_client_stop(&ctx, lwm2m_event_cb, true) == 0, NULL);
742 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_DISCONNECT), NULL);
743 }
744
ZTEST(lwm2m_rd_client,test_disable_server_connect)745 ZTEST(lwm2m_rd_client, test_disable_server_connect)
746 {
747 struct lwm2m_ctx ctx;
748
749 (void)memset(&ctx, 0x0, sizeof(ctx));
750
751 lwm2m_rd_client_init();
752 test_lwm2m_engine_start_service();
753 wait_for_service(1);
754
755 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
756 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
757 NULL);
758 test_prepare_pending_message_cb(&message_reply_cb_default);
759 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
760 NULL);
761 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_deleted;
762 stub_lwm2m_server_disable(true);
763 lwm2m_rd_client_server_disabled(0);
764 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_SERVER_DISABLED),
765 NULL);
766
767 wait_for_service(500);
768
769 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_created;
770 stub_lwm2m_server_disable(false);
771 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
772 NULL);
773 }
774
ZTEST(lwm2m_rd_client,test_fallback_to_bootstrap)775 ZTEST(lwm2m_rd_client, test_fallback_to_bootstrap)
776 {
777 struct lwm2m_ctx ctx;
778
779 (void)memset(&ctx, 0x0, sizeof(ctx));
780
781 lwm2m_rd_client_init();
782 test_lwm2m_engine_start_service();
783 wait_for_service(1);
784
785 lwm2m_get_bool_fake.custom_fake = lwm2m_get_bool_fake_true;
786 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
787 NULL);
788 test_prepare_pending_message_cb(&message_reply_timeout_cb_default);
789 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_TIMEOUT), NULL);
790 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_TIMEOUT), NULL);
791 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REG_TIMEOUT), NULL);
792
793 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_REG_FAILURE),
794 NULL);
795 }
796
ZTEST(lwm2m_rd_client,test_no_srv_fallback_to_bootstrap)797 ZTEST(lwm2m_rd_client, test_no_srv_fallback_to_bootstrap)
798 {
799 struct lwm2m_ctx ctx;
800
801 (void)memset(&ctx, 0x0, sizeof(ctx));
802
803 lwm2m_rd_client_init();
804 test_lwm2m_engine_start_service();
805 wait_for_service(1);
806
807 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_changed;
808 lwm2m_get_bool_fake.custom_fake = lwm2m_get_bool_fake_true;
809 stub_lwm2m_server_disable(true);
810 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
811 NULL);
812 test_prepare_pending_message_cb(&message_reply_cb_default);
813 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_BOOTSTRAP_REG_COMPLETE),
814 NULL);
815 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_created;
816 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
817 stub_lwm2m_server_disable(false);
818 engine_bootstrap_finish();
819 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
820 NULL);
821 }
822
ZTEST(lwm2m_rd_client,test_start_stop_ignore_engine_fault)823 ZTEST(lwm2m_rd_client, test_start_stop_ignore_engine_fault)
824 {
825 struct lwm2m_ctx ctx;
826
827 (void)memset(&ctx, 0x0, sizeof(ctx));
828
829 test_prepare_pending_message_cb(&message_reply_cb_default);
830
831 lwm2m_rd_client_init();
832 test_lwm2m_engine_start_service();
833 wait_for_service(1);
834
835 lwm2m_engine_context_init_fake.custom_fake = lwm2m_engine_context_init_fake1;
836 lwm2m_get_bool_fake.custom_fake = lwm2m_get_bool_fake_default;
837 lwm2m_sprint_ip_addr_fake.custom_fake = lwm2m_sprint_ip_addr_fake_default;
838 lwm2m_init_message_fake.custom_fake = lwm2m_init_message_fake_default;
839 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_created;
840 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
841 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
842 NULL);
843 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
844 NULL);
845
846 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_deleted;
847 zassert_true(lwm2m_rd_client_stop(&ctx, lwm2m_event_cb, true) == 0, NULL);
848 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_DISCONNECT), NULL);
849
850 int c = show_lwm2m_event_fake.call_count;
851
852 test_throw_network_error_from_engine(EIO);
853 wait_for_service(10);
854 zassert_equal(show_lwm2m_event_fake.call_count, c,
855 "Should not enter any other state and throw an event");
856 }
857
ZTEST(lwm2m_rd_client,test_start_suspend_ignore_engine_fault)858 ZTEST(lwm2m_rd_client, test_start_suspend_ignore_engine_fault)
859 {
860 struct lwm2m_ctx ctx;
861
862 (void)memset(&ctx, 0x0, sizeof(ctx));
863
864 test_prepare_pending_message_cb(&message_reply_cb_default);
865
866 lwm2m_rd_client_init();
867 test_lwm2m_engine_start_service();
868 wait_for_service(1);
869
870 lwm2m_engine_context_init_fake.custom_fake = lwm2m_engine_context_init_fake1;
871 lwm2m_get_bool_fake.custom_fake = lwm2m_get_bool_fake_default;
872 lwm2m_sprint_ip_addr_fake.custom_fake = lwm2m_sprint_ip_addr_fake_default;
873 lwm2m_init_message_fake.custom_fake = lwm2m_init_message_fake_default;
874 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_created;
875 coap_find_options_fake.custom_fake = coap_find_options_do_registration_reply_cb_ok;
876 zassert_true(lwm2m_rd_client_start(&ctx, "Test", 0, lwm2m_event_cb, lwm2m_observe_cb) == 0,
877 NULL);
878 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_REGISTRATION_COMPLETE),
879 NULL);
880
881 coap_header_get_code_fake.custom_fake = coap_header_get_code_fake_deleted;
882 zassert_true(lwm2m_rd_client_pause() == 0, NULL);
883 zassert_true(expect_lwm2m_rd_client_event(LWM2M_RD_CLIENT_EVENT_ENGINE_SUSPENDED), NULL);
884
885 int c = show_lwm2m_event_fake.call_count;
886
887 test_throw_network_error_from_engine(EIO);
888 wait_for_service(10);
889 zassert_equal(show_lwm2m_event_fake.call_count, c,
890 "Should not enter any other state and throw an event");
891 }
892