Import Mbed OS hard-float snapshot
This commit is contained in:
1
storage/kvstore/tests/UNITTESTS/.mbedignore
Normal file
1
storage/kvstore/tests/UNITTESTS/.mbedignore
Normal file
@@ -0,0 +1 @@
|
||||
*
|
||||
111
storage/kvstore/tests/UNITTESTS/FileSystemStore/moduletest.cpp
Normal file
111
storage/kvstore/tests/UNITTESTS/FileSystemStore/moduletest.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
/* Copyright (c) 2020 ARM Limited
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* http://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 "blockdevice/HeapBlockDevice.h"
|
||||
#include "kvstore/FileSystemStore.h"
|
||||
#include "littlefs/LittleFileSystem.h"
|
||||
#include "mbed_error.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#define HEAPBLOCK_SIZE (4096)
|
||||
|
||||
using namespace mbed;
|
||||
|
||||
class FileSystemStoreModuleTest : public testing::Test {
|
||||
protected:
|
||||
HeapBlockDevice heap{HEAPBLOCK_SIZE};
|
||||
LittleFileSystem *fs;
|
||||
FileSystemStore *store;
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
fs = new LittleFileSystem("kvstore", &heap);
|
||||
if(fs->mount(&heap) != MBED_SUCCESS) {
|
||||
EXPECT_EQ(fs->reformat(&heap), MBED_SUCCESS);
|
||||
}
|
||||
store = new FileSystemStore(fs);
|
||||
EXPECT_EQ(store->init(), MBED_SUCCESS);
|
||||
}
|
||||
|
||||
virtual void TearDown()
|
||||
{
|
||||
EXPECT_EQ(store->deinit(), MBED_SUCCESS);
|
||||
delete store;
|
||||
EXPECT_EQ(fs->unmount(), MBED_SUCCESS);
|
||||
delete fs;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(FileSystemStoreModuleTest, init)
|
||||
{
|
||||
EXPECT_EQ(store->deinit(), MBED_SUCCESS);
|
||||
EXPECT_EQ(store->init(), MBED_SUCCESS);
|
||||
EXPECT_EQ(store->init(), MBED_SUCCESS);
|
||||
}
|
||||
|
||||
TEST_F(FileSystemStoreModuleTest, set_get)
|
||||
{
|
||||
char buf[100];
|
||||
size_t size;
|
||||
EXPECT_EQ(store->set("key", "data", 5, 0), MBED_SUCCESS);
|
||||
EXPECT_EQ(store->get("key", buf, 100, &size), MBED_SUCCESS);
|
||||
EXPECT_EQ(size, 5);
|
||||
EXPECT_STREQ("data", buf);
|
||||
}
|
||||
|
||||
TEST_F(FileSystemStoreModuleTest, erased_set_get)
|
||||
{
|
||||
EXPECT_EQ(store->deinit(), MBED_SUCCESS);
|
||||
EXPECT_EQ(heap.init(), MBED_SUCCESS);
|
||||
EXPECT_EQ(heap.erase(0, heap.size()), MBED_SUCCESS);
|
||||
EXPECT_EQ(heap.deinit(), MBED_SUCCESS);
|
||||
EXPECT_EQ(store->init(), MBED_SUCCESS);
|
||||
char buf[100];
|
||||
size_t size;
|
||||
EXPECT_EQ(store->set("key", "data", 5, 0), MBED_SUCCESS);
|
||||
EXPECT_EQ(store->get("key", buf, 100, &size), MBED_SUCCESS);
|
||||
EXPECT_EQ(size, 5);
|
||||
EXPECT_STREQ("data", buf);
|
||||
}
|
||||
|
||||
TEST_F(FileSystemStoreModuleTest, set_deinit_init_get)
|
||||
{
|
||||
char buf[100];
|
||||
size_t size;
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
EXPECT_EQ(store->set("key", "data", 5, 0), MBED_SUCCESS);
|
||||
EXPECT_EQ(store->deinit(), MBED_SUCCESS);
|
||||
EXPECT_EQ(store->init(), MBED_SUCCESS);
|
||||
EXPECT_EQ(store->get("key", buf, 100, &size), MBED_SUCCESS);
|
||||
EXPECT_EQ(size, 5);
|
||||
EXPECT_STREQ("data", buf);
|
||||
EXPECT_EQ(store->remove("key"), MBED_SUCCESS);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(FileSystemStoreModuleTest, set_multiple_iterate)
|
||||
{
|
||||
char buf[100];
|
||||
KVStore::iterator_t iterator;
|
||||
EXPECT_EQ(store->set("primary_key", "data", 5, 0), MBED_SUCCESS);
|
||||
EXPECT_EQ(store->set("primary_second_key", "value", 6, 0), MBED_SUCCESS);
|
||||
EXPECT_EQ(store->iterator_open(&iterator, "primary"), MBED_SUCCESS);
|
||||
EXPECT_EQ(store->iterator_next(iterator, buf, 100), MBED_SUCCESS);
|
||||
EXPECT_EQ(store->iterator_next(iterator, buf, 100), MBED_SUCCESS);
|
||||
EXPECT_EQ(store->iterator_next(iterator, buf, 100), MBED_ERROR_ITEM_NOT_FOUND);
|
||||
EXPECT_EQ(store->iterator_close(iterator), MBED_SUCCESS);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
####################
|
||||
# UNIT TESTS
|
||||
####################
|
||||
|
||||
set(unittest-includes ${unittest-includes}
|
||||
.
|
||||
..
|
||||
../features/frameworks/mbed-trace/mbed-trace
|
||||
)
|
||||
|
||||
set(unittest-sources
|
||||
../storage/blockdevice/source/HeapBlockDevice.cpp
|
||||
../storage/kvstore/source/FileSystemStore.cpp
|
||||
../storage/filesystem/littlefs/source/LittleFileSystem.cpp
|
||||
../storage/filesystem/source/Dir.cpp
|
||||
../storage/filesystem/source/File.cpp
|
||||
../storage/filesystem/source/FileSystem.cpp
|
||||
../features/frameworks/mbed-trace/source/mbed_trace.c
|
||||
../storage/filesystem/littlefs/littlefs/lfs_util.c
|
||||
../storage/filesystem/littlefs/littlefs/lfs.c
|
||||
../platform/source/FileBase.cpp
|
||||
../platform/source/FileSystemHandle.cpp
|
||||
../platform/source/FileHandle.cpp
|
||||
)
|
||||
|
||||
set(unittest-test-sources
|
||||
${CMAKE_CURRENT_LIST_DIR}/moduletest.cpp
|
||||
stubs/mbed_atomic_stub.c
|
||||
stubs/mbed_assert_stub.cpp
|
||||
stubs/mbed_error.c
|
||||
stubs/kv_config_stub.cpp
|
||||
stubs/mbed_retarget_stub.cpp
|
||||
)
|
||||
|
||||
set(unittest-test-flags
|
||||
-DMBED_LFS_READ_SIZE=64
|
||||
-DMBED_LFS_PROG_SIZE=64
|
||||
-DMBED_LFS_BLOCK_SIZE=512
|
||||
-DMBED_LFS_LOOKAHEAD=512
|
||||
)
|
||||
151
storage/kvstore/tests/UNITTESTS/TDBStore/moduletest.cpp
Normal file
151
storage/kvstore/tests/UNITTESTS/TDBStore/moduletest.cpp
Normal file
@@ -0,0 +1,151 @@
|
||||
/* Copyright (c) 2020 ARM Limited
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* http://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 "blockdevice/HeapBlockDevice.h"
|
||||
#include "blockdevice/FlashSimBlockDevice.h"
|
||||
#include "kvstore/TDBStore.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#define BLOCK_SIZE (512)
|
||||
#define DEVICE_SIZE (BLOCK_SIZE*200)
|
||||
|
||||
using namespace mbed;
|
||||
|
||||
class TDBStoreModuleTest : public testing::Test {
|
||||
protected:
|
||||
HeapBlockDevice heap{DEVICE_SIZE};
|
||||
FlashSimBlockDevice flash{&heap};
|
||||
TDBStore tdb{&flash};
|
||||
|
||||
virtual void SetUp()
|
||||
{
|
||||
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.reset(), MBED_SUCCESS);
|
||||
}
|
||||
|
||||
virtual void TearDown()
|
||||
{
|
||||
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(TDBStoreModuleTest, init)
|
||||
{
|
||||
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
|
||||
}
|
||||
|
||||
TEST_F(TDBStoreModuleTest, set_get)
|
||||
{
|
||||
char buf[100];
|
||||
size_t size;
|
||||
EXPECT_EQ(tdb.set("key", "data", 5, 0), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.get("key", buf, 100, &size), MBED_SUCCESS);
|
||||
EXPECT_EQ(size, 5);
|
||||
EXPECT_STREQ("data", buf);
|
||||
}
|
||||
|
||||
TEST_F(TDBStoreModuleTest, erased_set_get)
|
||||
{
|
||||
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
|
||||
EXPECT_EQ(flash.init(), MBED_SUCCESS);
|
||||
EXPECT_EQ(flash.erase(0, flash.size()), MBED_SUCCESS);
|
||||
EXPECT_EQ(flash.deinit(), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
|
||||
char buf[100];
|
||||
size_t size;
|
||||
EXPECT_EQ(tdb.set("key", "data", 5, 0), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.get("key", buf, 100, &size), MBED_SUCCESS);
|
||||
EXPECT_EQ(size, 5);
|
||||
EXPECT_STREQ("data", buf);
|
||||
}
|
||||
|
||||
TEST_F(TDBStoreModuleTest, set_deinit_init_get)
|
||||
{
|
||||
char buf[100];
|
||||
size_t size;
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
EXPECT_EQ(tdb.set("key", "data", 5, 0), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.get("key", buf, 100, &size), MBED_SUCCESS);
|
||||
EXPECT_EQ(size, 5);
|
||||
EXPECT_STREQ("data", buf);
|
||||
EXPECT_EQ(tdb.remove("key"), MBED_SUCCESS);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(TDBStoreModuleTest, corrupted_set_deinit_init_get)
|
||||
{
|
||||
char buf[100];
|
||||
char *block = new char[BLOCK_SIZE];
|
||||
size_t size;
|
||||
EXPECT_EQ(heap.init(), MBED_SUCCESS); // Extra init, so the heap will not be deinitialized
|
||||
|
||||
srand(0); // Prefer to have always the same pattern
|
||||
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
|
||||
// Corrupt the first part of the storage
|
||||
for (int j = 0; j < heap.size()/BLOCK_SIZE/2; j++) {
|
||||
for (int k = 0; k < BLOCK_SIZE; k++) {
|
||||
block[k] = rand();
|
||||
}
|
||||
EXPECT_EQ(heap.program(block, BLOCK_SIZE * j, BLOCK_SIZE), MBED_SUCCESS);
|
||||
}
|
||||
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
|
||||
for (int j = 0; j < 100; ++j) {
|
||||
// Use random data, so the data has to be updated
|
||||
EXPECT_EQ(tdb.set("key", block+j, 50, 0), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.deinit(), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.init(), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.get("key", buf, 100, &size), MBED_SUCCESS);
|
||||
EXPECT_EQ(size, 50);
|
||||
EXPECT_EQ(0, memcmp(buf, block+j, size));
|
||||
}
|
||||
EXPECT_EQ(tdb.remove("key"), MBED_SUCCESS);
|
||||
}
|
||||
|
||||
EXPECT_EQ(heap.deinit(), MBED_SUCCESS);
|
||||
delete[] block;
|
||||
}
|
||||
|
||||
TEST_F(TDBStoreModuleTest, set_multiple_iterate)
|
||||
{
|
||||
char buf[100];
|
||||
KVStore::iterator_t iterator;
|
||||
EXPECT_EQ(tdb.set("primary_key", "data", 5, 0), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.set("primary_second_key", "value", 6, 0), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.iterator_open(&iterator, "primary"), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.iterator_next(iterator, buf, 100), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.iterator_next(iterator, buf, 100), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.iterator_next(iterator, buf, 100), MBED_ERROR_ITEM_NOT_FOUND);
|
||||
EXPECT_EQ(tdb.iterator_close(iterator), MBED_SUCCESS);
|
||||
}
|
||||
|
||||
TEST_F(TDBStoreModuleTest, reserved_data_set_get)
|
||||
{
|
||||
char reserved_key[] = "value";
|
||||
char buf[64];
|
||||
size_t size;
|
||||
EXPECT_EQ(tdb.reserved_data_set(reserved_key, 6), MBED_SUCCESS);
|
||||
EXPECT_EQ(tdb.reserved_data_get(buf, 64, &size), MBED_SUCCESS);
|
||||
EXPECT_STREQ("value", buf);
|
||||
EXPECT_EQ(size, 6);
|
||||
EXPECT_EQ(tdb.reserved_data_set(reserved_key, 6), MBED_ERROR_WRITE_FAILED);
|
||||
}
|
||||
27
storage/kvstore/tests/UNITTESTS/TDBStore/unittest.cmake
Normal file
27
storage/kvstore/tests/UNITTESTS/TDBStore/unittest.cmake
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
####################
|
||||
# UNIT TESTS
|
||||
####################
|
||||
|
||||
set(unittest-includes ${unittest-includes}
|
||||
.
|
||||
..
|
||||
../features/frameworks/mbed-trace/mbed-trace
|
||||
)
|
||||
|
||||
set(unittest-sources
|
||||
../storage/blockdevice/source/FlashSimBlockDevice.cpp
|
||||
../storage/blockdevice/source/HeapBlockDevice.cpp
|
||||
../storage/blockdevice/source/BufferedBlockDevice.cpp
|
||||
../storage/kvstore/source/TDBStore.cpp
|
||||
../features/frameworks/mbed-trace/source/mbed_trace.c
|
||||
stubs/mbed_atomic_stub.c
|
||||
stubs/mbed_assert_stub.cpp
|
||||
stubs/mbed_error.c
|
||||
)
|
||||
|
||||
set(unittest-test-sources
|
||||
${CMAKE_CURRENT_LIST_DIR}/moduletest.cpp
|
||||
)
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
|
||||
Reference in New Issue
Block a user