blob: 205defba241c67072a916dc18a69878fbc659ec7 [file] [log] [blame]
Wyatt Heplerec4b9352020-01-31 15:51:50 -08001// 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/checksum.h"
16
17#include "gtest/gtest.h"
18#include "pw_kvs/crc16_checksum.h"
19
20namespace pw::kvs {
21namespace {
22
23using std::byte;
24
25constexpr std::string_view kString =
26 "In the beginning the Universe was created. This has made a lot of "
27 "people very angry and been widely regarded as a bad move.";
28constexpr uint16_t kStringCrc = 0xC184;
29
30TEST(Checksum, UpdateAndVerify) {
31 ChecksumCrc16 crc16_algo;
32 ChecksumAlgorithm& algo = crc16_algo;
33
34 algo.Update(kString.data(), kString.size());
35 EXPECT_EQ(Status::OK, algo.Verify(as_bytes(span(&kStringCrc, 1))));
36}
37
38TEST(Checksum, Verify_Failure) {
39 ChecksumCrc16 algo;
40 EXPECT_EQ(Status::DATA_LOSS, algo.Verify(as_bytes(span(kString.data(), 2))));
41}
42
43TEST(Checksum, Verify_InvalidSize) {
44 ChecksumCrc16 algo;
45 EXPECT_EQ(Status::INVALID_ARGUMENT, algo.Verify({}));
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -080046 EXPECT_EQ(Status::INVALID_ARGUMENT,
47 algo.Verify(as_bytes(span(kString.substr(0, 1)))));
48}
49
50TEST(Checksum, Verify_LargerState_ComparesToTruncatedData) {
51 byte crc[3] = {byte{0x84}, byte{0xC1}, byte{0x33}};
52 ChecksumCrc16 algo;
53 ASSERT_GT(sizeof(crc), algo.size_bytes());
54
55 algo.Update(as_bytes(span(kString)));
56
57 EXPECT_EQ(Status::OK, algo.Verify(crc));
Wyatt Heplerec4b9352020-01-31 15:51:50 -080058}
59
60TEST(Checksum, Reset) {
61 ChecksumCrc16 crc_algo;
62 crc_algo.Update(as_bytes(span(kString)));
63 crc_algo.Reset();
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -080064
Alexei Frolov56034622020-02-07 13:17:00 -080065 span state = crc_algo.Finish();
66 EXPECT_EQ(state[0], byte{0xFF});
67 EXPECT_EQ(state[1], byte{0xFF});
Wyatt Heplerec4b9352020-01-31 15:51:50 -080068}
69
Wyatt Heplerc656af22020-02-12 14:49:14 -080070constexpr size_t kAlignment = 10;
71
72constexpr std::string_view kData =
73 "123456789_123456789_123456789_123456789_123456789_" // 50
74 "123456789_123456789_123456789_123456789_123456789_"; // 100
75const span<const byte> kBytes = as_bytes(span(kData));
76
77class PickyChecksum final : public AlignedChecksum<kAlignment, 32> {
78 public:
79 PickyChecksum() : AlignedChecksum(data_), data_{}, size_(0) {}
80
81 void Reset() override {}
82
83 void FinalizeAligned() override { EXPECT_EQ(kData.size(), size_); }
84
85 void UpdateAligned(span<const std::byte> data) override {
86 ASSERT_EQ(data.size() % kAlignment, 0u);
87 EXPECT_EQ(kData.substr(0, data.size()),
88 std::string_view(reinterpret_cast<const char*>(data.data()),
89 data.size()));
90
91 std::memcpy(&data_[size_], data.data(), data.size());
92 size_ += data.size();
93 }
94
95 private:
96 std::byte data_[kData.size()];
97 size_t size_;
98};
99
100TEST(AlignedChecksum, MaintainsAlignment) {
101 PickyChecksum checksum;
102
103 // Write values smaller than the alignment.
104 checksum.Update(kBytes.subspan(0, 1));
105 checksum.Update(kBytes.subspan(1, 9));
106
107 // Write values larger than the alignment but smaller than the buffer.
108 checksum.Update(kBytes.subspan(10, 11));
109
110 // Exactly fill the remainder of the buffer.
111 checksum.Update(kBytes.subspan(21, 11));
112
113 // Fill the buffer more than once.
114 checksum.Update(kBytes.subspan(32, 66));
115
116 // Write nothing.
117 checksum.Update(kBytes.subspan(98, 0));
118
119 // Write the remaining data.
120 checksum.Update(kBytes.subspan(98, 2));
121
122 auto state = checksum.Finish();
123 EXPECT_EQ(std::string_view(reinterpret_cast<const char*>(state.data()),
124 state.size()),
125 kData);
126 EXPECT_EQ(Status::OK, checksum.Verify(kBytes));
127}
128
Wyatt Heplerec4b9352020-01-31 15:51:50 -0800129} // namespace
130} // namespace pw::kvs