blob: 5e69f3fdbda4fc9b69c2f2512a51eb179d357d72 [file] [log] [blame]
David Rogers67cf06d2021-04-15 14:56:46 -07001// 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"
17#include "pw_kvs/fake_flash_memory.h"
18#include "pw_kvs/flash_partition_with_stats.h"
19#include "pw_kvs/key_value_store.h"
20
21namespace pw::kvs {
22namespace {
23
David Rogers67cf06d2021-04-15 14:56:46 -070024#ifndef PW_KVS_PUT_ITERATIONS
25#define PW_KVS_PUT_ITERATIONS 2
26#endif // PW_KVS_PUT_ITERATIONS
27constexpr int kPutIterations = PW_KVS_PUT_ITERATIONS;
28
29constexpr size_t kMaxEntries = 256;
30constexpr size_t kMaxUsableSectors = 256;
31
32// 4 x 4k sectors, 16 byte alignment
33FakeFlashMemoryBuffer<4 * 1024, 6> test_flash(16);
34
35FlashPartitionWithStatsBuffer<kMaxUsableSectors> test_partition(
36 &test_flash, 0, test_flash.sector_count());
37
38ChecksumCrc16 checksum;
39
40class EmptyInitializedKvs : public ::testing::Test {
41 protected:
42 // For KVS magic value always use a random 32 bit integer rather than a
43 // human readable 4 bytes. See pw_kvs/format.h for more information.
44 EmptyInitializedKvs()
45 : kvs_(&test_partition, {.magic = 0x873a9b50, .checksum = &checksum}) {
Adrien Larbanetd1ca56c2021-06-10 14:20:45 +000046 test_partition.Erase(0, test_partition.sector_count())
47 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
David Rogers67cf06d2021-04-15 14:56:46 -070048 ASSERT_EQ(OkStatus(), kvs_.Init());
49 }
50
51 KeyValueStoreBuffer<kMaxEntries, kMaxUsableSectors> kvs_;
52};
53
54TEST_F(EmptyInitializedKvs, Put_VaryingKeysAndValues) {
55 char value[] =
56 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" // 52
57 "34567890123"; // 64 (with final \0);
58 static_assert(sizeof(value) == 64);
59
60 test_partition.ResetCounters();
61
62 for (int i = 0; i < kPutIterations; ++i) {
63 for (unsigned key_size = 1; key_size < sizeof(value); ++key_size) {
64 for (unsigned value_size = 0; value_size < sizeof(value); ++value_size) {
65 ASSERT_EQ(OkStatus(),
66 kvs_.Put(std::string_view(value, key_size),
67 std::as_bytes(std::span(value, value_size))));
68 }
69 }
70 }
71
Adrien Larbanetd1ca56c2021-06-10 14:20:45 +000072 test_partition.SaveStorageStats(kvs_, "Put_VaryingKeysAndValues")
73 .IgnoreError(); // TODO(pwbug/387): Handle Status properly
David Rogers67cf06d2021-04-15 14:56:46 -070074}
75
76} // namespace
77} // namespace pw::kvs