Skip to content

Commit 4fa9cca

Browse files
authored
Merge pull request #12652 from jarlamsa/moduletest_filesystemstore
Add moduletests for FileSystemStore
2 parents 6964679 + e19cd14 commit 4fa9cca

File tree

5 files changed

+260
-0
lines changed

5 files changed

+260
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/* Copyright (c) 2020 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/kvstore/filesystemstore/FileSystemStore.h"
20+
#include "features/storage/filesystem/littlefs/LittleFileSystem.h"
21+
#include "mbed_error.h"
22+
#include <stdlib.h>
23+
24+
#define HEAPBLOCK_SIZE (4096)
25+
26+
using namespace mbed;
27+
28+
class FileSystemStoreModuleTest : public testing::Test {
29+
protected:
30+
HeapBlockDevice heap{HEAPBLOCK_SIZE};
31+
LittleFileSystem *fs;
32+
FileSystemStore *store;
33+
34+
virtual void SetUp()
35+
{
36+
fs = new LittleFileSystem("kvstore", &heap);
37+
if(fs->mount(&heap) != MBED_SUCCESS) {
38+
EXPECT_EQ(fs->reformat(&heap), MBED_SUCCESS);
39+
}
40+
store = new FileSystemStore(fs);
41+
EXPECT_EQ(store->init(), MBED_SUCCESS);
42+
}
43+
44+
virtual void TearDown()
45+
{
46+
EXPECT_EQ(store->deinit(), MBED_SUCCESS);
47+
delete store;
48+
EXPECT_EQ(fs->unmount(), MBED_SUCCESS);
49+
delete fs;
50+
}
51+
};
52+
53+
TEST_F(FileSystemStoreModuleTest, init)
54+
{
55+
EXPECT_EQ(store->deinit(), MBED_SUCCESS);
56+
EXPECT_EQ(store->init(), MBED_SUCCESS);
57+
EXPECT_EQ(store->init(), MBED_SUCCESS);
58+
}
59+
60+
TEST_F(FileSystemStoreModuleTest, set_get)
61+
{
62+
char buf[100];
63+
size_t size;
64+
EXPECT_EQ(store->set("key", "data", 5, 0), MBED_SUCCESS);
65+
EXPECT_EQ(store->get("key", buf, 100, &size), MBED_SUCCESS);
66+
EXPECT_EQ(size, 5);
67+
EXPECT_STREQ("data", buf);
68+
}
69+
70+
TEST_F(FileSystemStoreModuleTest, erased_set_get)
71+
{
72+
EXPECT_EQ(store->deinit(), MBED_SUCCESS);
73+
EXPECT_EQ(heap.init(), MBED_SUCCESS);
74+
EXPECT_EQ(heap.erase(0, heap.size()), MBED_SUCCESS);
75+
EXPECT_EQ(heap.deinit(), MBED_SUCCESS);
76+
EXPECT_EQ(store->init(), MBED_SUCCESS);
77+
char buf[100];
78+
size_t size;
79+
EXPECT_EQ(store->set("key", "data", 5, 0), MBED_SUCCESS);
80+
EXPECT_EQ(store->get("key", buf, 100, &size), MBED_SUCCESS);
81+
EXPECT_EQ(size, 5);
82+
EXPECT_STREQ("data", buf);
83+
}
84+
85+
TEST_F(FileSystemStoreModuleTest, set_deinit_init_get)
86+
{
87+
char buf[100];
88+
size_t size;
89+
for (int i = 0; i < 100; ++i) {
90+
EXPECT_EQ(store->set("key", "data", 5, 0), MBED_SUCCESS);
91+
EXPECT_EQ(store->deinit(), MBED_SUCCESS);
92+
EXPECT_EQ(store->init(), MBED_SUCCESS);
93+
EXPECT_EQ(store->get("key", buf, 100, &size), MBED_SUCCESS);
94+
EXPECT_EQ(size, 5);
95+
EXPECT_STREQ("data", buf);
96+
EXPECT_EQ(store->remove("key"), MBED_SUCCESS);
97+
}
98+
}
99+
100+
TEST_F(FileSystemStoreModuleTest, set_multiple_iterate)
101+
{
102+
char buf[100];
103+
KVStore::iterator_t iterator;
104+
EXPECT_EQ(store->set("primary_key", "data", 5, 0), MBED_SUCCESS);
105+
EXPECT_EQ(store->set("primary_second_key", "value", 6, 0), MBED_SUCCESS);
106+
EXPECT_EQ(store->iterator_open(&iterator, "primary"), MBED_SUCCESS);
107+
EXPECT_EQ(store->iterator_next(iterator, buf, 100), MBED_SUCCESS);
108+
EXPECT_EQ(store->iterator_next(iterator, buf, 100), MBED_SUCCESS);
109+
EXPECT_EQ(store->iterator_next(iterator, buf, 100), MBED_ERROR_ITEM_NOT_FOUND);
110+
EXPECT_EQ(store->iterator_close(iterator), MBED_SUCCESS);
111+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
####################
2+
# UNIT TESTS
3+
####################
4+
5+
set(unittest-includes ${unittest-includes}
6+
.
7+
..
8+
../features/frameworks/mbed-trace/mbed-trace
9+
)
10+
11+
set(unittest-sources
12+
../features/storage/blockdevice/HeapBlockDevice.cpp
13+
../features/storage/kvstore/filesystemstore/FileSystemStore.cpp
14+
../features/storage/filesystem/littlefs/LittleFileSystem.cpp
15+
../features/storage/filesystem/Dir.cpp
16+
../features/storage/filesystem/File.cpp
17+
../features/storage/filesystem/FileSystem.cpp
18+
../features/frameworks/mbed-trace/source/mbed_trace.c
19+
../features/storage/filesystem/littlefs/littlefs/lfs_util.c
20+
../features/storage/filesystem/littlefs/littlefs/lfs.c
21+
../platform/source/FileBase.cpp
22+
../platform/source/FileSystemHandle.cpp
23+
../platform/source/FileHandle.cpp
24+
)
25+
26+
set(unittest-test-sources
27+
moduletests/storage/kvstore/FileSystemStore/moduletest.cpp
28+
stubs/mbed_atomic_stub.c
29+
stubs/mbed_assert_stub.cpp
30+
stubs/mbed_error.c
31+
stubs/kv_config_stub.cpp
32+
stubs/mbed_retarget_stub.cpp
33+
)
34+
35+
set(unittest-test-flags
36+
-DMBED_LFS_READ_SIZE=64
37+
-DMBED_LFS_PROG_SIZE=64
38+
-DMBED_LFS_BLOCK_SIZE=512
39+
-DMBED_LFS_LOOKAHEAD=512
40+
)

UNITTESTS/stubs/kv_config_stub.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) , Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "features/storage/kvstore/conf/kv_config.h"
19+
20+
const char *get_filesystemstore_folder_path()
21+
{
22+
return "kvstore";
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) , Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "platform/mbed_retarget.h"
19+
#include "platform/FileHandle.h"
20+
21+
namespace mbed {
22+
void remove_filehandle(FileHandle *file)
23+
{
24+
(void)file;
25+
}
26+
}

UNITTESTS/target_h/platform/mbed_retarget.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,49 @@ namespace mbed {
300300
#define _IFCHR 0020000 //< character special
301301
#define _IFIFO 0010000 //< fifo special
302302

303+
#ifndef S_IFMT
304+
#define S_IFMT _IFMT //< type of file
305+
#endif
306+
#ifndef S_IFSOCK
307+
#define S_IFSOCK _IFSOCK //< socket
308+
#endif
309+
#ifndef S_IFLNK
310+
#define S_IFLNK _IFLNK //< symbolic link
311+
#endif
312+
#ifndef S_IFREG
313+
#define S_IFREG _IFREG //< regular
314+
#endif
315+
#ifndef S_IFBLK
316+
#define S_IFBLK _IFBLK //< block special
317+
#endif
318+
#ifndef S_IFDIR
319+
#define S_IFDIR _IFDIR //< directory
320+
#endif
321+
#ifndef S_IFCHR
322+
#define S_IFCHR _IFCHR //< character special
323+
#endif
324+
#ifndef S_IFIFO
325+
#define S_IFIFO _IFIFO //< fifo special
326+
#endif
327+
328+
#ifndef S_IRWXU
329+
#define S_IRWXU (S_IRUSR | S_IWUSR | S_IXUSR)
330+
#define S_IRUSR 0000400 ///< read permission, owner
331+
#define S_IWUSR 0000200 ///< write permission, owner
332+
#define S_IXUSR 0000100 ///< execute/search permission, owner
333+
#endif /* S_IRWXU */
334+
#ifndef S_IRWXG
335+
#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
336+
#define S_IRGRP 0000040 ///< read permission, group
337+
#define S_IWGRP 0000020 ///< write permission, group
338+
#define S_IXGRP 0000010 ///< execute/search permission, group
339+
#endif /* S_IRWXG */
340+
#ifndef S_IRWXO
341+
#define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
342+
#define S_IROTH 0000004 ///< read permission, other
343+
#define S_IWOTH 0000002 ///< write permission, other
344+
#define S_IXOTH 0000001 ///< execute/search permission, other
345+
#endif /* S_IRWXO */
303346

304347
#define O_RDONLY 0 ///< Open for reading
305348
#define O_WRONLY 1 ///< Open for writing
@@ -319,6 +362,23 @@ namespace mbed {
319362
#define STDOUT_FILENO 1
320363
#define STDERR_FILENO 2
321364

365+
#ifndef _STAT_VER
366+
struct stat {
367+
dev_t st_dev; ///< Device ID containing file
368+
ino_t st_ino; ///< File serial number
369+
mode_t st_mode; ///< Mode of file
370+
nlink_t st_nlink; ///< Number of links to file
371+
372+
uid_t st_uid; ///< User ID
373+
gid_t st_gid; ///< Group ID
374+
375+
off_t st_size; ///< Size of file in bytes
376+
377+
time_t st_atime; ///< Time of last access
378+
time_t st_mtime; ///< Time of last data modification
379+
time_t st_ctime; ///< Time of last status change
380+
};
381+
#endif
322382

323383
struct statvfs {
324384
unsigned long f_bsize; ///< Filesystem block size

0 commit comments

Comments
 (0)