David Rogers | 1039584 | 2020-09-16 15:39:35 -0700 | [diff] [blame] | 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 <array> |
| 16 | #include <cstddef> |
| 17 | #include <cstring> |
| 18 | #include <span> |
| 19 | |
| 20 | #include "gtest/gtest.h" |
| 21 | #include "pw_blob_store/blob_store.h" |
| 22 | #include "pw_kvs/crc16_checksum.h" |
| 23 | #include "pw_kvs/fake_flash_memory.h" |
| 24 | #include "pw_kvs/flash_memory.h" |
| 25 | #include "pw_kvs/test_key_value_store.h" |
| 26 | #include "pw_log/log.h" |
| 27 | #include "pw_random/xor_shift.h" |
| 28 | |
| 29 | namespace pw::blob_store { |
| 30 | namespace { |
| 31 | |
| 32 | class BlobStoreChunkTest : public ::testing::Test { |
| 33 | protected: |
| 34 | BlobStoreChunkTest() : flash_(kFlashAlignment), partition_(&flash_) {} |
| 35 | |
| 36 | void InitFlashTo(std::span<const std::byte> contents) { |
Adrien Larbanet | d1ca56c | 2021-06-10 14:20:45 +0000 | [diff] [blame^] | 37 | partition_.Erase() |
| 38 | .IgnoreError(); // TODO(pwbug/387): Handle Status properly |
David Rogers | 1039584 | 2020-09-16 15:39:35 -0700 | [diff] [blame] | 39 | std::memcpy(flash_.buffer().data(), contents.data(), contents.size()); |
| 40 | } |
| 41 | |
| 42 | void InitSourceBufferToRandom(uint64_t seed) { |
Adrien Larbanet | d1ca56c | 2021-06-10 14:20:45 +0000 | [diff] [blame^] | 43 | partition_.Erase() |
| 44 | .IgnoreError(); // TODO(pwbug/387): Handle Status properly |
David Rogers | 1039584 | 2020-09-16 15:39:35 -0700 | [diff] [blame] | 45 | random::XorShiftStarRng64 rng(seed); |
Adrien Larbanet | d1ca56c | 2021-06-10 14:20:45 +0000 | [diff] [blame^] | 46 | rng.Get(source_buffer_) |
| 47 | .IgnoreError(); // TODO(pwbug/387): Handle Status properly |
David Rogers | 1039584 | 2020-09-16 15:39:35 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void InitSourceBufferToFill(char fill) { |
Adrien Larbanet | d1ca56c | 2021-06-10 14:20:45 +0000 | [diff] [blame^] | 51 | partition_.Erase() |
| 52 | .IgnoreError(); // TODO(pwbug/387): Handle Status properly |
David Rogers | 1039584 | 2020-09-16 15:39:35 -0700 | [diff] [blame] | 53 | std::memset(source_buffer_.data(), fill, source_buffer_.size()); |
| 54 | } |
| 55 | |
| 56 | // Fill the source buffer with random pattern based on given seed, written to |
| 57 | // BlobStore in specified chunk size. |
| 58 | void ChunkWriteTest(size_t chunk_size) { |
| 59 | constexpr size_t kBufferSize = 256; |
| 60 | kvs::ChecksumCrc16 checksum; |
| 61 | |
| 62 | char name[16] = {}; |
| 63 | snprintf(name, sizeof(name), "Blob%u", static_cast<unsigned>(chunk_size)); |
| 64 | |
| 65 | BlobStoreBuffer<kBufferSize> blob( |
David Rogers | 1f08acb | 2020-11-17 23:11:58 -0800 | [diff] [blame] | 66 | name, partition_, &checksum, kvs::TestKvs(), kBufferSize); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 67 | EXPECT_EQ(OkStatus(), blob.Init()); |
David Rogers | 1039584 | 2020-09-16 15:39:35 -0700 | [diff] [blame] | 68 | |
| 69 | BlobStore::BlobWriter writer(blob); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 70 | EXPECT_EQ(OkStatus(), writer.Open()); |
| 71 | EXPECT_EQ(OkStatus(), writer.Erase()); |
David Rogers | 1039584 | 2020-09-16 15:39:35 -0700 | [diff] [blame] | 72 | |
| 73 | ByteSpan source = source_buffer_; |
| 74 | while (source.size_bytes() > 0) { |
| 75 | const size_t write_size = std::min(source.size_bytes(), chunk_size); |
| 76 | |
| 77 | PW_LOG_DEBUG("Do write of %u bytes, %u bytes remain", |
| 78 | static_cast<unsigned>(write_size), |
| 79 | static_cast<unsigned>(source.size_bytes())); |
| 80 | |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 81 | ASSERT_EQ(OkStatus(), writer.Write(source.first(write_size))); |
David Rogers | 1039584 | 2020-09-16 15:39:35 -0700 | [diff] [blame] | 82 | |
| 83 | source = source.subspan(write_size); |
| 84 | } |
| 85 | |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 86 | EXPECT_EQ(OkStatus(), writer.Close()); |
David Rogers | 1039584 | 2020-09-16 15:39:35 -0700 | [diff] [blame] | 87 | |
| 88 | // Use reader to check for valid data. |
| 89 | BlobStore::BlobReader reader(blob); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 90 | ASSERT_EQ(OkStatus(), reader.Open()); |
David Rogers | 0fadf44 | 2020-09-18 10:38:15 -0700 | [diff] [blame] | 91 | Result<ConstByteSpan> result = reader.GetMemoryMappedBlob(); |
David Rogers | 1039584 | 2020-09-16 15:39:35 -0700 | [diff] [blame] | 92 | ASSERT_TRUE(result.ok()); |
| 93 | VerifyFlash(result.value()); |
Wyatt Hepler | 1b3da3a | 2021-01-07 13:26:57 -0800 | [diff] [blame] | 94 | EXPECT_EQ(OkStatus(), reader.Close()); |
David Rogers | 1039584 | 2020-09-16 15:39:35 -0700 | [diff] [blame] | 95 | } |
| 96 | |
David Rogers | 0fadf44 | 2020-09-18 10:38:15 -0700 | [diff] [blame] | 97 | void VerifyFlash(ConstByteSpan verify_bytes) { |
David Rogers | 1039584 | 2020-09-16 15:39:35 -0700 | [diff] [blame] | 98 | // Should be defined as same size. |
| 99 | EXPECT_EQ(source_buffer_.size(), flash_.buffer().size_bytes()); |
| 100 | |
| 101 | // Can't allow it to march off the end of source_buffer_. |
| 102 | ASSERT_LE(verify_bytes.size_bytes(), source_buffer_.size()); |
| 103 | |
| 104 | for (size_t i = 0; i < verify_bytes.size_bytes(); i++) { |
| 105 | EXPECT_EQ(source_buffer_[i], verify_bytes[i]); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static constexpr size_t kFlashAlignment = 16; |
| 110 | static constexpr size_t kSectorSize = 2048; |
| 111 | static constexpr size_t kSectorCount = 2; |
| 112 | static constexpr size_t kBlobDataSize = (kSectorCount * kSectorSize); |
| 113 | |
| 114 | kvs::FakeFlashMemoryBuffer<kSectorSize, kSectorCount> flash_; |
| 115 | kvs::FlashPartition partition_; |
| 116 | std::array<std::byte, kBlobDataSize> source_buffer_; |
| 117 | }; |
| 118 | |
| 119 | TEST_F(BlobStoreChunkTest, ChunkWrite1) { |
| 120 | InitSourceBufferToRandom(0x8675309); |
| 121 | ChunkWriteTest(1); |
| 122 | } |
| 123 | |
| 124 | TEST_F(BlobStoreChunkTest, ChunkWrite2) { |
| 125 | InitSourceBufferToRandom(0x8675); |
| 126 | ChunkWriteTest(2); |
| 127 | } |
| 128 | |
| 129 | TEST_F(BlobStoreChunkTest, ChunkWrite3) { |
| 130 | InitSourceBufferToFill(0); |
| 131 | ChunkWriteTest(3); |
| 132 | } |
| 133 | |
| 134 | TEST_F(BlobStoreChunkTest, ChunkWrite4) { |
| 135 | InitSourceBufferToFill(1); |
| 136 | ChunkWriteTest(4); |
| 137 | } |
| 138 | |
| 139 | TEST_F(BlobStoreChunkTest, ChunkWrite5) { |
| 140 | InitSourceBufferToFill(0xff); |
| 141 | ChunkWriteTest(5); |
| 142 | } |
| 143 | |
| 144 | TEST_F(BlobStoreChunkTest, ChunkWrite16) { |
| 145 | InitSourceBufferToRandom(0x86); |
| 146 | ChunkWriteTest(16); |
| 147 | } |
| 148 | |
| 149 | TEST_F(BlobStoreChunkTest, ChunkWrite64) { |
| 150 | InitSourceBufferToRandom(0x9); |
| 151 | ChunkWriteTest(64); |
| 152 | } |
| 153 | |
| 154 | TEST_F(BlobStoreChunkTest, ChunkWrite256) { |
| 155 | InitSourceBufferToRandom(0x12345678); |
| 156 | ChunkWriteTest(256); |
| 157 | } |
| 158 | |
| 159 | TEST_F(BlobStoreChunkTest, ChunkWrite512) { |
| 160 | InitSourceBufferToRandom(0x42); |
| 161 | ChunkWriteTest(512); |
| 162 | } |
| 163 | |
| 164 | TEST_F(BlobStoreChunkTest, ChunkWrite4096) { |
| 165 | InitSourceBufferToRandom(0x89); |
| 166 | ChunkWriteTest(4096); |
| 167 | } |
| 168 | |
| 169 | TEST_F(BlobStoreChunkTest, ChunkWriteSingleFull) { |
| 170 | InitSourceBufferToRandom(0x98765); |
| 171 | ChunkWriteTest(kBlobDataSize); |
| 172 | } |
| 173 | |
| 174 | } // namespace |
| 175 | } // namespace pw::blob_store |