blob: 6a257d6fd69de2b279ea5647b8998938b87aa6ca [file] [log] [blame]
Wyatt Hepler16b04522020-02-07 16:00:14 -08001// 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 "gtest/gtest.h"
16#include "pw_kvs/crc16_checksum.h"
David Rogersd64cc012020-05-26 12:37:37 -070017#include "pw_kvs/fake_flash_memory.h"
David Rogers5cc5ce82020-03-13 19:19:03 -070018#include "pw_kvs/flash_partition_with_stats.h"
Wyatt Hepler16b04522020-02-07 16:00:14 -080019#include "pw_kvs/key_value_store.h"
20
21namespace pw::kvs {
22namespace {
23
24using std::byte;
25
David Rogers5cc5ce82020-03-13 19:19:03 -070026#ifndef PW_KVS_FUZZ_ITERATIONS
27#define PW_KVS_FUZZ_ITERATIONS 2
28#endif // PW_KVS_FUZZ_ITERATIONS
29constexpr int kFuzzIterations = PW_KVS_FUZZ_ITERATIONS;
30
Wyatt Hepler38ce30f2020-02-19 11:48:31 -080031constexpr size_t kMaxEntries = 256;
32constexpr size_t kMaxUsableSectors = 256;
33
Wyatt Heplercdd6dfc2020-02-18 12:04:04 -080034// 4 x 4k sectors, 16 byte alignment
David Rogersd64cc012020-05-26 12:37:37 -070035FakeFlashMemoryBuffer<4 * 1024, 6> test_flash(16);
David Rogers5cc5ce82020-03-13 19:19:03 -070036
David Rogersd5138f32020-04-28 15:18:56 -070037FlashPartitionWithStatsBuffer<kMaxUsableSectors> test_partition(
David Rogers5cc5ce82020-03-13 19:19:03 -070038 &test_flash, 0, test_flash.sector_count());
Wyatt Hepler16b04522020-02-07 16:00:14 -080039
40ChecksumCrc16 checksum;
Wyatt Hepler16b04522020-02-07 16:00:14 -080041
42class EmptyInitializedKvs : public ::testing::Test {
43 protected:
David Rogers436b3aa2020-08-03 08:44:10 -070044 // For KVS magic value always use a random 32 bit integer rather than a
45 // human readable 4 bytes. See pw_kvs/format.h for more information.
Wyatt Hepler22d0d9f2020-03-05 14:57:11 -080046 EmptyInitializedKvs()
David Rogers436b3aa2020-08-03 08:44:10 -070047 : kvs_(&test_partition, {.magic = 0x873a9b50, .checksum = &checksum}) {
Wyatt Hepler16b04522020-02-07 16:00:14 -080048 test_partition.Erase(0, test_partition.sector_count());
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080049 ASSERT_EQ(OkStatus(), kvs_.Init());
Wyatt Hepler16b04522020-02-07 16:00:14 -080050 }
51
Wyatt Hepler38ce30f2020-02-19 11:48:31 -080052 KeyValueStoreBuffer<kMaxEntries, kMaxUsableSectors> kvs_;
Wyatt Hepler16b04522020-02-07 16:00:14 -080053};
54
55TEST_F(EmptyInitializedKvs, Put_VaryingKeysAndValues) {
56 char value[] =
57 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // 52
58 "34567890123"; // 64 (with final \0);
59 static_assert(sizeof(value) == 64);
60
David Rogers5cc5ce82020-03-13 19:19:03 -070061 test_partition.ResetCounters();
62
63 for (int i = 0; i < kFuzzIterations; ++i) {
Wyatt Hepler16b04522020-02-07 16:00:14 -080064 for (unsigned key_size = 1; key_size < sizeof(value); ++key_size) {
65 for (unsigned value_size = 0; value_size < sizeof(value); ++value_size) {
Wyatt Hepler1b3da3a2021-01-07 13:26:57 -080066 ASSERT_EQ(OkStatus(),
Wyatt Hepler16b04522020-02-07 16:00:14 -080067 kvs_.Put(std::string_view(value, key_size),
Wyatt Heplere2cbadf2020-06-22 11:21:45 -070068 std::as_bytes(std::span(value, value_size))));
Wyatt Hepler16b04522020-02-07 16:00:14 -080069 }
70 }
71 }
David Rogers5cc5ce82020-03-13 19:19:03 -070072
73 test_partition.SaveStorageStats(kvs_, "fuzz Put_VaryingKeysAndValues");
Wyatt Hepler16b04522020-02-07 16:00:14 -080074}
75
76} // namespace
77} // namespace pw::kvs