Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_kvs / entry_test.cc
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 "pw_kvs/internal/entry.h"
16
17 #include <span>
18 #include <string_view>
19
20 #include "gtest/gtest.h"
21 #include "pw_bytes/array.h"
22 #include "pw_kvs/alignment.h"
23 #include "pw_kvs/checksum.h"
24 #include "pw_kvs/crc16_checksum.h"
25 #include "pw_kvs/fake_flash_memory.h"
26 #include "pw_kvs/flash_memory.h"
27 #include "pw_kvs/format.h"
28
29 namespace pw::kvs::internal {
30 namespace {
31
32 using std::byte;
33 using std::string_view;
34
35 // For magic value always use a random 32 bit integer rather than a human
36 // readable 4 bytes. See pw_kvs/format.h for more information.
37 constexpr EntryFormat kFormat{0x961c2ff9, nullptr};
38
39 TEST(Entry, Size_RoundsUpToAlignment) {
40   // Use FakeFlashMemory, rather than FakeFlashMemoryBuffer, so the class gets
41   // tested/used directly.
42   std::array<std::byte, 64 * 2> buffer;
43
44   // Flash alignment needs to be 1 due to how the partition is used in this
45   // test.
46   FakeFlashMemory flash(buffer, 64, 2, 1);
47
48   for (size_t alignment_bytes = 1; alignment_bytes <= 4096; ++alignment_bytes) {
49     FlashPartition partition(&flash, 0, flash.sector_count(), alignment_bytes);
50     const size_t align = AlignUp(alignment_bytes, Entry::kMinAlignmentBytes);
51
52     for (size_t value : {size_t(0), align - 1, align, align + 1, 2 * align}) {
53       Entry entry =
54           Entry::Valid(partition, 0, kFormat, "k", {nullptr, value}, 0);
55
56       ASSERT_EQ(AlignUp(sizeof(EntryHeader) + 1 /* key */ + value, align),
57                 entry.size());
58     }
59
60     Entry entry = Entry::Tombstone(partition, 0, kFormat, "k", 0);
61     ASSERT_EQ(AlignUp(sizeof(EntryHeader) + 1 /* key */, align), entry.size());
62   }
63 }
64
65 TEST(Entry, Construct_ValidEntry) {
66   FakeFlashMemoryBuffer<64, 2> flash(16);
67   FlashPartition partition(&flash, 0, flash.sector_count());
68
69   auto entry = Entry::Valid(
70       partition, 1, kFormat, "k", std::as_bytes(std::span("123")), 9876);
71
72   EXPECT_FALSE(entry.deleted());
73   EXPECT_EQ(entry.magic(), kFormat.magic);
74   EXPECT_EQ(entry.value_size(), sizeof("123"));
75   EXPECT_EQ(entry.transaction_id(), 9876u);
76 }
77
78 TEST(Entry, Construct_Tombstone) {
79   FakeFlashMemoryBuffer<64, 2> flash(16);
80   FlashPartition partition(&flash, 0, flash.sector_count());
81
82   auto entry = Entry::Tombstone(partition, 1, kFormat, "key", 123);
83
84   EXPECT_TRUE(entry.deleted());
85   EXPECT_EQ(entry.magic(), kFormat.magic);
86   EXPECT_EQ(entry.value_size(), 0u);
87   EXPECT_EQ(entry.transaction_id(), 123u);
88 }
89
90 // For magic value always use a unique random 32 bit integer rather than a human
91 // readable 4 bytes. See pw_kvs/format.h for more information.
92 constexpr uint32_t kMagicWithChecksum = 0xad165142;
93 constexpr uint32_t kTransactionId1 = 0x96979899;
94
95 constexpr auto kKey1 = bytes::String("key45");
96 constexpr auto kValue1 = bytes::String("VALUE!");
97 constexpr auto kPadding1 = bytes::String("\0\0\0\0\0");
98
99 constexpr auto kHeader1 = bytes::Concat(kMagicWithChecksum,
100                                         uint32_t(0x23aa),  // checksum (CRC16)
101                                         uint8_t(1),        // alignment (32 B)
102                                         uint8_t(kKey1.size()),     // key length
103                                         uint16_t(kValue1.size()),  // value size
104                                         kTransactionId1  // transaction ID
105 );
106
107 constexpr auto kEntryWithoutPadding1 = bytes::Concat(kHeader1, kKey1, kValue1);
108 constexpr auto kEntry1 = bytes::Concat(kEntryWithoutPadding1, kPadding1);
109 static_assert(kEntry1.size() == 32);
110
111 ChecksumCrc16 default_checksum;
112 constexpr EntryFormat kFormatWithChecksum{kMagicWithChecksum,
113                                           &default_checksum};
114 constexpr internal::EntryFormats kFormats(kFormatWithChecksum);
115
116 class ValidEntryInFlash : public ::testing::Test {
117  protected:
118   ValidEntryInFlash() : flash_(kEntry1), partition_(&flash_) {
119     EXPECT_EQ(OkStatus(), Entry::Read(partition_, 0, kFormats, &entry_));
120   }
121
122   FakeFlashMemoryBuffer<1024, 4> flash_;
123   FlashPartition partition_;
124   Entry entry_;
125 };
126
127 TEST_F(ValidEntryInFlash, PassesChecksumVerification) {
128   EXPECT_EQ(OkStatus(), entry_.VerifyChecksumInFlash());
129   EXPECT_EQ(OkStatus(), entry_.VerifyChecksum("key45", kValue1));
130 }
131
132 TEST_F(ValidEntryInFlash, HeaderContents) {
133   EXPECT_EQ(entry_.magic(), kMagicWithChecksum);
134   EXPECT_EQ(entry_.key_length(), 5u);
135   EXPECT_EQ(entry_.value_size(), 6u);
136   EXPECT_EQ(entry_.transaction_id(), kTransactionId1);
137   EXPECT_FALSE(entry_.deleted());
138 }
139
140 TEST_F(ValidEntryInFlash, ReadKey) {
141   Entry::KeyBuffer key = {};
142   auto result = entry_.ReadKey(key);
143
144   ASSERT_EQ(OkStatus(), result.status());
145   EXPECT_EQ(result.size(), entry_.key_length());
146   EXPECT_STREQ(key.data(), "key45");
147 }
148
149 TEST_F(ValidEntryInFlash, ReadValue) {
150   char value[32] = {};
151   auto result = entry_.ReadValue(std::as_writable_bytes(std::span(value)));
152
153   ASSERT_EQ(OkStatus(), result.status());
154   EXPECT_EQ(result.size(), entry_.value_size());
155   EXPECT_STREQ(value, "VALUE!");
156 }
157
158 TEST_F(ValidEntryInFlash, ReadValue_BufferTooSmall) {
159   char value[3] = {};
160   auto result = entry_.ReadValue(std::as_writable_bytes(std::span(value)));
161
162   ASSERT_EQ(Status::ResourceExhausted(), result.status());
163   EXPECT_EQ(3u, result.size());
164   EXPECT_EQ(value[0], 'V');
165   EXPECT_EQ(value[1], 'A');
166   EXPECT_EQ(value[2], 'L');
167 }
168
169 TEST_F(ValidEntryInFlash, ReadValue_WithOffset) {
170   char value[3] = {};
171   auto result = entry_.ReadValue(std::as_writable_bytes(std::span(value)), 3);
172
173   ASSERT_EQ(OkStatus(), result.status());
174   EXPECT_EQ(3u, result.size());
175   EXPECT_EQ(value[0], 'U');
176   EXPECT_EQ(value[1], 'E');
177   EXPECT_EQ(value[2], '!');
178 }
179
180 TEST_F(ValidEntryInFlash, ReadValue_WithOffset_BufferTooSmall) {
181   char value[1] = {};
182   auto result = entry_.ReadValue(std::as_writable_bytes(std::span(value)), 4);
183
184   ASSERT_EQ(Status::ResourceExhausted(), result.status());
185   EXPECT_EQ(1u, result.size());
186   EXPECT_EQ(value[0], 'E');
187 }
188
189 TEST_F(ValidEntryInFlash, ReadValue_WithOffset_EmptyRead) {
190   char value[16] = {'?'};
191   auto result = entry_.ReadValue(std::as_writable_bytes(std::span(value)), 6);
192
193   ASSERT_EQ(OkStatus(), result.status());
194   EXPECT_EQ(0u, result.size());
195   EXPECT_EQ(value[0], '?');
196 }
197
198 TEST_F(ValidEntryInFlash, ReadValue_WithOffset_PastEnd) {
199   char value[16] = {};
200   auto result = entry_.ReadValue(std::as_writable_bytes(std::span(value)), 7);
201
202   EXPECT_EQ(Status::OutOfRange(), result.status());
203   EXPECT_EQ(0u, result.size());
204 }
205
206 TEST(ValidEntry, Write) {
207   FakeFlashMemoryBuffer<1024, 4> flash;
208   FlashPartition partition(&flash, 0, flash.sector_count(), 32);
209
210   Entry entry = Entry::Valid(
211       partition, 64, kFormatWithChecksum, "key45", kValue1, kTransactionId1);
212
213   auto result = entry.Write("key45", kValue1);
214   EXPECT_EQ(OkStatus(), result.status());
215   EXPECT_EQ(32u, result.size());
216   EXPECT_EQ(std::memcmp(&flash.buffer()[64], kEntry1.data(), kEntry1.size()),
217             0);
218 }
219
220 constexpr auto kHeader2 = bytes::String(
221     "\x42\x51\x16\xad"  // magic
222     "\xba\xb3\x00\x00"  // checksum (CRC16)
223     "\x00"              // alignment
224     "\x01"              // key length
225     "\xff\xff"          // value size
226     "\x00\x01\x02\x03"  // transaction ID
227 );
228
229 constexpr auto kKeyAndPadding2 =
230     bytes::String("K\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
231
232 class TombstoneEntryInFlash : public ::testing::Test {
233  protected:
234   TombstoneEntryInFlash()
235       : flash_(bytes::Concat(kHeader2, kKeyAndPadding2)), partition_(&flash_) {
236     EXPECT_EQ(OkStatus(), Entry::Read(partition_, 0, kFormats, &entry_));
237   }
238
239   FakeFlashMemoryBuffer<1024, 4> flash_;
240   FlashPartition partition_;
241   Entry entry_;
242 };
243
244 TEST_F(TombstoneEntryInFlash, PassesChecksumVerification) {
245   EXPECT_EQ(OkStatus(), entry_.VerifyChecksumInFlash());
246   EXPECT_EQ(OkStatus(), entry_.VerifyChecksum("K", {}));
247 }
248
249 TEST_F(TombstoneEntryInFlash, HeaderContents) {
250   EXPECT_EQ(entry_.magic(), kMagicWithChecksum);
251   EXPECT_EQ(entry_.key_length(), 1u);
252   EXPECT_EQ(entry_.value_size(), 0u);
253   EXPECT_EQ(entry_.transaction_id(), 0x03020100u);
254   EXPECT_TRUE(entry_.deleted());
255 }
256
257 TEST_F(TombstoneEntryInFlash, ReadKey) {
258   Entry::KeyBuffer key = {};
259   auto result = entry_.ReadKey(key);
260
261   ASSERT_EQ(OkStatus(), result.status());
262   EXPECT_EQ(result.size(), entry_.key_length());
263   EXPECT_STREQ(key.data(), "K");
264 }
265
266 TEST_F(TombstoneEntryInFlash, ReadValue) {
267   char value[32] = {};
268   auto result = entry_.ReadValue(std::as_writable_bytes(std::span(value)));
269
270   ASSERT_EQ(OkStatus(), result.status());
271   EXPECT_EQ(0u, result.size());
272 }
273
274 TEST(TombstoneEntry, Write) {
275   FakeFlashMemoryBuffer<1024, 4> flash;
276   FlashPartition partition(&flash);
277   ChecksumCrc16 checksum;
278
279   Entry entry =
280       Entry::Tombstone(partition, 16, kFormatWithChecksum, "K", 0x03020100);
281
282   auto result = entry.Write("K", {});
283   EXPECT_EQ(OkStatus(), result.status());
284   EXPECT_EQ(32u, result.size());
285   EXPECT_EQ(std::memcmp(&flash.buffer()[16],
286                         bytes::Concat(kHeader2, kKeyAndPadding2).data(),
287                         kEntry1.size()),
288             0);
289 }
290
291 TEST(Entry, Checksum_NoChecksumRequiresZero) {
292   FakeFlashMemoryBuffer<1024, 4> flash(kEntry1);
293   FlashPartition partition(&flash);
294   Entry entry;
295
296   const EntryFormat format{kMagicWithChecksum, nullptr};
297   const internal::EntryFormats formats(format);
298
299   ASSERT_EQ(OkStatus(), Entry::Read(partition, 0, formats, &entry));
300
301   EXPECT_EQ(Status::DataLoss(), entry.VerifyChecksumInFlash());
302   EXPECT_EQ(Status::DataLoss(), entry.VerifyChecksum({}, {}));
303
304   std::memset(&flash.buffer()[4], 0, 4);  // set the checksum field to 0
305   ASSERT_EQ(OkStatus(), Entry::Read(partition, 0, formats, &entry));
306   EXPECT_EQ(OkStatus(), entry.VerifyChecksumInFlash());
307   EXPECT_EQ(OkStatus(), entry.VerifyChecksum({}, {}));
308 }
309
310 TEST(Entry, Checksum_ChecksPadding) {
311   FakeFlashMemoryBuffer<1024, 4> flash(
312       bytes::Concat(kHeader1, kKey1, kValue1, bytes::String("\0\0\0\0\1")));
313   FlashPartition partition(&flash);
314   Entry entry;
315   ASSERT_EQ(OkStatus(), Entry::Read(partition, 0, kFormats, &entry));
316
317   // Last byte in padding is a 1; should fail.
318   EXPECT_EQ(Status::DataLoss(), entry.VerifyChecksumInFlash());
319
320   // The in-memory verification fills in 0s for the padding.
321   EXPECT_EQ(OkStatus(), entry.VerifyChecksum("key45", kValue1));
322
323   flash.buffer()[kEntry1.size() - 1] = byte{0};
324   EXPECT_EQ(OkStatus(), entry.VerifyChecksumInFlash());
325 }
326
327 TEST_F(ValidEntryInFlash, Update_SameFormat_TransactionIdIsUpdated) {
328   ASSERT_EQ(OkStatus(),
329             entry_.Update(kFormatWithChecksum, kTransactionId1 + 3));
330
331   EXPECT_EQ(kFormatWithChecksum.magic, entry_.magic());
332   EXPECT_EQ(0u, entry_.address());
333   EXPECT_EQ(kTransactionId1 + 3, entry_.transaction_id());
334   EXPECT_FALSE(entry_.deleted());
335 }
336
337 TEST_F(ValidEntryInFlash,
338        Update_DifferentFormat_MagicAndTransactionIdAreUpdated) {
339   ASSERT_EQ(OkStatus(), entry_.Update(kFormat, kTransactionId1 + 6));
340
341   EXPECT_EQ(kFormat.magic, entry_.magic());
342   EXPECT_EQ(0u, entry_.address());
343   EXPECT_EQ(kTransactionId1 + 6, entry_.transaction_id());
344   EXPECT_FALSE(entry_.deleted());
345 }
346
347 TEST_F(ValidEntryInFlash, Update_ReadError_WithChecksumIsError) {
348   flash_.InjectReadError(FlashError::Unconditional(Status::Aborted()));
349
350   EXPECT_EQ(Status::Aborted(),
351             entry_.Update(kFormatWithChecksum, kTransactionId1 + 1));
352 }
353
354 // For magic value always use a random 32 bit integer rather than a human
355 // readable 4 bytes. See pw_kvs/format.h for more information.
356 constexpr EntryFormat kNoChecksumFormat{.magic = 0x721bad24,
357                                         .checksum = nullptr};
358
359 TEST_F(ValidEntryInFlash, Update_ReadError_NoChecksumIsOkay) {
360   flash_.InjectReadError(FlashError::Unconditional(Status::Aborted()));
361
362   EXPECT_EQ(OkStatus(), entry_.Update(kNoChecksumFormat, kTransactionId1 + 1));
363 }
364
365 TEST_F(ValidEntryInFlash, Copy) {
366   auto result = entry_.Copy(123);
367
368   EXPECT_EQ(OkStatus(), result.status());
369   EXPECT_EQ(entry_.size(), result.size());
370   EXPECT_EQ(0,
371             std::memcmp(
372                 &flash_.buffer().data()[123], kEntry1.data(), kEntry1.size()));
373 }
374
375 TEST_F(ValidEntryInFlash, Copy_ReadError) {
376   flash_.InjectReadError(FlashError::Unconditional(Status::Unimplemented()));
377   auto result = entry_.Copy(kEntry1.size());
378   EXPECT_EQ(Status::Unimplemented(), result.status());
379   EXPECT_EQ(0u, result.size());
380 }
381
382 constexpr uint32_t ByteSum(std::span<const byte> bytes, uint32_t value = 0) {
383   for (byte b : bytes) {
384     value += unsigned(b);
385   }
386   return value;
387 }
388
389 // Sums the bytes, adding one to each byte so that zeroes change the checksum.
390 class ChecksumSummation final : public ChecksumAlgorithm {
391  public:
392   ChecksumSummation()
393       : ChecksumAlgorithm(std::as_bytes(std::span(&sum_, 1))), sum_(0) {}
394
395   void Reset() override { sum_ = 0; }
396
397   void Update(std::span<const byte> data) override {
398     for (byte b : data) {
399       sum_ += unsigned(b) + 1;  // Add 1 so zero-value bytes affect checksum.
400     }
401   }
402
403  private:
404   uint32_t sum_;
405 } sum_checksum;
406
407 // For magic value always use a random 32 bit integer rather than a human
408 // readable 4 bytes. See pw_kvs/format.h for more information.
409 constexpr uint32_t kMagicWithSum = 0x6093aadb;
410 constexpr EntryFormat kFormatWithSum{kMagicWithSum, &sum_checksum};
411 constexpr internal::EntryFormats kFormatsWithSum(kFormatWithSum);
412
413 template <size_t alignment>
414 constexpr auto MakeNewFormatWithSumEntry() {
415   constexpr uint8_t alignment_units = (alignment + 15) / 16 - 1;
416   constexpr size_t size = AlignUp(kEntryWithoutPadding1.size(), alignment);
417
418   constexpr uint32_t checksum =
419       ByteSum(bytes::Concat(kFormatWithSum.magic)) + 0 /* checksum */ +
420       alignment_units + kKey1.size() + kValue1.size() +
421       ByteSum(bytes::Concat(kTransactionId1 + 1)) + ByteSum(kKey1) +
422       ByteSum(kValue1) + size /* +1 for each byte in the checksum */;
423
424   constexpr auto kNewHeader1 =
425       bytes::Concat(kFormatWithSum.magic,      // magic
426                     checksum,                  // checksum (byte sum)
427                     alignment_units,           // alignment (in 16 B units)
428                     uint8_t(kKey1.size()),     // key length
429                     uint16_t(kValue1.size()),  // value size
430                     kTransactionId1 + 1);      // transaction ID
431   constexpr size_t padding = Padding(kEntryWithoutPadding1.size(), alignment);
432   return bytes::Concat(
433       kNewHeader1, kKey1, kValue1, bytes::Initialized<padding>(0));
434 }
435
436 TEST_F(ValidEntryInFlash, UpdateAndCopy_DifferentFormatSmallerAlignment) {
437   // Uses 16-bit alignment, smaller than the original entry's alignment.
438   ASSERT_EQ(OkStatus(), entry_.Update(kFormatWithSum, kTransactionId1 + 1));
439
440   StatusWithSize result = entry_.Copy(kEntry1.size());
441   ASSERT_EQ(OkStatus(), result.status());
442   EXPECT_EQ(kEntry1.size(), result.size());
443
444   constexpr auto new_data = MakeNewFormatWithSumEntry<16>();
445   static_assert(new_data.size() == 32);
446
447   EXPECT_EQ(
448       0,
449       std::memcmp(
450           &flash_.buffer()[kEntry1.size()], new_data.data(), new_data.size()));
451   Entry new_entry;
452   ASSERT_EQ(OkStatus(),
453             Entry::Read(partition_, 32, kFormatsWithSum, &new_entry));
454   EXPECT_EQ(OkStatus(), new_entry.VerifyChecksumInFlash());
455   EXPECT_EQ(kFormatWithSum.magic, new_entry.magic());
456   EXPECT_EQ(kTransactionId1 + 1, new_entry.transaction_id());
457 }
458
459 TEST(ValidEntryInFlash, UpdateAndCopy_DifferentFormatSameAlignment) {
460   // Use 32-bit alignment, the same as the original entry's alignment.
461   FakeFlashMemoryBuffer<1024, 4> flash(kEntry1);
462   FlashPartition partition(&flash, 0, 4, 32);
463   Entry entry;
464   ASSERT_EQ(OkStatus(), Entry::Read(partition, 0, kFormats, &entry));
465
466   ASSERT_EQ(OkStatus(), entry.Update(kFormatWithSum, kTransactionId1 + 1));
467
468   StatusWithSize result = entry.Copy(32);
469   ASSERT_EQ(OkStatus(), result.status());
470   EXPECT_EQ(AlignUp(kEntry1.size(), 32), result.size());
471
472   constexpr auto new_data = MakeNewFormatWithSumEntry<32>();
473   static_assert(new_data.size() == 32);
474
475   EXPECT_EQ(0,
476             std::memcmp(&flash.buffer()[32], new_data.data(), new_data.size()));
477
478   Entry new_entry;
479   ASSERT_EQ(OkStatus(),
480             Entry::Read(partition, 32, kFormatsWithSum, &new_entry));
481   EXPECT_EQ(OkStatus(), new_entry.VerifyChecksumInFlash());
482   EXPECT_EQ(kTransactionId1 + 1, new_entry.transaction_id());
483 }
484
485 TEST(ValidEntryInFlash, UpdateAndCopy_DifferentFormatLargerAlignment) {
486   // Use 64-bit alignment, larger than the original entry's alignment.
487   FakeFlashMemoryBuffer<1024, 4> flash(kEntry1);
488   FlashPartition partition(&flash, 0, 4, 64);
489   Entry entry;
490   ASSERT_EQ(OkStatus(), Entry::Read(partition, 0, kFormats, &entry));
491
492   ASSERT_EQ(OkStatus(), entry.Update(kFormatWithSum, kTransactionId1 + 1));
493
494   StatusWithSize result = entry.Copy(64);
495   ASSERT_EQ(OkStatus(), result.status());
496   EXPECT_EQ(AlignUp(kEntry1.size(), 64), result.size());
497
498   constexpr auto new_data = MakeNewFormatWithSumEntry<64>();
499   static_assert(new_data.size() == 64);
500
501   EXPECT_EQ(0,
502             std::memcmp(&flash.buffer()[64], new_data.data(), new_data.size()));
503
504   Entry new_entry;
505   ASSERT_EQ(OkStatus(),
506             Entry::Read(partition, 64, kFormatsWithSum, &new_entry));
507   EXPECT_EQ(OkStatus(), new_entry.VerifyChecksumInFlash());
508   EXPECT_EQ(kTransactionId1 + 1, new_entry.transaction_id());
509 }
510
511 TEST_F(ValidEntryInFlash, UpdateAndCopy_NoChecksum_UpdatesToNewFormat) {
512   // For magic value always use a random 32 bit integer rather than a human
513   // readable 4 bytes. See pw_kvs/format.h for more information.
514   constexpr EntryFormat no_checksum{.magic = 0x43fae18f, .checksum = nullptr};
515
516   ASSERT_EQ(OkStatus(), entry_.Update(no_checksum, kTransactionId1 + 1));
517
518   auto result = entry_.Copy(kEntry1.size());
519   ASSERT_EQ(OkStatus(), result.status());
520   EXPECT_EQ(kEntry1.size(), result.size());
521
522   constexpr auto kNewHeader1 =
523       bytes::Concat(no_checksum.magic,  // magic
524                     uint32_t(0),        // checksum (none)
525                     uint8_t(0),         // alignment (changed to 16 B from 32)
526                     uint8_t(kKey1.size()),     // key length
527                     uint16_t(kValue1.size()),  // value size
528                     kTransactionId1 + 1);      // transaction ID
529   constexpr auto kNewEntry1 =
530       bytes::Concat(kNewHeader1, kKey1, kValue1, kPadding1);
531
532   EXPECT_EQ(0,
533             std::memcmp(&flash_.buffer()[kEntry1.size()],
534                         kNewEntry1.data(),
535                         kNewEntry1.size()));
536 }
537
538 TEST_F(ValidEntryInFlash, UpdateAndCopyMultple_DifferentFormat) {
539   ASSERT_EQ(OkStatus(), entry_.Update(kFormatWithSum, kTransactionId1 + 6));
540
541   FlashPartition::Address new_address = entry_.size();
542
543   for (int i = 0; i < 10; i++) {
544     StatusWithSize copy_result = entry_.Copy(new_address + (i * entry_.size()));
545     ASSERT_EQ(OkStatus(), copy_result.status());
546     ASSERT_EQ(kEntry1.size(), copy_result.size());
547   }
548
549   for (int j = 0; j < 10; j++) {
550     Entry entry;
551     FlashPartition::Address read_address = (new_address + (j * entry_.size()));
552     ASSERT_EQ(OkStatus(),
553               Entry::Read(partition_, read_address, kFormatsWithSum, &entry));
554
555     EXPECT_EQ(OkStatus(), entry.VerifyChecksumInFlash());
556     EXPECT_EQ(kFormatWithSum.magic, entry.magic());
557     EXPECT_EQ(read_address, entry.address());
558     EXPECT_EQ(kTransactionId1 + 6, entry.transaction_id());
559     EXPECT_FALSE(entry.deleted());
560   }
561 }
562
563 TEST_F(ValidEntryInFlash, DifferentFormat_UpdatedCopy_FailsWithWrongMagic) {
564   ASSERT_EQ(OkStatus(), entry_.Update(kFormatWithSum, kTransactionId1 + 6));
565
566   FlashPartition::Address new_address = entry_.size();
567
568   StatusWithSize copy_result = entry_.Copy(new_address);
569   ASSERT_EQ(OkStatus(), copy_result.status());
570   ASSERT_EQ(kEntry1.size(), copy_result.size());
571
572   Entry entry;
573   ASSERT_EQ(Status::DataLoss(),
574             Entry::Read(partition_, new_address, kFormats, &entry));
575 }
576
577 TEST_F(ValidEntryInFlash, UpdateAndCopy_WriteError) {
578   flash_.InjectWriteError(FlashError::Unconditional(Status::Cancelled()));
579
580   ASSERT_EQ(OkStatus(), entry_.Update(kNoChecksumFormat, kTransactionId1 + 1));
581
582   auto result = entry_.Copy(kEntry1.size());
583   EXPECT_EQ(Status::Cancelled(), result.status());
584   EXPECT_EQ(kEntry1.size(), result.size());
585 }
586
587 }  // namespace
588 }  // namespace pw::kvs::internal