1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include <algorithm>
16
17 #include "gtest/gtest.h"
18 #include "pw_kvs/flash_memory.h"
19 #include "pw_kvs/flash_test_partition.h"
20 #include "pw_kvs_private/config.h"
21 #include "pw_log/log.h"
22 #include "pw_span/span.h"
23
24 namespace pw::kvs::PartitionTest {
25 namespace {
26
27 #if !defined(PW_FLASH_TEST_ITERATIONS) || (PW_FLASH_TEST_ITERATIONS <= 0)
28 #error PW_FLASH_TEST_ITERATIONS must be defined and > 0
29 #endif // PW_FLASH_TEST_ITERATIONS
30
31 #if !defined(PW_FLASH_TEST_WRITE_SIZE) || (PW_FLASH_TEST_WRITE_SIZE <= 0)
32 #error PW_FLASH_TEST_WRITE_SIZE must be defined and > 0
33 #endif // PW_FLASH_TEST_WRITE_SIZE
34
35 constexpr size_t kTestIterations = PW_FLASH_TEST_ITERATIONS;
36 constexpr size_t kTestWriteSize = PW_FLASH_TEST_WRITE_SIZE;
37
38 size_t error_count = 0;
39
WriteData(FlashPartition & partition,uint8_t fill_byte)40 void WriteData(FlashPartition& partition, uint8_t fill_byte) {
41 uint8_t test_data[kMaxFlashAlignment];
42 memset(test_data, fill_byte, sizeof(test_data));
43
44 const size_t write_size =
45 std::max(kTestWriteSize, partition.alignment_bytes());
46
47 ASSERT_EQ(OkStatus(), partition.Erase(0, partition.sector_count()));
48
49 const size_t chunks_per_sector = partition.sector_size_bytes() / write_size;
50
51 // Fill partition sector by sector. Fill the sector with an integer number
52 // of write_size-size chunks. If the sector is not evenly divisible by
53 // write_size-size, the remainder is not written.
54 for (size_t sector_index = 0; sector_index < partition.sector_count();
55 sector_index++) {
56 FlashPartition::Address address =
57 sector_index * partition.sector_size_bytes();
58
59 for (size_t chunk_index = 0; chunk_index < chunks_per_sector;
60 chunk_index++) {
61 StatusWithSize status =
62 partition.Write(address, as_bytes(span(test_data, write_size)));
63 ASSERT_EQ(OkStatus(), status.status());
64 ASSERT_EQ(write_size, status.size());
65 address += write_size;
66 }
67 }
68
69 // Check the fill result. Use expect so the test doesn't bail on error.
70 // Count the errors and print if any errors are found.
71 for (size_t sector_index = 0; sector_index < partition.sector_count();
72 sector_index++) {
73 FlashPartition::Address address =
74 sector_index * partition.sector_size_bytes();
75
76 for (size_t chunk_index = 0; chunk_index < chunks_per_sector;
77 chunk_index++) {
78 memset(test_data, 0, sizeof(test_data));
79 StatusWithSize status = partition.Read(address, write_size, test_data);
80
81 EXPECT_EQ(OkStatus(), status.status());
82 EXPECT_EQ(write_size, status.size());
83 if (!status.ok() || (write_size != status.size())) {
84 error_count++;
85 PW_LOG_DEBUG(" Read Error [%s], %u of %u",
86 status.status().str(),
87 unsigned(status.size()),
88 unsigned(write_size));
89 continue;
90 }
91
92 for (size_t i = 0; i < write_size; i++) {
93 if (test_data[i] != fill_byte) {
94 error_count++;
95 PW_LOG_DEBUG(
96 " Error %u, Read compare @ address %x, got 0x%02x, "
97 "expected 0x%02x",
98 unsigned(error_count),
99 unsigned(address + i),
100 unsigned(test_data[i]),
101 unsigned(fill_byte));
102 }
103 }
104
105 address += write_size;
106 }
107 }
108
109 EXPECT_EQ(error_count, 0U);
110 if (error_count != 0) {
111 PW_LOG_ERROR("Partition test, fill '%c', %u errors found",
112 fill_byte,
113 unsigned(error_count));
114 }
115 }
116
TEST(FlashPartitionTest,FillTest)117 TEST(FlashPartitionTest, FillTest) {
118 FlashPartition& test_partition = FlashTestPartition();
119
120 ASSERT_GE(kMaxFlashAlignment, test_partition.alignment_bytes());
121
122 for (size_t i = 0; i < kTestIterations; i++) {
123 PW_LOG_DEBUG("FillTest iteration %u, write '0'", unsigned(i));
124 WriteData(test_partition, 0);
125 PW_LOG_DEBUG("FillTest iteration %u, write '0xff'", unsigned(i));
126 WriteData(test_partition, 0xff);
127 PW_LOG_DEBUG("FillTest iteration %u, write '0x55'", unsigned(i));
128 WriteData(test_partition, 0x55);
129 PW_LOG_DEBUG("FillTest iteration %u, write '0xa3'", unsigned(i));
130 WriteData(test_partition, 0xa3);
131 PW_LOG_DEBUG("Completed iterations %u, Total errors %u",
132 unsigned(i),
133 unsigned(error_count));
134 }
135 }
136
TEST(FlashPartitionTest,EraseTest)137 TEST(FlashPartitionTest, EraseTest) {
138 FlashPartition& test_partition = FlashTestPartition();
139
140 static const uint8_t fill_byte = 0x55;
141 uint8_t test_data[kMaxFlashAlignment];
142 memset(test_data, fill_byte, sizeof(test_data));
143
144 ASSERT_GE(kMaxFlashAlignment, test_partition.alignment_bytes());
145
146 const size_t block_size =
147 std::min(sizeof(test_data), test_partition.sector_size_bytes());
148 auto data_span = span(test_data, block_size);
149
150 ASSERT_EQ(OkStatus(), test_partition.Erase(0, test_partition.sector_count()));
151
152 // Write to the first page of each sector.
153 for (size_t sector_index = 0; sector_index < test_partition.sector_count();
154 sector_index++) {
155 FlashPartition::Address address =
156 sector_index * test_partition.sector_size_bytes();
157
158 StatusWithSize status = test_partition.Write(address, as_bytes(data_span));
159 ASSERT_EQ(OkStatus(), status.status());
160 ASSERT_EQ(block_size, status.size());
161 }
162
163 // Preset the flag to make sure the check actually sets it.
164 bool is_erased = true;
165 ASSERT_EQ(OkStatus(), test_partition.IsErased(&is_erased));
166 ASSERT_EQ(false, is_erased);
167
168 ASSERT_EQ(OkStatus(), test_partition.Erase());
169
170 // Preset the flag to make sure the check actually sets it.
171 is_erased = false;
172 ASSERT_EQ(OkStatus(), test_partition.IsErased(&is_erased));
173 ASSERT_EQ(true, is_erased);
174
175 // Read the first page of each sector and make sure it has been erased.
176 for (size_t sector_index = 0; sector_index < test_partition.sector_count();
177 sector_index++) {
178 FlashPartition::Address address =
179 sector_index * test_partition.sector_size_bytes();
180
181 StatusWithSize status =
182 test_partition.Read(address, data_span.size_bytes(), data_span.data());
183 EXPECT_EQ(OkStatus(), status.status());
184 EXPECT_EQ(data_span.size_bytes(), status.size());
185
186 EXPECT_EQ(true, test_partition.AppearsErased(as_bytes(data_span)));
187 }
188 }
189
TEST(FlashPartitionTest,AlignmentCheck)190 TEST(FlashPartitionTest, AlignmentCheck) {
191 FlashPartition& test_partition = FlashTestPartition();
192 const size_t alignment = test_partition.alignment_bytes();
193 const size_t sector_size_bytes = test_partition.sector_size_bytes();
194
195 EXPECT_LE(kTestWriteSize, kMaxFlashAlignment);
196 EXPECT_GT(kTestWriteSize, 0u);
197 EXPECT_EQ(kMaxFlashAlignment % kTestWriteSize, 0U);
198
199 EXPECT_LE(alignment, kMaxFlashAlignment);
200 EXPECT_GT(alignment, 0u);
201 EXPECT_EQ(kMaxFlashAlignment % alignment, 0U);
202
203 EXPECT_LE(kMaxFlashAlignment, sector_size_bytes);
204 EXPECT_LE(sector_size_bytes % kMaxFlashAlignment, 0U);
205 }
206
207 #define TESTING_CHECK_FAILURES_IS_SUPPORTED 0
208 #if TESTING_CHECK_FAILURES_IS_SUPPORTED
209 // TODO(davidrogers): Ensure that this test triggers an assert.
TEST(FlashPartitionTest,BadWriteAddressAlignment)210 TEST(FlashPartitionTest, BadWriteAddressAlignment) {
211 FlashPartition& test_partition = FlashTestPartition();
212
213 // Can't get bad alignment with alignment of 1.
214 if (test_partition.alignment_bytes() == 1) {
215 return;
216 }
217
218 std::array<std::byte, kMaxFlashAlignment> source_data;
219 test_partition.Write(1, source_data);
220 }
221
222 // TODO(davidrogers): Ensure that this test triggers an assert.
TEST(FlashPartitionTest,BadWriteSizeAlignment)223 TEST(FlashPartitionTest, BadWriteSizeAlignment) {
224 FlashPartition& test_partition = FlashTestPartition();
225
226 // Can't get bad alignment with alignment of 1.
227 if (test_partition.alignment_bytes() == 1) {
228 return;
229 }
230
231 std::array<std::byte, 1> source_data;
232 test_partition.Write(0, source_data);
233 }
234
235 // TODO(davidrogers): Ensure that this test triggers an assert.
TEST(FlashPartitionTest,BadEraseAddressAlignment)236 TEST(FlashPartitionTest, BadEraseAddressAlignment) {
237 FlashPartition& test_partition = FlashTestPartition();
238
239 // Can't get bad alignment with sector size of 1.
240 if (test_partition.sector_size_bytes() == 1) {
241 return;
242 }
243
244 // Try Erase at address 1 for 1 sector.
245 test_partition.Erase(1, 1);
246 }
247
248 #endif // TESTING_CHECK_FAILURES_IS_SUPPORTED
249
TEST(FlashPartitionTest,IsErased)250 TEST(FlashPartitionTest, IsErased) {
251 FlashPartition& test_partition = FlashTestPartition();
252 const size_t write_size =
253 std::max(kTestWriteSize, test_partition.alignment_bytes());
254
255 // Make sure the partition is big enough to do this test.
256 ASSERT_GE(test_partition.size_bytes(), 3 * kMaxFlashAlignment);
257
258 ASSERT_EQ(OkStatus(), test_partition.Erase());
259
260 bool is_erased = true;
261 ASSERT_EQ(OkStatus(), test_partition.IsErased(&is_erased));
262 ASSERT_EQ(true, is_erased);
263
264 static const uint8_t fill_byte = 0x55;
265 uint8_t test_data[kMaxFlashAlignment];
266 memset(test_data, fill_byte, sizeof(test_data));
267 auto data_span = span(test_data);
268
269 // Write the chunk with fill byte.
270 StatusWithSize status = test_partition.Write(write_size, as_bytes(data_span));
271 ASSERT_EQ(OkStatus(), status.status());
272 ASSERT_EQ(data_span.size_bytes(), status.size());
273
274 EXPECT_EQ(OkStatus(), test_partition.IsErased(&is_erased));
275 EXPECT_EQ(false, is_erased);
276
277 // Check the chunk that was written.
278 EXPECT_EQ(OkStatus(),
279 test_partition.IsRegionErased(
280 write_size, data_span.size_bytes(), &is_erased));
281 EXPECT_EQ(false, is_erased);
282
283 // Check a region that starts erased but later has been written.
284 EXPECT_EQ(OkStatus(),
285 test_partition.IsRegionErased(0, 2 * write_size, &is_erased));
286 EXPECT_EQ(false, is_erased);
287
288 // Check erased for a region smaller than kMaxFlashAlignment. This has been a
289 // bug in the past.
290 EXPECT_EQ(OkStatus(),
291 test_partition.IsRegionErased(0, write_size, &is_erased));
292 EXPECT_EQ(true, is_erased);
293 }
294
295 } // namespace
296 } // namespace pw::kvs::PartitionTest
297