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_rotate)10 ZTEST(fcb_test_with_2sectors_set, test_fcb_rotate)
11 {
12 	struct fcb *fcb;
13 	int rc;
14 	int old_id;
15 	struct fcb_entry loc;
16 	uint8_t test_data[128] = {0};
17 	int elem_cnts[2] = {0, 0};
18 	int cnts[2];
19 	struct append_arg aa_arg = {
20 		.elem_cnts = cnts
21 	};
22 
23 	fcb = &test_fcb;
24 
25 	old_id = fcb->f_active_id;
26 	rc = fcb_rotate(fcb);
27 	zassert_true(rc == 0, "fcb_rotate call failure");
28 	zassert_true(fcb->f_active_id == old_id + 1,
29 		     "flash location id should increased");
30 
31 	/*
32 	 * Now fill up the
33 	 */
34 	while (1) {
35 		rc = fcb_append(fcb, sizeof(test_data), &loc);
36 		if (rc == -ENOSPC) {
37 			break;
38 		}
39 		if (loc.fe_sector == &test_fcb_sector[0]) {
40 			elem_cnts[0]++;
41 		} else if (loc.fe_sector == &test_fcb_sector[1]) {
42 			elem_cnts[1]++;
43 		} else {
44 			zassert_true(0,
45 				     "unexpected flash area of appended loc");
46 		}
47 
48 		rc = flash_area_write(fcb->fap, FCB_ENTRY_FA_DATA_OFF(loc),
49 				      test_data, sizeof(test_data));
50 		zassert_true(rc == 0, "flash_area_write call failure");
51 
52 		rc = fcb_append_finish(fcb, &loc);
53 		zassert_true(rc == 0, "fcb_append_finish call failure");
54 	}
55 	zassert_true(elem_cnts[0] > 0 && elem_cnts[0] == elem_cnts[1],
56 		     "unexpected entry number was appended");
57 
58 	old_id = fcb->f_active_id;
59 	rc = fcb_rotate(fcb);
60 	zassert_true(rc == 0, "fcb_rotate call failure");
61 	zassert_true(fcb->f_active_id == old_id,
62 		     "flash location should be kept");
63 
64 	(void)memset(cnts, 0, sizeof(cnts));
65 	rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
66 	zassert_true(rc == 0, "fcb_walk call failure");
67 	zassert_true(aa_arg.elem_cnts[0] == elem_cnts[0] ||
68 		     aa_arg.elem_cnts[1] == elem_cnts[1],
69 		     "fcb_walk: entry count got different than expected");
70 	zassert_true(aa_arg.elem_cnts[0] == 0 || aa_arg.elem_cnts[1] == 0,
71 		     "fcb_walk: entry count got different than expected");
72 
73 	/*
74 	 * One sector is full. The other one should have one entry in it.
75 	 */
76 	rc = fcb_append(fcb, sizeof(test_data), &loc);
77 	zassert_true(rc == 0, "fcb_append call failure");
78 
79 	rc = flash_area_write(fcb->fap, FCB_ENTRY_FA_DATA_OFF(loc), test_data,
80 			      sizeof(test_data));
81 	zassert_true(rc == 0, "flash_area_write call failure");
82 
83 	rc = fcb_append_finish(fcb, &loc);
84 	zassert_true(rc == 0, "fcb_append_finish call failure");
85 
86 	old_id = fcb->f_active_id;
87 	rc = fcb_rotate(fcb);
88 	zassert_true(rc == 0, "fcb_rotate call failure");
89 	zassert_true(fcb->f_active_id == old_id,
90 		     "flash location should be kept");
91 
92 	(void)memset(cnts, 0, sizeof(cnts));
93 	rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
94 	zassert_true(rc == 0, "fcb_walk call failure");
95 	zassert_true(aa_arg.elem_cnts[0] == 1 || aa_arg.elem_cnts[1] == 1,
96 		     "fcb_walk: entry count got different than expected");
97 	zassert_true(aa_arg.elem_cnts[0] == 0 || aa_arg.elem_cnts[1] == 0,
98 		     "fcb_walk: entry count got different than expected");
99 }
100