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_4sectors_set,test_fcb_multi_scratch)10 ZTEST(fcb_test_with_4sectors_set, test_fcb_multi_scratch)
11 {
12 struct fcb *fcb;
13 int rc;
14 struct fcb_entry loc;
15 uint8_t test_data[128] = {0};
16 int elem_cnts[4];
17 int idx;
18 int cnts[4];
19 struct append_arg aa_arg = {
20 .elem_cnts = cnts
21 };
22
23 fcb = &test_fcb;
24 fcb->f_scratch_cnt = 1U;
25
26 /*
27 * Now fill up everything. We should be able to get 3 of the sectors
28 * full.
29 */
30 (void)memset(elem_cnts, 0, sizeof(elem_cnts));
31 while (1) {
32 rc = fcb_append(fcb, sizeof(test_data), &loc);
33 if (rc == -ENOSPC) {
34 break;
35 }
36 idx = loc.fe_sector - &test_fcb_sector[0];
37 elem_cnts[idx]++;
38
39 rc = flash_area_write(fcb->fap, FCB_ENTRY_FA_DATA_OFF(loc),
40 test_data, sizeof(test_data));
41 zassert_true(rc == 0, "flash_area_write call failure");
42
43 rc = fcb_append_finish(fcb, &loc);
44 zassert_true(rc == 0, "fcb_append_finish call failure");
45 }
46
47 zassert_true(elem_cnts[0] > 0, "unexpected entry number was appended");
48 zassert_true(elem_cnts[0] == elem_cnts[1] &&
49 elem_cnts[0] == elem_cnts[2],
50 "unexpected entry number was appended");
51 zassert_true(elem_cnts[3] == 0, "unexpected entry number was appended");
52
53 /*
54 * Ask to use scratch block, then fill it up.
55 */
56 rc = fcb_append_to_scratch(fcb);
57 zassert_true(rc == 0, "fcb_append_to_scratch call failure");
58
59 while (1) {
60 rc = fcb_append(fcb, sizeof(test_data), &loc);
61 if (rc == -ENOSPC) {
62 break;
63 }
64 idx = loc.fe_sector - &test_fcb_sector[0];
65 elem_cnts[idx]++;
66
67 rc = flash_area_write(fcb->fap, FCB_ENTRY_FA_DATA_OFF(loc),
68 test_data, sizeof(test_data));
69 zassert_true(rc == 0, "flash_area_write call failure");
70
71 rc = fcb_append_finish(fcb, &loc);
72 zassert_true(rc == 0, "fcb_append_finish call failure");
73 }
74 zassert_true(elem_cnts[3] == elem_cnts[0],
75 "unexpected entry number was appended");
76
77 /*
78 * Rotate
79 */
80 rc = fcb_rotate(fcb);
81 zassert_true(rc == 0, "fcb_rotate call failure");
82
83 (void)memset(&cnts, 0, sizeof(cnts));
84 rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
85 zassert_true(rc == 0, "fcb_walk call failure");
86
87 zassert_true(cnts[0] == 0, "unexpected entry count");
88 zassert_true(cnts[1] > 0, "unexpected entry count");
89 zassert_true(cnts[1] == cnts[2] && cnts[1] == cnts[3],
90 "unexpected entry count");
91
92 rc = fcb_append_to_scratch(fcb);
93 zassert_true(rc == 0, "fcb_append_to_scratch call failure");
94 rc = fcb_append_to_scratch(fcb);
95 zassert_true(rc != 0, "fcb_append_to_scratch call should fail");
96 }
97