From f8896d5b6a27ae111eb9862653771cebc058f7f9 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Tue, 2 Jan 2024 17:00:42 +0100 Subject: [PATCH] test: more mkdwarfs tests --- test/tool_main_test.cpp | 99 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 3 deletions(-) diff --git a/test/tool_main_test.cpp b/test/tool_main_test.cpp index 7142d0e91..e77228266 100644 --- a/test/tool_main_test.cpp +++ b/test/tool_main_test.cpp @@ -32,6 +32,7 @@ #include #include "dwarfs/filesystem_v2.h" +#include "dwarfs/logger.h" #include "dwarfs/util.h" #include "dwarfs_tool_main.h" @@ -95,8 +96,7 @@ class mkdwarfs_tester { mkdwarfs_tester(std::shared_ptr pos) : fa{std::make_shared()} , os{std::move(pos)} - , iol{std::make_unique(os, fa)} - , lgr{std::make_unique()} { + , iol{std::make_unique(os, fa)} { setup_locale(); } @@ -107,8 +107,31 @@ class mkdwarfs_tester { return mkdwarfs_tester(std::make_shared()); } + void add_stream_logger(std::ostream& os, + logger::level_type level = logger::VERBOSE) { + lgr = std::make_unique( + std::make_shared(os, os), os, level); + } + void add_root_dir() { os->add("", {1, 040755, 1, 0, 0, 10, 42, 0, 0, 0}); } + void add_file_tree(double avg_size = 4096.0, int dimension = 20) { + size_t max_size{128 * static_cast(avg_size)}; + std::mt19937_64 rng{42}; + std::exponential_distribution<> size_dist{1 / avg_size}; + + for (int x = 0; x < dimension; ++x) { + os->add_dir(fmt::format("{}", x)); + for (int y = 0; y < dimension; ++y) { + os->add_dir(fmt::format("{}/{}", x, y)); + for (int z = 0; z < dimension; ++z) { + auto size = std::min(max_size, static_cast(size_dist(rng))); + os->add_file(fmt::format("{}/{}/{}", x, y, z), size, true); + } + } + } + } + int run(std::vector args) { args.insert(args.begin(), "mkdwarfs"); return mkdwarfs_main(args, iol->get()); @@ -121,6 +144,9 @@ class mkdwarfs_tester { int run(std::string args) { return run(test::parse_args(args)); } filesystem_v2 fs_from_data(std::string data) { + if (!lgr) { + lgr = std::make_unique(); + } auto mm = std::make_shared(std::move(data)); return filesystem_v2(*lgr, mm); } @@ -141,7 +167,7 @@ class mkdwarfs_tester { std::shared_ptr fa; std::shared_ptr os; std::unique_ptr iol; - std::unique_ptr lgr; + std::unique_ptr lgr; }; std::optional @@ -341,6 +367,9 @@ TEST_P(mkdwarfs_input_list_test, basic) { EXPECT_EQ(0, t.run({"--input-list", input_file, "-o", image_file})); + std::ostringstream oss; + t.add_stream_logger(oss, logger::DEBUG); + auto fs = t.fs_from_file(image_file); auto link = fs.find("/somelink"); @@ -618,3 +647,67 @@ TEST(mkdwarfs_test, recompress) { EXPECT_TRUE(fs.find("/random")); } } + +class mkdwarfs_build_options_test + : public testing::TestWithParam {}; + +TEST_P(mkdwarfs_build_options_test, basic) { + auto opts = GetParam(); + auto options = test::parse_args(opts); + std::string const image_file = "test.dwarfs"; + + std::vector args = {"-i", "/", "-o", image_file}; + args.insert(args.end(), options.begin(), options.end()); + + auto t = mkdwarfs_tester::create_empty(); + + t.add_root_dir(); + t.add_file_tree(); + t.os->add_local_files(audio_data_dir); + + EXPECT_EQ(0, t.run(args)); + + auto fs = t.fs_from_file(image_file); +} + +namespace { + +constexpr std::array const build_options = { + "--categorize --order=none", + "--categorize --order=path", + "--categorize --order=revpath --file-hash=sha512", + "--categorize --order=similarity", + "--categorize --order=nilsimsa", + "--categorize --order=nilsimsa:16", + "--categorize --order=nilsimsa:16:16 --max-similarity-size=1M", +}; + +} // namespace + +INSTANTIATE_TEST_SUITE_P(dwarfs, mkdwarfs_build_options_test, + ::testing::ValuesIn(build_options)); + +TEST(mkdwarfs_test, order_invalid) { + mkdwarfs_tester t; + EXPECT_NE(0, t.run({"-i", "/", "-o", "-", "--order=grmpf"})); + EXPECT_THAT(t.err(), ::testing::HasSubstr("invalid inode order mode")); +} + +TEST(mkdwarfs_test, order_nilsimsa_not_numeric) { + mkdwarfs_tester t; + EXPECT_NE(0, t.run({"-i", "/", "-o", "-", "--order=nilsimsa:grmpf"})); + EXPECT_THAT(t.err(), ::testing::HasSubstr("is not numeric for order")); +} + +TEST(mkdwarfs_test, order_nilsimsa_too_many_options) { + mkdwarfs_tester t; + EXPECT_NE(0, t.run({"-i", "/", "-o", "-", "--order=nilsimsa:1:2:3"})); + EXPECT_THAT(t.err(), + ::testing::HasSubstr("too many options for inode order mode")); +} + +TEST(mkdwarfs_test, order_nilsimsa_cannot_be_less) { + mkdwarfs_tester t; + EXPECT_NE(0, t.run({"-i", "/", "-o", "-", "--order=nilsimsa:-1:-1"})); + EXPECT_THAT(t.err(), ::testing::HasSubstr("cannot be less than 0 for order")); +}