pw_kvs: Move test; disable dumping to file
- Move long-running Put test to a separate file.
- Do not dump the KVS state to file by default to prevent files from
showing up when the tests run.
Change-Id: Ie466d06aa3e22819cad03713e866afae5c63cbe6
diff --git a/pw_kvs/BUILD b/pw_kvs/BUILD
index 9f267b0..522325c 100644
--- a/pw_kvs/BUILD
+++ b/pw_kvs/BUILD
@@ -91,6 +91,17 @@
)
pw_cc_test(
+ name = "key_value_store_fuzz_test",
+ srcs = ["key_value_store_fuzz_test.cc"],
+ deps = [
+ ":crc16",
+ ":in_memory_fake_flash",
+ ":pw_kvs",
+ "//pw_checksum",
+ ],
+)
+
+pw_cc_test(
name = "key_value_store_test",
srcs = ["key_value_store_test.cc"],
deps = [
diff --git a/pw_kvs/BUILD.gn b/pw_kvs/BUILD.gn
index dab9de4..d798273 100644
--- a/pw_kvs/BUILD.gn
+++ b/pw_kvs/BUILD.gn
@@ -87,6 +87,7 @@
":checksum_test",
":entry_test",
":key_value_store_test",
+ ":key_value_store_fuzz_test",
]
}
@@ -123,6 +124,17 @@
]
}
+pw_test("key_value_store_fuzz_test") {
+ deps = [
+ ":crc16",
+ ":in_memory_fake_flash",
+ ":pw_kvs",
+ ]
+ sources = [
+ "key_value_store_fuzz_test.cc",
+ ]
+}
+
pw_doc_group("docs") {
sources = [
"docs.rst",
diff --git a/pw_kvs/key_value_store_fuzz_test.cc b/pw_kvs/key_value_store_fuzz_test.cc
new file mode 100644
index 0000000..3ffaaa5
--- /dev/null
+++ b/pw_kvs/key_value_store_fuzz_test.cc
@@ -0,0 +1,60 @@
+// Copyright 2020 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include "gtest/gtest.h"
+#include "pw_kvs/crc16_checksum.h"
+#include "pw_kvs/in_memory_fake_flash.h"
+#include "pw_kvs/key_value_store.h"
+
+namespace pw::kvs {
+namespace {
+
+using std::byte;
+
+InMemoryFakeFlash<4 * 1024, 4> test_flash(
+ 16); // 4 x 4k sectors, 16 byte alignment
+FlashPartition test_partition(&test_flash, 0, test_flash.sector_count());
+
+ChecksumCrc16 checksum;
+constexpr EntryHeaderFormat kFormat{.magic = 0xBAD'C0D3, .checksum = &checksum};
+
+class EmptyInitializedKvs : public ::testing::Test {
+ protected:
+ EmptyInitializedKvs() : kvs_(&test_partition, kFormat) {
+ test_partition.Erase(0, test_partition.sector_count());
+ ASSERT_EQ(Status::OK, kvs_.Init());
+ }
+
+ KeyValueStore kvs_;
+};
+
+TEST_F(EmptyInitializedKvs, Put_VaryingKeysAndValues) {
+ char value[] =
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // 52
+ "34567890123"; // 64 (with final \0);
+ static_assert(sizeof(value) == 64);
+
+ for (int i = 0; i < 2; ++i) {
+ for (unsigned key_size = 1; key_size < sizeof(value); ++key_size) {
+ for (unsigned value_size = 0; value_size < sizeof(value); ++value_size) {
+ ASSERT_EQ(Status::OK,
+ kvs_.Put(std::string_view(value, key_size),
+ as_bytes(span(value, value_size))));
+ }
+ }
+ }
+}
+
+} // namespace
+} // namespace pw::kvs
diff --git a/pw_kvs/key_value_store_test.cc b/pw_kvs/key_value_store_test.cc
index 90043dc..c54d6e7 100644
--- a/pw_kvs/key_value_store_test.cc
+++ b/pw_kvs/key_value_store_test.cc
@@ -12,22 +12,16 @@
// License for the specific language governing permissions and limitations under
// the License.
+#define DUMP_KVS_STATE_TO_FILE 0
+#define USE_MEMORY_BUFFER 1
+#define PW_LOG_USE_ULTRA_SHORT_NAMES 1
+
#include "pw_kvs/key_value_store.h"
#include <array>
#include <cstdio>
-#if defined(__linux__)
-#include <vector>
-#endif // defined(__linux__)
#include <cstring>
-#include <type_traits>
-
-#include "pw_span/span.h"
-
-#define PW_LOG_USE_ULTRA_SHORT_NAMES 1
-#include "pw_log/log.h"
-
-#define USE_MEMORY_BUFFER 1
+#include <vector>
#include "gtest/gtest.h"
#include "pw_checksum/ccitt_crc16.h"
@@ -36,6 +30,7 @@
#include "pw_kvs_private/format.h"
#include "pw_kvs_private/macros.h"
#include "pw_log/log.h"
+#include "pw_span/span.h"
#include "pw_status/status.h"
#include "pw_string/string_builder.h"
@@ -90,7 +85,7 @@
FlashPartition partition;
public:
-#if defined(__linux__)
+#if DUMP_KVS_STATE_TO_FILE
Status Dump(const char* filename) {
std::FILE* out_file = std::fopen(filename, "w+");
if (out_file == nullptr) {
@@ -121,11 +116,8 @@
return status;
}
#else
- Status Dump(const char* filename) {
- (void)(filename);
- return Status::OK;
- }
-#endif // defined(__linux__)
+ Status Dump(const char*) { return Status::OK; }
+#endif // DUMP_KVS_STATE_TO_FILE
};
typedef FlashWithPartitionFake<4 * 128 /*sector size*/, 6 /*sectors*/> Flash;
@@ -165,8 +157,6 @@
min_put_size_(
RoundUpForAlignment(chunk_header_size_ + key_size_ + data_size_)) {}
- size_t SectorHeaderSize() { return 0; }
- size_t SectorHeaderMetaSize() { return 0; }
size_t ChunkHeaderSize() { return chunk_header_size_; }
size_t DataSize() { return data_size_; }
size_t KeySize() { return key_size_; }
@@ -269,23 +259,6 @@
}
}
-TEST_F(EmptyInitializedKvs, Put_VaryingKeysAndValues) {
- char value[] =
- "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // 52
- "34567890123"; // 64 (with final \0);
- static_assert(sizeof(value) == 64);
-
- for (int i = 0; i < 2; ++i) {
- for (unsigned key_size = 1; key_size < sizeof(value); ++key_size) {
- for (unsigned value_size = 0; value_size < sizeof(value); ++value_size) {
- ASSERT_EQ(Status::OK,
- kvs_.Put(std::string_view(value, key_size),
- as_bytes(span(value, value_size))));
- }
- }
- }
-}
-
TEST_F(EmptyInitializedKvs, Delete_GetDeletedKey_ReturnsNotFound) {
ASSERT_EQ(Status::OK, kvs_.Put("kEy", as_bytes(span("123"))));
ASSERT_EQ(Status::OK, kvs_.Delete("kEy"));
@@ -943,8 +916,7 @@
}
}
- size_t expected_remaining = test_partition.sector_size_bytes() -
- kvs_attr.SectorHeaderSize() - kSizeToFill;
+ size_t expected_remaining = test_partition.sector_size_bytes() - kSizeToFill;
ASSERT_EQ(new_keyvalue_size, expected_remaining);
const char* kNewKey = "NewKey";