1 /*
2  * Copyright (c) 2018 Nordic Semiconductor ASA
3  * Copyright (c) 2015 Runtime Inc
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include "settings_test.h"
9 #include "settings/settings_fcb.h"
10 
11 #ifdef TEST_LONG
12 #define TESTS_S2_FCB_ITERATIONS 32
13 #else
14 #define TESTS_S2_FCB_ITERATIONS 2
15 #endif
16 
ZTEST(settings_config_fcb,test_config_save_2_fcb)17 ZTEST(settings_config_fcb, test_config_save_2_fcb)
18 {
19 	int rc;
20 	struct settings_fcb cf;
21 
22 	int i;
23 
24 	/* this case need c_test_handler[0] to handle access to val8 */
25 	rc = settings_register(&c_test_handlers[0]);
26 	zassert_true(rc == 0 || rc == -EEXIST, "settings_register fail");
27 	rc = settings_register(&c_test_handlers[1]);
28 	zassert_true(rc == 0 || rc == -EEXIST, "settings_register fail");
29 
30 	config_wipe_srcs();
31 
32 	cf.cf_fcb.f_magic = CONFIG_SETTINGS_FCB_MAGIC;
33 	cf.cf_fcb.f_sectors = fcb_sectors;
34 	cf.cf_fcb.f_sector_cnt = ARRAY_SIZE(fcb_sectors);
35 
36 	rc = settings_fcb_src(&cf);
37 	zassert_true(rc == 0, "can't register FCB as configuration source");
38 	settings_mount_fcb_backend(&cf);
39 
40 	rc = settings_fcb_dst(&cf);
41 	zassert_true(rc == 0,
42 		     "can't register FCB as configuration destination");
43 
44 	test_config_fill_area(test_ref_value, 0);
45 	memcpy(val_string, test_ref_value, sizeof(val_string));
46 
47 	c2_var_count = 1;
48 	printk("save val8 and val_string\n");
49 	val8 = 42U;
50 	rc = settings_save();
51 	zassert_true(rc == 0, "fcb write error");
52 
53 	printk("load val8 and val_string\n");
54 	val8 = 0U;
55 	(void)memset(val_string[0], 0, sizeof(val_string[0]));
56 	rc = settings_load();
57 	zassert_true(rc == 0, "fcb read error");
58 	zassert_true(val8 == 42U, "bad value read");
59 	zassert_str_equal(val_string[0], test_ref_value[0], "bad value read");
60 	test_export_block = 1;
61 
62 	/*
63 	 * Now add the number of settings to max. Keep adjusting the test_data,
64 	 * check that rollover happens when it's supposed to.
65 	 */
66 	c2_var_count = 64;
67 
68 	for (i = 0; i < TESTS_S2_FCB_ITERATIONS; i++) {
69 		test_config_fill_area(test_ref_value, i);
70 		memcpy(val_string, test_ref_value, sizeof(val_string));
71 
72 		rc = settings_save();
73 		zassert_true(rc == 0, "fcb write error");
74 
75 		(void)memset(val_string, 0, sizeof(val_string));
76 
77 		val8 = 0U;
78 		rc = settings_load();
79 		zassert_true(rc == 0, "fcb read error");
80 		zassert_true(!memcmp(val_string, test_ref_value,
81 				     sizeof(val_string)),
82 			     "bad value read");
83 		zassert_true(val8 == 42U, "bad value read");
84 	}
85 	c2_var_count = 0;
86 	test_export_block = 0;
87 	settings_unregister(&c_test_handlers[1]);
88 	settings_unregister(&c_test_handlers[0]);
89 }
90