blob: becb23d9d895a6b386dc34f15e799cfc2a729b23 [file] [log] [blame]
David Rogers10395842020-09-16 15:39:35 -07001// 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
29namespace pw::blob_store {
30namespace {
31
32class BlobStoreChunkTest : public ::testing::Test {
33 protected:
34 BlobStoreChunkTest() : flash_(kFlashAlignment), partition_(&flash_) {}
35
36 void InitFlashTo(std::span<const std::byte> contents) {
Adrien Larbanetd1ca56c2021-06-10 14:20:45 +000037 partition_.Erase()
38 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
David Rogers10395842020-09-16 15:39:35 -070039 std::memcpy(flash_.buffer().data(), contents.data(), contents.size());
40 }
41
42 void InitSourceBufferToRandom(uint64_t seed) {
Adrien Larbanetd1ca56c2021-06-10 14:20:45 +000043 partition_.Erase()
44 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
David Rogers10395842020-09-16 15:39:35 -070045 random::XorShiftStarRng64 rng(seed);
Adrien Larbanetd1ca56c2021-06-10 14:20:45 +000046 rng.Get(source_buffer_)
47 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
David Rogers10395842020-09-16 15:39:35 -070048 }
49
50 void InitSourceBufferToFill(char fill) {
Adrien Larbanetd1ca56c2021-06-10 14:20:45 +000051 partition_.Erase()
52 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
David Rogers10395842020-09-16 15:39:35 -070053 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 Rogers1f08acb2020-11-17 23:11:58 -080066 name, partition_, &checksum, kvs::TestKvs(), kBufferSize);
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080067 EXPECT_EQ(OkStatus(), blob.Init());
David Rogers10395842020-09-16 15:39:35 -070068
69 BlobStore::BlobWriter writer(blob);
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080070 EXPECT_EQ(OkStatus(), writer.Open());
71 EXPECT_EQ(OkStatus(), writer.Erase());
David Rogers10395842020-09-16 15:39:35 -070072
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 Hepler1b3da3a2021-01-07 13:26:57 -080081 ASSERT_EQ(OkStatus(), writer.Write(source.first(write_size)));
David Rogers10395842020-09-16 15:39:35 -070082
83 source = source.subspan(write_size);
84 }
85
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080086 EXPECT_EQ(OkStatus(), writer.Close());
David Rogers10395842020-09-16 15:39:35 -070087
88 // Use reader to check for valid data.
89 BlobStore::BlobReader reader(blob);
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080090 ASSERT_EQ(OkStatus(), reader.Open());
David Rogers0fadf442020-09-18 10:38:15 -070091 Result<ConstByteSpan> result = reader.GetMemoryMappedBlob();
David Rogers10395842020-09-16 15:39:35 -070092 ASSERT_TRUE(result.ok());
93 VerifyFlash(result.value());
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080094 EXPECT_EQ(OkStatus(), reader.Close());
David Rogers10395842020-09-16 15:39:35 -070095 }
96
David Rogers0fadf442020-09-18 10:38:15 -070097 void VerifyFlash(ConstByteSpan verify_bytes) {
David Rogers10395842020-09-16 15:39:35 -070098 // 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
119TEST_F(BlobStoreChunkTest, ChunkWrite1) {
120 InitSourceBufferToRandom(0x8675309);
121 ChunkWriteTest(1);
122}
123
124TEST_F(BlobStoreChunkTest, ChunkWrite2) {
125 InitSourceBufferToRandom(0x8675);
126 ChunkWriteTest(2);
127}
128
129TEST_F(BlobStoreChunkTest, ChunkWrite3) {
130 InitSourceBufferToFill(0);
131 ChunkWriteTest(3);
132}
133
134TEST_F(BlobStoreChunkTest, ChunkWrite4) {
135 InitSourceBufferToFill(1);
136 ChunkWriteTest(4);
137}
138
139TEST_F(BlobStoreChunkTest, ChunkWrite5) {
140 InitSourceBufferToFill(0xff);
141 ChunkWriteTest(5);
142}
143
144TEST_F(BlobStoreChunkTest, ChunkWrite16) {
145 InitSourceBufferToRandom(0x86);
146 ChunkWriteTest(16);
147}
148
149TEST_F(BlobStoreChunkTest, ChunkWrite64) {
150 InitSourceBufferToRandom(0x9);
151 ChunkWriteTest(64);
152}
153
154TEST_F(BlobStoreChunkTest, ChunkWrite256) {
155 InitSourceBufferToRandom(0x12345678);
156 ChunkWriteTest(256);
157}
158
159TEST_F(BlobStoreChunkTest, ChunkWrite512) {
160 InitSourceBufferToRandom(0x42);
161 ChunkWriteTest(512);
162}
163
164TEST_F(BlobStoreChunkTest, ChunkWrite4096) {
165 InitSourceBufferToRandom(0x89);
166 ChunkWriteTest(4096);
167}
168
169TEST_F(BlobStoreChunkTest, ChunkWriteSingleFull) {
170 InitSourceBufferToRandom(0x98765);
171 ChunkWriteTest(kBlobDataSize);
172}
173
174} // namespace
175} // namespace pw::blob_store