1 /*
2  * Copyright (c) 2023 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/mgmt/mcumgr/mgmt/mgmt.h>
10 #include <zephyr/mgmt/mcumgr/transport/smp_dummy.h>
11 #include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>
12 #include <zephyr/mgmt/mcumgr/grp/settings_mgmt/settings_mgmt.h>
13 #include <zcbor_common.h>
14 #include <zcbor_decode.h>
15 #include <zcbor_encode.h>
16 #include <mgmt/mcumgr/util/zcbor_bulk.h>
17 #include <string.h>
18 #include <zephyr/settings/settings.h>
19 #include <zephyr/sys/byteorder.h>
20 #include <smp_internal.h>
21 #include "smp_test_util.h"
22 #include "settings.h"
23 
24 #define SMP_RESPONSE_WAIT_TIME 3
25 #define ZCBOR_BUFFER_SIZE 64
26 #define OUTPUT_BUFFER_SIZE 64
27 #define ZCBOR_HISTORY_ARRAY_SIZE 4
28 
29 #define TEST_RESPONSE_OK_DATA_LENGTH sizeof(test_response_ok_data)
30 #define TEST_RESPONSE_OK_LENGTH (sizeof(struct smp_hdr) + TEST_RESPONSE_OK_DATA_LENGTH)
31 #define TEST_RESPONSE_ERROR_DATA_LENGTH sizeof(test_response_error_data)
32 #define TEST_RESPONSE_ERROR_LENGTH (sizeof(struct smp_hdr) + TEST_RESPONSE_ERROR_DATA_LENGTH)
33 
34 #define TEST_RESPONSE_READ_DATA_LENGTH (sizeof(test_response_read_data_start) + \
35 					sizeof(test_response_read_data_end))
36 #define TEST_RESPONSE_READ_LENGTH (sizeof(struct smp_hdr) + TEST_RESPONSE_READ_DATA_LENGTH)
37 
38 static struct net_buf *nb;
39 static bool access_read_got;
40 static bool access_write_got;
41 static bool access_delete_got;
42 static bool access_load_got;
43 static bool access_save_got;
44 static bool access_commit_got;
45 static bool access_invalid_got;
46 static bool event_invalid_got;
47 static bool block_access;
48 static char access_name[32];
49 
50 static const uint8_t val_aa_valid_1[4] = {
51 	83, 86, 77, 15,
52 };
53 static const uint8_t val_aa_valid_2[4] = {
54 	93, 35, 86, 92,
55 };
56 static const uint8_t val_aa_invalid[4] = {
57 	0, 0, 0, 0
58 };
59 static const uint8_t val_bb_valid_1 = 0xab;
60 
61 static const uint8_t test_response_ok_data[2] = {
62 	0xbf, 0xff
63 };
64 
65 static const uint8_t test_response_error_data[8] = {
66 	0xbf, 0x62, 0x72, 0x63, 0x19, 0x01, 0x00, 0xff
67 };
68 
69 static const uint8_t test_response_read_data_start[5] = {
70 	0xbf, 0x63, 0x76, 0x61, 0x6c
71 };
72 
73 static const uint8_t test_response_read_data_end[1] = {
74 	0xff
75 };
76 
cleanup_test(void * p)77 static void cleanup_test(void *p)
78 {
79 	if (nb != NULL) {
80 		net_buf_unref(nb);
81 		nb = NULL;
82 	}
83 
84 	access_read_got = false;
85 	access_write_got = false;
86 	access_delete_got = false;
87 	access_load_got = false;
88 	access_save_got = false;
89 	access_commit_got = false;
90 	access_invalid_got = false;
91 	event_invalid_got = false;
92 	block_access = false;
93 	memset(access_name, 0, sizeof(access_name));
94 
95 	settings_state_reset();
96 }
97 
ZTEST(settings_mgmt,test_commit)98 ZTEST(settings_mgmt, test_commit)
99 {
100 	uint8_t buffer[ZCBOR_BUFFER_SIZE];
101 	uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
102 	bool ok;
103 	uint16_t buffer_size;
104 	zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
105 	zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
106 	bool received;
107 	bool set_called;
108 	bool get_called;
109 	bool export_called;
110 	bool commit_called;
111 	struct smp_hdr *header;
112 
113 	memset(buffer, 0, sizeof(buffer));
114 	memset(buffer_out, 0, sizeof(buffer_out));
115 	buffer_size = 0;
116 	memset(zse, 0, sizeof(zse));
117 	memset(zsd, 0, sizeof(zsd));
118 
119 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
120 
121 	ok = create_settings_mgmt_commit_packet(zse, buffer, buffer_out, &buffer_size);
122 	zassert_true(ok, "Expected packet creation to be successful");
123 
124 	/* Enable dummy SMP backend and ready for usage */
125 	smp_dummy_enable();
126 	smp_dummy_clear_state();
127 
128 	/* Send query command to dummy SMP backend */
129 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
130 	smp_dummy_add_data();
131 
132 	/* For a short duration to see if response has been received */
133 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
134 	zassert_true(received, "Expected to receive data but timed out");
135 
136 	/* Retrieve response buffer */
137 	nb = smp_dummy_get_outgoing();
138 	smp_dummy_disable();
139 
140 	/* Check response is as expected */
141 	zassert_equal(nb->len, TEST_RESPONSE_OK_LENGTH, "SMP response mismatch");
142 	header = (struct smp_hdr *)nb->data;
143 	zassert_equal(header->nh_len, sys_cpu_to_be16(TEST_RESPONSE_OK_DATA_LENGTH),
144 		      "SMP header length mismatch");
145 	zassert_equal(header->nh_flags, 0, "SMP header flags mismatch");
146 	zassert_equal(header->nh_op, MGMT_OP_WRITE_RSP, "SMP header operation mismatch");
147 	zassert_equal(header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
148 		      "SMP header group mismatch");
149 	zassert_equal(header->nh_seq, 1, "SMP header sequence number mismatch");
150 	zassert_equal(header->nh_id, SETTINGS_MGMT_ID_COMMIT, "SMP header command ID mismatch");
151 	zassert_equal(header->nh_version, 1, "SMP header version mismatch");
152 	zassert_mem_equal(&nb->data[(TEST_RESPONSE_OK_LENGTH - TEST_RESPONSE_OK_DATA_LENGTH)],
153 			  test_response_ok_data, TEST_RESPONSE_OK_DATA_LENGTH,
154 			  "SMP data mismatch");
155 
156 	/* Check events */
157 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
158 	zassert_false(access_read_got, "Did not expect read access notification");
159 	zassert_false(access_write_got, "Did not expect write access notification");
160 	zassert_false(access_delete_got, "Did not expect delete access notification");
161 	zassert_false(access_load_got, "Did not expect load access notification");
162 	zassert_false(access_save_got, "Did not expect save access notification");
163 	zassert_true(access_commit_got, "Expected commit access notification");
164 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
165 	zassert_false(event_invalid_got, "Did not expect an invalid event");
166 
167 	zassert_false(set_called, "Did not expect setting set function to be called");
168 	zassert_false(get_called, "Did not expect setting get function to be called");
169 	zassert_false(export_called, "Did not expect setting export function to be called");
170 	zassert_true(commit_called, "Expected setting commit function to be called");
171 
172 	/* Clean up test */
173 	cleanup_test(NULL);
174 
175 	/* Force notification callback to return an error */
176 	block_access = true;
177 
178 	/* Enable dummy SMP backend and ready for usage */
179 	smp_dummy_enable();
180 	smp_dummy_clear_state();
181 
182 	/* Send query command to dummy SMP backend */
183 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
184 	smp_dummy_add_data();
185 
186 	/* For a short duration to see if response has been received */
187 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
188 	zassert_true(received, "Expected to receive data but timed out");
189 
190 	/* Retrieve response buffer */
191 	nb = smp_dummy_get_outgoing();
192 	smp_dummy_disable();
193 
194 	/* Check response is as expected */
195 	zassert_equal(nb->len, TEST_RESPONSE_ERROR_LENGTH, "SMP response mismatch");
196 	header = (struct smp_hdr *)nb->data;
197 	zassert_equal(header->nh_len, sys_cpu_to_be16(TEST_RESPONSE_ERROR_DATA_LENGTH),
198 		      "SMP header length mismatch");
199 	zassert_equal(header->nh_flags, 0, "SMP header flags mismatch");
200 	zassert_equal(header->nh_op, MGMT_OP_WRITE_RSP, "SMP header operation mismatch");
201 	zassert_equal(header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
202 		      "SMP header group mismatch");
203 	zassert_equal(header->nh_seq, 1, "SMP header sequence number mismatch");
204 	zassert_equal(header->nh_id, SETTINGS_MGMT_ID_COMMIT, "SMP header command ID mismatch");
205 	zassert_equal(header->nh_version, 1, "SMP header version mismatch");
206 	zassert_mem_equal(&nb->data[(TEST_RESPONSE_ERROR_LENGTH -
207 				     TEST_RESPONSE_ERROR_DATA_LENGTH)],
208 			  test_response_error_data, TEST_RESPONSE_ERROR_DATA_LENGTH,
209 			  "SMP data mismatch");
210 
211 	/* Check events */
212 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
213 	zassert_false(access_read_got, "Did not expect read access notification");
214 	zassert_false(access_write_got, "Did not expect write access notification");
215 	zassert_false(access_delete_got, "Did not expect delete access notification");
216 	zassert_false(access_load_got, "Did not expect load access notification");
217 	zassert_false(access_save_got, "Did not expect save access notification");
218 	zassert_true(access_commit_got, "Expected commit access notification");
219 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
220 	zassert_false(event_invalid_got, "Did not expect an invalid event");
221 
222 	zassert_false(set_called, "Did not expect setting set function to be called");
223 	zassert_false(get_called, "Did not expect setting get function to be called");
224 	zassert_false(export_called, "Did not expect setting export function to be called");
225 	zassert_false(commit_called, "Did not expect setting commit function to be called");
226 }
227 
ZTEST(settings_mgmt,test_save)228 ZTEST(settings_mgmt, test_save)
229 {
230 	uint8_t buffer[ZCBOR_BUFFER_SIZE];
231 	uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
232 	bool ok;
233 	uint16_t buffer_size;
234 	zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
235 	zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
236 	bool received;
237 	bool set_called;
238 	bool get_called;
239 	bool export_called;
240 	bool commit_called;
241 	struct smp_hdr *header;
242 
243 	memset(buffer, 0, sizeof(buffer));
244 	memset(buffer_out, 0, sizeof(buffer_out));
245 	buffer_size = 0;
246 	memset(zse, 0, sizeof(zse));
247 	memset(zsd, 0, sizeof(zsd));
248 
249 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
250 
251 	ok = create_settings_mgmt_save_packet(zse, buffer, buffer_out, &buffer_size);
252 	zassert_true(ok, "Expected packet creation to be successful");
253 
254 	/* Enable dummy SMP backend and ready for usage */
255 	smp_dummy_enable();
256 	smp_dummy_clear_state();
257 
258 	/* Send query command to dummy SMP backend */
259 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
260 	smp_dummy_add_data();
261 
262 	/* For a short duration to see if response has been received */
263 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
264 	zassert_true(received, "Expected to receive data but timed out");
265 
266 	/* Retrieve response buffer */
267 	nb = smp_dummy_get_outgoing();
268 	smp_dummy_disable();
269 
270 	/* Check response is as expected */
271 	zassert_equal(nb->len, TEST_RESPONSE_OK_LENGTH, "SMP response mismatch");
272 	header = (struct smp_hdr *)nb->data;
273 	zassert_equal(header->nh_len, sys_cpu_to_be16(TEST_RESPONSE_OK_DATA_LENGTH),
274 		      "SMP header length mismatch");
275 	zassert_equal(header->nh_flags, 0, "SMP header flags mismatch");
276 	zassert_equal(header->nh_op, MGMT_OP_WRITE_RSP, "SMP header operation mismatch");
277 	zassert_equal(header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
278 		      "SMP header group mismatch");
279 	zassert_equal(header->nh_seq, 1, "SMP header sequence number mismatch");
280 	zassert_equal(header->nh_id, SETTINGS_MGMT_ID_LOAD_SAVE, "SMP header command ID mismatch");
281 	zassert_equal(header->nh_version, 1, "SMP header version mismatch");
282 	zassert_mem_equal(&nb->data[(TEST_RESPONSE_OK_LENGTH - TEST_RESPONSE_OK_DATA_LENGTH)],
283 			  test_response_ok_data, TEST_RESPONSE_OK_DATA_LENGTH,
284 			  "SMP data mismatch");
285 
286 	/* Check events */
287 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
288 	zassert_false(access_read_got, "Did not expect read access notification");
289 	zassert_false(access_write_got, "Did not expect write access notification");
290 	zassert_false(access_delete_got, "Did not expect delete access notification");
291 	zassert_false(access_load_got, "Did not expect load access notification");
292 	zassert_true(access_save_got, "Expected save access notification");
293 	zassert_false(access_commit_got, "Did not expect commit access notification");
294 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
295 	zassert_false(event_invalid_got, "Did not expect an invalid event");
296 
297 	zassert_false(set_called, "Did not expect setting set function to be called");
298 	zassert_false(get_called, "Did not expect setting get function to be called");
299 	zassert_true(export_called, "Expected setting export function to be called");
300 	zassert_false(commit_called, "Did not expect setting commit function to be called");
301 
302 	/* Clean up test */
303 	cleanup_test(NULL);
304 
305 	/* Force notification callback to return an error */
306 	block_access = true;
307 
308 	/* Enable dummy SMP backend and ready for usage */
309 	smp_dummy_enable();
310 	smp_dummy_clear_state();
311 
312 	/* Send query command to dummy SMP backend */
313 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
314 	smp_dummy_add_data();
315 
316 	/* For a short duration to see if response has been received */
317 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
318 	zassert_true(received, "Expected to receive data but timed out");
319 
320 	/* Retrieve response buffer */
321 	nb = smp_dummy_get_outgoing();
322 	smp_dummy_disable();
323 
324 	/* Check response is as expected */
325 	zassert_equal(nb->len, TEST_RESPONSE_ERROR_LENGTH, "SMP response mismatch");
326 	header = (struct smp_hdr *)nb->data;
327 	zassert_equal(header->nh_len, sys_cpu_to_be16(TEST_RESPONSE_ERROR_DATA_LENGTH),
328 		      "SMP header length mismatch");
329 	zassert_equal(header->nh_flags, 0, "SMP header flags mismatch");
330 	zassert_equal(header->nh_op, MGMT_OP_WRITE_RSP, "SMP header operation mismatch");
331 	zassert_equal(header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
332 		      "SMP header group mismatch");
333 	zassert_equal(header->nh_seq, 1, "SMP header sequence number mismatch");
334 	zassert_equal(header->nh_id, SETTINGS_MGMT_ID_LOAD_SAVE, "SMP header command ID mismatch");
335 	zassert_equal(header->nh_version, 1, "SMP header version mismatch");
336 	zassert_mem_equal(&nb->data[(TEST_RESPONSE_ERROR_LENGTH -
337 				     TEST_RESPONSE_ERROR_DATA_LENGTH)],
338 			  test_response_error_data, TEST_RESPONSE_ERROR_DATA_LENGTH,
339 			  "SMP data mismatch");
340 
341 	/* Check events */
342 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
343 	zassert_false(access_read_got, "Did not expect read access notification");
344 	zassert_false(access_write_got, "Did not expect write access notification");
345 	zassert_false(access_delete_got, "Did not expect delete access notification");
346 	zassert_false(access_load_got, "Did not expect load access notification");
347 	zassert_true(access_save_got, "Expected save access notification");
348 	zassert_false(access_commit_got, "Did not expect commit access notification");
349 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
350 	zassert_false(event_invalid_got, "Did not expect an invalid event");
351 
352 	zassert_false(set_called, "Did not expect setting set function to be called");
353 	zassert_false(get_called, "Did not expect setting get function to be called");
354 	zassert_false(export_called, "Did not expect setting export function to be called");
355 	zassert_false(commit_called, "Did not expect setting commit function to be called");
356 }
357 
ZTEST(settings_mgmt,test_set_read)358 ZTEST(settings_mgmt, test_set_read)
359 {
360 	uint8_t buffer[ZCBOR_BUFFER_SIZE];
361 	uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
362 	bool ok;
363 	uint16_t buffer_size;
364 	zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
365 	zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
366 	bool received;
367 	bool set_called;
368 	bool get_called;
369 	bool export_called;
370 	bool commit_called;
371 	struct smp_hdr *header;
372 	struct zcbor_string tmp_data = { 0 };
373 	struct smp_hdr *smp_header;
374 	size_t decoded = 0;
375 	uint8_t loop_number = 0;
376 
377 	struct zcbor_map_decode_key_val output_decode[] = {
378 		ZCBOR_MAP_DECODE_KEY_DECODER("val", zcbor_bstr_decode, &tmp_data),
379 	};
380 
381 	memset(buffer, 0, sizeof(buffer));
382 	memset(buffer_out, 0, sizeof(buffer_out));
383 	buffer_size = 0;
384 	memset(zse, 0, sizeof(zse));
385 	memset(zsd, 0, sizeof(zsd));
386 
387 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
388 
389 	/* Set "test_val/aa" value */
390 	ok = create_settings_mgmt_write_packet(zse, buffer, buffer_out, &buffer_size,
391 					       "test_val/aa", val_aa_valid_1,
392 					       sizeof(val_aa_valid_1));
393 	zassert_true(ok, "Expected packet creation to be successful");
394 
395 	/* Enable dummy SMP backend and ready for usage */
396 	smp_dummy_enable();
397 	smp_dummy_clear_state();
398 
399 	/* Send query command to dummy SMP backend */
400 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
401 	smp_dummy_add_data();
402 
403 	/* For a short duration to see if response has been received */
404 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
405 	zassert_true(received, "Expected to receive data but timed out");
406 
407 	/* Retrieve response buffer */
408 	nb = smp_dummy_get_outgoing();
409 	smp_dummy_disable();
410 
411 	/* Check response is as expected */
412 	zassert_equal(nb->len, TEST_RESPONSE_OK_LENGTH, "SMP response mismatch");
413 	header = (struct smp_hdr *)nb->data;
414 	zassert_equal(header->nh_len, sys_cpu_to_be16(TEST_RESPONSE_OK_DATA_LENGTH),
415 		      "SMP header length mismatch");
416 	zassert_equal(header->nh_flags, 0, "SMP header flags mismatch");
417 	zassert_equal(header->nh_op, MGMT_OP_WRITE_RSP, "SMP header operation mismatch");
418 	zassert_equal(header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
419 		      "SMP header group mismatch");
420 	zassert_equal(header->nh_seq, 1, "SMP header sequence number mismatch");
421 	zassert_equal(header->nh_id, SETTINGS_MGMT_ID_READ_WRITE,
422 		      "SMP header command ID mismatch");
423 	zassert_equal(header->nh_version, 1, "SMP header version mismatch");
424 	zassert_mem_equal(&nb->data[(TEST_RESPONSE_OK_LENGTH - TEST_RESPONSE_OK_DATA_LENGTH)],
425 			  test_response_ok_data, TEST_RESPONSE_OK_DATA_LENGTH,
426 			  "SMP data mismatch");
427 
428 	/* Check events */
429 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
430 	zassert_false(access_read_got, "Did not expect read access notification");
431 	zassert_true(access_write_got, "Expected write access notification");
432 	zassert_false(access_delete_got, "Did not expect delete access notification");
433 	zassert_false(access_load_got, "Did not expect load access notification");
434 	zassert_false(access_save_got, "Did not expect save access notification");
435 	zassert_false(access_commit_got, "Did not expect commit access notification");
436 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
437 	zassert_false(event_invalid_got, "Did not expect an invalid event");
438 
439 	zassert_true(set_called, "Expected setting set function to be called");
440 	zassert_false(get_called, "Did not expect setting get function to be called");
441 	zassert_false(export_called, "Did not expect setting export function to be called");
442 	zassert_false(commit_called, "Did not expect setting commit function to be called");
443 
444 	while (loop_number <= sizeof(val_aa_valid_1)) {
445 		/* Clean up test */
446 		cleanup_test(NULL);
447 		zcbor_map_decode_bulk_reset(output_decode, ARRAY_SIZE(output_decode));
448 
449 		zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
450 
451 		/* Get "test_val/aa" value */
452 		ok = create_settings_mgmt_read_packet(zse, buffer, buffer_out, &buffer_size,
453 						      "test_val/aa", (uint32_t)loop_number);
454 		zassert_true(ok, "Expected packet creation to be successful");
455 
456 		/* Enable dummy SMP backend and ready for usage */
457 		smp_dummy_enable();
458 		smp_dummy_clear_state();
459 
460 		/* Send query command to dummy SMP backend */
461 		(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
462 		smp_dummy_add_data();
463 
464 		/* For a short duration to see if response has been received */
465 		received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
466 		zassert_true(received, "Expected to receive data but timed out");
467 
468 		/* Retrieve response buffer */
469 		nb = smp_dummy_get_outgoing();
470 		smp_dummy_disable();
471 
472 		/* Check response is as expected */
473 		zassert_true(nb->len > TEST_RESPONSE_READ_LENGTH, "SMP response mismatch");
474 
475 		smp_header = net_buf_pull_mem(nb, sizeof(struct smp_hdr));
476 
477 		zassert_true(smp_header->nh_len > sys_cpu_to_be16(TEST_RESPONSE_READ_DATA_LENGTH),
478 			      "SMP header length mismatch");
479 		zassert_equal(smp_header->nh_flags, 0, "SMP header flags mismatch");
480 		zassert_equal(smp_header->nh_op, MGMT_OP_READ_RSP,
481 			      "SMP header operation mismatch");
482 		zassert_equal(smp_header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
483 			      "SMP header group mismatch");
484 		zassert_equal(smp_header->nh_seq, 1, "SMP header sequence number mismatch");
485 		zassert_equal(smp_header->nh_id, SETTINGS_MGMT_ID_READ_WRITE,
486 			      "SMP header command ID mismatch");
487 		zassert_equal(smp_header->nh_version, 1, "SMP header version mismatch");
488 
489 		/* Get the response value to compare */
490 		zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
491 		ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode),
492 					   &decoded) == 0;
493 		zassert_true(ok, "Expected decode to be successful");
494 		zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
495 
496 		/* Ensure the right amount of data was read and that the value matches */
497 		zassert_equal(tmp_data.len, (loop_number == 0 ? sizeof(val_aa_valid_1) :
498 			      loop_number), "Expected data size mismatch");
499 
500 		zassert_mem_equal(tmp_data.value, val_aa_valid_1, tmp_data.len,
501 				  "Read data mismatch");
502 
503 		/* Check events */
504 		settings_state_get(&set_called, &get_called, &export_called, &commit_called);
505 		zassert_true(access_read_got, "Expected read access notification");
506 		zassert_false(access_write_got, "Did not expect write access notification");
507 		zassert_false(access_delete_got, "Did not expect delete access notification");
508 		zassert_false(access_load_got, "Did not expect load access notification");
509 		zassert_false(access_save_got, "Did no expect save access notification");
510 		zassert_false(access_commit_got, "Did not expect commit access notification");
511 		zassert_false(access_invalid_got,
512 			      "Did not expect an invalid access notification type");
513 		zassert_false(event_invalid_got, "Did not expect an invalid event");
514 
515 		zassert_false(set_called, "Did not expect setting set function to be called");
516 		zassert_true(get_called, "Expected setting get function to be called");
517 		zassert_false(export_called, "Did not expect setting export function to be called");
518 		zassert_false(commit_called, "Did not expect setting commit function to be called");
519 
520 		++loop_number;
521 	}
522 }
523 
ZTEST(settings_mgmt,test_read_max_size)524 ZTEST(settings_mgmt, test_read_max_size)
525 {
526 	uint8_t buffer[ZCBOR_BUFFER_SIZE];
527 	uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
528 	bool ok;
529 	uint16_t buffer_size;
530 	zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
531 	zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
532 	bool received;
533 	bool set_called;
534 	bool get_called;
535 	bool export_called;
536 	bool commit_called;
537 	struct zcbor_string tmp_data = { 0 };
538 	struct smp_hdr *smp_header;
539 	size_t decoded = 0;
540 	uint32_t max_size_response = 0;
541 
542 	struct zcbor_map_decode_key_val output_decode[] = {
543 		ZCBOR_MAP_DECODE_KEY_DECODER("val", zcbor_bstr_decode, &tmp_data),
544 		ZCBOR_MAP_DECODE_KEY_DECODER("max_size", zcbor_uint32_decode, &max_size_response),
545 	};
546 
547 	memset(buffer, 0, sizeof(buffer));
548 	memset(buffer_out, 0, sizeof(buffer_out));
549 	buffer_size = 0;
550 	memset(zse, 0, sizeof(zse));
551 	memset(zsd, 0, sizeof(zsd));
552 
553 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
554 
555 	/* Get "test_val/aa" value */
556 	ok = create_settings_mgmt_read_packet(zse, buffer, buffer_out, &buffer_size,
557 					      "test_val/aa", 4019);
558 	zassert_true(ok, "Expected packet creation to be successful");
559 
560 	/* Enable dummy SMP backend and ready for usage */
561 	smp_dummy_enable();
562 	smp_dummy_clear_state();
563 
564 	/* Send query command to dummy SMP backend */
565 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
566 	smp_dummy_add_data();
567 
568 	/* For a short duration to see if response has been received */
569 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
570 	zassert_true(received, "Expected to receive data but timed out");
571 
572 	/* Retrieve response buffer */
573 	nb = smp_dummy_get_outgoing();
574 	smp_dummy_disable();
575 
576 	/* Check response is as expected */
577 	zassert_true(nb->len > TEST_RESPONSE_READ_LENGTH, "SMP response mismatch");
578 
579 	smp_header = net_buf_pull_mem(nb, sizeof(struct smp_hdr));
580 
581 	zassert_true(smp_header->nh_len > sys_cpu_to_be16(TEST_RESPONSE_READ_DATA_LENGTH),
582 		      "SMP header length mismatch");
583 	zassert_equal(smp_header->nh_flags, 0, "SMP header flags mismatch");
584 	zassert_equal(smp_header->nh_op, MGMT_OP_READ_RSP, "SMP header operation mismatch");
585 	zassert_equal(smp_header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
586 		      "SMP header group mismatch");
587 	zassert_equal(smp_header->nh_seq, 1, "SMP header sequence number mismatch");
588 	zassert_equal(smp_header->nh_id, SETTINGS_MGMT_ID_READ_WRITE,
589 		      "SMP header command ID mismatch");
590 	zassert_equal(smp_header->nh_version, 1, "SMP header version mismatch");
591 
592 	/* Get the response value to compare */
593 	zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
594 	ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
595 	zassert_true(ok, "Expected decode to be successful");
596 	zassert_equal(decoded, 2, "Expected to receive 2 decoded zcbor elements");
597 
598 	/* Check events */
599 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
600 	zassert_true(access_read_got, "Expected read access notification");
601 	zassert_false(access_write_got, "Did not expect write access notification");
602 	zassert_false(access_delete_got, "Did not expect delete access notification");
603 	zassert_false(access_load_got, "Did not expect load access notification");
604 	zassert_false(access_save_got, "Did no expect save access notification");
605 	zassert_false(access_commit_got, "Did not expect commit access notification");
606 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
607 	zassert_false(event_invalid_got, "Did not expect an invalid event");
608 
609 	zassert_false(set_called, "Did not expect setting set function to be called");
610 	zassert_true(get_called, "Expected setting get function to be called");
611 	zassert_false(export_called, "Did not expect setting export function to be called");
612 	zassert_false(commit_called, "Did not expect setting commit function to be called");
613 	zassert_equal(max_size_response, CONFIG_MCUMGR_GRP_SETTINGS_VALUE_LEN,
614 		      "Expected to get max_size response");
615 }
616 
ZTEST(settings_mgmt,test_set_disallowed)617 ZTEST(settings_mgmt, test_set_disallowed)
618 {
619 	uint8_t buffer[ZCBOR_BUFFER_SIZE];
620 	uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
621 	bool ok;
622 	uint16_t buffer_size;
623 	zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
624 	zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
625 	bool received;
626 	bool set_called;
627 	bool get_called;
628 	bool export_called;
629 	bool commit_called;
630 	struct smp_hdr *header;
631 	struct zcbor_string tmp_data = { 0 };
632 	struct smp_hdr *smp_header;
633 	size_t decoded = 0;
634 
635 	struct zcbor_map_decode_key_val output_decode[] = {
636 		ZCBOR_MAP_DECODE_KEY_DECODER("val", zcbor_bstr_decode, &tmp_data),
637 	};
638 
639 	memset(buffer, 0, sizeof(buffer));
640 	memset(buffer_out, 0, sizeof(buffer_out));
641 	buffer_size = 0;
642 	memset(zse, 0, sizeof(zse));
643 	memset(zsd, 0, sizeof(zsd));
644 
645 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
646 
647 	/* Set "test_val/bb" value when block is active */
648 	block_access = true;
649 	ok = create_settings_mgmt_write_packet(zse, buffer, buffer_out, &buffer_size,
650 					       "test_val/bb", &val_bb_valid_1,
651 					       sizeof(val_bb_valid_1));
652 	zassert_true(ok, "Expected packet creation to be successful");
653 
654 	/* Enable dummy SMP backend and ready for usage */
655 	smp_dummy_enable();
656 	smp_dummy_clear_state();
657 
658 	/* Send query command to dummy SMP backend */
659 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
660 	smp_dummy_add_data();
661 
662 	/* For a short duration to see if response has been received */
663 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
664 	zassert_true(received, "Expected to receive data but timed out");
665 
666 	/* Retrieve response buffer */
667 	nb = smp_dummy_get_outgoing();
668 	smp_dummy_disable();
669 
670 	/* Check response is as expected */
671 	zassert_equal(nb->len, TEST_RESPONSE_ERROR_LENGTH, "SMP response mismatch");
672 	header = (struct smp_hdr *)nb->data;
673 	zassert_equal(header->nh_len, sys_cpu_to_be16(TEST_RESPONSE_ERROR_DATA_LENGTH),
674 		      "SMP header length mismatch");
675 	zassert_equal(header->nh_flags, 0, "SMP header flags mismatch");
676 	zassert_equal(header->nh_op, MGMT_OP_WRITE_RSP, "SMP header operation mismatch");
677 	zassert_equal(header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
678 		      "SMP header group mismatch");
679 	zassert_equal(header->nh_seq, 1, "SMP header sequence number mismatch");
680 	zassert_equal(header->nh_id, SETTINGS_MGMT_ID_READ_WRITE,
681 		      "SMP header command ID mismatch");
682 	zassert_equal(header->nh_version, 1, "SMP header version mismatch");
683 	zassert_mem_equal(&nb->data[(TEST_RESPONSE_ERROR_LENGTH -
684 				     TEST_RESPONSE_ERROR_DATA_LENGTH)],
685 			  test_response_error_data, TEST_RESPONSE_ERROR_DATA_LENGTH,
686 			  "SMP data mismatch");
687 
688 	/* Check events */
689 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
690 	zassert_false(access_read_got, "Did not expect read access notification");
691 	zassert_true(access_write_got, "Expected write access notification");
692 	zassert_false(access_delete_got, "Did not expect delete access notification");
693 	zassert_false(access_load_got, "Did not expect load access notification");
694 	zassert_false(access_save_got, "Did not expect save access notification");
695 	zassert_false(access_commit_got, "Did not expect commit access notification");
696 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
697 	zassert_false(event_invalid_got, "Did not expect an invalid event");
698 
699 	zassert_false(set_called, "Did not expect setting set function to be called");
700 	zassert_false(get_called, "Did not expect setting get function to be called");
701 	zassert_false(export_called, "Did not expect setting export function to be called");
702 	zassert_false(commit_called, "Did not expect setting commit function to be called");
703 
704 	/* Clean up test */
705 	cleanup_test(NULL);
706 	zcbor_map_decode_bulk_reset(output_decode, ARRAY_SIZE(output_decode));
707 
708 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
709 
710 	/* Get "test_val/bb" value */
711 	ok = create_settings_mgmt_read_packet(zse, buffer, buffer_out, &buffer_size,
712 					      "test_val/bb", 0);
713 	zassert_true(ok, "Expected packet creation to be successful");
714 
715 	/* Enable dummy SMP backend and ready for usage */
716 	smp_dummy_enable();
717 	smp_dummy_clear_state();
718 
719 	/* Send query command to dummy SMP backend */
720 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
721 	smp_dummy_add_data();
722 
723 	/* For a short duration to see if response has been received */
724 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
725 	zassert_true(received, "Expected to receive data but timed out");
726 
727 	/* Retrieve response buffer */
728 	nb = smp_dummy_get_outgoing();
729 	smp_dummy_disable();
730 
731 	/* Check response is as expected */
732 	zassert_true(nb->len > TEST_RESPONSE_READ_LENGTH, "SMP response mismatch");
733 
734 	smp_header = net_buf_pull_mem(nb, sizeof(struct smp_hdr));
735 
736 	zassert_true(smp_header->nh_len > sys_cpu_to_be16(TEST_RESPONSE_READ_DATA_LENGTH),
737 		      "SMP header length mismatch");
738 	zassert_equal(smp_header->nh_flags, 0, "SMP header flags mismatch");
739 	zassert_equal(smp_header->nh_op, MGMT_OP_READ_RSP, "SMP header operation mismatch");
740 	zassert_equal(smp_header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
741 		      "SMP header group mismatch");
742 	zassert_equal(smp_header->nh_seq, 1, "SMP header sequence number mismatch");
743 	zassert_equal(smp_header->nh_id, SETTINGS_MGMT_ID_READ_WRITE,
744 		      "SMP header command ID mismatch");
745 	zassert_equal(smp_header->nh_version, 1, "SMP header version mismatch");
746 
747 	/* Get the response value to compare */
748 	zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
749 	ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
750 	zassert_true(ok, "Expected decode to be successful");
751 	zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
752 
753 	/* Ensure the right amount of data was read and that the value does not match */
754 	zassert_equal(tmp_data.len, sizeof(val_bb_valid_1), "Expected data size mismatch");
755 	zassert_not_equal(tmp_data.value[0], val_bb_valid_1, "Read data mismatch");
756 
757 	/* Check events */
758 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
759 	zassert_true(access_read_got, "Expected read access notification");
760 	zassert_false(access_write_got, "Did not expect write access notification");
761 	zassert_false(access_delete_got, "Did not expect delete access notification");
762 	zassert_false(access_load_got, "Did not expect load access notification");
763 	zassert_false(access_save_got, "Did no expect save access notification");
764 	zassert_false(access_commit_got, "Did not expect commit access notification");
765 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
766 	zassert_false(event_invalid_got, "Did not expect an invalid event");
767 
768 	zassert_false(set_called, "Did not expect setting set function to be called");
769 	zassert_true(get_called, "Expected setting get function to be called");
770 	zassert_false(export_called, "Did not expect setting export function to be called");
771 	zassert_false(commit_called, "Did not expect setting commit function to be called");
772 
773 	/* Clean up test */
774 	cleanup_test(NULL);
775 
776 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
777 
778 	/* Set "test_val/bb" value when block is not active */
779 	block_access = false;
780 	ok = create_settings_mgmt_write_packet(zse, buffer, buffer_out, &buffer_size,
781 					       "test_val/bb", &val_bb_valid_1,
782 					       sizeof(val_bb_valid_1));
783 	zassert_true(ok, "Expected packet creation to be successful");
784 
785 	/* Enable dummy SMP backend and ready for usage */
786 	smp_dummy_enable();
787 	smp_dummy_clear_state();
788 
789 	/* Send query command to dummy SMP backend */
790 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
791 	smp_dummy_add_data();
792 
793 	/* For a short duration to see if response has been received */
794 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
795 	zassert_true(received, "Expected to receive data but timed out");
796 
797 	/* Retrieve response buffer */
798 	nb = smp_dummy_get_outgoing();
799 	smp_dummy_disable();
800 
801 	/* Check response is as expected */
802 	zassert_equal(nb->len, TEST_RESPONSE_OK_LENGTH, "SMP response mismatch");
803 	header = (struct smp_hdr *)nb->data;
804 	zassert_equal(header->nh_len, sys_cpu_to_be16(TEST_RESPONSE_OK_DATA_LENGTH),
805 		      "SMP header length mismatch");
806 	zassert_equal(header->nh_flags, 0, "SMP header flags mismatch");
807 	zassert_equal(header->nh_op, MGMT_OP_WRITE_RSP, "SMP header operation mismatch");
808 	zassert_equal(header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
809 		      "SMP header group mismatch");
810 	zassert_equal(header->nh_seq, 1, "SMP header sequence number mismatch");
811 	zassert_equal(header->nh_id, SETTINGS_MGMT_ID_READ_WRITE,
812 		      "SMP header command ID mismatch");
813 	zassert_equal(header->nh_version, 1, "SMP header version mismatch");
814 	zassert_mem_equal(&nb->data[(TEST_RESPONSE_OK_LENGTH - TEST_RESPONSE_OK_DATA_LENGTH)],
815 			  test_response_ok_data, TEST_RESPONSE_OK_DATA_LENGTH,
816 			  "SMP data mismatch");
817 
818 	/* Check events */
819 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
820 	zassert_false(access_read_got, "Did not expect read access notification");
821 	zassert_true(access_write_got, "Expected write access notification");
822 	zassert_false(access_delete_got, "Did not expect delete access notification");
823 	zassert_false(access_load_got, "Did not expect load access notification");
824 	zassert_false(access_save_got, "Did not expect save access notification");
825 	zassert_false(access_commit_got, "Did not expect commit access notification");
826 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
827 	zassert_false(event_invalid_got, "Did not expect an invalid event");
828 
829 	zassert_true(set_called, "Expected setting set function to be called");
830 	zassert_false(get_called, "Did not expect setting get function to be called");
831 	zassert_false(export_called, "Did not expect setting export function to be called");
832 	zassert_false(commit_called, "Did not expect setting commit function to be called");
833 
834 	/* Clean up test */
835 	cleanup_test(NULL);
836 	zcbor_map_decode_bulk_reset(output_decode, ARRAY_SIZE(output_decode));
837 
838 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
839 
840 	/* Get "test_val/bb" value */
841 	ok = create_settings_mgmt_read_packet(zse, buffer, buffer_out, &buffer_size,
842 					      "test_val/bb", 0);
843 	zassert_true(ok, "Expected packet creation to be successful");
844 
845 	/* Enable dummy SMP backend and ready for usage */
846 	smp_dummy_enable();
847 	smp_dummy_clear_state();
848 
849 	/* Send query command to dummy SMP backend */
850 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
851 	smp_dummy_add_data();
852 
853 	/* For a short duration to see if response has been received */
854 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
855 	zassert_true(received, "Expected to receive data but timed out");
856 
857 	/* Retrieve response buffer */
858 	nb = smp_dummy_get_outgoing();
859 	smp_dummy_disable();
860 
861 	/* Check response is as expected */
862 	zassert_true(nb->len > TEST_RESPONSE_READ_LENGTH, "SMP response mismatch");
863 
864 	smp_header = net_buf_pull_mem(nb, sizeof(struct smp_hdr));
865 
866 	zassert_true(smp_header->nh_len > sys_cpu_to_be16(TEST_RESPONSE_READ_DATA_LENGTH),
867 		      "SMP header length mismatch");
868 	zassert_equal(smp_header->nh_flags, 0, "SMP header flags mismatch");
869 	zassert_equal(smp_header->nh_op, MGMT_OP_READ_RSP, "SMP header operation mismatch");
870 	zassert_equal(smp_header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
871 		      "SMP header group mismatch");
872 	zassert_equal(smp_header->nh_seq, 1, "SMP header sequence number mismatch");
873 	zassert_equal(smp_header->nh_id, SETTINGS_MGMT_ID_READ_WRITE,
874 		      "SMP header command ID mismatch");
875 	zassert_equal(smp_header->nh_version, 1, "SMP header version mismatch");
876 
877 	/* Get the response value to compare */
878 	zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
879 	ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
880 	zassert_true(ok, "Expected decode to be successful");
881 	zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
882 
883 	/* Ensure the right amount of data was read and that the value matches */
884 	zassert_equal(tmp_data.len, sizeof(val_bb_valid_1), "Expected data size mismatch");
885 	zassert_equal(tmp_data.value[0], val_bb_valid_1, "Read data mismatch");
886 
887 	/* Check events */
888 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
889 	zassert_true(access_read_got, "Expected read access notification");
890 	zassert_false(access_write_got, "Did not expect write access notification");
891 	zassert_false(access_delete_got, "Did not expect delete access notification");
892 	zassert_false(access_load_got, "Did not expect load access notification");
893 	zassert_false(access_save_got, "Did no expect save access notification");
894 	zassert_false(access_commit_got, "Did not expect commit access notification");
895 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
896 	zassert_false(event_invalid_got, "Did not expect an invalid event");
897 
898 	zassert_false(set_called, "Did not expect setting set function to be called");
899 	zassert_true(get_called, "Expected setting get function to be called");
900 	zassert_false(export_called, "Did not expect setting export function to be called");
901 	zassert_false(commit_called, "Did not expect setting commit function to be called");
902 }
903 
ZTEST(settings_mgmt,test_delete)904 ZTEST(settings_mgmt, test_delete)
905 {
906 	uint8_t buffer[ZCBOR_BUFFER_SIZE];
907 	uint8_t buffer_out[OUTPUT_BUFFER_SIZE];
908 	bool ok;
909 	uint16_t buffer_size;
910 	zcbor_state_t zse[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
911 	zcbor_state_t zsd[ZCBOR_HISTORY_ARRAY_SIZE] = { 0 };
912 	bool received;
913 	bool set_called;
914 	bool get_called;
915 	bool export_called;
916 	bool commit_called;
917 	struct smp_hdr *header;
918 	struct zcbor_string tmp_data = { 0 };
919 	struct smp_hdr *smp_header;
920 	size_t decoded = 0;
921 
922 	struct zcbor_map_decode_key_val output_decode[] = {
923 		ZCBOR_MAP_DECODE_KEY_DECODER("val", zcbor_bstr_decode, &tmp_data),
924 	};
925 
926 	memset(buffer, 0, sizeof(buffer));
927 	memset(buffer_out, 0, sizeof(buffer_out));
928 	buffer_size = 0;
929 	memset(zse, 0, sizeof(zse));
930 	memset(zsd, 0, sizeof(zsd));
931 
932 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
933 
934 	/* Set "test_val/aa" value */
935 	ok = create_settings_mgmt_write_packet(zse, buffer, buffer_out, &buffer_size,
936 					       "test_val/aa", val_aa_valid_1,
937 					       sizeof(val_aa_valid_1));
938 	zassert_true(ok, "Expected packet creation to be successful");
939 
940 	/* Enable dummy SMP backend and ready for usage */
941 	smp_dummy_enable();
942 	smp_dummy_clear_state();
943 
944 	/* Send query command to dummy SMP backend */
945 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
946 	smp_dummy_add_data();
947 
948 	/* For a short duration to see if response has been received */
949 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
950 	zassert_true(received, "Expected to receive data but timed out");
951 
952 	/* Retrieve response buffer */
953 	nb = smp_dummy_get_outgoing();
954 	smp_dummy_disable();
955 
956 	/* Check response is as expected */
957 	zassert_equal(nb->len, TEST_RESPONSE_OK_LENGTH, "SMP response mismatch");
958 	header = (struct smp_hdr *)nb->data;
959 	zassert_equal(header->nh_len, sys_cpu_to_be16(TEST_RESPONSE_OK_DATA_LENGTH),
960 		      "SMP header length mismatch");
961 	zassert_equal(header->nh_flags, 0, "SMP header flags mismatch");
962 	zassert_equal(header->nh_op, MGMT_OP_WRITE_RSP, "SMP header operation mismatch");
963 	zassert_equal(header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
964 		      "SMP header group mismatch");
965 	zassert_equal(header->nh_seq, 1, "SMP header sequence number mismatch");
966 	zassert_equal(header->nh_id, SETTINGS_MGMT_ID_READ_WRITE,
967 		      "SMP header command ID mismatch");
968 	zassert_equal(header->nh_version, 1, "SMP header version mismatch");
969 	zassert_mem_equal(&nb->data[(TEST_RESPONSE_OK_LENGTH - TEST_RESPONSE_OK_DATA_LENGTH)],
970 			  test_response_ok_data, TEST_RESPONSE_OK_DATA_LENGTH,
971 			  "SMP data mismatch");
972 
973 	/* Check events */
974 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
975 	zassert_false(access_read_got, "Did not expect read access notification");
976 	zassert_true(access_write_got, "Expected write access notification");
977 	zassert_false(access_delete_got, "Did not expect delete access notification");
978 	zassert_false(access_load_got, "Did not expect load access notification");
979 	zassert_false(access_save_got, "Did not expect save access notification");
980 	zassert_false(access_commit_got, "Did not expect commit access notification");
981 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
982 	zassert_false(event_invalid_got, "Did not expect an invalid event");
983 
984 	zassert_true(set_called, "Expected setting set function to be called");
985 	zassert_false(get_called, "Did not expect setting get function to be called");
986 	zassert_false(export_called, "Did not expect setting export function to be called");
987 	zassert_false(commit_called, "Did not expect setting commit function to be called");
988 
989 	/* Clean up test */
990 	cleanup_test(NULL);
991 	zcbor_map_decode_bulk_reset(output_decode, ARRAY_SIZE(output_decode));
992 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
993 
994 	/* Save data to persistent storage */
995 	ok = create_settings_mgmt_save_packet(zse, buffer, buffer_out, &buffer_size);
996 	zassert_true(ok, "Expected packet creation to be successful");
997 
998 	/* Enable dummy SMP backend and ready for usage */
999 	smp_dummy_enable();
1000 	smp_dummy_clear_state();
1001 
1002 	/* Send query command to dummy SMP backend */
1003 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1004 	smp_dummy_add_data();
1005 
1006 	/* For a short duration to see if response has been received */
1007 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1008 	zassert_true(received, "Expected to receive data but timed out");
1009 
1010 	/* Retrieve response buffer */
1011 	nb = smp_dummy_get_outgoing();
1012 	smp_dummy_disable();
1013 
1014 	/* Check response is as expected */
1015 	zassert_equal(nb->len, TEST_RESPONSE_OK_LENGTH, "SMP response mismatch");
1016 
1017 	smp_header = net_buf_pull_mem(nb, sizeof(struct smp_hdr));
1018 
1019 	zassert_equal(smp_header->nh_len, sys_cpu_to_be16(TEST_RESPONSE_OK_DATA_LENGTH),
1020 		      "SMP header length mismatch");
1021 	zassert_equal(smp_header->nh_flags, 0, "SMP header flags mismatch");
1022 	zassert_equal(smp_header->nh_op, MGMT_OP_WRITE_RSP, "SMP header operation mismatch");
1023 	zassert_equal(smp_header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
1024 		      "SMP header group mismatch");
1025 	zassert_equal(smp_header->nh_seq, 1, "SMP header sequence number mismatch");
1026 	zassert_equal(smp_header->nh_id, SETTINGS_MGMT_ID_LOAD_SAVE,
1027 		      "SMP header command ID mismatch");
1028 	zassert_equal(smp_header->nh_version, 1, "SMP header version mismatch");
1029 
1030 	/* Check events */
1031 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
1032 	zassert_false(access_read_got, "Did not expect read access notification");
1033 	zassert_false(access_write_got, "Did not expect write access notification");
1034 	zassert_false(access_delete_got, "Did not expect delete access notification");
1035 	zassert_false(access_load_got, "Did not expect load access notification");
1036 	zassert_true(access_save_got, "Expected save access notification");
1037 	zassert_false(access_commit_got, "Did not expect commit access notification");
1038 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
1039 	zassert_false(event_invalid_got, "Did not expect an invalid event");
1040 
1041 	zassert_false(set_called, "Did not expect setting set function to be called");
1042 	zassert_false(get_called, "Did not expect setting get function to be called");
1043 	zassert_false(get_called, "Did not expect setting get function to be called");
1044 	zassert_true(export_called, "Expected setting export function to be called");
1045 	zassert_false(commit_called, "Did not expect setting commit function to be called");
1046 
1047 	/* Clean up test */
1048 	cleanup_test(NULL);
1049 	zcbor_map_decode_bulk_reset(output_decode, ARRAY_SIZE(output_decode));
1050 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
1051 
1052 	/* Set "test_val/aa" value to other valid */
1053 	ok = create_settings_mgmt_write_packet(zse, buffer, buffer_out, &buffer_size,
1054 					       "test_val/aa", val_aa_valid_2,
1055 					       sizeof(val_aa_valid_2));
1056 	zassert_true(ok, "Expected packet creation to be successful");
1057 
1058 	/* Enable dummy SMP backend and ready for usage */
1059 	smp_dummy_enable();
1060 	smp_dummy_clear_state();
1061 
1062 	/* Send query command to dummy SMP backend */
1063 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1064 	smp_dummy_add_data();
1065 
1066 	/* For a short duration to see if response has been received */
1067 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1068 	zassert_true(received, "Expected to receive data but timed out");
1069 
1070 	/* Retrieve response buffer */
1071 	nb = smp_dummy_get_outgoing();
1072 	smp_dummy_disable();
1073 
1074 	/* Check response is as expected */
1075 	zassert_equal(nb->len, TEST_RESPONSE_OK_LENGTH, "SMP response mismatch");
1076 	header = (struct smp_hdr *)nb->data;
1077 	zassert_equal(header->nh_len, sys_cpu_to_be16(TEST_RESPONSE_OK_DATA_LENGTH),
1078 		      "SMP header length mismatch");
1079 	zassert_equal(header->nh_flags, 0, "SMP header flags mismatch");
1080 	zassert_equal(header->nh_op, MGMT_OP_WRITE_RSP, "SMP header operation mismatch");
1081 	zassert_equal(header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
1082 		      "SMP header group mismatch");
1083 	zassert_equal(header->nh_seq, 1, "SMP header sequence number mismatch");
1084 	zassert_equal(header->nh_id, SETTINGS_MGMT_ID_READ_WRITE,
1085 		      "SMP header command ID mismatch");
1086 	zassert_equal(header->nh_version, 1, "SMP header version mismatch");
1087 	zassert_mem_equal(&nb->data[(TEST_RESPONSE_OK_LENGTH - TEST_RESPONSE_OK_DATA_LENGTH)],
1088 			  test_response_ok_data, TEST_RESPONSE_OK_DATA_LENGTH,
1089 			  "SMP data mismatch");
1090 
1091 	/* Check events */
1092 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
1093 	zassert_false(access_read_got, "Did not expect read access notification");
1094 	zassert_true(access_write_got, "Expected write access notification");
1095 	zassert_false(access_delete_got, "Did not expect delete access notification");
1096 	zassert_false(access_load_got, "Did not expect load access notification");
1097 	zassert_false(access_save_got, "Did not expect save access notification");
1098 	zassert_false(access_commit_got, "Did not expect commit access notification");
1099 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
1100 	zassert_false(event_invalid_got, "Did not expect an invalid event");
1101 
1102 	zassert_true(set_called, "Expected setting set function to be called");
1103 	zassert_false(get_called, "Did not expect setting get function to be called");
1104 	zassert_false(export_called, "Did not expect setting export function to be called");
1105 	zassert_false(commit_called, "Did not expect setting commit function to be called");
1106 
1107 	/* Clean up test */
1108 	cleanup_test(NULL);
1109 	zcbor_map_decode_bulk_reset(output_decode, ARRAY_SIZE(output_decode));
1110 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
1111 
1112 	/* Get "test_val/aa" value */
1113 	ok = create_settings_mgmt_read_packet(zse, buffer, buffer_out, &buffer_size,
1114 					      "test_val/aa", 0);
1115 	zassert_true(ok, "Expected packet creation to be successful");
1116 
1117 	/* Enable dummy SMP backend and ready for usage */
1118 	smp_dummy_enable();
1119 	smp_dummy_clear_state();
1120 
1121 	/* Send query command to dummy SMP backend */
1122 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1123 	smp_dummy_add_data();
1124 
1125 	/* For a short duration to see if response has been received */
1126 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1127 	zassert_true(received, "Expected to receive data but timed out");
1128 
1129 	/* Retrieve response buffer */
1130 	nb = smp_dummy_get_outgoing();
1131 	smp_dummy_disable();
1132 
1133 	/* Check response is as expected */
1134 	zassert_true(nb->len > TEST_RESPONSE_READ_LENGTH, "SMP response mismatch");
1135 
1136 	smp_header = net_buf_pull_mem(nb, sizeof(struct smp_hdr));
1137 
1138 	zassert_true(smp_header->nh_len > sys_cpu_to_be16(TEST_RESPONSE_READ_DATA_LENGTH),
1139 		      "SMP header length mismatch");
1140 	zassert_equal(smp_header->nh_flags, 0, "SMP header flags mismatch");
1141 	zassert_equal(smp_header->nh_op, MGMT_OP_READ_RSP, "SMP header operation mismatch");
1142 	zassert_equal(smp_header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
1143 		      "SMP header group mismatch");
1144 	zassert_equal(smp_header->nh_seq, 1, "SMP header sequence number mismatch");
1145 	zassert_equal(smp_header->nh_id, SETTINGS_MGMT_ID_READ_WRITE,
1146 		      "SMP header command ID mismatch");
1147 	zassert_equal(smp_header->nh_version, 1, "SMP header version mismatch");
1148 
1149 	/* Get the response value to compare */
1150 	zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
1151 	ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
1152 	zassert_true(ok, "Expected decode to be successful");
1153 	zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
1154 
1155 	/* Ensure the right amount of data was read and that the value matches */
1156 	zassert_equal(tmp_data.len, sizeof(val_aa_valid_2), "Expected data size mismatch");
1157 	zassert_mem_equal(tmp_data.value, val_aa_valid_2, tmp_data.len, "Read data mismatch");
1158 
1159 	/* Check events */
1160 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
1161 	zassert_true(access_read_got, "Expected read access notification");
1162 	zassert_false(access_write_got, "Did not expect write access notification");
1163 	zassert_false(access_delete_got, "Did not expect delete access notification");
1164 	zassert_false(access_load_got, "Did not expect load access notification");
1165 	zassert_false(access_save_got, "Did no expect save access notification");
1166 	zassert_false(access_commit_got, "Did not expect commit access notification");
1167 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
1168 	zassert_false(event_invalid_got, "Did not expect an invalid event");
1169 
1170 	zassert_false(set_called, "Did not expect setting set function to be called");
1171 	zassert_true(get_called, "Expected setting get function to be called");
1172 	zassert_false(export_called, "Did not expect setting export function to be called");
1173 	zassert_false(commit_called, "Did not expect setting commit function to be called");
1174 
1175 	/* Clean up test */
1176 	cleanup_test(NULL);
1177 	zcbor_map_decode_bulk_reset(output_decode, ARRAY_SIZE(output_decode));
1178 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
1179 
1180 	/* Load data from persistent storage */
1181 	ok = create_settings_mgmt_load_packet(zse, buffer, buffer_out, &buffer_size);
1182 	zassert_true(ok, "Expected packet creation to be successful");
1183 
1184 	/* Enable dummy SMP backend and ready for usage */
1185 	smp_dummy_enable();
1186 	smp_dummy_clear_state();
1187 
1188 	/* Send query command to dummy SMP backend */
1189 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1190 	smp_dummy_add_data();
1191 
1192 	/* For a short duration to see if response has been received */
1193 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1194 	zassert_true(received, "Expected to receive data but timed out");
1195 
1196 	/* Retrieve response buffer */
1197 	nb = smp_dummy_get_outgoing();
1198 	smp_dummy_disable();
1199 
1200 	/* Check response is as expected */
1201 	zassert_equal(nb->len, TEST_RESPONSE_OK_LENGTH, "SMP response mismatch");
1202 
1203 	smp_header = net_buf_pull_mem(nb, sizeof(struct smp_hdr));
1204 
1205 	zassert_equal(smp_header->nh_len, sys_cpu_to_be16(TEST_RESPONSE_OK_DATA_LENGTH),
1206 		      "SMP header length mismatch");
1207 	zassert_equal(smp_header->nh_flags, 0, "SMP header flags mismatch");
1208 	zassert_equal(smp_header->nh_op, MGMT_OP_READ_RSP, "SMP header operation mismatch");
1209 	zassert_equal(smp_header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
1210 		      "SMP header group mismatch");
1211 	zassert_equal(smp_header->nh_seq, 1, "SMP header sequence number mismatch");
1212 	zassert_equal(smp_header->nh_id, SETTINGS_MGMT_ID_LOAD_SAVE,
1213 		      "SMP header command ID mismatch");
1214 	zassert_equal(smp_header->nh_version, 1, "SMP header version mismatch");
1215 
1216 	/* Check events */
1217 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
1218 	zassert_false(access_read_got, "Did not expect read access notification");
1219 	zassert_false(access_write_got, "Did not expect write access notification");
1220 	zassert_false(access_delete_got, "Did not expect delete access notification");
1221 	zassert_true(access_load_got, "Expect load access notification");
1222 	zassert_false(access_save_got, "Did not expect save access notification");
1223 	zassert_false(access_commit_got, "Did not expect commit access notification");
1224 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
1225 	zassert_false(event_invalid_got, "Did not expect an invalid event");
1226 
1227 	zassert_true(set_called, "Expected setting set function to be called");
1228 	zassert_false(get_called, "Did not expect setting get function to be called");
1229 	zassert_false(get_called, "Did not expect setting get function to be called");
1230 	zassert_false(export_called, "Did not expext setting export function to be called");
1231 	zassert_true(commit_called, "Expected setting commit function to be called");
1232 
1233 	/* Clean up test */
1234 	cleanup_test(NULL);
1235 	zcbor_map_decode_bulk_reset(output_decode, ARRAY_SIZE(output_decode));
1236 
1237 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
1238 
1239 	/* Get "test_val/aa" value */
1240 	ok = create_settings_mgmt_read_packet(zse, buffer, buffer_out, &buffer_size,
1241 					      "test_val/aa", 0);
1242 	zassert_true(ok, "Expected packet creation to be successful");
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 	zassert_true(received, "Expected to receive data but timed out");
1255 
1256 	/* Retrieve response buffer */
1257 	nb = smp_dummy_get_outgoing();
1258 	smp_dummy_disable();
1259 
1260 	/* Check response is as expected */
1261 	zassert_true(nb->len > TEST_RESPONSE_READ_LENGTH, "SMP response mismatch");
1262 
1263 	smp_header = net_buf_pull_mem(nb, sizeof(struct smp_hdr));
1264 
1265 	zassert_true(smp_header->nh_len > sys_cpu_to_be16(TEST_RESPONSE_READ_DATA_LENGTH),
1266 		      "SMP header length mismatch");
1267 	zassert_equal(smp_header->nh_flags, 0, "SMP header flags mismatch");
1268 	zassert_equal(smp_header->nh_op, MGMT_OP_READ_RSP, "SMP header operation mismatch");
1269 	zassert_equal(smp_header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
1270 		      "SMP header group mismatch");
1271 	zassert_equal(smp_header->nh_seq, 1, "SMP header sequence number mismatch");
1272 	zassert_equal(smp_header->nh_id, SETTINGS_MGMT_ID_READ_WRITE,
1273 		      "SMP header command ID mismatch");
1274 	zassert_equal(smp_header->nh_version, 1, "SMP header version mismatch");
1275 
1276 	/* Get the response value to compare */
1277 	zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
1278 	ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
1279 	zassert_true(ok, "Expected decode to be successful");
1280 	zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
1281 
1282 	/* Ensure the right amount of data was read and that the value matches */
1283 	zassert_equal(tmp_data.len, sizeof(val_aa_valid_1), "Expected data size mismatch");
1284 	zassert_mem_equal(tmp_data.value, val_aa_valid_1, tmp_data.len, "Read data mismatch");
1285 
1286 	/* Check events */
1287 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
1288 	zassert_true(access_read_got, "Expected read access notification");
1289 	zassert_false(access_write_got, "Did not expect write access notification");
1290 	zassert_false(access_delete_got, "Did not expect delete access notification");
1291 	zassert_false(access_load_got, "Did not expect load access notification");
1292 	zassert_false(access_save_got, "Did no expect save access notification");
1293 	zassert_false(access_commit_got, "Did not expect commit access notification");
1294 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
1295 	zassert_false(event_invalid_got, "Did not expect an invalid event");
1296 
1297 	zassert_false(set_called, "Did not expect setting set function to be called");
1298 	zassert_true(get_called, "Expected setting get function to be called");
1299 	zassert_false(export_called, "Did not expect setting export function to be called");
1300 	zassert_false(commit_called, "Did not expect setting commit function to be called");
1301 
1302 	/* Clean up test */
1303 	cleanup_test(NULL);
1304 	zcbor_map_decode_bulk_reset(output_decode, ARRAY_SIZE(output_decode));
1305 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
1306 
1307 	/* Delete "test_val/aa" value */
1308 	ok = create_settings_mgmt_delete_packet(zse, buffer, buffer_out, &buffer_size,
1309 						"test_val/aa");
1310 	zassert_true(ok, "Expected packet creation to be successful");
1311 
1312 	/* Enable dummy SMP backend and ready for usage */
1313 	smp_dummy_enable();
1314 	smp_dummy_clear_state();
1315 
1316 	/* Send query command to dummy SMP backend */
1317 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1318 	smp_dummy_add_data();
1319 
1320 	/* For a short duration to see if response has been received */
1321 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1322 	zassert_true(received, "Expected to receive data but timed out");
1323 
1324 	/* Retrieve response buffer */
1325 	nb = smp_dummy_get_outgoing();
1326 	smp_dummy_disable();
1327 
1328 	/* Check response is as expected */
1329 	zassert_equal(nb->len, TEST_RESPONSE_OK_LENGTH, "SMP response mismatch");
1330 
1331 	smp_header = net_buf_pull_mem(nb, sizeof(struct smp_hdr));
1332 
1333 	zassert_equal(smp_header->nh_len, sys_cpu_to_be16(TEST_RESPONSE_OK_DATA_LENGTH),
1334 		      "SMP header length mismatch");
1335 	zassert_equal(smp_header->nh_flags, 0, "SMP header flags mismatch");
1336 	zassert_equal(smp_header->nh_op, MGMT_OP_WRITE_RSP, "SMP header operation mismatch");
1337 	zassert_equal(smp_header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
1338 		      "SMP header group mismatch");
1339 	zassert_equal(smp_header->nh_seq, 1, "SMP header sequence number mismatch");
1340 	zassert_equal(smp_header->nh_id, SETTINGS_MGMT_ID_DELETE,
1341 		      "SMP header command ID mismatch");
1342 	zassert_equal(smp_header->nh_version, 1, "SMP header version mismatch");
1343 
1344 	/* Check events */
1345 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
1346 	zassert_false(access_read_got, "Did not expect read access notification");
1347 	zassert_false(access_write_got, "Did not expect write access notification");
1348 	zassert_true(access_delete_got, "Expected delete access notification");
1349 	zassert_false(access_load_got, "Did not expect load access notification");
1350 	zassert_false(access_save_got, "Did no expect save access notification");
1351 	zassert_false(access_commit_got, "Did not expect commit access notification");
1352 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
1353 	zassert_false(event_invalid_got, "Did not expect an invalid event");
1354 
1355 	zassert_false(set_called, "Did not expect setting set function to be called");
1356 	zassert_false(get_called, "Did not expect setting get function to be called");
1357 	zassert_false(get_called, "Did not expect setting get function to be called");
1358 	zassert_false(export_called, "Did not expect setting export function to be called");
1359 	zassert_false(commit_called, "Did not expect setting commit function to be called");
1360 
1361 	/* Clean up test */
1362 	cleanup_test(NULL);
1363 	zcbor_map_decode_bulk_reset(output_decode, ARRAY_SIZE(output_decode));
1364 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
1365 
1366 	/* Set "test_val/aa" value to other value */
1367 	ok = create_settings_mgmt_write_packet(zse, buffer, buffer_out, &buffer_size,
1368 					       "test_val/aa", val_aa_invalid,
1369 					       sizeof(val_aa_invalid));
1370 	zassert_true(ok, "Expected packet creation to be successful");
1371 
1372 	/* Enable dummy SMP backend and ready for usage */
1373 	smp_dummy_enable();
1374 	smp_dummy_clear_state();
1375 
1376 	/* Send query command to dummy SMP backend */
1377 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1378 	smp_dummy_add_data();
1379 
1380 	/* For a short duration to see if response has been received */
1381 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1382 	zassert_true(received, "Expected to receive data but timed out");
1383 
1384 	/* Retrieve response buffer */
1385 	nb = smp_dummy_get_outgoing();
1386 	smp_dummy_disable();
1387 
1388 	/* Check response is as expected */
1389 	zassert_equal(nb->len, TEST_RESPONSE_OK_LENGTH, "SMP response mismatch");
1390 	header = (struct smp_hdr *)nb->data;
1391 	zassert_equal(header->nh_len, sys_cpu_to_be16(TEST_RESPONSE_OK_DATA_LENGTH),
1392 		      "SMP header length mismatch");
1393 	zassert_equal(header->nh_flags, 0, "SMP header flags mismatch");
1394 	zassert_equal(header->nh_op, MGMT_OP_WRITE_RSP, "SMP header operation mismatch");
1395 	zassert_equal(header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
1396 		      "SMP header group mismatch");
1397 	zassert_equal(header->nh_seq, 1, "SMP header sequence number mismatch");
1398 	zassert_equal(header->nh_id, SETTINGS_MGMT_ID_READ_WRITE,
1399 		      "SMP header command ID mismatch");
1400 	zassert_equal(header->nh_version, 1, "SMP header version mismatch");
1401 	zassert_mem_equal(&nb->data[(TEST_RESPONSE_OK_LENGTH - TEST_RESPONSE_OK_DATA_LENGTH)],
1402 			  test_response_ok_data, TEST_RESPONSE_OK_DATA_LENGTH,
1403 			  "SMP data mismatch");
1404 
1405 	/* Check events */
1406 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
1407 	zassert_false(access_read_got, "Did not expect read access notification");
1408 	zassert_true(access_write_got, "Expected write access notification");
1409 	zassert_false(access_delete_got, "Did not expect delete access notification");
1410 	zassert_false(access_load_got, "Did not expect load access notification");
1411 	zassert_false(access_save_got, "Did not expect save access notification");
1412 	zassert_false(access_commit_got, "Did not expect commit access notification");
1413 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
1414 	zassert_false(event_invalid_got, "Did not expect an invalid event");
1415 
1416 	zassert_true(set_called, "Expected setting set function to be called");
1417 	zassert_false(get_called, "Did not expect setting get function to be called");
1418 	zassert_false(export_called, "Did not expect setting export function to be called");
1419 	zassert_false(commit_called, "Did not expect setting commit function to be called");
1420 
1421 	/* Clean up test */
1422 	cleanup_test(NULL);
1423 	zcbor_map_decode_bulk_reset(output_decode, ARRAY_SIZE(output_decode));
1424 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
1425 
1426 	/* Load data from persistent storage */
1427 	ok = create_settings_mgmt_load_packet(zse, buffer, buffer_out, &buffer_size);
1428 	zassert_true(ok, "Expected packet creation to be successful");
1429 
1430 	/* Enable dummy SMP backend and ready for usage */
1431 	smp_dummy_enable();
1432 	smp_dummy_clear_state();
1433 
1434 	/* Send query command to dummy SMP backend */
1435 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1436 	smp_dummy_add_data();
1437 
1438 	/* For a short duration to see if response has been received */
1439 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1440 	zassert_true(received, "Expected to receive data but timed out");
1441 
1442 	/* Retrieve response buffer */
1443 	nb = smp_dummy_get_outgoing();
1444 	smp_dummy_disable();
1445 
1446 	/* Check response is as expected */
1447 	zassert_equal(nb->len, TEST_RESPONSE_OK_LENGTH, "SMP response mismatch");
1448 
1449 	smp_header = net_buf_pull_mem(nb, sizeof(struct smp_hdr));
1450 
1451 	zassert_equal(smp_header->nh_len, sys_cpu_to_be16(TEST_RESPONSE_OK_DATA_LENGTH),
1452 		      "SMP header length mismatch");
1453 	zassert_equal(smp_header->nh_flags, 0, "SMP header flags mismatch");
1454 	zassert_equal(smp_header->nh_op, MGMT_OP_READ_RSP, "SMP header operation mismatch");
1455 	zassert_equal(smp_header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
1456 		      "SMP header group mismatch");
1457 	zassert_equal(smp_header->nh_seq, 1, "SMP header sequence number mismatch");
1458 	zassert_equal(smp_header->nh_id, SETTINGS_MGMT_ID_LOAD_SAVE,
1459 		      "SMP header command ID mismatch");
1460 	zassert_equal(smp_header->nh_version, 1, "SMP header version mismatch");
1461 
1462 	/* Check events */
1463 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
1464 	zassert_false(access_read_got, "Did not expect read access notification");
1465 	zassert_false(access_write_got, "Did not expect write access notification");
1466 	zassert_false(access_delete_got, "Did not expect delete access notification");
1467 	zassert_true(access_load_got, "Expected load access notification");
1468 	zassert_false(access_save_got, "Did not expect save access notification");
1469 	zassert_false(access_commit_got, "Did not expect commit access notification");
1470 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
1471 	zassert_false(event_invalid_got, "Did not expect an invalid event");
1472 
1473 	zassert_true(set_called, "Expected setting set function to be called");
1474 	zassert_false(get_called, "Did not expect setting get function to be called");
1475 	zassert_false(get_called, "Did not expect setting get function to be called");
1476 	zassert_false(export_called, "Did not expect setting export function to be called");
1477 	zassert_true(commit_called, "Expected setting commit function to be called");
1478 
1479 	/* Clean up test */
1480 	cleanup_test(NULL);
1481 	zcbor_map_decode_bulk_reset(output_decode, ARRAY_SIZE(output_decode));
1482 	zcbor_new_encode_state(zse, 2, buffer, ARRAY_SIZE(buffer), 0);
1483 
1484 	/* Get "test_val/aa" value */
1485 	ok = create_settings_mgmt_read_packet(zse, buffer, buffer_out, &buffer_size,
1486 					      "test_val/aa", 0);
1487 	zassert_true(ok, "Expected packet creation to be successful");
1488 
1489 	/* Enable dummy SMP backend and ready for usage */
1490 	smp_dummy_enable();
1491 	smp_dummy_clear_state();
1492 
1493 	/* Send query command to dummy SMP backend */
1494 	(void)smp_dummy_tx_pkt(buffer_out, buffer_size);
1495 	smp_dummy_add_data();
1496 
1497 	/* For a short duration to see if response has been received */
1498 	received = smp_dummy_wait_for_data(SMP_RESPONSE_WAIT_TIME);
1499 	zassert_true(received, "Expected to receive data but timed out");
1500 
1501 	/* Retrieve response buffer */
1502 	nb = smp_dummy_get_outgoing();
1503 	smp_dummy_disable();
1504 
1505 	/* Check response is as expected */
1506 	zassert_true(nb->len > TEST_RESPONSE_READ_LENGTH, "SMP response mismatch");
1507 
1508 	smp_header = net_buf_pull_mem(nb, sizeof(struct smp_hdr));
1509 
1510 	zassert_true(smp_header->nh_len > sys_cpu_to_be16(TEST_RESPONSE_READ_DATA_LENGTH),
1511 		      "SMP header length mismatch");
1512 	zassert_equal(smp_header->nh_flags, 0, "SMP header flags mismatch");
1513 	zassert_equal(smp_header->nh_op, MGMT_OP_READ_RSP, "SMP header operation mismatch");
1514 	zassert_equal(smp_header->nh_group, sys_cpu_to_be16(MGMT_GROUP_ID_SETTINGS),
1515 		      "SMP header group mismatch");
1516 	zassert_equal(smp_header->nh_seq, 1, "SMP header sequence number mismatch");
1517 	zassert_equal(smp_header->nh_id, SETTINGS_MGMT_ID_READ_WRITE,
1518 		      "SMP header command ID mismatch");
1519 	zassert_equal(smp_header->nh_version, 1, "SMP header version mismatch");
1520 
1521 	/* Get the response value to compare */
1522 	zcbor_new_decode_state(zsd, 4, nb->data, nb->len, 1, NULL, 0);
1523 	ok = zcbor_map_decode_bulk(zsd, output_decode, ARRAY_SIZE(output_decode), &decoded) == 0;
1524 	zassert_true(ok, "Expected decode to be successful");
1525 	zassert_equal(decoded, 1, "Expected to receive 1 decoded zcbor element");
1526 
1527 	/* Ensure the right amount of data was read and that the value matches */
1528 	zassert_equal(tmp_data.len, sizeof(val_aa_invalid), "Expected data size mismatch");
1529 	zassert_mem_equal(tmp_data.value, val_aa_invalid, tmp_data.len, "Read data mismatch");
1530 
1531 	/* Check events */
1532 	settings_state_get(&set_called, &get_called, &export_called, &commit_called);
1533 	zassert_true(access_read_got, "Expected read access notification");
1534 	zassert_false(access_write_got, "Did not expect write access notification");
1535 	zassert_false(access_delete_got, "Did not expect delete access notification");
1536 	zassert_false(access_load_got, "Did not expect load access notification");
1537 	zassert_false(access_save_got, "Did no expect save access notification");
1538 	zassert_false(access_commit_got, "Did not expect commit access notification");
1539 	zassert_false(access_invalid_got, "Did not expect an invalid access notification type");
1540 	zassert_false(event_invalid_got, "Did not expect an invalid event");
1541 
1542 	zassert_false(set_called, "Did not expect setting set function to be called");
1543 	zassert_true(get_called, "Expected setting get function to be called");
1544 	zassert_false(export_called, "Did not expect setting export function to be called");
1545 	zassert_false(commit_called, "Did not expect setting commit function to be called");
1546 }
1547 
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)1548 static enum mgmt_cb_return mgmt_event_cmd_callback(uint32_t event, enum mgmt_cb_return prev_status,
1549 						   int32_t *rc, uint16_t *group, bool *abort_more,
1550 						   void *data, size_t data_size)
1551 {
1552 	if (event == MGMT_EVT_OP_SETTINGS_MGMT_ACCESS) {
1553 		struct settings_mgmt_access *settings_data = (struct settings_mgmt_access *)data;
1554 
1555 		if (settings_data->access == SETTINGS_ACCESS_READ) {
1556 			access_read_got = true;
1557 		} else if (settings_data->access == SETTINGS_ACCESS_WRITE) {
1558 			access_write_got = true;
1559 		} else if (settings_data->access == SETTINGS_ACCESS_DELETE) {
1560 			access_delete_got = true;
1561 		} else if (settings_data->access == SETTINGS_ACCESS_LOAD) {
1562 			access_load_got = true;
1563 		} else if (settings_data->access == SETTINGS_ACCESS_SAVE) {
1564 			access_save_got = true;
1565 		} else if (settings_data->access == SETTINGS_ACCESS_COMMIT) {
1566 			access_commit_got = true;
1567 		} else {
1568 			access_invalid_got = true;
1569 		}
1570 
1571 		if ((settings_data->access == SETTINGS_ACCESS_READ ||
1572 		     settings_data->access == SETTINGS_ACCESS_WRITE ||
1573 		     settings_data->access == SETTINGS_ACCESS_DELETE) &&
1574 		    settings_data->name != NULL) {
1575 			strncpy(access_name, settings_data->name, (sizeof(access_name) - 1));
1576 		}
1577 
1578 		if (block_access == true) {
1579 			*rc = MGMT_ERR_EPERUSER;
1580 			return MGMT_CB_ERROR_RC;
1581 		}
1582 	} else {
1583 		event_invalid_got = true;
1584 	}
1585 
1586 	return MGMT_CB_OK;
1587 }
1588 
1589 static struct mgmt_callback mgmt_event_callback = {
1590 	.callback = mgmt_event_cmd_callback,
1591 	.event_id = MGMT_EVT_OP_SETTINGS_MGMT_ACCESS,
1592 };
1593 
setup_test(void)1594 static void *setup_test(void)
1595 {
1596 	int rc;
1597 
1598 	mgmt_callback_register(&mgmt_event_callback);
1599 	rc = settings_subsys_init();
1600 
1601 	zassert_ok(rc, "Expected settings to init");
1602 
1603 	return NULL;
1604 }
1605 
1606 ZTEST_SUITE(settings_mgmt, NULL, setup_test, NULL, cleanup_test, NULL);
1607