From: Mateusz Majewski Date: Tue, 26 Sep 2023 06:15:21 +0000 (+0200) Subject: Make testable X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fheads%2Fsandbox%2Fmmajewski2%2Ftesting;p=platform%2Fcore%2Fsystem%2Finitrd.git Make testable --- diff --git a/CMakeLists.txt b/CMakeLists.txt index f792260..4918e47 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,9 @@ CONFIGURE_FILE(data/initrd-file-dmverity.list.in data/initrd-file-dmverity.list @ONLY) +find_package(GTest REQUIRED) +enable_testing() + # reboot SET(REBOOT_BINARY_NAME "reboot") SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -Wall -fPIE") diff --git a/packaging/initrd.spec b/packaging/initrd.spec index a109ed6..eea33a5 100644 --- a/packaging/initrd.spec +++ b/packaging/initrd.spec @@ -13,6 +13,7 @@ Source1: ramdisk-flush.service BuildRequires: cmake BuildRequires: pkgconfig(openssl3) +BuildRequires: gtest-devel Requires(posttrans): bash Requires(posttrans): grep @@ -67,6 +68,9 @@ cp %{SOURCE1001} . -DINSTALL_DIR=%{_sbindir} make %{_smp_mflags} +%check +ctest -V %{?_smp_mflags} + %install %make_install diff --git a/src/parse-dynparts/CMakeLists.txt b/src/parse-dynparts/CMakeLists.txt index c6ffccf..bedf1e9 100644 --- a/src/parse-dynparts/CMakeLists.txt +++ b/src/parse-dynparts/CMakeLists.txt @@ -3,8 +3,14 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) add_subdirectory(liblp) -add_executable(parse-dynparts main.cpp) +add_executable(parse-dynparts main.cpp lib.cpp) target_compile_options(parse-dynparts PRIVATE -Wall -Wextra -pedantic -fPIE) - target_link_libraries(parse-dynparts PRIVATE lp) -INSTALL(TARGETS parse-dynparts DESTINATION ${INSTALL_DIR}) \ No newline at end of file +install(TARGETS parse-dynparts DESTINATION ${INSTALL_DIR}) + +add_executable(parse-dynparts-test test.cpp lib.cpp) +target_compile_options(parse-dynparts-test PRIVATE -Wall -Wextra -pedantic -fPIE) +target_link_libraries(parse-dynparts-test PRIVATE GTest::gtest_main lp) + +include(GoogleTest) +gtest_discover_tests(parse-dynparts-test) diff --git a/src/parse-dynparts/lib.cpp b/src/parse-dynparts/lib.cpp new file mode 100644 index 0000000..a5b1303 --- /dev/null +++ b/src/parse-dynparts/lib.cpp @@ -0,0 +1,67 @@ +#include "lib.hpp" + +#include +#include +#include + +using namespace android::fs_mgr; +using std::endl; + +std::optional go(const android::fs_mgr::LpMetadata &metadata, bool list_tables, std::string_view prog_name, std::ostream &messages) { + std::string table; + + // Code structure taken from Android's system/core/fs_mgr/fs_mgr_dm_linear.cpp + for (auto partition : metadata.partitions) { + if (!partition.num_extents) { + messages << "Skipping zero-length logical partition: " + << GetPartitionName(partition) << endl; + continue; + } + if (partition.attributes & LP_PARTITION_ATTR_DISABLED) { + messages << "Skipping disabled partition: " << GetPartitionName(partition) + << endl; + continue; + } + + std::ostringstream line; + + if(list_tables) + line << GetPartitionName(partition) << " "; + else { + bool read_only = partition.attributes & LP_PARTITION_ATTR_READONLY; + line << GetPartitionName(partition) << ",,," << (read_only ? "ro," : "rw,"); + } + + uint64_t sector = 0; + for (size_t i = 0; i < partition.num_extents; i++) { + const auto& extent = metadata.extents[partition.first_extent_index + i]; + switch (extent.target_type) { + case LP_TARGET_TYPE_ZERO: + line << sector << " " << extent.num_sectors << " zero"; + break; + case LP_TARGET_TYPE_LINEAR: { + if (extent.target_source != 0) { + messages << "This utility does not yet support multiple block devices" + << endl; + return std::nullopt; + } + + line << sector << " " << extent.num_sectors << " linear " + << prog_name << " " << extent.target_data; + break; + } + default: + messages << "Unknown target type in metadata: " << extent.target_type + << endl; + return std::nullopt; + } + sector += extent.num_sectors; + } + + if (!table.empty() && list_tables) table += "\n"; + if (!table.empty() && !list_tables) table += ";"; + table += line.str(); + } + + return table; +} diff --git a/src/parse-dynparts/lib.hpp b/src/parse-dynparts/lib.hpp new file mode 100644 index 0000000..50cd80b --- /dev/null +++ b/src/parse-dynparts/lib.hpp @@ -0,0 +1,6 @@ +#pragma once + +#include +#include + +std::optional go(const android::fs_mgr::LpMetadata &metadata, bool list_tables, std::string_view prog_name, std::ostream &messages); diff --git a/src/parse-dynparts/main.cpp b/src/parse-dynparts/main.cpp index 022821a..def6831 100644 --- a/src/parse-dynparts/main.cpp +++ b/src/parse-dynparts/main.cpp @@ -4,6 +4,8 @@ #include #include +#include "lib.hpp" + using namespace android::fs_mgr; using std::cerr; @@ -27,62 +29,11 @@ int main(int argc, char* argv[]) { return 1; } - std::string table; - - // Code structure taken from Android's system/core/fs_mgr/fs_mgr_dm_linear.cpp - for (auto partition : metadata->partitions) { - if (!partition.num_extents) { - cerr << "Skipping zero-length logical partition: " - << GetPartitionName(partition) << endl; - continue; - } - if (partition.attributes & LP_PARTITION_ATTR_DISABLED) { - cerr << "Skipping disabled partition: " << GetPartitionName(partition) - << endl; - continue; - } - - std::ostringstream line; - - if(list_tables) - line << GetPartitionName(partition) << " "; - else { - bool read_only = partition.attributes & LP_PARTITION_ATTR_READONLY; - line << GetPartitionName(partition) << ",,," << (read_only ? "ro," : "rw,"); - } - - uint64_t sector = 0; - for (size_t i = 0; i < partition.num_extents; i++) { - const auto& extent = metadata->extents[partition.first_extent_index + i]; - switch (extent.target_type) { - case LP_TARGET_TYPE_ZERO: - line << sector << " " << extent.num_sectors << " zero"; - break; - case LP_TARGET_TYPE_LINEAR: { - if (extent.target_source != 0) { - cerr << "This utility does not yet support multiple block devices" - << endl; - return 1; - } - - line << sector << " " << extent.num_sectors << " linear " - << argv[1] << " " << extent.target_data; - break; - } - default: - cerr << "Unknown target type in metadata: " << extent.target_type - << endl; - return false; - } - sector += extent.num_sectors; - } - - if (!table.empty() && list_tables) table += "\n"; - if (!table.empty() && !list_tables) table += ";"; - table += line.str(); - } - - cout << table << endl; + auto out = go(*metadata, list_tables, argv[1], cerr); + if (out) + cout << *out << "\n"; + else + return 1; return 0; } diff --git a/src/parse-dynparts/test.cpp b/src/parse-dynparts/test.cpp new file mode 100644 index 0000000..26526b7 --- /dev/null +++ b/src/parse-dynparts/test.cpp @@ -0,0 +1,12 @@ +#include + +#include "lib.hpp" + +using namespace android::fs_mgr; + +TEST(ParseDynpartsTest, Example) { + LpMetadata metadata {}; + std::ostringstream messages; + EXPECT_EQ(go(metadata, true, "test", messages), ""); + EXPECT_EQ(messages.str(), ""); +}