1 /*
2 * Copyright (c) 2017 Nordic Semiconductor ASA
3 * Copyright (c) 2015 Runtime Inc
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include "fcb_test.h"
9
ZTEST(fcb_test_with_2sectors_set,test_fcb_reset)10 ZTEST(fcb_test_with_2sectors_set, test_fcb_reset)
11 {
12 struct fcb *fcb;
13 int rc;
14 int i;
15 struct fcb_entry loc;
16 uint8_t test_data[128];
17 int var_cnt;
18
19 fcb = &test_fcb;
20
21 var_cnt = 0;
22 rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
23 zassert_true(rc == 0, "fcb_walk call failure");
24 zassert_true(var_cnt == 0,
25 "fcb_walk: elements count read different than expected");
26
27 rc = fcb_append(fcb, 32, &loc);
28 zassert_true(rc == 0, "fcb_append call failure");
29
30 /*
31 * No ready ones yet. CRC should not match.
32 */
33 var_cnt = 0;
34 rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
35 zassert_true(rc == 0, "fcb_walk call failure");
36 zassert_true(var_cnt == 0,
37 "fcb_walk: elements count read different than expected");
38
39 for (i = 0; i < sizeof(test_data); i++) {
40 test_data[i] = fcb_test_append_data(32, i);
41 }
42 rc = flash_area_write(fcb->fap, FCB_ENTRY_FA_DATA_OFF(loc), test_data,
43 32);
44 zassert_true(rc == 0, "flash_area_write call failure");
45
46 rc = fcb_append_finish(fcb, &loc);
47 zassert_true(rc == 0, "fcb_append_finish call failure");
48
49 /*
50 * one entry
51 */
52 var_cnt = 32;
53 rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
54 zassert_true(rc == 0, "fcb_walk call failure");
55 zassert_true(var_cnt == 33,
56 "fcb_walk: elements count read different than expected");
57
58 /*
59 * Pretend reset
60 */
61 (void)memset(fcb, 0, sizeof(*fcb));
62 fcb->f_sector_cnt = 2U;
63 fcb->f_sectors = test_fcb_sector;
64
65 rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb);
66 zassert_true(rc == 0, "fcb_init call failure");
67
68 var_cnt = 32;
69 rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
70 zassert_true(rc == 0, "fcb_walk call failure");
71 zassert_true(var_cnt == 33,
72 "fcb_walk: elements count read different than expected");
73
74 rc = fcb_append(fcb, 33, &loc);
75 zassert_true(rc == 0, "fcb_append call failure");
76
77 for (i = 0; i < sizeof(test_data); i++) {
78 test_data[i] = fcb_test_append_data(33, i);
79 }
80 rc = flash_area_write(fcb->fap, FCB_ENTRY_FA_DATA_OFF(loc), test_data,
81 33);
82 zassert_true(rc == 0, "flash_area_write call failure");
83
84 rc = fcb_append_finish(fcb, &loc);
85 zassert_true(rc == 0, "fcb_append_finish call failure");
86
87 var_cnt = 32;
88 rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
89 zassert_true(rc == 0, "fcb_walk call failure");
90 zassert_true(var_cnt == 34,
91 "fcb_walk: elements count read different than expected");
92
93 /*
94 * Add partial one, make sure that we survive reset then.
95 */
96 rc = fcb_append(fcb, 34, &loc);
97 zassert_true(rc == 0, "fcb_append call failure");
98
99 (void)memset(fcb, 0, sizeof(*fcb));
100 fcb->f_sector_cnt = 2U;
101 fcb->f_sectors = test_fcb_sector;
102
103 rc = fcb_init(TEST_FCB_FLASH_AREA_ID, fcb);
104 zassert_true(rc == 0, "fcb_init call failure");
105
106 /*
107 * Walk should skip that.
108 */
109 var_cnt = 32;
110 rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
111 zassert_true(rc == 0, "fcb_walk call failure");
112 zassert_true(var_cnt == 34,
113 "fcb_walk: elements count read different than expected");
114
115 /* Add a 3rd one, should go behind corrupt entry */
116 rc = fcb_append(fcb, 34, &loc);
117 zassert_true(rc == 0, "fcb_append call failure");
118
119 for (i = 0; i < sizeof(test_data); i++) {
120 test_data[i] = fcb_test_append_data(34, i);
121 }
122 rc = flash_area_write(fcb->fap, FCB_ENTRY_FA_DATA_OFF(loc), test_data,
123 34);
124 zassert_true(rc == 0, "flash_area_write call failure");
125
126 rc = fcb_append_finish(fcb, &loc);
127 zassert_true(rc == 0, "fcb_append_finish call failure");
128
129 /*
130 * Walk should skip corrupt entry, but report the next one.
131 */
132 var_cnt = 32;
133 rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
134 zassert_true(rc == 0, "fcb_walk call failure");
135 zassert_true(var_cnt == 35,
136 "fcb_walk: elements count read different than expected");
137 }
138