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_buffer.h"
15
16 #include <cstddef>
17 #include <type_traits>
18
19 #include "gtest/gtest.h"
20 #include "pw_bytes/span.h"
21 #include "pw_random/xor_shift.h"
22 #include "pw_span/span.h"
23
24 namespace pw::persistent_ram {
25 namespace {
26
27 class PersistentTest : public ::testing::Test {
28 protected:
29 static constexpr size_t kBufferSize = 256;
PersistentTest()30 PersistentTest() { ZeroPersistentMemory(); }
31
32 // Emulate invalidation of persistent section(s).
ZeroPersistentMemory()33 void ZeroPersistentMemory() { memset(buffer_, 0, sizeof(buffer_)); }
RandomFillMemory()34 void RandomFillMemory() {
35 random::XorShiftStarRng64 rng(0x9ad75);
36 rng.Get(buffer_);
37 }
38
GetPersistentBuffer()39 PersistentBuffer<kBufferSize>& GetPersistentBuffer() {
40 return *(new (buffer_) PersistentBuffer<kBufferSize>());
41 }
42
43 // Allocate a chunk of aligned storage that can be independently controlled.
44 alignas(PersistentBuffer<kBufferSize>)
45 std::byte buffer_[sizeof(PersistentBuffer<kBufferSize>)];
46 };
47
TEST_F(PersistentTest,DefaultConstructionAndDestruction)48 TEST_F(PersistentTest, DefaultConstructionAndDestruction) {
49 constexpr uint32_t kExpectedNumber = 0x6C2C6582;
50 {
51 // Emulate a boot where the persistent sections were invalidated.
52 // Although the fixture always does this, we do this an extra time to be
53 // 100% confident that an integrity check cannot be accidentally selected
54 // which results in reporting there is valid data when zero'd.
55 ZeroPersistentMemory();
56 auto& persistent = GetPersistentBuffer();
57 auto writer = persistent.GetWriter();
58 EXPECT_EQ(persistent.size(), 0u);
59
60 ASSERT_EQ(OkStatus(), writer.Write(as_bytes(span(&kExpectedNumber, 1))));
61 ASSERT_TRUE(persistent.has_value());
62
63 persistent.~PersistentBuffer(); // Emulate shutdown / global destructors.
64 }
65
66 { // Emulate a boot where persistent memory was kept as is.
67 auto& persistent = GetPersistentBuffer();
68 ASSERT_TRUE(persistent.has_value());
69 EXPECT_EQ(persistent.size(), sizeof(kExpectedNumber));
70
71 uint32_t temp = 0;
72 memcpy(&temp, persistent.data(), sizeof(temp));
73 EXPECT_EQ(temp, kExpectedNumber);
74 }
75 }
76
TEST_F(PersistentTest,LongData)77 TEST_F(PersistentTest, LongData) {
78 constexpr std::string_view kTestString(
79 "A nice string should remain valid even if written incrementally!");
80 constexpr size_t kWriteSize = 5;
81
82 { // Initialize the buffer.
83 RandomFillMemory();
84 auto& persistent = GetPersistentBuffer();
85 ASSERT_FALSE(persistent.has_value());
86
87 auto writer = persistent.GetWriter();
88 for (size_t i = 0; i < kTestString.length(); i += kWriteSize) {
89 ASSERT_EQ(OkStatus(),
90 writer.Write(kTestString.data() + i,
91 std::min(kWriteSize, kTestString.length() - i)));
92 }
93 // Need to manually write a null terminator since std::string_view doesn't
94 // include one in the string length.
95 ASSERT_EQ(OkStatus(), writer.Write(std::byte(0)));
96
97 persistent.~PersistentBuffer(); // Emulate shutdown / global destructors.
98 }
99
100 { // Ensure data is valid.
101 auto& persistent = GetPersistentBuffer();
102 ASSERT_TRUE(persistent.has_value());
103 ASSERT_STREQ(kTestString.data(),
104 reinterpret_cast<const char*>(persistent.data()));
105 }
106 }
107
TEST_F(PersistentTest,ZeroDataIsNoValue)108 TEST_F(PersistentTest, ZeroDataIsNoValue) {
109 ZeroPersistentMemory();
110 auto& persistent = GetPersistentBuffer();
111 EXPECT_FALSE(persistent.has_value());
112 }
113
TEST_F(PersistentTest,RandomDataIsInvalid)114 TEST_F(PersistentTest, RandomDataIsInvalid) {
115 RandomFillMemory();
116 auto& persistent = GetPersistentBuffer();
117 ASSERT_FALSE(persistent.has_value());
118 }
119
TEST_F(PersistentTest,AppendingData)120 TEST_F(PersistentTest, AppendingData) {
121 constexpr std::string_view kTestString("Test string one!");
122 constexpr uint32_t kTestNumber = 42;
123
124 { // Initialize the buffer.
125 RandomFillMemory();
126 auto& persistent = GetPersistentBuffer();
127 auto writer = persistent.GetWriter();
128 EXPECT_EQ(persistent.size(), 0u);
129
130 // Write an integer.
131 ASSERT_EQ(OkStatus(), writer.Write(as_bytes(span(&kTestNumber, 1))));
132 ASSERT_TRUE(persistent.has_value());
133
134 persistent.~PersistentBuffer(); // Emulate shutdown / global destructors.
135 }
136
137 { // Get a pointer to the buffer and validate the contents.
138 auto& persistent = GetPersistentBuffer();
139 ASSERT_TRUE(persistent.has_value());
140 EXPECT_EQ(persistent.size(), sizeof(kTestNumber));
141
142 // Write more data.
143 auto writer = persistent.GetWriter();
144 EXPECT_EQ(persistent.size(), sizeof(kTestNumber));
145 ASSERT_EQ(OkStatus(),
146 writer.Write(as_bytes(span<const char>(kTestString))));
147
148 persistent.~PersistentBuffer(); // Emulate shutdown / global destructors.
149 }
150 { // Ensure data was appended.
151 auto& persistent = GetPersistentBuffer();
152 ASSERT_TRUE(persistent.has_value());
153 EXPECT_EQ(persistent.size(), sizeof(kTestNumber) + kTestString.length());
154 }
155 }
156
157 } // namespace
158 } // namespace pw::persistent_ram
159