diff --git a/tests/functional/CMakeLists.txt b/tests/functional/CMakeLists.txt index 1fc5291047..6b9430de14 100644 --- a/tests/functional/CMakeLists.txt +++ b/tests/functional/CMakeLists.txt @@ -40,4 +40,5 @@ endfunction() add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/boost_ut) add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/io_expander) +add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/file_manager) add_subdirectory(${TESTS_FUNCTIONAL_TESTS_DIR}/qdac) diff --git a/tests/functional/include/tests/utils.h b/tests/functional/include/tests/utils.h index 6af8efd52e..90b4d3632a 100644 --- a/tests/functional/include/tests/utils.h +++ b/tests/functional/include/tests/utils.h @@ -6,18 +6,44 @@ #include "rtos/ThisThread.h" +#include "FATFileSystem.h" #include "LogKit.h" +#include "SDBlockDevice.h" -namespace utils::time { +namespace utils { -using namespace leka; -using namespace std::chrono; +namespace time { -inline auto start = rtos::Kernel::Clock::now(); -inline auto stop = rtos::Kernel::Clock::now(); -inline auto delta = [] { return static_cast((stop - start).count()); }; + using namespace leka; + using namespace std::chrono; -} // namespace utils::time + inline auto start = rtos::Kernel::Clock::now(); + inline auto stop = rtos::Kernel::Clock::now(); + inline auto delta = [] { return static_cast((stop - start).count()); }; + +} // namespace time + +namespace sd { + + namespace internal { + + inline auto bd = SDBlockDevice {SD_SPI_MOSI, SD_SPI_MISO, SD_SPI_SCK}; + inline auto fs = FATFileSystem {"fs"}; + + constexpr auto default_frequency = uint64_t {25'000'000}; + + } // namespace internal + + inline void init() + { + internal::bd.init(); + internal::bd.frequency(internal::default_frequency); + internal::fs.mount(&internal::bd); + } + +} // namespace sd + +} // namespace utils // NOLINTNEXTLINE #define utils_start(msg) \ diff --git a/tests/functional/tests/file_manager/CMakeLists.txt b/tests/functional/tests/file_manager/CMakeLists.txt new file mode 100644 index 0000000000..97a023f62e --- /dev/null +++ b/tests/functional/tests/file_manager/CMakeLists.txt @@ -0,0 +1,16 @@ +# Leka - LekaOS +# Copyright 2022 APF France handicap +# SPDX-License-Identifier: Apache-2.0 + +register_functional_test( + TARGET + functional_ut_file_manager + + INCLUDE_DIRECTORIES + + SOURCES + suite_file_manager.cpp + + LINK_LIBRARIES + FileManagerKit +) diff --git a/tests/functional/tests/file_manager/suite_file_manager.cpp b/tests/functional/tests/file_manager/suite_file_manager.cpp new file mode 100644 index 0000000000..4e7d788149 --- /dev/null +++ b/tests/functional/tests/file_manager/suite_file_manager.cpp @@ -0,0 +1,111 @@ +// Leka - LekaOS +// Copyright 2022 APF France handicap +// SPDX-License-Identifier: Apache-2.0 + +#include + +#include "FileManagerKit.h" +#include "boost/ut.hpp" +#include "filesystem" +#include "tests/config.h" +#include "tests/utils.h" + +using namespace leka; +using namespace boost::ut; + +struct path { + static inline const auto root = std::filesystem::path {"/fs/var/tmp/test"}; + + static inline const auto dir = root / "dir" / ""; + static inline const auto sub_dir = root / "dir" / "sub"; + static inline const auto dir_file = root / "dir" / "dir_file.txt"; + static inline const auto sub_dir_file = root / "dir" / "sub" / "sub_dir_file.txt"; + + static inline const auto all = std::to_array({dir_file, sub_dir, dir, root}); + + static inline auto remove_all = []() { + log << "Cleaning up files, directories"; + + for (const auto &p: path::all) { + if (std::filesystem::exists(p)) { + log << "Removing:" << p; + std::filesystem::remove(p); + expect(not std::filesystem::exists(p)) << p << "still exists"; + } else { + log << "Doesn't exit:" << p; + } + } + log << "\n"; + }; +}; + +suite suite_file_manager_kit = [] { + auto file = FileManagerKit::File {}; + auto input_data = std::to_array("Hello, Leka!"); + + utils::sd::init(); + path::remove_all(); + + "open existing file"_test = [&] { + auto open = file.open("/fs/usr/test/file-1-ok.txt", "w"); + + expect(open >> fatal) << "Failed to open file"; + }; + + "create root directory"_test = [&] { + auto created = FileManagerKit::create_directory(path::root); + + expect(created >> fatal) << "Failed to create directory" << path::root; + }; + + "create directory"_test = [&] { + auto created = FileManagerKit::create_directory(path::dir); + + expect(created >> fatal) << "Failed to create directory" << path::dir; + }; + + "create subdirectory"_test = [&] { + auto created = FileManagerKit::create_directory(path::sub_dir); + + expect(created >> fatal) << "Failed to create directory"; + }; + + "create & open new file"_test = [&] { + auto open = file.open(path::dir_file, "w"); + auto exists = std::filesystem::exists(path::dir_file); + + expect(open >> fatal) << "Failed to open file"; + expect(exists >> fatal) << "Failed to create file"; + }; + + "close new file"_test = [&] { + file.close(); + + expect(not file.is_open()) << "Failed to close file"; + }; + + "write to new file "_test = [&] { + file.open(path::dir_file, "w"); + auto bytes = file.write(input_data); + file.close(); + + expect(bytes == std::size(input_data) >> fatal) << "Failed to write file"; + }; + + "read new file "_test = [&] { + auto output_data = std::array {}; + + file.open(path::dir_file, "r"); + auto bytes = file.read(output_data); + file.close(); + + expect(bytes == std::size(input_data) >> fatal) << "Failed to read file, bytes =" << bytes; + + auto found = std::ranges::search(output_data, input_data); + + expect(not found.empty()); + }; + + file.close(); + path::remove_all(); +};