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/alignment.h"
16
17 #include <cstring>
18 #include <string_view>
19
20 #include "gtest/gtest.h"
21 #include "pw_status/status_with_size.h"
22
23 namespace pw::kvs {
24 namespace {
25
26 using namespace std::string_view_literals;
27 using std::byte;
28
TEST(AlignUp,Zero)29 TEST(AlignUp, Zero) {
30 EXPECT_EQ(0u, AlignUp(0, 1));
31 EXPECT_EQ(0u, AlignUp(0, 2));
32 EXPECT_EQ(0u, AlignUp(0, 15));
33 }
34
TEST(AlignUp,Aligned)35 TEST(AlignUp, Aligned) {
36 for (size_t i = 1; i < 130; ++i) {
37 EXPECT_EQ(i, AlignUp(i, i));
38 EXPECT_EQ(2 * i, AlignUp(2 * i, i));
39 EXPECT_EQ(3 * i, AlignUp(3 * i, i));
40 }
41 }
42
TEST(AlignUp,NonAligned_PowerOf2)43 TEST(AlignUp, NonAligned_PowerOf2) {
44 EXPECT_EQ(32u, AlignUp(1, 32));
45 EXPECT_EQ(32u, AlignUp(31, 32));
46 EXPECT_EQ(64u, AlignUp(33, 32));
47 EXPECT_EQ(64u, AlignUp(45, 32));
48 EXPECT_EQ(64u, AlignUp(63, 32));
49 EXPECT_EQ(128u, AlignUp(127, 32));
50 }
51
TEST(AlignUp,NonAligned_NonPowerOf2)52 TEST(AlignUp, NonAligned_NonPowerOf2) {
53 EXPECT_EQ(2u, AlignUp(1, 2));
54
55 EXPECT_EQ(15u, AlignUp(1, 15));
56 EXPECT_EQ(15u, AlignUp(14, 15));
57 EXPECT_EQ(30u, AlignUp(16, 15));
58 }
59
TEST(AlignDown,Zero)60 TEST(AlignDown, Zero) {
61 EXPECT_EQ(0u, AlignDown(0, 1));
62 EXPECT_EQ(0u, AlignDown(0, 2));
63 EXPECT_EQ(0u, AlignDown(0, 15));
64 }
65
TEST(AlignDown,Aligned)66 TEST(AlignDown, Aligned) {
67 for (size_t i = 1; i < 130; ++i) {
68 EXPECT_EQ(i, AlignDown(i, i));
69 EXPECT_EQ(2 * i, AlignDown(2 * i, i));
70 EXPECT_EQ(3 * i, AlignDown(3 * i, i));
71 }
72 }
73
TEST(AlignDown,NonAligned_PowerOf2)74 TEST(AlignDown, NonAligned_PowerOf2) {
75 EXPECT_EQ(0u, AlignDown(1, 32));
76 EXPECT_EQ(0u, AlignDown(31, 32));
77 EXPECT_EQ(32u, AlignDown(33, 32));
78 EXPECT_EQ(32u, AlignDown(45, 32));
79 EXPECT_EQ(32u, AlignDown(63, 32));
80 EXPECT_EQ(96u, AlignDown(127, 32));
81 }
82
TEST(AlignDown,NonAligned_NonPowerOf2)83 TEST(AlignDown, NonAligned_NonPowerOf2) {
84 EXPECT_EQ(0u, AlignDown(1, 2));
85
86 EXPECT_EQ(0u, AlignDown(1, 15));
87 EXPECT_EQ(0u, AlignDown(14, 15));
88 EXPECT_EQ(15u, AlignDown(16, 15));
89 }
90
TEST(Padding,Zero)91 TEST(Padding, Zero) {
92 EXPECT_EQ(0u, Padding(0, 1));
93 EXPECT_EQ(0u, Padding(0, 2));
94 EXPECT_EQ(0u, Padding(0, 15));
95 }
96
TEST(Padding,Aligned)97 TEST(Padding, Aligned) {
98 for (size_t i = 1; i < 130; ++i) {
99 EXPECT_EQ(0u, Padding(i, i));
100 EXPECT_EQ(0u, Padding(2 * i, i));
101 EXPECT_EQ(0u, Padding(3 * i, i));
102 }
103 }
104
TEST(Padding,NonAligned_PowerOf2)105 TEST(Padding, NonAligned_PowerOf2) {
106 EXPECT_EQ(31u, Padding(1, 32));
107 EXPECT_EQ(1u, Padding(31, 32));
108 EXPECT_EQ(31u, Padding(33, 32));
109 EXPECT_EQ(19u, Padding(45, 32));
110 EXPECT_EQ(1u, Padding(63, 32));
111 EXPECT_EQ(1u, Padding(127, 32));
112 }
113
TEST(Padding,NonAligned_NonPowerOf2)114 TEST(Padding, NonAligned_NonPowerOf2) {
115 EXPECT_EQ(1u, Padding(1, 2));
116
117 EXPECT_EQ(14u, Padding(1, 15));
118 EXPECT_EQ(1u, Padding(14, 15));
119 EXPECT_EQ(14u, Padding(16, 15));
120 }
121
122 constexpr size_t kAlignment = 10;
123
124 constexpr std::string_view kData =
125 "123456789_123456789_123456789_123456789_123456789_" // 50
126 "123456789_123456789_123456789_123456789_123456789_"; // 100
127
128 const std::span<const byte> kBytes = std::as_bytes(std::span(kData));
129
130 // The output function checks that the data is properly aligned and matches
131 // the expected value (should always be 123456789_...).
__anonc9a34e0c0202(std::span<const byte> data) 132 OutputToFunction check_against_data([](std::span<const byte> data) {
133 EXPECT_EQ(data.size() % kAlignment, 0u);
134 EXPECT_EQ(kData.substr(0, data.size()),
135 std::string_view(reinterpret_cast<const char*>(data.data()),
136 data.size()));
137 return StatusWithSize(data.size());
138 });
139
TEST(AlignedWriter,Write_VaryingLengths)140 TEST(AlignedWriter, Write_VaryingLengths) {
141 AlignedWriterBuffer<32> writer(kAlignment, check_against_data);
142
143 // Write values smaller than the alignment.
144 EXPECT_EQ(OkStatus(), writer.Write(kBytes.subspan(0, 1)).status());
145 EXPECT_EQ(OkStatus(), writer.Write(kBytes.subspan(1, 9)).status());
146
147 // Write values larger than the alignment but smaller than the buffer.
148 EXPECT_EQ(OkStatus(), writer.Write(kBytes.subspan(10, 11)).status());
149
150 // Exactly fill the remainder of the buffer.
151 EXPECT_EQ(OkStatus(), writer.Write(kBytes.subspan(21, 11)).status());
152
153 // Fill the buffer more than once.
154 EXPECT_EQ(OkStatus(), writer.Write(kBytes.subspan(32, 66)).status());
155
156 // Write nothing.
157 EXPECT_EQ(OkStatus(), writer.Write(kBytes.subspan(98, 0)).status());
158
159 // Write the remaining data.
160 EXPECT_EQ(OkStatus(), writer.Write(kBytes.subspan(98, 2)).status());
161
162 auto result = writer.Flush();
163 EXPECT_EQ(OkStatus(), result.status());
164 EXPECT_EQ(kData.size(), result.size());
165 }
166
TEST(AlignedWriter,DestructorFlushes)167 TEST(AlignedWriter, DestructorFlushes) {
168 static size_t called_with_bytes;
169 called_with_bytes = 0;
170
171 OutputToFunction output([](std::span<const byte> data) {
172 called_with_bytes += data.size();
173 return StatusWithSize(data.size());
174 });
175
176 {
177 AlignedWriterBuffer<64> writer(3, output);
178 writer.Write(std::as_bytes(std::span("What is this?")))
179 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
180 EXPECT_EQ(called_with_bytes, 0u); // Buffer not full; no output yet.
181 }
182
183 EXPECT_EQ(called_with_bytes, AlignUp(sizeof("What is this?"), 3));
184 }
185
186 // Output class that can be programmed to fail for testing purposes.
187 // TODO(hepler): If we create a general pw_io / pw_stream module, this and
188 // InputWithErrorInjection should be made into generic test utility classes,
189 // similar to FakeFlashMemory.
190 struct OutputWithErrorInjection final : public Output {
191 public:
192 enum { kKeepGoing, kBreakOnNext, kBroken } state = kKeepGoing;
193
194 private:
DoWritepw::kvs::__anonc9a34e0c0111::OutputWithErrorInjection195 StatusWithSize DoWrite(std::span<const byte> data) override {
196 switch (state) {
197 case kKeepGoing:
198 return StatusWithSize(data.size());
199 case kBreakOnNext:
200 state = kBroken;
201 break;
202 case kBroken:
203 ADD_FAILURE();
204 break;
205 }
206 return StatusWithSize::Unknown(data.size());
207 }
208 };
209
TEST(AlignedWriter,Write_NoFurtherWritesOnFailure)210 TEST(AlignedWriter, Write_NoFurtherWritesOnFailure) {
211 OutputWithErrorInjection output;
212
213 {
214 AlignedWriterBuffer<4> writer(3, output);
215 writer.Write(std::as_bytes(std::span("Everything is fine.")))
216 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
217 output.state = OutputWithErrorInjection::kBreakOnNext;
218 EXPECT_EQ(Status::Unknown(),
219 writer.Write(std::as_bytes(std::span("No more writes, okay?")))
220 .status());
221 writer.Flush().IgnoreError(); // TODO(pwbug/387): Handle Status properly
222 }
223 }
224
TEST(AlignedWriter,Write_ReturnsTotalBytesWritten)225 TEST(AlignedWriter, Write_ReturnsTotalBytesWritten) {
226 static Status return_status;
227 return_status = OkStatus();
228
229 OutputToFunction output([](std::span<const byte> data) {
230 return StatusWithSize(return_status, data.size());
231 });
232
233 AlignedWriterBuffer<22> writer(10, output);
234
235 StatusWithSize result =
236 writer.Write(std::as_bytes(std::span("12345678901"sv)));
237 EXPECT_EQ(OkStatus(), result.status());
238 EXPECT_EQ(0u, result.size()); // No writes; haven't filled buffer.
239
240 result = writer.Write(std::as_bytes(std::span("2345678901"sv)));
241 EXPECT_EQ(OkStatus(), result.status());
242 EXPECT_EQ(20u, result.size());
243
244 return_status = Status::PermissionDenied();
245
246 result = writer.Write(std::as_bytes(std::span("2345678901234567890"sv)));
247 EXPECT_EQ(Status::PermissionDenied(), result.status());
248 EXPECT_EQ(40u, result.size());
249 }
250
TEST(AlignedWriter,Flush_Ok_ReturnsTotalBytesWritten)251 TEST(AlignedWriter, Flush_Ok_ReturnsTotalBytesWritten) {
252 OutputToFunction output(
253 [](std::span<const byte> data) { return StatusWithSize(data.size()); });
254
255 AlignedWriterBuffer<4> writer(2, output);
256
257 EXPECT_EQ(OkStatus(),
258 writer.Write(std::as_bytes(std::span("12345678901"sv))).status());
259
260 StatusWithSize result = writer.Flush();
261 EXPECT_EQ(OkStatus(), result.status());
262 EXPECT_EQ(12u, result.size());
263 }
264
TEST(AlignedWriter,Flush_Error_ReturnsTotalBytesWritten)265 TEST(AlignedWriter, Flush_Error_ReturnsTotalBytesWritten) {
266 OutputToFunction output([](std::span<const byte> data) {
267 return StatusWithSize::Aborted(data.size());
268 });
269
270 AlignedWriterBuffer<20> writer(10, output);
271
272 EXPECT_EQ(0u, writer.Write(std::as_bytes(std::span("12345678901"sv))).size());
273
274 StatusWithSize result = writer.Flush();
275 EXPECT_EQ(Status::Aborted(), result.status());
276 EXPECT_EQ(20u, result.size());
277 }
278
279 // Input class that can be programmed to fail for testing purposes.
280 class InputWithErrorInjection final : public Input {
281 public:
BreakOnIndex(size_t index)282 void BreakOnIndex(size_t index) { break_on_index_ = index; }
283
284 private:
DoRead(std::span<byte> data)285 StatusWithSize DoRead(std::span<byte> data) override {
286 EXPECT_LE(index_ + data.size(), kBytes.size());
287
288 if (index_ + data.size() > kBytes.size()) {
289 return StatusWithSize::Internal();
290 }
291
292 // Check if reading from the index that was programmed to cause an error.
293 if (index_ <= break_on_index_ && break_on_index_ <= index_ + data.size()) {
294 return StatusWithSize::Aborted();
295 }
296
297 std::memcpy(data.data(), kBytes.data(), data.size());
298 index_ += data.size();
299 return StatusWithSize(data.size());
300 }
301
302 size_t index_ = 0;
303 size_t break_on_index_ = size_t(-1);
304 };
305
TEST(AlignedWriter,WriteFromInput_Successful)306 TEST(AlignedWriter, WriteFromInput_Successful) {
307 AlignedWriterBuffer<32> writer(kAlignment, check_against_data);
308
309 InputWithErrorInjection input;
310 StatusWithSize result = writer.Write(input, kData.size());
311 EXPECT_EQ(OkStatus(), result.status());
312 EXPECT_LE(result.size(), kData.size()); // May not have written it all yet.
313
314 result = writer.Flush();
315 EXPECT_EQ(OkStatus(), result.status());
316 EXPECT_EQ(kData.size(), result.size());
317 }
318
TEST(AlignedWriter,WriteFromInput_InputError)319 TEST(AlignedWriter, WriteFromInput_InputError) {
320 AlignedWriterBuffer<kAlignment> writer(kAlignment, check_against_data);
321
322 InputWithErrorInjection input;
323 input.BreakOnIndex(kAlignment + 2);
324
325 StatusWithSize result = writer.Write(input, kData.size());
326 EXPECT_EQ(Status::Aborted(), result.status());
327 EXPECT_LE(result.size(), kAlignment); // Wrote the first chunk, nothing more.
328 }
329
TEST(AlignedWriter,WriteFromInput_OutputError)330 TEST(AlignedWriter, WriteFromInput_OutputError) {
331 InputWithErrorInjection input;
332 OutputWithErrorInjection output;
333
334 AlignedWriterBuffer<4> writer(3, output);
335 output.state = OutputWithErrorInjection::kBreakOnNext;
336
337 StatusWithSize result = writer.Write(input, kData.size());
338 EXPECT_EQ(Status::Unknown(), result.status());
339 EXPECT_EQ(3u, result.size()); // Attempted to write 3 bytes.
340 }
341
342 } // namespace
343 } // namespace pw::kvs
344