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