-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add moduletests for FileSystemStore #12652
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
UNITTESTS/moduletests/storage/kvstore/FileSystemStore/moduletest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 "features/storage/blockdevice/HeapBlockDevice.h" | ||
#include "features/storage/kvstore/filesystemstore/FileSystemStore.h" | ||
#include "features/storage/filesystem/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); | ||
} |
40 changes: 40 additions & 0 deletions
40
UNITTESTS/moduletests/storage/kvstore/FileSystemStore/unittest.cmake
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#################### | ||
# UNIT TESTS | ||
#################### | ||
|
||
set(unittest-includes ${unittest-includes} | ||
. | ||
.. | ||
../features/frameworks/mbed-trace/mbed-trace | ||
) | ||
|
||
set(unittest-sources | ||
../features/storage/blockdevice/HeapBlockDevice.cpp | ||
../features/storage/kvstore/filesystemstore/FileSystemStore.cpp | ||
../features/storage/filesystem/littlefs/LittleFileSystem.cpp | ||
../features/storage/filesystem/Dir.cpp | ||
../features/storage/filesystem/File.cpp | ||
../features/storage/filesystem/FileSystem.cpp | ||
../features/frameworks/mbed-trace/source/mbed_trace.c | ||
../features/storage/filesystem/littlefs/littlefs/lfs_util.c | ||
../features/storage/filesystem/littlefs/littlefs/lfs.c | ||
../platform/source/FileBase.cpp | ||
../platform/source/FileSystemHandle.cpp | ||
../platform/source/FileHandle.cpp | ||
) | ||
|
||
set(unittest-test-sources | ||
moduletests/storage/kvstore/FileSystemStore/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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) , Arm Limited and affiliates. | ||
* 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 "features/storage/kvstore/conf/kv_config.h" | ||
|
||
const char *get_filesystemstore_folder_path() | ||
{ | ||
return "kvstore"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) , Arm Limited and affiliates. | ||
* 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 "platform/mbed_retarget.h" | ||
#include "platform/FileHandle.h" | ||
|
||
namespace mbed { | ||
void remove_filehandle(FileHandle *file) | ||
{ | ||
(void)file; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume these test can't be run in random order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the tests are run in the order they are written. But that shouldn't matter as the tests are separate and setup and teardown are ran for each test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jarlamsa This test case fails if
BlockDevice::erase()
actually performs an erase, unless we callfs->reformat()
afterwards. We didn't see this issue becauseHeapBlockDevice::erase()
is essentially a no-op. But now I'm working on de-allocating the heap buffer on erase, and get the failure. (Same happens, if we memset the buffer to 0xFF instead of de-allocate it.)I can add the reformat to make it pass. But erasing the underlying BlockDevice sounds like restarting everything from scratch? Could you please explain me the use case we're trying to test here? Thanks in advance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you want to deallocate the heap buffer on erase? That doesn't sound like correct behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If deallocating the heap is not correct, then what about a BlockDevice that does an actual erase (e.g. like many flash devices) and sets values to
0xFF
, for example? If I didn't miss anything, this test case relies on the assumption that data is unaltered whenBlockDevice::erase()
is called?In my opinion,
FileSystemStore
should be compatible with any block device that's compliant withBlockDevice
API requirements, rather than tied to specific behaviours? (I mean, I personally think a test case is only meaningful if it tests something generally applicable.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Referring to this test case, I think it can be safely deleted, erasing the whole heap underneath the filesystem just corrupts the filesystem.
But regarding the deallocation of the allocated memory from the heap when erasing, it really sounds just wrong. Now the allocation for
HeapBlockDevice
is done on init and deallocation is done on destructor, how do you plan to change this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HeapBlockDevice::init()
only allocates an array of null pointers to blocks, not individual blocks. Only when a block is used inHeapBlockDevice::program()
, the particular blocks being programmed is allocated and added to the array of pointers.My
HeapBlockDevice::erase()
change only frees blocks being erased. The destructor, as before, frees all block and the array of pointers. So in terms of behaviour, it's still self-consistent, I think.But if the change looks redundant I can remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, that explains it. LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cheers