1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 * Copyright (c) 2023 Jamie M.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/ztest.h>
9 #include <zephyr/net_buf.h>
10 #include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
11 #include <zephyr/mgmt/mcumgr/transport/smp_dummy.h>
12 #include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>
13 #include <zephyr/mgmt/mcumgr/grp/os_mgmt/os_mgmt.h>
14 #include <zcbor_common.h>
15 #include <zcbor_decode.h>
16 #include <zcbor_encode.h>
17 #include <mgmt/mcumgr/util/zcbor_bulk.h>
18 #include <string.h>
19 #include <smp_internal.h>
20 #include <zephyr/drivers/rtc.h>
21 #include "smp_test_util.h"
22
23 #define SMP_RESPONSE_WAIT_TIME 3
24 #define ZCBOR_BUFFER_SIZE 256
25 #define OUTPUT_BUFFER_SIZE 256
26 #define ZCBOR_HISTORY_ARRAY_SIZE 4
27
28 /* Test sets */
29 enum {
30 OS_MGMT_DATETIME_TEST_SET_TIME_NOT_SET = 0,
31 OS_MGMT_DATETIME_TEST_SET_TIME_SET,
32 #ifdef CONFIG_MCUMGR_GRP_OS_DATETIME_HOOK
33 OS_MGMT_DATETIME_TEST_SET_HOOKS,
34 #endif
35
36 OS_MGMT_DATETIME_TEST_SET_COUNT
37 };
38
39 static struct net_buf *nb;
40
41 struct state {
42 uint8_t test_set;
43 };
44
45 static struct state test_state = {
46 .test_set = 0,
47 };
48
49 static struct rtc_time valid_time = {
50 .tm_sec = 13,
51 .tm_min = 40,
52 .tm_hour = 4,
53 .tm_mday = 4,
54 .tm_mon = 8,
55 .tm_year = 2023,
56 };
57
58 static struct rtc_time valid_time2 = {
59 .tm_sec = 5,
60 .tm_min = 4,
61 .tm_hour = 3,
62 .tm_mday = 2,
63 .tm_mon = 1,
64 .tm_year = 2001,
65 };
66
67 static const char valid_time_string[] = "2023-08-04T04:40:13";
68 static const char valid_time2_string[] = "2001-01-02T03:04:05";
69 static const char invalid_time_string[] = "abcdefghij";
70 static const char invalid_time2_string[] = "20a1-b1-aTbb:dd:qq";
71 static const char invalid_time3_string[] = "1820-01-02T03:04:05";
72
73 struct group_error {
74 uint16_t group;
75 uint16_t rc;
76 bool found;
77 };
78
79 #ifdef CONFIG_MCUMGR_GRP_OS_DATETIME_HOOK
80 static bool hook_get_ran;
81 static bool hook_set_ran;
82 static bool hook_other_ran;
83 static uint8_t hook_set_data_size;
84 static uint8_t hook_set_data[64];
85 #endif
86
87 static void cleanup_test(void *p);
88
mcumgr_ret_decode(zcbor_state_t * state,struct group_error * result)89 static bool mcumgr_ret_decode(zcbor_state_t *state, struct group_error *result)
90 {
91 bool ok;
92 size_t decoded;
93 uint32_t tmp_group;
94 uint32_t tmp_rc;
95
96 struct zcbor_map_decode_key_val output_decode[] = {
97 ZCBOR_MAP_DECODE_KEY_DECODER("group", zcbor_uint32_decode, &tmp_group),
98 ZCBOR_MAP_DECODE_KEY_DECODER("rc", zcbor_uint32_decode, &tmp_rc),
99 };
100
101 result->found = false;
102
103 ok = zcbor_map_decode_bulk(state, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
104
105 if (ok &&
106 zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode), "group") &&
107 zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode), "rc")) {
108 result->group = (uint16_t)tmp_group;
109 result->rc = (uint16_t)tmp_rc;
110 result->found = true;
111 }
112
113 return ok;
114 }
115
116 #ifdef CONFIG_MCUMGR_GRP_OS_DATETIME_HOOK
os_mgmt_datetime_callback(uint32_t event,enum mgmt_cb_return prev_status,int32_t * rc,uint16_t * group,bool * abort_more,void * data,size_t data_size)117 static enum mgmt_cb_return os_mgmt_datetime_callback(uint32_t event,
118 enum mgmt_cb_return prev_status, int32_t *rc,
119 uint16_t *group, bool *abort_more,
120 void *data, size_t data_size)
121 {
122 if (event == MGMT_EVT_OP_OS_MGMT_DATETIME_GET) {
123 hook_get_ran = true;
124
125 *rc = MGMT_ERR_EBUSY;
126 return MGMT_CB_ERROR_RC;
127 } else if (event == MGMT_EVT_OP_OS_MGMT_DATETIME_SET) {
128 hook_set_ran = true;
129 hook_set_data_size = data_size;
130 memcpy(hook_set_data, data, data_size);
131
132 *rc = MGMT_ERR_EACCESSDENIED;
133 return MGMT_CB_ERROR_RC;
134 }
135
136 hook_other_ran = true;
137 return MGMT_CB_OK;
138 }
139
140 static struct mgmt_callback os_datetime_callbacks = {
141 .callback = os_mgmt_datetime_callback,
142 .event_id = (MGMT_EVT_OP_OS_MGMT_DATETIME_GET | MGMT_EVT_OP_OS_MGMT_DATETIME_SET),
143 };
144 #endif
145
ZTEST(os_mgmt_datetime_not_set,test_datetime_get_not_set_v1)146 ZTEST(os_mgmt_datetime_not_set, test_datetime_get_not_set_v1)
147 {
148 uint8_t buffer[ZCBOR_BUFFER_SIZE];
149 uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
150 bool ok;
151 uint16_t buffer_size;
152 zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
153 zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
154 bool received;
155 struct zcbor_string output = { 0 };
156 size_t decoded = 0;
157 struct group_error group_error;
158 int rc;
159
160 struct zcbor_map_decode_key_val output_decode[] = {
161 ZCBOR_MAP_DECODE_KEY_DECODER("datetime", zcbor_tstr_decode, &output),
162 ZCBOR_MAP_DECODE_KEY_DECODER("rc", zcbor_int32_decode, &rc),
163 ZCBOR_MAP_DECODE_KEY_DECODER("err", mcumgr_ret_decode, &group_error),
164 };
165
166 memset(buffer, 0, sizeof(buffer));
167 memset(buffer_out, 0, sizeof(buffer_out));
168 buffer_size = 0;
169 memset(zse, 0, sizeof(zse));
170 memset(zsd, 0, sizeof(zsd));
171
172 zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
173 ok = create_mcumgr_datetime_get_packet(zse, false, buffer, buffer_out, &buffer_size);
174 zassert_true(ok, "Expected packet creation to be successful");
175
176 /* Enable dummy SMP backend and ready for usage */
177 smp_dummy_enable();
178 smp_dummy_clear_state();
179
180 /* Send query command to dummy SMP backend */
181 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
182 smp_dummy_add_data();
183
184 /* For a short duration to see if response has been received */
185 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
186 zassert_true(received, "Expected to receive data but timed out");
187
188 /* Retrieve response buffer and ensure validity */
189 nb = smp_dummy_get_outgoing();
190 smp_dummy_disable();
191
192 /* Process received data by removing header */
193 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
194 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
195
196 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
197 zassert_true(ok, "Expected decode to be successful");
198 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
199 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
200 "datetime"), "Did not expect to receive datetime element");
201 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
202 "rc"), "Did not expect to receive rc element");
203 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
204 "err"), "Expected to receive err element");
205 zassert_equal(rc, MGMT_ERR_ENOENT, "Expected 'rc' to be no entity");
206 }
207
ZTEST(os_mgmt_datetime_not_set,test_datetime_get_not_set_v2)208 ZTEST(os_mgmt_datetime_not_set, test_datetime_get_not_set_v2)
209 {
210 uint8_t buffer[ZCBOR_BUFFER_SIZE];
211 uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
212 bool ok;
213 uint16_t buffer_size;
214 zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
215 zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
216 bool received;
217 struct zcbor_string output = { 0 };
218 size_t decoded = 0;
219 struct group_error group_error;
220 int rc;
221
222 struct zcbor_map_decode_key_val output_decode[] = {
223 ZCBOR_MAP_DECODE_KEY_DECODER("datetime", zcbor_tstr_decode, &output),
224 ZCBOR_MAP_DECODE_KEY_DECODER("rc", zcbor_int32_decode, &rc),
225 ZCBOR_MAP_DECODE_KEY_DECODER("err", mcumgr_ret_decode, &group_error),
226 };
227
228 memset(buffer, 0, sizeof(buffer));
229 memset(buffer_out, 0, sizeof(buffer_out));
230 buffer_size = 0;
231 memset(zse, 0, sizeof(zse));
232 memset(zsd, 0, sizeof(zsd));
233
234 zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
235 ok = create_mcumgr_datetime_get_packet(zse, true, buffer, buffer_out, &buffer_size);
236 zassert_true(ok, "Expected packet creation to be successful");
237
238 /* Enable dummy SMP backend and ready for usage */
239 smp_dummy_enable();
240 smp_dummy_clear_state();
241
242 /* Send query command to dummy SMP backend */
243 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
244 smp_dummy_add_data();
245
246 /* For a short duration to see if response has been received */
247 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
248 zassert_true(received, "Expected to receive data but timed out");
249
250 /* Retrieve response buffer and ensure validity */
251 nb = smp_dummy_get_outgoing();
252 smp_dummy_disable();
253
254 /* Process received data by removing header */
255 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
256 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
257
258 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
259 zassert_true(ok, "Expected decode to be successful");
260 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
261 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
262 "datetime"), "Did not expect to receive datetime element");
263 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
264 "rc"), "Did not expect to receive rc element");
265 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
266 "err"), "Expected to receive err element");
267 zassert_equal(group_error.group, MGMT_GROUP_ID_OS, "Expected 'err' -> 'group' to be OS");
268 zassert_equal(group_error.rc, OS_MGMT_ERR_RTC_NOT_SET,
269 "Expected 'err' -> 'rc' to be RTC not set");
270 }
271
ZTEST(os_mgmt_datetime_not_set,test_datetime_set_invalid_v1_1)272 ZTEST(os_mgmt_datetime_not_set, test_datetime_set_invalid_v1_1)
273 {
274 uint8_t buffer[ZCBOR_BUFFER_SIZE];
275 uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
276 bool ok;
277 uint16_t buffer_size;
278 zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
279 zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
280 bool received;
281 struct zcbor_string output = { 0 };
282 size_t decoded = 0;
283 struct group_error group_error;
284 int rc;
285
286 struct zcbor_map_decode_key_val output_decode[] = {
287 ZCBOR_MAP_DECODE_KEY_DECODER("datetime", zcbor_tstr_decode, &output),
288 ZCBOR_MAP_DECODE_KEY_DECODER("rc", zcbor_int32_decode, &rc),
289 ZCBOR_MAP_DECODE_KEY_DECODER("err", mcumgr_ret_decode, &group_error),
290 };
291
292 memset(buffer, 0, sizeof(buffer));
293 memset(buffer_out, 0, sizeof(buffer_out));
294 buffer_size = 0;
295 memset(zse, 0, sizeof(zse));
296 memset(zsd, 0, sizeof(zsd));
297
298 zcbor_new_encode_state(zse, 4, buffer, ARRAY_SIZE(buffer), 0);
299 ok = create_mcumgr_datetime_set_packet_str(zse, false, invalid_time_string, buffer,
300 buffer_out, &buffer_size);
301 zassert_true(ok, "Expected packet creation to be successful");
302
303 /* Enable dummy SMP backend and ready for usage */
304 smp_dummy_enable();
305 smp_dummy_clear_state();
306
307 /* Send query command to dummy SMP backend */
308 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
309 smp_dummy_add_data();
310
311 /* For a short duration to see if response has been received */
312 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
313 zassert_true(received, "Expected to receive data but timed out");
314
315 /* Retrieve response buffer and ensure validity */
316 nb = smp_dummy_get_outgoing();
317 smp_dummy_disable();
318
319 /* Process received data by removing header */
320 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
321 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
322
323 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
324 zassert_true(ok, "Expected decode to be successful");
325 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
326 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
327 "datetime"), "Did not expect to receive datetime element");
328 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
329 "rc"), "Expected to receive rc element");
330 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
331 "err"), "Did not expect to receive err element");
332 zassert_equal(rc, MGMT_ERR_EINVAL, "Expected 'rc' to be invalid value");
333
334 /* Clean up test */
335 cleanup_test(NULL);
336 memset(buffer, 0, sizeof(buffer));
337 memset(buffer_out, 0, sizeof(buffer_out));
338 buffer_size = 0;
339 memset(zse, 0, sizeof(zse));
340 memset(zsd, 0, sizeof(zsd));
341 output_decode[0].found = false;
342 output_decode[1].found = false;
343 output_decode[2].found = false;
344
345 /* Query time and ensure it is not set */
346 zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
347 ok = create_mcumgr_datetime_get_packet(zse, false, buffer, buffer_out, &buffer_size);
348 zassert_true(ok, "Expected packet creation to be successful");
349
350 /* Enable dummy SMP backend and ready for usage */
351 smp_dummy_enable();
352 smp_dummy_clear_state();
353
354 /* Send query command to dummy SMP backend */
355 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
356 smp_dummy_add_data();
357
358 /* For a short duration to see if response has been received */
359 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
360 zassert_true(received, "Expected to receive data but timed out");
361
362 /* Retrieve response buffer and ensure validity */
363 nb = smp_dummy_get_outgoing();
364 smp_dummy_disable();
365
366 /* Process received data by removing header */
367 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
368 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
369
370 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
371 zassert_true(ok, "Expected decode to be successful");
372 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
373 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
374 "datetime"), "Did not expect to receive datetime element");
375 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
376 "rc"), "Did not expect to receive rc element");
377 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
378 "err"), "Expected to receive err element");
379 zassert_equal(rc, MGMT_ERR_ENOENT, "Expected 'rc' to be no entity");
380 }
381
ZTEST(os_mgmt_datetime_not_set,test_datetime_set_invalid_v1_2)382 ZTEST(os_mgmt_datetime_not_set, test_datetime_set_invalid_v1_2)
383 {
384 uint8_t buffer[ZCBOR_BUFFER_SIZE];
385 uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
386 bool ok;
387 uint16_t buffer_size;
388 zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
389 zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
390 bool received;
391 struct zcbor_string output = { 0 };
392 size_t decoded = 0;
393 struct group_error group_error;
394 int rc;
395
396 struct zcbor_map_decode_key_val output_decode[] = {
397 ZCBOR_MAP_DECODE_KEY_DECODER("datetime", zcbor_tstr_decode, &output),
398 ZCBOR_MAP_DECODE_KEY_DECODER("rc", zcbor_int32_decode, &rc),
399 ZCBOR_MAP_DECODE_KEY_DECODER("err", mcumgr_ret_decode, &group_error),
400 };
401
402 memset(buffer, 0, sizeof(buffer));
403 memset(buffer_out, 0, sizeof(buffer_out));
404 buffer_size = 0;
405 memset(zse, 0, sizeof(zse));
406 memset(zsd, 0, sizeof(zsd));
407
408 zcbor_new_encode_state(zse, 4, buffer, ARRAY_SIZE(buffer), 0);
409 ok = create_mcumgr_datetime_set_packet_str(zse, false, invalid_time2_string, buffer,
410 buffer_out, &buffer_size);
411 zassert_true(ok, "Expected packet creation to be successful");
412
413 /* Enable dummy SMP backend and ready for usage */
414 smp_dummy_enable();
415 smp_dummy_clear_state();
416
417 /* Send query command to dummy SMP backend */
418 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
419 smp_dummy_add_data();
420
421 /* For a short duration to see if response has been received */
422 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
423 zassert_true(received, "Expected to receive data but timed out");
424
425 /* Retrieve response buffer and ensure validity */
426 nb = smp_dummy_get_outgoing();
427 smp_dummy_disable();
428
429 /* Process received data by removing header */
430 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
431 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
432
433 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
434 zassert_true(ok, "Expected decode to be successful");
435 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
436 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
437 "datetime"), "Did not expect to receive datetime element");
438 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
439 "rc"), "Expected to receive rc element");
440 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
441 "err"), "Did not expect to receive err element");
442 zassert_equal(rc, MGMT_ERR_EINVAL, "Expected 'rc' to be invalid value");
443
444 /* Clean up test */
445 cleanup_test(NULL);
446 memset(buffer, 0, sizeof(buffer));
447 memset(buffer_out, 0, sizeof(buffer_out));
448 buffer_size = 0;
449 memset(zse, 0, sizeof(zse));
450 memset(zsd, 0, sizeof(zsd));
451 output_decode[0].found = false;
452 output_decode[1].found = false;
453 output_decode[2].found = false;
454
455 /* Query time and ensure it is not set */
456 zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
457 ok = create_mcumgr_datetime_get_packet(zse, false, buffer, buffer_out, &buffer_size);
458 zassert_true(ok, "Expected packet creation to be successful");
459
460 /* Enable dummy SMP backend and ready for usage */
461 smp_dummy_enable();
462 smp_dummy_clear_state();
463
464 /* Send query command to dummy SMP backend */
465 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
466 smp_dummy_add_data();
467
468 /* For a short duration to see if response has been received */
469 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
470 zassert_true(received, "Expected to receive data but timed out");
471
472 /* Retrieve response buffer and ensure validity */
473 nb = smp_dummy_get_outgoing();
474 smp_dummy_disable();
475
476 /* Process received data by removing header */
477 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
478 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
479
480 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
481 zassert_true(ok, "Expected decode to be successful");
482 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
483 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
484 "datetime"), "Did not expect to receive datetime element");
485 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
486 "rc"), "Did not expect to receive rc element");
487 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
488 "err"), "Expected to receive err element");
489 zassert_equal(rc, MGMT_ERR_ENOENT, "Expected 'rc' to be no entity");
490 }
491
ZTEST(os_mgmt_datetime_not_set,test_datetime_set_invalid_v1_3)492 ZTEST(os_mgmt_datetime_not_set, test_datetime_set_invalid_v1_3)
493 {
494 uint8_t buffer[ZCBOR_BUFFER_SIZE];
495 uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
496 bool ok;
497 uint16_t buffer_size;
498 zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
499 zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
500 bool received;
501 struct zcbor_string output = { 0 };
502 size_t decoded = 0;
503 struct group_error group_error;
504 int rc;
505
506 struct zcbor_map_decode_key_val output_decode[] = {
507 ZCBOR_MAP_DECODE_KEY_DECODER("datetime", zcbor_tstr_decode, &output),
508 ZCBOR_MAP_DECODE_KEY_DECODER("rc", zcbor_int32_decode, &rc),
509 ZCBOR_MAP_DECODE_KEY_DECODER("err", mcumgr_ret_decode, &group_error),
510 };
511
512 memset(buffer, 0, sizeof(buffer));
513 memset(buffer_out, 0, sizeof(buffer_out));
514 buffer_size = 0;
515 memset(zse, 0, sizeof(zse));
516 memset(zsd, 0, sizeof(zsd));
517
518 zcbor_new_encode_state(zse, 4, buffer, ARRAY_SIZE(buffer), 0);
519 ok = create_mcumgr_datetime_set_packet_str(zse, false, invalid_time3_string, buffer,
520 buffer_out, &buffer_size);
521 zassert_true(ok, "Expected packet creation to be successful");
522
523 /* Enable dummy SMP backend and ready for usage */
524 smp_dummy_enable();
525 smp_dummy_clear_state();
526
527 /* Send query command to dummy SMP backend */
528 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
529 smp_dummy_add_data();
530
531 /* For a short duration to see if response has been received */
532 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
533 zassert_true(received, "Expected to receive data but timed out");
534
535 /* Retrieve response buffer and ensure validity */
536 nb = smp_dummy_get_outgoing();
537 smp_dummy_disable();
538
539 /* Process received data by removing header */
540 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
541 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
542
543 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
544 zassert_true(ok, "Expected decode to be successful");
545 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
546 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
547 "datetime"), "Did not expect to receive datetime element");
548 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
549 "rc"), "Expected to receive rc element");
550 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
551 "err"), "Did not expect to receive err element");
552 zassert_equal(rc, MGMT_ERR_EINVAL, "Expected 'rc' to be invalid value");
553
554 /* Clean up test */
555 cleanup_test(NULL);
556 memset(buffer, 0, sizeof(buffer));
557 memset(buffer_out, 0, sizeof(buffer_out));
558 buffer_size = 0;
559 memset(zse, 0, sizeof(zse));
560 memset(zsd, 0, sizeof(zsd));
561 output_decode[0].found = false;
562 output_decode[1].found = false;
563 output_decode[2].found = false;
564
565 /* Query time and ensure it is not set */
566 zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
567 ok = create_mcumgr_datetime_get_packet(zse, false, buffer, buffer_out, &buffer_size);
568 zassert_true(ok, "Expected packet creation to be successful");
569
570 /* Enable dummy SMP backend and ready for usage */
571 smp_dummy_enable();
572 smp_dummy_clear_state();
573
574 /* Send query command to dummy SMP backend */
575 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
576 smp_dummy_add_data();
577
578 /* For a short duration to see if response has been received */
579 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
580 zassert_true(received, "Expected to receive data but timed out");
581
582 /* Retrieve response buffer and ensure validity */
583 nb = smp_dummy_get_outgoing();
584 smp_dummy_disable();
585
586 /* Process received data by removing header */
587 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
588 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
589
590 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
591 zassert_true(ok, "Expected decode to be successful");
592 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
593 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
594 "datetime"), "Did not expect to receive datetime element");
595 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
596 "rc"), "Did not expect to receive rc element");
597 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
598 "err"), "Expected to receive err element");
599 zassert_equal(rc, MGMT_ERR_ENOENT, "Expected 'rc' to be no entity");
600 }
601
ZTEST(os_mgmt_datetime_not_set,test_datetime_set_invalid_v2_1)602 ZTEST(os_mgmt_datetime_not_set, test_datetime_set_invalid_v2_1)
603 {
604 uint8_t buffer[ZCBOR_BUFFER_SIZE];
605 uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
606 bool ok;
607 uint16_t buffer_size;
608 zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
609 zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
610 bool received;
611 struct zcbor_string output = { 0 };
612 size_t decoded = 0;
613 struct group_error group_error;
614 int rc;
615
616 struct zcbor_map_decode_key_val output_decode[] = {
617 ZCBOR_MAP_DECODE_KEY_DECODER("datetime", zcbor_tstr_decode, &output),
618 ZCBOR_MAP_DECODE_KEY_DECODER("rc", zcbor_int32_decode, &rc),
619 ZCBOR_MAP_DECODE_KEY_DECODER("err", mcumgr_ret_decode, &group_error),
620 };
621
622 memset(buffer, 0, sizeof(buffer));
623 memset(buffer_out, 0, sizeof(buffer_out));
624 buffer_size = 0;
625 memset(zse, 0, sizeof(zse));
626 memset(zsd, 0, sizeof(zsd));
627
628 zcbor_new_encode_state(zse, 4, buffer, ARRAY_SIZE(buffer), 0);
629 ok = create_mcumgr_datetime_set_packet_str(zse, true, invalid_time2_string, buffer,
630 buffer_out, &buffer_size);
631 zassert_true(ok, "Expected packet creation to be successful");
632
633 /* Enable dummy SMP backend and ready for usage */
634 smp_dummy_enable();
635 smp_dummy_clear_state();
636
637 /* Send query command to dummy SMP backend */
638 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
639 smp_dummy_add_data();
640
641 /* For a short duration to see if response has been received */
642 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
643 zassert_true(received, "Expected to receive data but timed out");
644
645 /* Retrieve response buffer and ensure validity */
646 nb = smp_dummy_get_outgoing();
647 smp_dummy_disable();
648
649 /* Process received data by removing header */
650 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
651 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
652
653 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
654 zassert_true(ok, "Expected decode to be successful");
655 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
656 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
657 "datetime"), "Did not expect to receive datetime element");
658 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
659 "rc"), "Expected to receive rc element");
660 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
661 "err"), "Did not expect to receive err element");
662 zassert_equal(rc, MGMT_ERR_EINVAL, "Expected 'rc' to be invalid value");
663
664 /* Clean up test */
665 cleanup_test(NULL);
666 memset(buffer, 0, sizeof(buffer));
667 memset(buffer_out, 0, sizeof(buffer_out));
668 buffer_size = 0;
669 memset(zse, 0, sizeof(zse));
670 memset(zsd, 0, sizeof(zsd));
671 output_decode[0].found = false;
672 output_decode[1].found = false;
673 output_decode[2].found = false;
674
675 /* Query time and ensure it is not set */
676 zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
677 ok = create_mcumgr_datetime_get_packet(zse, true, buffer, buffer_out, &buffer_size);
678 zassert_true(ok, "Expected packet creation to be successful");
679
680 /* Enable dummy SMP backend and ready for usage */
681 smp_dummy_enable();
682 smp_dummy_clear_state();
683
684 /* Send query command to dummy SMP backend */
685 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
686 smp_dummy_add_data();
687
688 /* For a short duration to see if response has been received */
689 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
690 zassert_true(received, "Expected to receive data but timed out");
691
692 /* Retrieve response buffer and ensure validity */
693 nb = smp_dummy_get_outgoing();
694 smp_dummy_disable();
695
696 /* Process received data by removing header */
697 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
698 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
699
700 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
701 zassert_true(ok, "Expected decode to be successful");
702 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
703 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
704 "datetime"), "Did not expect to receive datetime element");
705 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
706 "rc"), "Did not expect to receive rc element");
707 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
708 "err"), "Expected to receive err element");
709 zassert_equal(group_error.group, MGMT_GROUP_ID_OS, "Expected 'err' -> 'group' to be OS");
710 zassert_equal(group_error.rc, OS_MGMT_ERR_RTC_NOT_SET,
711 "Expected 'err' -> 'rc' to be RTC not set");
712 }
713
ZTEST(os_mgmt_datetime_not_set,test_datetime_set_invalid_v2_2)714 ZTEST(os_mgmt_datetime_not_set, test_datetime_set_invalid_v2_2)
715 {
716 uint8_t buffer[ZCBOR_BUFFER_SIZE];
717 uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
718 bool ok;
719 uint16_t buffer_size;
720 zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
721 zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
722 bool received;
723 struct zcbor_string output = { 0 };
724 size_t decoded = 0;
725 struct group_error group_error;
726 int rc;
727
728 struct zcbor_map_decode_key_val output_decode[] = {
729 ZCBOR_MAP_DECODE_KEY_DECODER("datetime", zcbor_tstr_decode, &output),
730 ZCBOR_MAP_DECODE_KEY_DECODER("rc", zcbor_int32_decode, &rc),
731 ZCBOR_MAP_DECODE_KEY_DECODER("err", mcumgr_ret_decode, &group_error),
732 };
733
734 memset(buffer, 0, sizeof(buffer));
735 memset(buffer_out, 0, sizeof(buffer_out));
736 buffer_size = 0;
737 memset(zse, 0, sizeof(zse));
738 memset(zsd, 0, sizeof(zsd));
739
740 zcbor_new_encode_state(zse, 4, buffer, ARRAY_SIZE(buffer), 0);
741 ok = create_mcumgr_datetime_set_packet_str(zse, true, invalid_time2_string, buffer,
742 buffer_out, &buffer_size);
743 zassert_true(ok, "Expected packet creation to be successful");
744
745 /* Enable dummy SMP backend and ready for usage */
746 smp_dummy_enable();
747 smp_dummy_clear_state();
748
749 /* Send query command to dummy SMP backend */
750 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
751 smp_dummy_add_data();
752
753 /* For a short duration to see if response has been received */
754 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
755 zassert_true(received, "Expected to receive data but timed out");
756
757 /* Retrieve response buffer and ensure validity */
758 nb = smp_dummy_get_outgoing();
759 smp_dummy_disable();
760
761 /* Process received data by removing header */
762 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
763 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
764
765 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
766 zassert_true(ok, "Expected decode to be successful");
767 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
768 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
769 "datetime"), "Did not expect to receive datetime element");
770 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
771 "rc"), "Expected to receive rc element");
772 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
773 "err"), "Did not expect to receive err element");
774 zassert_equal(rc, MGMT_ERR_EINVAL, "Expected 'rc' to be invalid value");
775
776 /* Clean up test */
777 cleanup_test(NULL);
778 memset(buffer, 0, sizeof(buffer));
779 memset(buffer_out, 0, sizeof(buffer_out));
780 buffer_size = 0;
781 memset(zse, 0, sizeof(zse));
782 memset(zsd, 0, sizeof(zsd));
783 output_decode[0].found = false;
784 output_decode[1].found = false;
785 output_decode[2].found = false;
786
787 /* Query time and ensure it is not set */
788 zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
789 ok = create_mcumgr_datetime_get_packet(zse, true, buffer, buffer_out, &buffer_size);
790 zassert_true(ok, "Expected packet creation to be successful");
791
792 /* Enable dummy SMP backend and ready for usage */
793 smp_dummy_enable();
794 smp_dummy_clear_state();
795
796 /* Send query command to dummy SMP backend */
797 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
798 smp_dummy_add_data();
799
800 /* For a short duration to see if response has been received */
801 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
802 zassert_true(received, "Expected to receive data but timed out");
803
804 /* Retrieve response buffer and ensure validity */
805 nb = smp_dummy_get_outgoing();
806 smp_dummy_disable();
807
808 /* Process received data by removing header */
809 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
810 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
811
812 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
813 zassert_true(ok, "Expected decode to be successful");
814 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
815 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
816 "datetime"), "Did not expect to receive datetime element");
817 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
818 "rc"), "Did not expect to receive rc element");
819 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
820 "err"), "Expected to receive err element");
821 zassert_equal(group_error.group, MGMT_GROUP_ID_OS, "Expected 'err' -> 'group' to be OS");
822 zassert_equal(group_error.rc, OS_MGMT_ERR_RTC_NOT_SET,
823 "Expected 'err' -> 'rc' to be RTC not set");
824 }
825
ZTEST(os_mgmt_datetime_not_set,test_datetime_set_invalid_v2_3)826 ZTEST(os_mgmt_datetime_not_set, test_datetime_set_invalid_v2_3)
827 {
828 uint8_t buffer[ZCBOR_BUFFER_SIZE];
829 uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
830 bool ok;
831 uint16_t buffer_size;
832 zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
833 zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
834 bool received;
835 struct zcbor_string output = { 0 };
836 size_t decoded = 0;
837 struct group_error group_error;
838 int rc;
839
840 struct zcbor_map_decode_key_val output_decode[] = {
841 ZCBOR_MAP_DECODE_KEY_DECODER("datetime", zcbor_tstr_decode, &output),
842 ZCBOR_MAP_DECODE_KEY_DECODER("rc", zcbor_int32_decode, &rc),
843 ZCBOR_MAP_DECODE_KEY_DECODER("err", mcumgr_ret_decode, &group_error),
844 };
845
846 memset(buffer, 0, sizeof(buffer));
847 memset(buffer_out, 0, sizeof(buffer_out));
848 buffer_size = 0;
849 memset(zse, 0, sizeof(zse));
850 memset(zsd, 0, sizeof(zsd));
851
852 zcbor_new_encode_state(zse, 4, buffer, ARRAY_SIZE(buffer), 0);
853 ok = create_mcumgr_datetime_set_packet_str(zse, true, invalid_time3_string, buffer,
854 buffer_out, &buffer_size);
855 zassert_true(ok, "Expected packet creation to be successful");
856
857 /* Enable dummy SMP backend and ready for usage */
858 smp_dummy_enable();
859 smp_dummy_clear_state();
860
861 /* Send query command to dummy SMP backend */
862 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
863 smp_dummy_add_data();
864
865 /* For a short duration to see if response has been received */
866 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
867 zassert_true(received, "Expected to receive data but timed out");
868
869 /* Retrieve response buffer and ensure validity */
870 nb = smp_dummy_get_outgoing();
871 smp_dummy_disable();
872
873 /* Process received data by removing header */
874 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
875 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
876
877 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
878 zassert_true(ok, "Expected decode to be successful");
879 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
880 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
881 "datetime"), "Did not expect to receive datetime element");
882 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
883 "rc"), "Expected to receive rc element");
884 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
885 "err"), "Did not expect to receive err element");
886 zassert_equal(rc, MGMT_ERR_EINVAL, "Expected 'rc' to be invalid value");
887
888 /* Clean up test */
889 cleanup_test(NULL);
890 memset(buffer, 0, sizeof(buffer));
891 memset(buffer_out, 0, sizeof(buffer_out));
892 buffer_size = 0;
893 memset(zse, 0, sizeof(zse));
894 memset(zsd, 0, sizeof(zsd));
895 output_decode[0].found = false;
896 output_decode[1].found = false;
897 output_decode[2].found = false;
898
899 /* Query time and ensure it is not set */
900 zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
901 ok = create_mcumgr_datetime_get_packet(zse, true, buffer, buffer_out, &buffer_size);
902 zassert_true(ok, "Expected packet creation to be successful");
903
904 /* Enable dummy SMP backend and ready for usage */
905 smp_dummy_enable();
906 smp_dummy_clear_state();
907
908 /* Send query command to dummy SMP backend */
909 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
910 smp_dummy_add_data();
911
912 /* For a short duration to see if response has been received */
913 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
914 zassert_true(received, "Expected to receive data but timed out");
915
916 /* Retrieve response buffer and ensure validity */
917 nb = smp_dummy_get_outgoing();
918 smp_dummy_disable();
919
920 /* Process received data by removing header */
921 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
922 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
923
924 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
925 zassert_true(ok, "Expected decode to be successful");
926 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
927 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
928 "datetime"), "Did not expect to receive datetime element");
929 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
930 "rc"), "Did not expect to receive rc element");
931 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
932 "err"), "Expected to receive err element");
933 zassert_equal(group_error.group, MGMT_GROUP_ID_OS, "Expected 'err' -> 'group' to be OS");
934 zassert_equal(group_error.rc, OS_MGMT_ERR_RTC_NOT_SET,
935 "Expected 'err' -> 'rc' to be RTC not set");
936 }
937
ZTEST(os_mgmt_datetime_set,test_datetime_set_v1)938 ZTEST(os_mgmt_datetime_set, test_datetime_set_v1)
939 {
940 uint8_t buffer[ZCBOR_BUFFER_SIZE];
941 uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
942 bool ok;
943 uint16_t buffer_size;
944 zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
945 zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
946 bool received;
947 struct zcbor_string output = { 0 };
948 size_t decoded = 0;
949 struct group_error group_error;
950 int rc;
951
952 struct zcbor_map_decode_key_val output_decode[] = {
953 ZCBOR_MAP_DECODE_KEY_DECODER("datetime", zcbor_tstr_decode, &output),
954 ZCBOR_MAP_DECODE_KEY_DECODER("rc", zcbor_int32_decode, &rc),
955 ZCBOR_MAP_DECODE_KEY_DECODER("err", mcumgr_ret_decode, &group_error),
956 };
957
958 memset(buffer, 0, sizeof(buffer));
959 memset(buffer_out, 0, sizeof(buffer_out));
960 buffer_size = 0;
961 memset(zse, 0, sizeof(zse));
962 memset(zsd, 0, sizeof(zsd));
963
964 zcbor_new_encode_state(zse, 4, buffer, ARRAY_SIZE(buffer), 0);
965 ok = create_mcumgr_datetime_set_packet(zse, false, &valid_time, buffer, buffer_out,
966 &buffer_size);
967 zassert_true(ok, "Expected packet creation to be successful");
968
969 /* Enable dummy SMP backend and ready for usage */
970 smp_dummy_enable();
971 smp_dummy_clear_state();
972
973 /* Send query command to dummy SMP backend */
974 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
975 smp_dummy_add_data();
976
977 /* For a short duration to see if response has been received */
978 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
979 zassert_true(received, "Expected to receive data but timed out");
980
981 /* Retrieve response buffer and ensure validity */
982 nb = smp_dummy_get_outgoing();
983 smp_dummy_disable();
984
985 /* Process received data by removing header */
986 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
987 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
988
989 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
990 zassert_true(ok, "Expected decode to be successful");
991 zassert_equal(decoded, 0, "Did not expect to receive any decoded zcbor element");
992 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
993 "datetime"), "Did not expect to receive datetime element");
994 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
995 "rc"), "Did not expect to receive rc element");
996 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
997 "err"), "Did not expect to receive err element");
998
999 /* Clean up test */
1000 cleanup_test(NULL);
1001 memset(buffer, 0, sizeof(buffer));
1002 memset(buffer_out, 0, sizeof(buffer_out));
1003 buffer_size = 0;
1004 memset(zse, 0, sizeof(zse));
1005 memset(zsd, 0, sizeof(zsd));
1006 output_decode[0].found = false;
1007 output_decode[1].found = false;
1008 output_decode[2].found = false;
1009
1010 /* Query time and ensure it is set */
1011 zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
1012 ok = create_mcumgr_datetime_get_packet(zse, false, buffer, buffer_out, &buffer_size);
1013 zassert_true(ok, "Expected packet creation to be successful");
1014
1015 /* Enable dummy SMP backend and ready for usage */
1016 smp_dummy_enable();
1017 smp_dummy_clear_state();
1018
1019 /* Send query command to dummy SMP backend */
1020 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1021 smp_dummy_add_data();
1022
1023 /* For a short duration to see if response has been received */
1024 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1025 zassert_true(received, "Expected to receive data but timed out");
1026
1027 /* Retrieve response buffer and ensure validity */
1028 nb = smp_dummy_get_outgoing();
1029 smp_dummy_disable();
1030
1031 /* Process received data by removing header */
1032 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
1033 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
1034
1035 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
1036 zassert_true(ok, "Expected decode to be successful");
1037 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
1038 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1039 "datetime"), "Expected to receive datetime element");
1040 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1041 "rc"), "Did not expect to receive rc element");
1042 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1043 "err"), "Did not expected to receive err element");
1044
1045 /* Check that the date/time is as expected */
1046 zassert_equal(output.len, strlen(valid_time_string),
1047 "Expected received datetime length mismatch");
1048 zassert_mem_equal(output.value, valid_time_string, strlen(valid_time_string),
1049 "Expected received datetime value mismatch");
1050 }
1051
ZTEST(os_mgmt_datetime_set,test_datetime_set_v2)1052 ZTEST(os_mgmt_datetime_set, test_datetime_set_v2)
1053 {
1054 uint8_t buffer[ZCBOR_BUFFER_SIZE];
1055 uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
1056 bool ok;
1057 uint16_t buffer_size;
1058 zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
1059 zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
1060 bool received;
1061 struct zcbor_string output = { 0 };
1062 size_t decoded = 0;
1063 struct group_error group_error;
1064 int rc;
1065
1066 struct zcbor_map_decode_key_val output_decode[] = {
1067 ZCBOR_MAP_DECODE_KEY_DECODER("datetime", zcbor_tstr_decode, &output),
1068 ZCBOR_MAP_DECODE_KEY_DECODER("rc", zcbor_int32_decode, &rc),
1069 ZCBOR_MAP_DECODE_KEY_DECODER("err", mcumgr_ret_decode, &group_error),
1070 };
1071
1072 memset(buffer, 0, sizeof(buffer));
1073 memset(buffer_out, 0, sizeof(buffer_out));
1074 buffer_size = 0;
1075 memset(zse, 0, sizeof(zse));
1076 memset(zsd, 0, sizeof(zsd));
1077
1078 zcbor_new_encode_state(zse, 4, buffer, ARRAY_SIZE(buffer), 0);
1079 ok = create_mcumgr_datetime_set_packet(zse, true, &valid_time2, buffer, buffer_out,
1080 &buffer_size);
1081 zassert_true(ok, "Expected packet creation to be successful");
1082
1083 /* Enable dummy SMP backend and ready for usage */
1084 smp_dummy_enable();
1085 smp_dummy_clear_state();
1086
1087 /* Send query command to dummy SMP backend */
1088 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1089 smp_dummy_add_data();
1090
1091 /* For a short duration to see if response has been received */
1092 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1093 zassert_true(received, "Expected to receive data but timed out");
1094
1095 /* Retrieve response buffer and ensure validity */
1096 nb = smp_dummy_get_outgoing();
1097 smp_dummy_disable();
1098
1099 /* Process received data by removing header */
1100 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
1101 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
1102
1103 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
1104 zassert_true(ok, "Expected decode to be successful");
1105 zassert_equal(decoded, 0, "Did not expect to receive any decoded zcbor element");
1106 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1107 "datetime"), "Did not expect to receive datetime element");
1108 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1109 "rc"), "Did not expect to receive rc element");
1110 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1111 "err"), "Did not expect to receive err element");
1112
1113 /* Clean up test */
1114 cleanup_test(NULL);
1115 memset(buffer, 0, sizeof(buffer));
1116 memset(buffer_out, 0, sizeof(buffer_out));
1117 buffer_size = 0;
1118 memset(zse, 0, sizeof(zse));
1119 memset(zsd, 0, sizeof(zsd));
1120 output_decode[0].found = false;
1121 output_decode[1].found = false;
1122 output_decode[2].found = false;
1123
1124 /* Query time and ensure it is set */
1125 zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
1126 ok = create_mcumgr_datetime_get_packet(zse, false, buffer, buffer_out, &buffer_size);
1127 zassert_true(ok, "Expected packet creation to be successful");
1128
1129 /* Enable dummy SMP backend and ready for usage */
1130 smp_dummy_enable();
1131 smp_dummy_clear_state();
1132
1133 /* Send query command to dummy SMP backend */
1134 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1135 smp_dummy_add_data();
1136
1137 /* For a short duration to see if response has been received */
1138 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1139 zassert_true(received, "Expected to receive data but timed out");
1140
1141 /* Retrieve response buffer and ensure validity */
1142 nb = smp_dummy_get_outgoing();
1143 smp_dummy_disable();
1144
1145 /* Process received data by removing header */
1146 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
1147 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
1148
1149 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
1150 zassert_true(ok, "Expected decode to be successful");
1151 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
1152 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1153 "datetime"), "Expected to receive datetime element");
1154 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1155 "rc"), "Did not expect to receive rc element");
1156 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1157 "err"), "Did not expected to receive err element");
1158
1159 /* Check that the date/time is as expected */
1160 zassert_equal(output.len, strlen(valid_time2_string),
1161 "Expected received datetime length mismatch");
1162 zassert_mem_equal(output.value, valid_time2_string, strlen(valid_time2_string),
1163 "Expected received datetime value mismatch");
1164 }
1165
1166 #ifdef CONFIG_MCUMGR_GRP_OS_DATETIME_HOOK
setup_os_datetime_callbacks(void)1167 static void *setup_os_datetime_callbacks(void)
1168 {
1169 mgmt_callback_register(&os_datetime_callbacks);
1170 return NULL;
1171 }
1172
destroy_os_datetime_callbacks(void * p)1173 static void destroy_os_datetime_callbacks(void *p)
1174 {
1175 mgmt_callback_unregister(&os_datetime_callbacks);
1176 }
1177
ZTEST(os_mgmt_datetime_hook,test_datetime_set_valid_hook_v1)1178 ZTEST(os_mgmt_datetime_hook, test_datetime_set_valid_hook_v1)
1179 {
1180 uint8_t buffer[ZCBOR_BUFFER_SIZE];
1181 uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
1182 bool ok;
1183 uint16_t buffer_size;
1184 zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
1185 zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
1186 bool received;
1187 struct zcbor_string output = { 0 };
1188 size_t decoded = 0;
1189 struct group_error group_error;
1190 int rc;
1191 struct rtc_time *hook_data;
1192
1193 struct zcbor_map_decode_key_val output_decode[] = {
1194 ZCBOR_MAP_DECODE_KEY_DECODER("datetime", zcbor_tstr_decode, &output),
1195 ZCBOR_MAP_DECODE_KEY_DECODER("rc", zcbor_int32_decode, &rc),
1196 ZCBOR_MAP_DECODE_KEY_DECODER("err", mcumgr_ret_decode, &group_error),
1197 };
1198
1199 memset(buffer, 0, sizeof(buffer));
1200 memset(buffer_out, 0, sizeof(buffer_out));
1201 buffer_size = 0;
1202 memset(zse, 0, sizeof(zse));
1203 memset(zsd, 0, sizeof(zsd));
1204
1205 zcbor_new_encode_state(zse, 4, buffer, ARRAY_SIZE(buffer), 0);
1206 ok = create_mcumgr_datetime_set_packet(zse, false, &valid_time2, buffer, buffer_out,
1207 &buffer_size);
1208 zassert_true(ok, "Expected packet creation to be successful");
1209
1210 /* Enable dummy SMP backend and ready for usage */
1211 smp_dummy_enable();
1212 smp_dummy_clear_state();
1213
1214 /* Send query command to dummy SMP backend */
1215 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1216 smp_dummy_add_data();
1217
1218 /* For a short duration to see if response has been received */
1219 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1220 zassert_true(received, "Expected to receive data but timed out");
1221
1222 /* Retrieve response buffer and ensure validity */
1223 nb = smp_dummy_get_outgoing();
1224 smp_dummy_disable();
1225
1226 /* Process received data by removing header */
1227 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
1228 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
1229
1230 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
1231 zassert_true(ok, "Expected decode to be successful");
1232 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
1233 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1234 "datetime"), "Did not expect to receive datetime element");
1235 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1236 "rc"), "Expected to receive rc element");
1237 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1238 "err"), "Did not expect to receive err element");
1239 zassert_equal(rc, MGMT_ERR_EACCESSDENIED, "Expected 'rc' to be access denied");
1240
1241 /* Check hook actions are as expected */
1242 hook_data = (struct rtc_time *)hook_set_data;
1243 zassert_false(hook_get_ran, "Did not expect get hook to run");
1244 zassert_true(hook_set_ran, "Expected set hook to run");
1245 zassert_false(hook_other_ran, "Did not expect other hooks to run");
1246 zassert_equal(hook_set_data_size, sizeof(valid_time2),
1247 "Expected data size to match time struct size");
1248 zassert_equal(valid_time2.tm_sec, hook_data->tm_sec, "Expected value mismatch");
1249 zassert_equal(valid_time2.tm_min, hook_data->tm_min, "Expected value mismatch");
1250 zassert_equal(valid_time2.tm_hour, hook_data->tm_hour, "Expected value mismatch");
1251 zassert_equal(valid_time2.tm_mday, hook_data->tm_mday, "Expected value mismatch");
1252 zassert_equal(valid_time2.tm_mon, (hook_data->tm_mon + 1), "Expected value mismatch");
1253 zassert_equal(valid_time2.tm_year, (hook_data->tm_year + 1900),
1254 "Expected value mismatch");
1255
1256 /* Clean up test */
1257 cleanup_test(NULL);
1258 memset(buffer, 0, sizeof(buffer));
1259 memset(buffer_out, 0, sizeof(buffer_out));
1260 buffer_size = 0;
1261 memset(zse, 0, sizeof(zse));
1262 memset(zsd, 0, sizeof(zsd));
1263 output_decode[0].found = false;
1264 output_decode[1].found = false;
1265 output_decode[2].found = false;
1266
1267 /* Query time and ensure it is not set */
1268 zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
1269 ok = create_mcumgr_datetime_get_packet(zse, false, buffer, buffer_out, &buffer_size);
1270 zassert_true(ok, "Expected packet creation to be successful");
1271
1272 /* Enable dummy SMP backend and ready for usage */
1273 smp_dummy_enable();
1274 smp_dummy_clear_state();
1275
1276 /* Send query command to dummy SMP backend */
1277 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1278 smp_dummy_add_data();
1279
1280 /* For a short duration to see if response has been received */
1281 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1282 zassert_true(received, "Expected to receive data but timed out");
1283
1284 /* Retrieve response buffer and ensure validity */
1285 nb = smp_dummy_get_outgoing();
1286 smp_dummy_disable();
1287
1288 /* Process received data by removing header */
1289 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
1290 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
1291
1292 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
1293 zassert_true(ok, "Expected decode to be successful");
1294 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
1295 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1296 "datetime"), "Did not expect to receive datetime element");
1297 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1298 "rc"), "Expected to receive rc element");
1299 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1300 "err"), "Did not expect to receive err element");
1301 zassert_equal(rc, MGMT_ERR_EBUSY, "Expected 'rc' to be busy");
1302
1303 /* Check hook actions are as expected */
1304 zassert_true(hook_get_ran, "Expected get hook to run");
1305 zassert_false(hook_set_ran, "Did not expect set hook to run");
1306 zassert_false(hook_other_ran, "Did not expect other hooks to run");
1307 zassert_equal(hook_set_data_size, 0, "Expected data size to be 0");
1308 }
1309
ZTEST(os_mgmt_datetime_hook,test_datetime_set_valid_hook_v2)1310 ZTEST(os_mgmt_datetime_hook, test_datetime_set_valid_hook_v2)
1311 {
1312 uint8_t buffer[ZCBOR_BUFFER_SIZE];
1313 uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
1314 bool ok;
1315 uint16_t buffer_size;
1316 zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
1317 zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
1318 bool received;
1319 struct zcbor_string output = { 0 };
1320 size_t decoded = 0;
1321 struct group_error group_error;
1322 int rc;
1323 struct rtc_time *hook_data;
1324
1325 struct zcbor_map_decode_key_val output_decode[] = {
1326 ZCBOR_MAP_DECODE_KEY_DECODER("datetime", zcbor_tstr_decode, &output),
1327 ZCBOR_MAP_DECODE_KEY_DECODER("rc", zcbor_int32_decode, &rc),
1328 ZCBOR_MAP_DECODE_KEY_DECODER("err", mcumgr_ret_decode, &group_error),
1329 };
1330
1331 memset(buffer, 0, sizeof(buffer));
1332 memset(buffer_out, 0, sizeof(buffer_out));
1333 buffer_size = 0;
1334 memset(zse, 0, sizeof(zse));
1335 memset(zsd, 0, sizeof(zsd));
1336
1337 zcbor_new_encode_state(zse, 4, buffer, ARRAY_SIZE(buffer), 0);
1338 ok = create_mcumgr_datetime_set_packet(zse, true, &valid_time2, buffer, buffer_out,
1339 &buffer_size);
1340 zassert_true(ok, "Expected packet creation to be successful");
1341
1342 /* Enable dummy SMP backend and ready for usage */
1343 smp_dummy_enable();
1344 smp_dummy_clear_state();
1345
1346 /* Send query command to dummy SMP backend */
1347 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1348 smp_dummy_add_data();
1349
1350 /* For a short duration to see if response has been received */
1351 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1352 zassert_true(received, "Expected to receive data but timed out");
1353
1354 /* Retrieve response buffer and ensure validity */
1355 nb = smp_dummy_get_outgoing();
1356 smp_dummy_disable();
1357
1358 /* Process received data by removing header */
1359 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
1360 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
1361
1362 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
1363 zassert_true(ok, "Expected decode to be successful");
1364 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
1365 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1366 "datetime"), "Did not expect to receive datetime element");
1367 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1368 "rc"), "Expected to receive rc element");
1369 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1370 "err"), "Did not expect to receive err element");
1371 zassert_equal(rc, MGMT_ERR_EACCESSDENIED, "Expected 'rc' to be access denied");
1372
1373 /* Check hook actions are as expected */
1374 hook_data = (struct rtc_time *)hook_set_data;
1375 zassert_false(hook_get_ran, "Did not expect get hook to run");
1376 zassert_true(hook_set_ran, "Expected set hook to run");
1377 zassert_false(hook_other_ran, "Did not expect other hooks to run");
1378 zassert_equal(hook_set_data_size, sizeof(valid_time2),
1379 "Expected data size to match time struct size");
1380 zassert_equal(valid_time2.tm_sec, hook_data->tm_sec, "Expected value mismatch");
1381 zassert_equal(valid_time2.tm_min, hook_data->tm_min, "Expected value mismatch");
1382 zassert_equal(valid_time2.tm_hour, hook_data->tm_hour, "Expected value mismatch");
1383 zassert_equal(valid_time2.tm_mday, hook_data->tm_mday, "Expected value mismatch");
1384 zassert_equal(valid_time2.tm_mon, (hook_data->tm_mon + 1), "Expected value mismatch");
1385 zassert_equal(valid_time2.tm_year, (hook_data->tm_year + 1900), "Expected value mismatch");
1386
1387 /* Clean up test */
1388 cleanup_test(NULL);
1389 memset(buffer, 0, sizeof(buffer));
1390 memset(buffer_out, 0, sizeof(buffer_out));
1391 buffer_size = 0;
1392 memset(zse, 0, sizeof(zse));
1393 memset(zsd, 0, sizeof(zsd));
1394 output_decode[0].found = false;
1395 output_decode[1].found = false;
1396 output_decode[2].found = false;
1397
1398 /* Query time and ensure it is not set */
1399 zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
1400 ok = create_mcumgr_datetime_get_packet(zse, true, buffer, buffer_out, &buffer_size);
1401 zassert_true(ok, "Expected packet creation to be successful");
1402
1403 /* Enable dummy SMP backend and ready for usage */
1404 smp_dummy_enable();
1405 smp_dummy_clear_state();
1406
1407 /* Send query command to dummy SMP backend */
1408 (void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1409 smp_dummy_add_data();
1410
1411 /* For a short duration to see if response has been received */
1412 received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1413 zassert_true(received, "Expected to receive data but timed out");
1414
1415 /* Retrieve response buffer and ensure validity */
1416 nb = smp_dummy_get_outgoing();
1417 smp_dummy_disable();
1418
1419 /* Process received data by removing header */
1420 (void)net_buf_pull(nb, sizeof(struct smp_hdr));
1421 zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
1422
1423 ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
1424 zassert_true(ok, "Expected decode to be successful");
1425 zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
1426 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1427 "datetime"), "Did not expect to receive datetime element");
1428 zassert_true(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1429 "rc"), "Expected to receive rc element");
1430 zassert_false(zcbor_map_decode_bulk_key_found(output_decode, ARRAY_SIZE(output_decode),
1431 "err"), "Did not expect to receive err element");
1432 zassert_equal(rc, MGMT_ERR_EBUSY, "Expected 'rc' to be busy");
1433
1434 /* Check hook actions are as expected */
1435 zassert_true(hook_get_ran, "Expected get hook to run");
1436 zassert_false(hook_set_ran, "Did not expect set hook to run");
1437 zassert_false(hook_other_ran, "Did not expect other hooks to run");
1438 zassert_equal(hook_set_data_size, 0, "Expected data size to be 0");
1439 }
1440 #endif
1441
cleanup_test(void * p)1442 static void cleanup_test(void *p)
1443 {
1444 if (nb != NULL) {
1445 net_buf_unref(nb);
1446 nb = NULL;
1447 }
1448
1449 #ifdef CONFIG_MCUMGR_GRP_OS_DATETIME_HOOK
1450 hook_get_ran = false;
1451 hook_set_ran = false;
1452 hook_other_ran = false;
1453 hook_set_data_size = 0;
1454 hook_set_data[0] = 0;
1455 #endif
1456 }
1457
test_main(void)1458 void test_main(void)
1459 {
1460 while (test_state.test_set < OS_MGMT_DATETIME_TEST_SET_COUNT) {
1461 ztest_run_all(&test_state, false, 1, 1);
1462 ++test_state.test_set;
1463 }
1464
1465 ztest_verify_all_test_suites_ran();
1466 }
1467
time_not_set_predicate(const void * state)1468 static bool time_not_set_predicate(const void *state)
1469 {
1470 return ((struct state *)state)->test_set == OS_MGMT_DATETIME_TEST_SET_TIME_NOT_SET;
1471 }
1472
time_set_predicate(const void * state)1473 static bool time_set_predicate(const void *state)
1474 {
1475 return ((struct state *)state)->test_set == OS_MGMT_DATETIME_TEST_SET_TIME_SET;
1476 }
1477
1478 #ifdef CONFIG_MCUMGR_GRP_OS_DATETIME_HOOK
hooks_predicate(const void * state)1479 static bool hooks_predicate(const void *state)
1480 {
1481 return ((struct state *)state)->test_set == OS_MGMT_DATETIME_TEST_SET_HOOKS;
1482 }
1483 #endif
1484
1485 /* Time not set test set */
1486 ZTEST_SUITE(os_mgmt_datetime_not_set, time_not_set_predicate, NULL, NULL, cleanup_test, NULL);
1487
1488 #ifdef CONFIG_MCUMGR_GRP_OS_DATETIME_HOOK
1489 /* Hook test set */
1490 ZTEST_SUITE(os_mgmt_datetime_hook, hooks_predicate, setup_os_datetime_callbacks, NULL,
1491 cleanup_test, destroy_os_datetime_callbacks);
1492 #endif
1493
1494 /* Time set test set */
1495 ZTEST_SUITE(os_mgmt_datetime_set, time_set_predicate, NULL, NULL, cleanup_test, NULL);
1496