Wyatt Hepler | ec4b935 | 2020-01-31 15:51:50 -0800 | [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 "pw_kvs/checksum.h" |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | #include "pw_kvs/crc16_checksum.h" |
| 19 | |
| 20 | namespace pw::kvs { |
| 21 | namespace { |
| 22 | |
| 23 | using std::byte; |
| 24 | |
| 25 | constexpr 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."; |
| 28 | constexpr uint16_t kStringCrc = 0xC184; |
| 29 | |
| 30 | TEST(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 | |
| 38 | TEST(Checksum, Verify_Failure) { |
| 39 | ChecksumCrc16 algo; |
| 40 | EXPECT_EQ(Status::DATA_LOSS, algo.Verify(as_bytes(span(kString.data(), 2)))); |
| 41 | } |
| 42 | |
| 43 | TEST(Checksum, Verify_InvalidSize) { |
| 44 | ChecksumCrc16 algo; |
| 45 | EXPECT_EQ(Status::INVALID_ARGUMENT, algo.Verify({})); |
Wyatt Hepler | 6e3a83b | 2020-02-04 07:36:45 -0800 | [diff] [blame] | 46 | EXPECT_EQ(Status::INVALID_ARGUMENT, |
| 47 | algo.Verify(as_bytes(span(kString.substr(0, 1))))); |
| 48 | } |
| 49 | |
| 50 | TEST(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 Hepler | ec4b935 | 2020-01-31 15:51:50 -0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | TEST(Checksum, Reset) { |
| 61 | ChecksumCrc16 crc_algo; |
| 62 | crc_algo.Update(as_bytes(span(kString))); |
| 63 | crc_algo.Reset(); |
Wyatt Hepler | 6e3a83b | 2020-02-04 07:36:45 -0800 | [diff] [blame] | 64 | |
Alexei Frolov | 5603462 | 2020-02-07 13:17:00 -0800 | [diff] [blame] | 65 | span state = crc_algo.Finish(); |
| 66 | EXPECT_EQ(state[0], byte{0xFF}); |
| 67 | EXPECT_EQ(state[1], byte{0xFF}); |
Wyatt Hepler | ec4b935 | 2020-01-31 15:51:50 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Wyatt Hepler | c656af2 | 2020-02-12 14:49:14 -0800 | [diff] [blame] | 70 | constexpr size_t kAlignment = 10; |
| 71 | |
| 72 | constexpr std::string_view kData = |
| 73 | "123456789_123456789_123456789_123456789_123456789_" // 50 |
| 74 | "123456789_123456789_123456789_123456789_123456789_"; // 100 |
| 75 | const span<const byte> kBytes = as_bytes(span(kData)); |
| 76 | |
| 77 | class 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 | |
| 100 | TEST(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 Hepler | ec4b935 | 2020-01-31 15:51:50 -0800 | [diff] [blame] | 129 | } // namespace |
| 130 | } // namespace pw::kvs |