1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/net_buf.h>
9 #include <zephyr/storage/flash_map.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/img_mgmt/img_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 <zephyr/sys/byteorder.h>
20 #include <smp_internal.h>
21 #include "smp_test_util.h"
22 
23 #define SMP_RESPONSE_WAIT_TIME 3
24 #define ZCBOR_BUFFER_SIZE 128
25 #define OUTPUT_BUFFER_SIZE 512
26 #define ZCBOR_HISTORY_ARRAY_SIZE 10
27 
28 static struct net_buf *nb;
29 static bool slot_info_image_callback_got;
30 static bool slot_info_slot_callback_got;
31 static bool other_callback_got;
32 static bool block_access;
33 static bool add_field;
34 
35 struct partition_entries_t {
36 	uint8_t partition_id;
37 	uint32_t size;
38 };
39 
40 struct partition_entries_t partition_entries[] = {
41 	{ FIXED_PARTITION_ID(slot0_partition), 0 },
42 	{ FIXED_PARTITION_ID(slot1_partition), 0 },
43 #if FIXED_PARTITION_EXISTS(slot2_partition)
44 	{ FIXED_PARTITION_ID(slot2_partition), 0 },
45 #endif
46 #if FIXED_PARTITION_EXISTS(slot3_partition)
47 	{ FIXED_PARTITION_ID(slot3_partition), 0 },
48 #endif
49 };
50 
cleanup_test(void * p)51 static void cleanup_test(void *p)
52 {
53 	if (nb != NULL) {
54 		net_buf_unref(nb);
55 		nb = NULL;
56 	}
57 
58 	slot_info_image_callback_got = false;
59 	slot_info_slot_callback_got = false;
60 	other_callback_got = false;
61 	block_access = false;
62 	add_field = false;
63 }
64 
65 struct slot_info_t {
66 	uint32_t slot;
67 	bool slot_received;
68 	uint32_t size;
69 	bool size_received;
70 	uint32_t upload_image_id;
71 	bool upload_image_id_received;
72 	uint32_t test2;
73 	bool test2_received;
74 };
75 
76 struct image_info_t {
77 	uint32_t image;
78 	bool image_received;
79 	struct slot_info_t slots[2];
80 	uint32_t max_image_size;
81 	bool max_image_size_received;
82 	uint32_t test1;
83 	bool test1_received;
84 
85 	uint8_t current_slot;
86 };
87 
88 struct receive_info_t {
89 	struct image_info_t images[CONFIG_UPDATEABLE_IMAGE_NUMBER];
90 
91 	uint8_t current_image;
92 };
93 
parse_slot_entries(zcbor_state_t * state,void * user_data)94 static bool parse_slot_entries(zcbor_state_t *state, void *user_data)
95 {
96 	size_t decoded = 0;
97 	struct image_info_t *image_data = (struct image_info_t *)user_data;
98 	bool ok;
99 
100 	if (!zcbor_list_start_decode(state)) {
101 		return false;
102 	}
103 
104 	while (!zcbor_array_at_end(state)) {
105 		struct slot_info_t *slot_data = &image_data->slots[image_data->current_slot];
106 		struct zcbor_map_decode_key_val output_decode[] = {
107 			ZCBOR_MAP_DECODE_KEY_DECODER("slot", zcbor_uint32_decode,
108 						     &slot_data->slot),
109 			ZCBOR_MAP_DECODE_KEY_DECODER("size", zcbor_uint32_decode,
110 						     &slot_data->size),
111 			ZCBOR_MAP_DECODE_KEY_DECODER("upload_image_id", zcbor_uint32_decode,
112 						     &slot_data->upload_image_id),
113 			ZCBOR_MAP_DECODE_KEY_DECODER("test2", zcbor_uint32_decode,
114 						     &slot_data->test2),
115 		};
116 
117 
118 		ok = zcbor_map_decode_bulk(state, output_decode, ARRAY_SIZE(output_decode),
119 					   &decoded) == 0;
120 		zassert_true(ok, "Expected decode to be successful");
121 		zassert_true((decoded == 2 || decoded == 3 || decoded == 4),
122 			     "Expected to receive 2-4 decoded zcbor elements");
123 
124 		slot_data->slot_received = zcbor_map_decode_bulk_key_found(output_decode,
125 									ARRAY_SIZE(output_decode),
126 									"slot");
127 		slot_data->size_received = zcbor_map_decode_bulk_key_found(output_decode,
128 									ARRAY_SIZE(output_decode),
129 									"slot");
130 		slot_data->upload_image_id_received = zcbor_map_decode_bulk_key_found(
131 									output_decode,
132 									ARRAY_SIZE(output_decode),
133 									"upload_image_id");
134 		slot_data->test2_received = zcbor_map_decode_bulk_key_found(output_decode,
135 									ARRAY_SIZE(output_decode),
136 									"test2");
137 
138 		++image_data->current_slot;
139 	}
140 
141 	(void)zcbor_list_end_decode(state);
142 
143 	return true;
144 }
145 
parse_images_entries(zcbor_state_t * state,void * user_data)146 static bool parse_images_entries(zcbor_state_t *state, void *user_data)
147 {
148 	struct receive_info_t *receive_data = (struct receive_info_t *)user_data;
149 	bool ok;
150 
151 	if (!zcbor_list_start_decode(state)) {
152 		return false;
153 	}
154 
155 	while (!zcbor_array_at_end(state)) {
156 		size_t decoded = 0;
157 		struct image_info_t *image_data =
158 					&receive_data->images[receive_data->current_image];
159 
160 		struct zcbor_map_decode_key_val output_decode[] = {
161 			ZCBOR_MAP_DECODE_KEY_DECODER("image", zcbor_uint32_decode,
162 						     &image_data->image),
163 			ZCBOR_MAP_DECODE_KEY_DECODER("slots", parse_slot_entries, image_data),
164 			ZCBOR_MAP_DECODE_KEY_DECODER("max_image_size", zcbor_uint32_decode,
165 						     &image_data->max_image_size),
166 			ZCBOR_MAP_DECODE_KEY_DECODER("test1", zcbor_uint32_decode,
167 						     &image_data->test1),
168 		};
169 
170 		ok = zcbor_map_decode_bulk(state, output_decode, ARRAY_SIZE(output_decode),
171 					   &decoded) == 0;
172 		zassert_true(ok, "Expected decode to be successful");
173 		zassert_true((decoded == 2 || decoded == 3 || decoded == 4),
174 			     "Expected to receive 2-4 decoded zcbor elements");
175 
176 		image_data->image_received = zcbor_map_decode_bulk_key_found(output_decode,
177 								ARRAY_SIZE(output_decode),
178 								"image");
179 		image_data->max_image_size_received = zcbor_map_decode_bulk_key_found(
180 								output_decode,
181 								ARRAY_SIZE(output_decode),
182 								"max_image_size");
183 		image_data->test1_received = zcbor_map_decode_bulk_key_found(output_decode,
184 								ARRAY_SIZE(output_decode),
185 								"test1");
186 
187 		++receive_data->current_image;
188 	}
189 
190 	(void)zcbor_list_end_decode(state);
191 
192 	return true;
193 }
194 
ZTEST(img_mgmt,test_list)195 ZTEST(img_mgmt, test_list)
196 {
197 	uint8_t buffer[ZCBOR_BUFFER_SIZE];
198 	uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
199 	bool ok;
200 	uint16_t buffer_size;
201 	zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
202 	zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
203 	bool received;
204 	struct smp_hdr *header;
205 	size_t decoded = 0;
206 	struct receive_info_t receive_response = { 0 };
207 	uint8_t i = 0;
208 
209 	struct zcbor_map_decode_key_val output_decode[] = {
210 		ZCBOR_MAP_DECODE_KEY_DECODER("images", parse_images_entries, &receive_response),
211 	};
212 
213 	memset(buffer, 0, sizeof(buffer));
214 	memset(buffer_out, 0, sizeof(buffer_out));
215 	buffer_size = 0;
216 	memset(zse, 0, sizeof(zse));
217 	memset(zsd, 0, sizeof(zsd));
218 
219 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
220 
221 	ok = create_img_mgmt_slot_info_packet(zse, buffer, buffer_out, &buffer_size);
222 	zassert_true(ok, "Expected packet creation to be successful");
223 
224 	/* Enable dummy SMP backend and ready for usage */
225 	smp_dummy_enable();
226 	smp_dummy_clear_state();
227 
228 	/* Send query command to dummy SMP backend */
229 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
230 	smp_dummy_add_data();
231 
232 	/* Wait for a short duration to see if response has been received */
233 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
234 	zassert_true(received, "Expected to receive data but timed out");
235 
236 	/* Retrieve response buffer */
237 	nb = smp_dummy_get_outgoing();
238 	smp_dummy_disable();
239 
240 	/* Check response is as expected */
241 	header = net_buf_pull_mem(nb, sizeof(struct smp_hdr));
242 
243 	zassert_equal(header->nh_flags, 0, "SMP header flags mismatch");
244 	zassert_equal(header->nh_op, MGMT_OP_READ_RSP, "SMP header operation mismatch");
245 	zassert_equal(header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_IMAGE),
246 		      "SMP header group mismatch");
247 	zassert_equal(header->nh_seq, 1, "SMP header sequence number mismatch");
248 	zassert_equal(header->nh_id, IMG_MGMT_ID_SLOT_INFO, "SMP header command ID mismatch");
249 	zassert_equal(header->nh_version, 1, "SMP header version mismatch");
250 
251 	/* Get the response value to compare */
252 	zcbor_new_decode_state(zsd, 8, nb->data, nb->len, 1, NULL, 0);
253 	ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
254 	zassert_true(ok, "Expected decode to be successful");
255 	zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
256 
257 	/* Ensure the right amount of data was read and that the values match */
258 	zassert_equal(receive_response.current_image, CONFIG_UPDATEABLE_IMAGE_NUMBER,
259 		      "Expected data mismatch");
260 
261 	while (i < receive_response.current_image) {
262 		struct image_info_t *current_image =
263 				&receive_response.images[i];
264 		uint8_t l = 0;
265 #ifdef CONFIG_MCUMGR_GRP_IMG_TOO_LARGE_SYSBUILD
266 		uint32_t expected_max_size;
267 #endif
268 
269 		zassert_equal(current_image->current_slot, 2, "Expected data mismatch");
270 		zassert_true(current_image->image_received, "Expected data mismatch");
271 
272 #ifdef CONFIG_MCUMGR_GRP_IMG_TOO_LARGE_SYSBUILD
273 		expected_max_size = MAX(partition_entries[(i * 2)].size,
274 					partition_entries[((i * 2) + 1)].size);
275 		expected_max_size -= CONFIG_MCUBOOT_UPDATE_FOOTER_SIZE;
276 
277 		zassert_true(current_image->max_image_size_received, "Expected data mismatch");
278 		zassert_equal(current_image->max_image_size, expected_max_size,
279 			      "Expected data mismatch");
280 #else
281 		zassert_false(current_image->max_image_size_received, "Expected data mismatch");
282 #endif
283 
284 		zassert_false(current_image->test1_received, "Expected data mismatch");
285 
286 		while (l < current_image->current_slot) {
287 			struct slot_info_t *current_slot =
288 					&current_image->slots[l];
289 
290 			zassert_true(current_slot->slot_received, "Expected data mismatch");
291 			zassert_true(current_slot->size_received, "Expected data mismatch");
292 			zassert_false(current_slot->test2_received, "Expected data mismatch");
293 
294 			if (l == 1) {
295 				zassert_true(current_slot->upload_image_id_received,
296 					     "Expected data mismatch");
297 				zassert_equal(current_slot->upload_image_id, i,
298 					      "Expected data mismatch");
299 			} else {
300 				zassert_false(current_slot->upload_image_id_received,
301 					      "Expected data mismatch");
302 			}
303 
304 			zassert_equal(current_slot->slot, l, "Expected data mismatch");
305 			zassert_equal(current_slot->size, partition_entries[((i * 2) + l)].size,
306 				      "Expected data mismatch");
307 
308 			++l;
309 		}
310 
311 		++i;
312 	}
313 
314 	zassert_false(slot_info_image_callback_got, "Did not expect to get image callback");
315 	zassert_false(slot_info_slot_callback_got, "Did not expect to get slot callback");
316 	zassert_false(other_callback_got, "Did not expect to get other callback");
317 
318 	/* Clean up test */
319 	cleanup_test(NULL);
320 }
321 
ZTEST(img_mgmt,test_blocked)322 ZTEST(img_mgmt, test_blocked)
323 {
324 	uint8_t buffer[ZCBOR_BUFFER_SIZE];
325 	uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
326 	bool ok;
327 	uint16_t buffer_size;
328 	zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
329 	zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
330 	bool received;
331 	struct smp_hdr *header;
332 	size_t decoded = 0;
333 	uint32_t rc = 0;
334 
335 	struct zcbor_map_decode_key_val output_decode[] = {
336 		ZCBOR_MAP_DECODE_KEY_DECODER("rc", zcbor_uint32_decode, &rc),
337 	};
338 
339 	memset(buffer, 0, sizeof(buffer));
340 	memset(buffer_out, 0, sizeof(buffer_out));
341 	buffer_size = 0;
342 	memset(zse, 0, sizeof(zse));
343 	memset(zsd, 0, sizeof(zsd));
344 
345 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
346 
347 	ok = create_img_mgmt_slot_info_packet(zse, buffer, buffer_out, &buffer_size);
348 	zassert_true(ok, "Expected packet creation to be successful");
349 
350 	block_access = true;
351 
352 	/* Enable dummy SMP backend and ready for usage */
353 	smp_dummy_enable();
354 	smp_dummy_clear_state();
355 
356 	/* Send query command to dummy SMP backend */
357 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
358 	smp_dummy_add_data();
359 
360 	/* Wait for a short duration to see if response has been received */
361 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
362 	zassert_true(received, "Expected to receive data but timed out");
363 
364 	/* Retrieve response buffer */
365 	nb = smp_dummy_get_outgoing();
366 	smp_dummy_disable();
367 
368 	/* Check response is as expected */
369 	header = net_buf_pull_mem(nb, sizeof(struct smp_hdr));
370 
371 	zassert_equal(header->nh_flags, 0, "SMP header flags mismatch");
372 	zassert_equal(header->nh_op, MGMT_OP_READ_RSP, "SMP header operation mismatch");
373 	zassert_equal(header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_IMAGE),
374 		      "SMP header group mismatch");
375 	zassert_equal(header->nh_seq, 1, "SMP header sequence number mismatch");
376 	zassert_equal(header->nh_id, IMG_MGMT_ID_SLOT_INFO, "SMP header command ID mismatch");
377 	zassert_equal(header->nh_version, 1, "SMP header version mismatch");
378 
379 	/* Get the response value to compare */
380 	zcbor_new_decode_state(zsd, 8, nb->data, nb->len, 1, NULL, 0);
381 	ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
382 	zassert_true(ok, "Expected decode to be successful");
383 	zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
384 
385 	zassert_true(slot_info_slot_callback_got, "Expected callback to have ran");
386 	zassert_false(slot_info_image_callback_got, "Did not expect other callback to have ran");
387 	zassert_false(other_callback_got, "Did not expect invalid callback to have ran");
388 	zassert_equal(rc, MGMT_ERR_EPERUSER, "Expected error was not returned");
389 
390 	/* Clean up test */
391 	cleanup_test(NULL);
392 }
393 
ZTEST(img_mgmt,test_callback)394 ZTEST(img_mgmt, test_callback)
395 {
396 	uint8_t buffer[ZCBOR_BUFFER_SIZE];
397 	uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
398 	bool ok;
399 	uint16_t buffer_size;
400 	zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
401 	zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
402 	bool received;
403 	struct smp_hdr *header;
404 	size_t decoded = 0;
405 	struct receive_info_t receive_response = { 0 };
406 	uint8_t i = 0;
407 
408 	struct zcbor_map_decode_key_val output_decode[] = {
409 		ZCBOR_MAP_DECODE_KEY_DECODER("images", parse_images_entries, &receive_response),
410 	};
411 
412 	memset(buffer, 0, sizeof(buffer));
413 	memset(buffer_out, 0, sizeof(buffer_out));
414 	buffer_size = 0;
415 	memset(zse, 0, sizeof(zse));
416 	memset(zsd, 0, sizeof(zsd));
417 
418 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
419 
420 	ok = create_img_mgmt_slot_info_packet(zse, buffer, buffer_out, &buffer_size);
421 	zassert_true(ok, "Expected packet creation to be successful");
422 
423 	add_field = true;
424 
425 	/* Enable dummy SMP backend and ready for usage */
426 	smp_dummy_enable();
427 	smp_dummy_clear_state();
428 
429 	/* Send query command to dummy SMP backend */
430 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
431 	smp_dummy_add_data();
432 
433 	/* Wait for a short duration to see if response has been received */
434 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
435 	zassert_true(received, "Expected to receive data but timed out");
436 
437 	/* Retrieve response buffer */
438 	nb = smp_dummy_get_outgoing();
439 	smp_dummy_disable();
440 
441 	/* Check response is as expected */
442 	header = net_buf_pull_mem(nb, sizeof(struct smp_hdr));
443 
444 	zassert_equal(header->nh_flags, 0, "SMP header flags mismatch");
445 	zassert_equal(header->nh_op, MGMT_OP_READ_RSP, "SMP header operation mismatch");
446 	zassert_equal(header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_IMAGE),
447 		      "SMP header group mismatch");
448 	zassert_equal(header->nh_seq, 1, "SMP header sequence number mismatch");
449 	zassert_equal(header->nh_id, IMG_MGMT_ID_SLOT_INFO, "SMP header command ID mismatch");
450 	zassert_equal(header->nh_version, 1, "SMP header version mismatch");
451 
452 	/* Get the response value to compare */
453 	zcbor_new_decode_state(zsd, 8, nb->data, nb->len, 1, NULL, 0);
454 	ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
455 	zassert_true(ok, "Expected decode to be successful");
456 	zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
457 
458 	/* Ensure the right amount of data was read and that the values match */
459 	zassert_equal(receive_response.current_image, CONFIG_UPDATEABLE_IMAGE_NUMBER,
460 		      "Expected data mismatch");
461 
462 	while (i < receive_response.current_image) {
463 		struct image_info_t *current_image =
464 				&receive_response.images[i];
465 		uint8_t l = 0;
466 #ifdef CONFIG_MCUMGR_GRP_IMG_TOO_LARGE_SYSBUILD
467 		uint32_t expected_max_size;
468 #endif
469 
470 		zassert_equal(current_image->current_slot, 2, "Expected data mismatch");
471 		zassert_true(current_image->image_received, "Expected data mismatch");
472 
473 #ifdef CONFIG_MCUMGR_GRP_IMG_TOO_LARGE_SYSBUILD
474 		expected_max_size = MAX(partition_entries[(i * 2)].size,
475 					partition_entries[((i * 2) + 1)].size);
476 		expected_max_size -= CONFIG_MCUBOOT_UPDATE_FOOTER_SIZE;
477 
478 		zassert_true(current_image->max_image_size_received, "Expected data mismatch");
479 		zassert_equal(current_image->max_image_size, expected_max_size,
480 			      "Expected data mismatch");
481 #else
482 		zassert_false(current_image->max_image_size_received, "Expected data mismatch");
483 #endif
484 
485 		zassert_true(current_image->test1_received, "Expected data mismatch");
486 		zassert_equal(current_image->test1, (i + 18), "Expected data mismatch");
487 
488 		while (l < current_image->current_slot) {
489 			struct slot_info_t *current_slot =
490 					&current_image->slots[l];
491 
492 			zassert_true(current_slot->test2_received, "Expected data mismatch");
493 			zassert_true(current_slot->slot_received, "Expected data mismatch");
494 			zassert_true(current_slot->size_received, "Expected data mismatch");
495 
496 			if (l == 1) {
497 				zassert_true(current_slot->upload_image_id_received,
498 					     "Expected data mismatch");
499 				zassert_equal(current_slot->upload_image_id, i,
500 					      "Expected data mismatch");
501 			} else {
502 				zassert_false(current_slot->upload_image_id_received,
503 					      "Expected data mismatch");
504 			}
505 
506 			zassert_equal(current_slot->slot, l, "Expected data mismatch");
507 			zassert_equal(current_slot->size, partition_entries[((i * 2) + l)].size,
508 				      "Expected data mismatch");
509 			zassert_equal(current_slot->test2,
510 				      ((current_image->image * 2) + current_slot->slot + 3),
511 				      "Expected data mismatch");
512 
513 			++l;
514 		}
515 
516 		++i;
517 	}
518 
519 	zassert_true(slot_info_image_callback_got, "Expected to get image callback");
520 	zassert_true(slot_info_slot_callback_got, "Expected to get slot callback");
521 	zassert_false(other_callback_got, "Did not expect to get other callback");
522 
523 	/* Clean up test */
524 	cleanup_test(NULL);
525 }
526 
mgmt_event_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)527 static enum mgmt_cb_return mgmt_event_cmd_callback(uint32_t event, enum mgmt_cb_return prev_status,
528 						   int32_t *rc, uint16_t *group, bool *abort_more,
529 						   void *data, size_t data_size)
530 {
531 	if (event == MGMT_EVT_OP_IMG_MGMT_SLOT_INFO_IMAGE) {
532 		if (add_field) {
533 			struct img_mgmt_slot_info_image *img_data =
534 							(struct img_mgmt_slot_info_image *)data;
535 			uint32_t temp = (img_data->image + 18);
536 			bool ok;
537 
538 			slot_info_image_callback_got = true;
539 
540 			ok = zcbor_tstr_put_lit(img_data->zse, "test1") &&
541 			     zcbor_uint32_encode(img_data->zse, &temp);
542 
543 			if (!ok) {
544 				*rc = MGMT_ERR_EUNKNOWN;
545 				return MGMT_CB_ERROR_RC;
546 			}
547 		}
548 	} else if (event == MGMT_EVT_OP_IMG_MGMT_SLOT_INFO_SLOT) {
549 		if (block_access) {
550 			slot_info_slot_callback_got = true;
551 			*rc = MGMT_ERR_EPERUSER;
552 			return MGMT_CB_ERROR_RC;
553 		} else if (add_field) {
554 			struct img_mgmt_slot_info_slot *slot_data =
555 							(struct img_mgmt_slot_info_slot *)data;
556 			uint32_t temp = ((slot_data->image * 2) + slot_data->slot + 3);
557 			bool ok;
558 
559 			slot_info_slot_callback_got = true;
560 
561 			ok = zcbor_tstr_put_lit(slot_data->zse, "test2") &&
562 			     zcbor_uint32_encode(slot_data->zse, &temp);
563 
564 			if (!ok) {
565 				*rc = MGMT_ERR_EUNKNOWN;
566 				return MGMT_CB_ERROR_RC;
567 			}
568 		}
569 	} else {
570 		other_callback_got = true;
571 	}
572 
573 	return MGMT_CB_OK;
574 }
575 
576 static struct mgmt_callback mgmt_event_callback = {
577 	.callback = mgmt_event_cmd_callback,
578 	.event_id = (MGMT_EVT_OP_IMG_MGMT_SLOT_INFO_IMAGE | MGMT_EVT_OP_IMG_MGMT_SLOT_INFO_SLOT),
579 };
580 
setup_test(void)581 static void *setup_test(void)
582 {
583 	int rc;
584 	uint8_t i = 0;
585 
586 	while (i < ARRAY_SIZE(partition_entries)) {
587 		const struct flash_area *fa;
588 
589 		rc = flash_area_open(partition_entries[i].partition_id, &fa);
590 		zassert_ok(rc, "Expected data mismatch");
591 		flash_area_close(fa);
592 
593 		partition_entries[i].size = fa->fa_size;
594 
595 		++i;
596 	}
597 
598 	mgmt_callback_register(&mgmt_event_callback);
599 
600 	return NULL;
601 }
602 
603 ZTEST_SUITE(img_mgmt, NULL, setup_test, NULL, cleanup_test, NULL);
604