aboutsummaryrefslogtreecommitdiff
path: root/pw_kvs
diff options
context:
space:
mode:
authorDavid Rogers <davidrogers@google.com>2020-08-03 08:44:10 -0700
committerCQ Bot Account <commit-bot@chromium.org>2020-08-03 16:54:45 +0000
commit436b3aa926a030f40bfc7124e2c65907a748ff67 (patch)
tree4713640af9311d24603fc25eb966b55944474c65 /pw_kvs
parent795fecf5a2a174adbc391e9d143c02995780d6ef (diff)
downloadpigweed-436b3aa926a030f40bfc7124e2c65907a748ff67.tar.gz
pw_kvs: Update all KVS magics to be random numbers
To set the proper example for KVS users, update all KVS tests to use random numbers for the magic. Add comments to each magic use to explain use of random number. Change-Id: I76a4012cf7848cc800705fa129a2aca49bd1cd72 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/15241 Reviewed-by: Wyatt Hepler <hepler@google.com> Reviewed-by: Keir Mierle <keir@google.com> Commit-Queue: David Rogers <davidrogers@google.com>
Diffstat (limited to 'pw_kvs')
-rw-r--r--pw_kvs/entry_cache_test.cc22
-rw-r--r--pw_kvs/entry_test.cc26
-rw-r--r--pw_kvs/key_value_store_binary_format_test.cc12
-rw-r--r--pw_kvs/key_value_store_fuzz_test.cc4
-rw-r--r--pw_kvs/key_value_store_initialized_test.cc4
-rw-r--r--pw_kvs/key_value_store_map_test.cc4
-rw-r--r--pw_kvs/key_value_store_test.cc36
-rw-r--r--pw_kvs/key_value_store_wear_test.cc4
-rw-r--r--pw_kvs/public/pw_kvs/format.h3
9 files changed, 83 insertions, 32 deletions
diff --git a/pw_kvs/entry_cache_test.cc b/pw_kvs/entry_cache_test.cc
index 01fa3a1fa..60fa73603 100644
--- a/pw_kvs/entry_cache_test.cc
+++ b/pw_kvs/entry_cache_test.cc
@@ -195,10 +195,12 @@ TEST_F(EmptyEntryCache, Iterator_Const_CanBeAssignedFromMutable) {
}
constexpr size_t kSectorSize = 64;
-
-constexpr auto kTheEntry = AsBytes(uint32_t(12345), // magic
- uint32_t(0), // checksum
- uint8_t(0), // alignment (16 B)
+constexpr uint32_t kMagic = 0xa14ae726;
+// For KVS entry magic value always use a random 32 bit integer rather than a
+// human readable 4 bytes. See pw_kvs/format.h for more information.
+constexpr auto kTheEntry = AsBytes(uint32_t(kMagic), // magic
+ uint32_t(0), // checksum
+ uint8_t(0), // alignment (16 B)
uint8_t(sizeof(kTheKey) - 1), // key length
uint16_t(0), // value size
uint32_t(123), // transaction ID
@@ -210,8 +212,10 @@ constexpr size_t kSize1 = kTheEntry.size() + kPadding1.size();
constexpr char kCollision1[] = "9FDC";
constexpr char kCollision2[] = "axzzK";
+// For KVS entry magic value always use a random 32 bit integer rather than a
+// human readable 4 bytes. See pw_kvs/format.h for more information.
constexpr auto kCollisionEntry =
- AsBytes(uint32_t(12345), // magic
+ AsBytes(uint32_t(kMagic), // magic
uint32_t(0), // checksum
uint8_t(0), // alignment (16 B)
uint8_t(sizeof(kCollision1) - 1), // key length
@@ -222,8 +226,10 @@ constexpr std::array<byte, kSectorSize - kCollisionEntry.size() % kSectorSize>
kPadding2{};
constexpr size_t kSize2 = kCollisionEntry.size() + kPadding2.size();
+// For KVS entry magic value always use a random 32 bit integer rather than a
+// human readable 4 bytes. See pw_kvs/format.h for more information.
constexpr auto kDeletedEntry =
- AsBytes(uint32_t(12345), // magic
+ AsBytes(uint32_t(kMagic), // magic
uint32_t(0), // checksum
uint8_t(0), // alignment (16 B)
uint8_t(sizeof("delorted") - 1), // key length
@@ -233,7 +239,9 @@ constexpr auto kDeletedEntry =
constexpr std::array<byte, kSectorSize - kDeletedEntry.size() % kSectorSize>
kPadding3{};
-constexpr EntryFormat kFormat{.magic = uint32_t(12345), .checksum = nullptr};
+// For KVS entry magic value always use a random 32 bit integer rather than a
+// human readable 4 bytes. See pw_kvs/format.h for more information.
+constexpr EntryFormat kFormat{.magic = uint32_t(kMagic), .checksum = nullptr};
class InitializedEntryCache : public EmptyEntryCache {
protected:
diff --git a/pw_kvs/entry_test.cc b/pw_kvs/entry_test.cc
index 309b7aced..a1afab221 100644
--- a/pw_kvs/entry_test.cc
+++ b/pw_kvs/entry_test.cc
@@ -32,7 +32,9 @@ namespace {
using std::byte;
using std::string_view;
-constexpr EntryFormat kFormat{0xbeef, nullptr};
+// For magic value always use a random 32 bit integer rather than a human
+// readable 4 bytes. See pw_kvs/format.h for more information.
+constexpr EntryFormat kFormat{0x961c2ff9, nullptr};
TEST(Entry, Size_RoundsUpToAlignment) {
// Use FakeFlashMemory, rather than FakeFlashMemoryBuffer, so the class gets
@@ -85,7 +87,9 @@ TEST(Entry, Construct_Tombstone) {
EXPECT_EQ(entry.transaction_id(), 123u);
}
-constexpr uint32_t kMagicWithChecksum = 0x600df00d;
+// For magic value always use a unique random 32 bit integer rather than a human
+// readable 4 bytes. See pw_kvs/format.h for more information.
+constexpr uint32_t kMagicWithChecksum = 0xad165142;
constexpr uint32_t kTransactionId1 = 0x96979899;
constexpr auto kKey1 = ByteStr("key45");
@@ -93,7 +97,7 @@ constexpr auto kValue1 = ByteStr("VALUE!");
constexpr auto kPadding1 = ByteStr("\0\0\0\0\0");
constexpr auto kHeader1 = AsBytes(kMagicWithChecksum,
- uint32_t(0x65c5), // checksum (CRC16)
+ uint32_t(0x23aa), // checksum (CRC16)
uint8_t(1), // alignment (32 B)
uint8_t(kKey1.size()), // key length
uint16_t(kValue1.size()), // value size
@@ -214,8 +218,8 @@ TEST(ValidEntry, Write) {
}
constexpr auto kHeader2 = ByteStr(
- "\x0d\xf0\x0d\x60" // magic
- "\xd5\xf5\x00\x00" // checksum (CRC16)
+ "\x42\x51\x16\xad" // magic
+ "\xba\xb3\x00\x00" // checksum (CRC16)
"\x00" // alignment
"\x01" // key length
"\xff\xff" // value size
@@ -346,7 +350,9 @@ TEST_F(ValidEntryInFlash, Update_ReadError_WithChecksumIsError) {
entry_.Update(kFormatWithChecksum, kTransactionId1 + 1));
}
-constexpr EntryFormat kNoChecksumFormat{.magic = 0xf000000d,
+// For magic value always use a random 32 bit integer rather than a human
+// readable 4 bytes. See pw_kvs/format.h for more information.
+constexpr EntryFormat kNoChecksumFormat{.magic = 0x721bad24,
.checksum = nullptr};
TEST_F(ValidEntryInFlash, Update_ReadError_NoChecksumIsOkay) {
@@ -397,7 +403,9 @@ class ChecksumSummation final : public ChecksumAlgorithm {
uint32_t sum_;
} sum_checksum;
-constexpr uint32_t kMagicWithSum = 0x12345678;
+// For magic value always use a random 32 bit integer rather than a human
+// readable 4 bytes. See pw_kvs/format.h for more information.
+constexpr uint32_t kMagicWithSum = 0x6093aadb;
constexpr EntryFormat kFormatWithSum{kMagicWithSum, &sum_checksum};
constexpr internal::EntryFormats kFormatsWithSum(kFormatWithSum);
@@ -499,7 +507,9 @@ TEST(ValidEntryInFlash, UpdateAndCopy_DifferentFormatLargerAlignment) {
}
TEST_F(ValidEntryInFlash, UpdateAndCopy_NoChecksum_UpdatesToNewFormat) {
- constexpr EntryFormat no_checksum{.magic = 0xf000000d, .checksum = nullptr};
+ // For magic value always use a random 32 bit integer rather than a human
+ // readable 4 bytes. See pw_kvs/format.h for more information.
+ constexpr EntryFormat no_checksum{.magic = 0x43fae18f, .checksum = nullptr};
ASSERT_EQ(Status::OK, entry_.Update(no_checksum, kTransactionId1 + 1));
diff --git a/pw_kvs/key_value_store_binary_format_test.cc b/pw_kvs/key_value_store_binary_format_test.cc
index 5921e82b6..cf8ec41d4 100644
--- a/pw_kvs/key_value_store_binary_format_test.cc
+++ b/pw_kvs/key_value_store_binary_format_test.cc
@@ -129,7 +129,9 @@ constexpr auto MakeDeletedEntry(uint32_t magic,
return data;
}
-constexpr uint32_t kMagic = 0xc001beef;
+// For KVS magic value always use a random 32 bit integer rather than a
+// human readable 4 bytes. See pw_kvs/format.h for more information.
+constexpr uint32_t kMagic = 0x5ab2f0b5;
constexpr Options kNoGcOptions{
.gc_on_write = GargbageCollectOnWrite::kDisabled,
@@ -592,7 +594,9 @@ TEST_F(KvsErrorRecovery, Put_WriteFailure_EntryNotAddedButBytesMarkedWritten) {
EXPECT_EQ(stats.missing_redundant_entries_recovered, 0u);
}
-constexpr uint32_t kAltMagic = 0xbadD00D;
+// For KVS magic value always use a random 32 bit integer rather than a
+// human readable 4 bytes. See pw_kvs/format.h for more information.
+constexpr uint32_t kAltMagic = 0x41a2db83;
constexpr uint32_t AltChecksum(std::span<const byte> data, uint32_t state) {
for (byte b : data) {
@@ -607,7 +611,9 @@ constexpr auto kAltEntry =
MakeValidEntry<AltChecksum>(kAltMagic, 32, "A Key", ByteStr("XD"));
constexpr uint32_t NoChecksum(std::span<const byte>, uint32_t) { return 0; }
-constexpr uint32_t kNoChecksumMagic = 0x6000061e;
+// For KVS magic value always use a random 32 bit integer rather than a
+// human readable 4 bytes. See pw_kvs/format.h for more information.
+constexpr uint32_t kNoChecksumMagic = 0xd49ba138;
constexpr auto kNoChecksumEntry =
MakeValidEntry<NoChecksum>(kNoChecksumMagic, 64, "kee", ByteStr("O_o"));
diff --git a/pw_kvs/key_value_store_fuzz_test.cc b/pw_kvs/key_value_store_fuzz_test.cc
index 2a3475021..fcfca4cb5 100644
--- a/pw_kvs/key_value_store_fuzz_test.cc
+++ b/pw_kvs/key_value_store_fuzz_test.cc
@@ -41,8 +41,10 @@ ChecksumCrc16 checksum;
class EmptyInitializedKvs : public ::testing::Test {
protected:
+ // For KVS magic value always use a random 32 bit integer rather than a
+ // human readable 4 bytes. See pw_kvs/format.h for more information.
EmptyInitializedKvs()
- : kvs_(&test_partition, {.magic = 0xBAD'C0D3, .checksum = &checksum}) {
+ : kvs_(&test_partition, {.magic = 0x873a9b50, .checksum = &checksum}) {
test_partition.Erase(0, test_partition.sector_count());
ASSERT_EQ(Status::OK, kvs_.Init());
}
diff --git a/pw_kvs/key_value_store_initialized_test.cc b/pw_kvs/key_value_store_initialized_test.cc
index 17c7d6018..fb343d66c 100644
--- a/pw_kvs/key_value_store_initialized_test.cc
+++ b/pw_kvs/key_value_store_initialized_test.cc
@@ -75,7 +75,9 @@ class KvsAttributes {
constexpr std::array<const char*, 3> keys{"TestKey1", "Key2", "TestKey3"};
ChecksumCrc16 checksum;
-constexpr EntryFormat default_format{.magic = 0xBAD'C0D3,
+// For KVS magic value always use a random 32 bit integer rather than a
+// human readable 4 bytes. See pw_kvs/format.h for more information.
+constexpr EntryFormat default_format{.magic = 0x5b9a341e,
.checksum = &checksum};
class EmptyInitializedKvs : public ::testing::Test {
diff --git a/pw_kvs/key_value_store_map_test.cc b/pw_kvs/key_value_store_map_test.cc
index b1de09378..87d0dee68 100644
--- a/pw_kvs/key_value_store_map_test.cc
+++ b/pw_kvs/key_value_store_map_test.cc
@@ -85,7 +85,9 @@ class KvsTester {
kParams.partition_start_sector,
kParams.partition_sector_count,
kParams.partition_alignment),
- kvs_(&partition_, {.magic = 0xBAD'C0D3, .checksum = nullptr}) {
+ // For KVS magic value always use a random 32 bit integer rather than a
+ // human readable 4 bytes. See pw_kvs/format.h for more information.
+ kvs_(&partition_, {.magic = 0xc857e51d, .checksum = nullptr}) {
EXPECT_EQ(Status::OK, partition_.Erase());
Status result = kvs_.Init();
EXPECT_EQ(Status::OK, result);
diff --git a/pw_kvs/key_value_store_test.cc b/pw_kvs/key_value_store_test.cc
index 4479c5699..af981f1a3 100644
--- a/pw_kvs/key_value_store_test.cc
+++ b/pw_kvs/key_value_store_test.cc
@@ -158,7 +158,9 @@ FlashPartition large_test_partition(&large_test_flash,
constexpr std::array<const char*, 3> keys{"TestKey1", "Key2", "TestKey3"};
ChecksumCrc16 checksum;
-constexpr EntryFormat default_format{.magic = 0xBAD'C0D3,
+// For KVS magic value always use a random 32 bit integer rather than a
+// human readable 4 bytes. See pw_kvs/format.h for more information.
+constexpr EntryFormat default_format{.magic = 0xa6cb3c16,
.checksum = &checksum};
} // namespace
@@ -168,7 +170,9 @@ TEST(InitCheck, TooFewSectors) {
FakeFlashMemoryBuffer<4 * 1024, 1> test_flash(16);
FlashPartition test_partition(&test_flash, 0, test_flash.sector_count());
- constexpr EntryFormat format{.magic = 0xBAD'C0D3, .checksum = nullptr};
+ // For KVS magic value always use a random 32 bit integer rather than a
+ // human readable 4 bytes. See pw_kvs/format.h for more information.
+ constexpr EntryFormat format{.magic = 0x89bb14d2, .checksum = nullptr};
KeyValueStoreBuffer<kMaxEntries, kMaxUsableSectors> kvs(&test_partition,
format);
@@ -182,7 +186,9 @@ TEST(InitCheck, ZeroSectors) {
// Set FlashPartition to have 0 sectors.
FlashPartition test_partition(&test_flash, 0, 0);
- constexpr EntryFormat format{.magic = 0xBAD'C0D3, .checksum = nullptr};
+ // For KVS magic value always use a random 32 bit integer rather than a
+ // human readable 4 bytes. See pw_kvs/format.h for more information.
+ constexpr EntryFormat format{.magic = 0xd1da57c1, .checksum = nullptr};
KeyValueStoreBuffer<kMaxEntries, kMaxUsableSectors> kvs(&test_partition,
format);
@@ -196,7 +202,9 @@ TEST(InitCheck, TooManySectors) {
// Set FlashPartition to have 0 sectors.
FlashPartition test_partition(&test_flash, 0, test_flash.sector_count());
- constexpr EntryFormat format{.magic = 0xBAD'C0D3, .checksum = nullptr};
+ // For KVS magic value always use a random 32 bit integer rather than a
+ // human readable 4 bytes. See pw_kvs/format.h for more information.
+ constexpr EntryFormat format{.magic = 0x610f6d17, .checksum = nullptr};
KeyValueStoreBuffer<kMaxEntries, 2> kvs(&test_partition, format);
EXPECT_EQ(kvs.Init(), Status::FAILED_PRECONDITION);
@@ -218,8 +226,10 @@ TEST(InMemoryKvs, WriteOneKeyMultipleTimes) {
DBG("xxx xxxx");
DBG("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
- // Create and initialize the KVS.
- constexpr EntryFormat format{.magic = 0xBAD'C0D3, .checksum = nullptr};
+ // Create and initialize the KVS. For KVS magic value always use a random 32
+ // bit integer rather than a human readable 4 bytes. See pw_kvs/format.h for
+ // more information.
+ constexpr EntryFormat format{.magic = 0x83a9257, .checksum = nullptr};
KeyValueStoreBuffer<kMaxEntries, kMaxUsableSectors> kvs(&flash.partition,
format);
ASSERT_OK(kvs.Init());
@@ -257,8 +267,10 @@ TEST(InMemoryKvs, WritingMultipleKeysIncreasesSize) {
Flash flash;
ASSERT_OK(flash.partition.Erase());
- // Create and initialize the KVS.
- constexpr EntryFormat format{.magic = 0xBAD'C0D3, .checksum = nullptr};
+ // Create and initialize the KVS. For KVS magic value always use a random 32
+ // bit integer rather than a human readable 4 bytes. See pw_kvs/format.h for
+ // more information.
+ constexpr EntryFormat format{.magic = 0x2ed3a058, .checksum = nullptr};
KeyValueStoreBuffer<kMaxEntries, kMaxUsableSectors> kvs(&flash.partition,
format);
ASSERT_OK(kvs.Init());
@@ -284,7 +296,9 @@ TEST(InMemoryKvs, WriteAndReadOneKey) {
ASSERT_OK(flash.partition.Erase());
// Create and initialize the KVS.
- constexpr EntryFormat format{.magic = 0xBAD'C0D3, .checksum = nullptr};
+ // For KVS magic value always use a random 32 bit integer rather than a
+ // human readable 4 bytes. See pw_kvs/format.h for more information.
+ constexpr EntryFormat format{.magic = 0x5d70896, .checksum = nullptr};
KeyValueStoreBuffer<kMaxEntries, kMaxUsableSectors> kvs(&flash.partition,
format);
ASSERT_OK(kvs.Init());
@@ -344,7 +358,9 @@ TEST(InMemoryKvs, Basic) {
ASSERT_EQ(Status::OK, flash.partition.Erase());
// Create and initialize the KVS.
- constexpr EntryFormat format{.magic = 0xBAD'C0D3, .checksum = nullptr};
+ // For KVS magic value always use a random 32 bit integer rather than a
+ // human readable 4 bytes. See pw_kvs/format.h for more information.
+ constexpr EntryFormat format{.magic = 0x7bf19895, .checksum = nullptr};
KeyValueStoreBuffer<kMaxEntries, kMaxUsableSectors> kvs(&flash.partition,
format);
ASSERT_OK(kvs.Init());
diff --git a/pw_kvs/key_value_store_wear_test.cc b/pw_kvs/key_value_store_wear_test.cc
index eb5f93052..d6cadc4d6 100644
--- a/pw_kvs/key_value_store_wear_test.cc
+++ b/pw_kvs/key_value_store_wear_test.cc
@@ -25,7 +25,9 @@
namespace pw::kvs {
namespace {
-constexpr EntryFormat format{.magic = 0xBAD'C0D3, .checksum = nullptr};
+// For KVS magic value always use a random 32 bit integer rather than a
+// human readable 4 bytes. See pw_kvs/format.h for more information.
+constexpr EntryFormat format{.magic = 0x1bce4ad5, .checksum = nullptr};
class WearTest : public ::testing::Test {
protected:
diff --git a/pw_kvs/public/pw_kvs/format.h b/pw_kvs/public/pw_kvs/format.h
index f6b33e7cf..19cd174c2 100644
--- a/pw_kvs/public/pw_kvs/format.h
+++ b/pw_kvs/public/pw_kvs/format.h
@@ -26,6 +26,9 @@ namespace internal {
// Disk format of the header used for each key-value entry.
struct EntryHeader {
+ // For KVS magic value always use a random 32 bit integer rather than a
+ // human readable 4 bytes. See pw_kvs/format.h::EntryFormat for more
+ // information.
uint32_t magic;
// The checksum of the entire entry, including the header, key, value, and