|
| 1 | +/* Copyright (c) 2019 ARM Limited |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "gtest/gtest.h" |
| 18 | +#include "features/storage/blockdevice/HeapBlockDevice.h" |
| 19 | +#include "features/storage/blockdevice/FlashSimBlockDevice.h" |
| 20 | +#include "features/storage/kvstore/tdbstore/TDBStore.h" |
| 21 | +#include <stdlib.h> |
| 22 | +#include "stubs/EmulatedSD.h" |
| 23 | + |
| 24 | +#define BLOCK_SIZE (512) |
| 25 | +#define DEVICE_SIZE (BLOCK_SIZE*20) |
| 26 | + |
| 27 | +using namespace mbed; |
| 28 | +// For testing purposes, block device should keep its data between test runs |
| 29 | +// to ensure that we can cope with dirty-images |
| 30 | +HeapBlockDevice sd{DEVICE_SIZE}; |
| 31 | + |
| 32 | +// For debugging purpose, we could use file to store the BlockDevice content |
| 33 | +//EmulatedSD sd{"../UNITTESTS/moduletests/storage/kvstore/TDBStore/broken_image.img"}; |
| 34 | + |
| 35 | +class TDBStoreModuleTest : public testing::Test { |
| 36 | +protected: |
| 37 | + FlashSimBlockDevice flash{&sd}; |
| 38 | + TDBStore tdb{&flash}; |
| 39 | + |
| 40 | + virtual void SetUp() |
| 41 | + { |
| 42 | + EXPECT_EQ(tdb.init(), MBED_SUCCESS); |
| 43 | + EXPECT_EQ(tdb.reset(), MBED_SUCCESS); |
| 44 | + } |
| 45 | + |
| 46 | + virtual void TearDown() |
| 47 | + { |
| 48 | + EXPECT_EQ(tdb.deinit(), MBED_SUCCESS); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +TEST_F(TDBStoreModuleTest, init) |
| 53 | +{ |
| 54 | + EXPECT_EQ(tdb.deinit(), MBED_SUCCESS); |
| 55 | + EXPECT_EQ(tdb.init(), MBED_SUCCESS); |
| 56 | + EXPECT_EQ(tdb.init(), MBED_SUCCESS); |
| 57 | +} |
| 58 | + |
| 59 | +TEST_F(TDBStoreModuleTest, set_get) |
| 60 | +{ |
| 61 | + char buf[100]; |
| 62 | + size_t size; |
| 63 | + EXPECT_EQ(tdb.set("key", "data", 5, 0), MBED_SUCCESS); |
| 64 | + EXPECT_EQ(tdb.get("key", buf, 100, &size), MBED_SUCCESS); |
| 65 | + EXPECT_EQ(size, 5); |
| 66 | + EXPECT_STREQ("data", buf); |
| 67 | +} |
| 68 | + |
| 69 | +TEST_F(TDBStoreModuleTest, reset) |
| 70 | +{ |
| 71 | + char buf[100]; |
| 72 | + size_t size, read; |
| 73 | + // Write so much, that we are sure that garbage collection have kicked up |
| 74 | + for (int i = 0; i < 100; ++i) { |
| 75 | + size = sprintf(buf, "data%d", i); |
| 76 | + EXPECT_EQ(tdb.set("key", buf, size, 0), MBED_SUCCESS); |
| 77 | + } |
| 78 | + EXPECT_EQ(tdb.reset(), MBED_SUCCESS); |
| 79 | + EXPECT_EQ(tdb.deinit(), MBED_SUCCESS); |
| 80 | + EXPECT_EQ(tdb.init(), MBED_SUCCESS); |
| 81 | + // After reset->deinit()->init() there should be no data |
| 82 | + EXPECT_NE(tdb.get("key", buf, 100, &read), MBED_SUCCESS); |
| 83 | +} |
| 84 | + |
| 85 | +TEST_F(TDBStoreModuleTest, set_deinit_init_get) |
| 86 | +{ |
| 87 | + char buf[100]; |
| 88 | + size_t size; |
| 89 | + for (int i = 0; i < 10; ++i) { |
| 90 | + EXPECT_EQ(tdb.set("key", "data", 5, 0), MBED_SUCCESS); |
| 91 | + EXPECT_EQ(tdb.deinit(), MBED_SUCCESS); |
| 92 | + EXPECT_EQ(tdb.init(), MBED_SUCCESS); |
| 93 | + EXPECT_EQ(tdb.get("key", buf, 100, &size), MBED_SUCCESS); |
| 94 | + EXPECT_EQ(size, 5); |
| 95 | + EXPECT_STREQ("data", buf); |
| 96 | + EXPECT_EQ(tdb.remove("key"), MBED_SUCCESS); |
| 97 | + } |
| 98 | +} |
0 commit comments