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 
ZTEST(settings_config_fcb,test_config_compress_reset)11 ZTEST(settings_config_fcb, test_config_compress_reset)
12 {
13 	int rc;
14 	struct settings_fcb cf;
15 	struct flash_sector *fa;
16 	int elems[4];
17 	int i;
18 
19 	rc = settings_register(&c_test_handlers[1]);
20 	zassert_true(rc == 0 || rc == -EEXIST, "settings_register fail");
21 	config_wipe_srcs();
22 	config_wipe_fcb(fcb_sectors, ARRAY_SIZE(fcb_sectors));
23 
24 	cf.cf_fcb.f_magic = CONFIG_SETTINGS_FCB_MAGIC;
25 	cf.cf_fcb.f_sectors = fcb_sectors;
26 	cf.cf_fcb.f_sector_cnt = ARRAY_SIZE(fcb_sectors);
27 
28 	rc = settings_fcb_src(&cf);
29 	zassert_true(rc == 0, "can't register FCB as configuration source");
30 	settings_mount_fcb_backend(&cf);
31 
32 	rc = settings_fcb_dst(&cf);
33 	zassert_true(rc == 0,
34 		     "can't register FCB as configuration destination");
35 
36 	c2_var_count = 1;
37 	(void)memset(elems, 0, sizeof(elems));
38 
39 	for (i = 0; ; i++) {
40 		test_config_fill_area(test_ref_value, i);
41 		memcpy(val_string, test_ref_value, sizeof(val_string));
42 
43 		rc = settings_save();
44 		zassert_true(rc == 0, "fcb write error");
45 
46 		if (cf.cf_fcb.f_active.fe_sector == &fcb_sectors[2]) {
47 			/*
48 			 * Started using space just before scratch.
49 			 */
50 			break;
51 		}
52 		(void)memset(val_string, 0, sizeof(val_string));
53 
54 		rc = settings_load();
55 		zassert_true(rc == 0, "fcb read error");
56 		zassert_true(!memcmp(val_string, test_ref_value,
57 				     SETTINGS_MAX_VAL_LEN),
58 			     "bad value read");
59 	}
60 
61 	fa = cf.cf_fcb.f_active.fe_sector;
62 	rc = fcb_append_to_scratch(&cf.cf_fcb);
63 	zassert_true(rc == 0, "fcb_append_to_scratch call failure");
64 	zassert_true(fcb_free_sector_cnt(&cf.cf_fcb) == 0,
65 		     "expected non of free sectors");
66 	zassert_true(fa != cf.cf_fcb.f_active.fe_sector,
67 		     "active page should change");
68 
69 	config_wipe_srcs();
70 
71 	(void)memset(&cf, 0, sizeof(cf));
72 
73 	cf.cf_fcb.f_magic = CONFIG_SETTINGS_FCB_MAGIC;
74 	cf.cf_fcb.f_sectors = fcb_sectors;
75 	cf.cf_fcb.f_sector_cnt = ARRAY_SIZE(fcb_sectors);
76 
77 	rc = settings_fcb_src(&cf);
78 	zassert_true(rc == 0, "can't register FCB as configuration source");
79 
80 	rc = settings_fcb_dst(&cf);
81 	zassert_true(rc == 0,
82 		     "can't register FCB as configuration destination");
83 
84 
85 	zassert_true(fcb_free_sector_cnt(&cf.cf_fcb) == 1,
86 		     "expected one free sector");
87 	zassert_true(fa == cf.cf_fcb.f_active.fe_sector,
88 		   "active sector should become free after garbage collection");
89 
90 	c2_var_count = 0;
91 	settings_unregister(&c_test_handlers[1]);
92 }
93