Add first batch of tests
and fix some issues.
This commit is contained in:
@@ -9,11 +9,22 @@ link_libraries(miniffs)
|
||||
set(TESTS_SRC fs_opening.cpp)
|
||||
|
||||
add_executable(miniffs_test)
|
||||
target_compile_definitions(miniffs_test PUBLIC BUILD_PLATFORM_${BUILD_PLATFORM})
|
||||
target_include_directories(miniffs_test PUBLIC ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
|
||||
target_sources(miniffs_test PRIVATE ${TESTS_SRC})
|
||||
target_link_libraries(miniffs_test gtest gtest_main Threads::Threads)
|
||||
target_link_libraries(miniffs_test gtest gtest_main Threads::Threads miniffs)
|
||||
|
||||
file(GLOB TEST_FSIMG *.mffs)
|
||||
|
||||
add_custom_command(
|
||||
TARGET miniffs_test POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy
|
||||
${TEST_FSIMG}
|
||||
${CMAKE_CURRENT_BINARY_DIR}/
|
||||
)
|
||||
|
||||
gtest_discover_tests(miniffs_test
|
||||
WORKING_DIRECTORY ${PROJECT_DIR}
|
||||
PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_DIR}"
|
||||
)
|
||||
|
||||
|
||||
@@ -4,4 +4,50 @@
|
||||
*
|
||||
* Copyright (c) 2008-2022 986-Studio. All rights reserved.
|
||||
*
|
||||
******************************************************************************/
|
||||
******************************************************************************/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <miniffs.h>
|
||||
#include "get_fs.h"
|
||||
|
||||
TEST(FSOpening, OpeningAFileSystem)
|
||||
{
|
||||
miniffs_t *fs = get_fs("simple_test.mffs");
|
||||
ASSERT_NE(fs, nullptr);
|
||||
}
|
||||
|
||||
TEST(FSOpening, OpeningANonExistingFile)
|
||||
{
|
||||
miniffs_t *fs = get_fs("i_do_not_exists.mffs");
|
||||
ASSERT_EQ(fs, nullptr);
|
||||
ASSERT_EQ(miniffs_geterror(), MINIFFS_INVALID_FS);
|
||||
}
|
||||
|
||||
TEST(FSOpening, OpeningFsWithWrongMagic)
|
||||
{
|
||||
miniffs_t *fs = get_fs("wrong_magic.mffs");
|
||||
ASSERT_EQ(fs, nullptr);
|
||||
ASSERT_EQ(miniffs_geterror(), MINIFFS_INVALID_FS);
|
||||
}
|
||||
|
||||
TEST(FSOpening, OpeningFsWithWrongVersion)
|
||||
{
|
||||
miniffs_t *fs = get_fs("wrong_version.mffs");
|
||||
ASSERT_EQ(fs, nullptr);
|
||||
ASSERT_EQ(miniffs_geterror(), MINIFFS_INVALID_FS);
|
||||
}
|
||||
|
||||
TEST(FSOpening, OpeningFsWithWrongNameLen)
|
||||
{
|
||||
miniffs_t *fs = get_fs("wrong_namelen.mffs");
|
||||
ASSERT_EQ(fs, nullptr);
|
||||
ASSERT_EQ(miniffs_geterror(), MINIFFS_INVALID_FS);
|
||||
}
|
||||
|
||||
TEST(FSOpening, OpeningFsWithWrongExtLen)
|
||||
{
|
||||
miniffs_t *fs = get_fs("wrong_extlen.mffs");
|
||||
ASSERT_EQ(fs, nullptr);
|
||||
ASSERT_EQ(miniffs_geterror(), MINIFFS_INVALID_FS);
|
||||
}
|
||||
47
test/get_fs.h
Normal file
47
test/get_fs.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/******************************************************************************
|
||||
* MiniFFS : Mini Flat File System
|
||||
* This file is part of the test suite of MiniFFS
|
||||
*
|
||||
* This file abstract the filesystem opening, to be able to test both FILE and
|
||||
* MEMORY backend.
|
||||
*
|
||||
* Copyright (c) 2008-2022 986-Studio. All rights reserved.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
static miniffs_t *get_fs(const char *filename)
|
||||
{
|
||||
#ifdef BUILD_PLATFORM_MEMORY
|
||||
char *fs_image;
|
||||
size_t fileSize;
|
||||
#ifdef _WIN32
|
||||
/* As windows do not provide an easy to use mmap equivalent, let's use the fallback
|
||||
* of opening the file, allocating memory and read the file in the said memory
|
||||
*/
|
||||
FILE *fp;
|
||||
fp = fopen(filename, "rb");
|
||||
fseek(fp, 0, SEEK_END);
|
||||
fileSize = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
fs_image = (char *)calloc(1, fileSize);
|
||||
fread(fs_image, 1, fileSize, fp);
|
||||
fclose(fp);
|
||||
#else
|
||||
int fd;
|
||||
struct stat FileStat;
|
||||
|
||||
fd = open(filename, O_RDWR);
|
||||
fstat(fd, &FileStat);
|
||||
fs_image = (char *)mmap(NULL, FileStat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
close(fd);
|
||||
fileSize = FileStat.st_size
|
||||
if (fs_image == MAP_FAILED)
|
||||
{
|
||||
fs_image = NULL;
|
||||
}
|
||||
#endif
|
||||
return miniffs_openfs((uintptr_t)fs_image);
|
||||
#else
|
||||
return miniffs_openfs(filename);
|
||||
#endif
|
||||
}
|
||||
BIN
test/simple_test.mffs
Normal file
BIN
test/simple_test.mffs
Normal file
Binary file not shown.
BIN
test/wrong_extlen.mffs
Normal file
BIN
test/wrong_extlen.mffs
Normal file
Binary file not shown.
BIN
test/wrong_magic.mffs
Normal file
BIN
test/wrong_magic.mffs
Normal file
Binary file not shown.
BIN
test/wrong_namelen.mffs
Normal file
BIN
test/wrong_namelen.mffs
Normal file
Binary file not shown.
BIN
test/wrong_version.mffs
Normal file
BIN
test/wrong_version.mffs
Normal file
Binary file not shown.
Reference in New Issue
Block a user