From bf33cba2216e1148e142b2e3e01f8693fc779fa7 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sun, 31 Dec 2023 19:27:02 +0100 Subject: [PATCH] test: add --set-time tests --- test/tool_main_test.cpp | 61 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/test/tool_main_test.cpp b/test/tool_main_test.cpp index cf1db9641..8facc66e0 100644 --- a/test/tool_main_test.cpp +++ b/test/tool_main_test.cpp @@ -132,6 +132,30 @@ class mkdwarfs_tester { test::test_logger lgr; }; +std::optional +build_with_args(std::vector opt_args = {}) { + std::string const image_file = "test.dwarfs"; + mkdwarfs_tester t; + std::vector args = {"-i", "/", "-o", image_file}; + args.insert(args.end(), opt_args.begin(), opt_args.end()); + if (t.run(args) != 0) { + return std::nullopt; + } + return t.fs_from_file(image_file); +} + +std::set get_all_fs_times(filesystem_v2 const& fs) { + std::set times; + fs.walk([&](auto const& e) { + file_stat st; + fs.getattr(e.inode(), &st); + times.insert(st.atime); + times.insert(st.ctime); + times.insert(st.mtime); + }); + return times; +} + } // namespace class mkdwarfs_main_test : public tool_main_test { @@ -431,3 +455,40 @@ TEST(mkdwarfs_test, dump_inodes) { ASSERT_TRUE(dump); EXPECT_GT(dump->size(), 100) << dump.value(); } + +TEST(mkdwarfs_test, set_time_now) { + auto t0 = + std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + + auto regfs = build_with_args(); + ASSERT_TRUE(regfs); + auto reg = get_all_fs_times(*regfs); + + auto optfs = build_with_args({"--set-time=now"}); + ASSERT_TRUE(optfs); + auto opt = get_all_fs_times(*optfs); + + auto t1 = + std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); + + EXPECT_EQ(reg.size(), 11); + EXPECT_EQ(opt.size(), 1); + + EXPECT_GE(*opt.begin(), t0); + EXPECT_LE(*opt.begin(), t1); +} + +TEST(mkdwarfs_test, set_time_epoch) { + auto regfs = build_with_args(); + ASSERT_TRUE(regfs); + auto reg = get_all_fs_times(*regfs); + + auto optfs = build_with_args({"--set-time=100000001"}); + ASSERT_TRUE(optfs); + auto opt = get_all_fs_times(*optfs); + + EXPECT_EQ(reg.size(), 11); + EXPECT_EQ(opt.size(), 1); + + EXPECT_EQ(*opt.begin(), 100000001); +}