Ewout van Bekkum | 32dc5c5 | 2021-03-16 11:35:37 -0700 | [diff] [blame] | 1 | // Copyright 2021 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 | #include "pw_persistent_ram/persistent.h" |
| 15 | |
| 16 | #include <type_traits> |
| 17 | |
| 18 | #include "gtest/gtest.h" |
Armando Montanez | 58f22dc | 2021-04-16 09:52:26 -0700 | [diff] [blame] | 19 | #include "pw_random/xor_shift.h" |
Ewout van Bekkum | 32dc5c5 | 2021-03-16 11:35:37 -0700 | [diff] [blame] | 20 | |
| 21 | namespace pw::persistent_ram { |
| 22 | namespace { |
| 23 | |
| 24 | class PersistentTest : public ::testing::Test { |
| 25 | protected: |
| 26 | PersistentTest() { ZeroPersistentMemory(); } |
| 27 | |
| 28 | // Emulate invalidation of persistent section(s). |
| 29 | void ZeroPersistentMemory() { memset(&buffer_, 0, sizeof(buffer_)); } |
| 30 | |
| 31 | // Allocate a chunk of aligned storage that can be independently controlled. |
| 32 | std::aligned_storage_t<sizeof(Persistent<uint32_t>), |
| 33 | alignof(Persistent<uint32_t>)> |
| 34 | buffer_; |
| 35 | }; |
| 36 | |
| 37 | TEST_F(PersistentTest, DefaultConstructionAndDestruction) { |
| 38 | { // Emulate a boot where the persistent sections were invalidated. |
| 39 | // Although the fixture always does this, we do this an extra time to be |
| 40 | // 100% confident that an integrity check cannot be accidentally selected |
| 41 | // which results in reporting there is valid data when zero'd. |
| 42 | ZeroPersistentMemory(); |
| 43 | auto& persistent = *(new (&buffer_) Persistent<uint32_t>()); |
| 44 | EXPECT_FALSE(persistent.has_value()); |
| 45 | |
| 46 | persistent = 42; |
| 47 | ASSERT_TRUE(persistent.has_value()); |
| 48 | EXPECT_EQ(42u, persistent.value()); |
| 49 | |
| 50 | persistent.~Persistent(); // Emulate shutdown / global destructors. |
| 51 | } |
| 52 | |
| 53 | { // Emulate a boot where persistent memory was kept as is. |
| 54 | auto& persistent = *(new (&buffer_) Persistent<uint32_t>()); |
| 55 | ASSERT_TRUE(persistent.has_value()); |
| 56 | EXPECT_EQ(42u, persistent.value()); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | TEST_F(PersistentTest, Reset) { |
| 61 | { // Emulate a boot where the persistent sections were invalidated. |
| 62 | auto& persistent = *(new (&buffer_) Persistent<uint32_t>()); |
| 63 | persistent = 42u; |
| 64 | EXPECT_TRUE(persistent.has_value()); |
Armando Montanez | 1b2a140 | 2021-04-16 10:53:48 -0700 | [diff] [blame] | 65 | persistent.Invalidate(); |
Ewout van Bekkum | 32dc5c5 | 2021-03-16 11:35:37 -0700 | [diff] [blame] | 66 | |
| 67 | persistent.~Persistent(); // Emulate shutdown / global destructors. |
| 68 | } |
| 69 | |
| 70 | { // Emulate a boot where persistent memory was kept as is. |
| 71 | auto& persistent = *(new (&buffer_) Persistent<uint32_t>()); |
| 72 | EXPECT_FALSE(persistent.has_value()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | TEST_F(PersistentTest, Emplace) { |
| 77 | auto& persistent = *(new (&buffer_) Persistent<uint32_t>()); |
| 78 | EXPECT_FALSE(persistent.has_value()); |
| 79 | |
| 80 | persistent.emplace(42u); |
| 81 | ASSERT_TRUE(persistent.has_value()); |
| 82 | EXPECT_EQ(42u, persistent.value()); |
| 83 | } |
| 84 | |
Armando Montanez | 9b085ce | 2021-03-19 15:12:25 -0700 | [diff] [blame] | 85 | class MutablePersistentTest : public ::testing::Test { |
| 86 | protected: |
| 87 | struct Coordinate { |
| 88 | int x; |
| 89 | int y; |
| 90 | int z; |
| 91 | }; |
| 92 | MutablePersistentTest() { ZeroPersistentMemory(); } |
| 93 | |
| 94 | // Emulate invalidation of persistent section(s). |
| 95 | void ZeroPersistentMemory() { memset(&buffer_, 0, sizeof(buffer_)); } |
Armando Montanez | 58f22dc | 2021-04-16 09:52:26 -0700 | [diff] [blame] | 96 | void RandomFillMemory() { |
| 97 | random::XorShiftStarRng64 rng(0x9ad75); |
| 98 | StatusWithSize sws = rng.Get(std::span<std::byte>( |
| 99 | reinterpret_cast<std::byte*>(&buffer_), sizeof(buffer_))); |
| 100 | ASSERT_TRUE(sws.ok()); |
| 101 | ASSERT_EQ(sws.size(), sizeof(buffer_)); |
| 102 | } |
Armando Montanez | 9b085ce | 2021-03-19 15:12:25 -0700 | [diff] [blame] | 103 | |
| 104 | // Allocate a chunk of aligned storage that can be independently controlled. |
| 105 | std::aligned_storage_t<sizeof(Persistent<Coordinate>), |
| 106 | alignof(Persistent<Coordinate>)> |
| 107 | buffer_; |
| 108 | }; |
| 109 | |
| 110 | TEST_F(MutablePersistentTest, DefaultConstructionAndDestruction) { |
| 111 | { |
| 112 | // Emulate a boot where the persistent sections were invalidated. |
| 113 | // Although the fixture always does this, we do this an extra time to be |
| 114 | // 100% confident that an integrity check cannot be accidentally selected |
| 115 | // which results in reporting there is valid data when zero'd. |
| 116 | ZeroPersistentMemory(); |
| 117 | auto& persistent = *(new (&buffer_) Persistent<Coordinate>()); |
| 118 | EXPECT_FALSE(persistent.has_value()); |
| 119 | |
| 120 | // Default construct of a Coordinate. |
| 121 | persistent.emplace(Coordinate({.x = 5, .y = 6, .z = 7})); |
| 122 | ASSERT_TRUE(persistent.has_value()); |
| 123 | { |
| 124 | auto mutable_persistent = persistent.mutator(); |
| 125 | mutable_persistent->x = 42; |
| 126 | (*mutable_persistent).y = 1337; |
| 127 | mutable_persistent->z = -99; |
| 128 | ASSERT_FALSE(persistent.has_value()); |
| 129 | } |
| 130 | |
| 131 | EXPECT_EQ(1337, persistent.value().y); |
| 132 | EXPECT_EQ(-99, persistent.value().z); |
| 133 | |
| 134 | persistent.~Persistent(); // Emulate shutdown / global destructors. |
| 135 | } |
| 136 | |
| 137 | { |
| 138 | // Emulate a boot where persistent memory was kept as is. |
| 139 | auto& persistent = *(new (&buffer_) Persistent<Coordinate>()); |
| 140 | ASSERT_TRUE(persistent.has_value()); |
| 141 | EXPECT_EQ(42, persistent.value().x); |
| 142 | } |
| 143 | } |
| 144 | |
Armando Montanez | 58f22dc | 2021-04-16 09:52:26 -0700 | [diff] [blame] | 145 | TEST_F(MutablePersistentTest, ResetObject) { |
| 146 | { |
| 147 | // Emulate a boot where the persistent sections were lost and ended up in |
| 148 | // random data. |
| 149 | RandomFillMemory(); |
| 150 | auto& persistent = *(new (&buffer_) Persistent<Coordinate>()); |
| 151 | |
| 152 | // Default construct of a Coordinate. |
| 153 | ASSERT_FALSE(persistent.has_value()); |
| 154 | { |
| 155 | auto mutable_persistent = persistent.mutator(GetterAction::kReset); |
| 156 | mutable_persistent->x = 42; |
| 157 | } |
| 158 | |
| 159 | EXPECT_EQ(42, persistent.value().x); |
| 160 | EXPECT_EQ(0, persistent.value().y); |
| 161 | EXPECT_EQ(0, persistent.value().z); |
| 162 | |
| 163 | persistent.~Persistent(); // Emulate shutdown / global destructors. |
| 164 | } |
| 165 | |
| 166 | { |
| 167 | // Emulate a boot where persistent memory was kept as is. |
| 168 | auto& persistent = *(new (&buffer_) Persistent<Coordinate>()); |
| 169 | ASSERT_TRUE(persistent.has_value()); |
| 170 | EXPECT_EQ(42, persistent.value().x); |
| 171 | EXPECT_EQ(0, persistent.value().y); |
| 172 | } |
| 173 | } |
| 174 | |
Ewout van Bekkum | 32dc5c5 | 2021-03-16 11:35:37 -0700 | [diff] [blame] | 175 | } // namespace |
| 176 | } // namespace pw::persistent_ram |