From 21ef9aeedbb49e10a9e9f2bb055ec3f3b5e8e20a Mon Sep 17 00:00:00 2001 From: Hongzhen Luo Date: Wed, 13 Nov 2024 17:37:38 +0800 Subject: [PATCH] [EROFS]: test: add test for pax-header tar Signed-off-by: Hongzhen Luo --- src/overlaybd/tar/erofs/test/CMakeLists.txt | 7 +- src/overlaybd/tar/erofs/test/test.cpp | 107 ++++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/src/overlaybd/tar/erofs/test/CMakeLists.txt b/src/overlaybd/tar/erofs/test/CMakeLists.txt index 290504fa..fd82eeb7 100644 --- a/src/overlaybd/tar/erofs/test/CMakeLists.txt +++ b/src/overlaybd/tar/erofs/test/CMakeLists.txt @@ -7,7 +7,12 @@ link_directories($ENV{GTEST}/lib) add_executable(erofs_test test.cpp) target_include_directories(erofs_test PUBLIC ${PHOTON_INCLUDE_DIR}) target_link_libraries(erofs_test gtest gtest_main pthread photon_static - tar_lib lsmt_lib gzip_lib gzindex_lib checksum_lib) + tar_lib lsmt_lib gzip_lib gzindex_lib checksum_lib overlaybd_image_lib) + +target_include_directories(erofs_test PUBLIC + ${PHOTON_INCLUDE_DIR} + ${rapidjson_SOURCE_DIR}/include +) add_test( NAME erofs_test diff --git a/src/overlaybd/tar/erofs/test/test.cpp b/src/overlaybd/tar/erofs/test/test.cpp index f9cbde37..e85825d3 100644 --- a/src/overlaybd/tar/erofs/test/test.cpp +++ b/src/overlaybd/tar/erofs/test/test.cpp @@ -24,12 +24,14 @@ #include #include #include +#include #include "../../../gzindex/gzfile.h" #include "../../../lsmt/file.h" #include "../liberofs.h" #include "../../tar_file.cpp" #include "../../../gzip/gz.h" #include "../../../../tools/sha256file.h" +#include "../../../../tools/comm_func.h" #define FILE_SIZE (2 * 1024 * 1024) @@ -250,6 +252,111 @@ TEST_F(ErofsTest, tar_meta) { delete tar_idx; } +class ErofsPax : public ::testing::Test { +protected: + photon::fs::IFileSystem *host_fs; + std::string workdir = "/tmp/pax_test"; + std::string src_url = "https://github.com/salvete/erofs-imgs/raw/main/pax.tar"; + std::string src_path = workdir + "/pax.tar"; + std::string fn_idx = workdir + "/index.idx"; + std::string fn_meta = workdir + "/erofs.fs.meta"; + std::string sha256_path = workdir + "/sha256"; + + virtual void SetUp() override{ + ASSERT_EQ(0, prepare_host_fs()); + ASSERT_EQ(0, download(src_url, src_path)); + } + + virtual void TearDown() override{ + ASSERT_NE(nullptr, host_fs); + if (host_fs->access(src_path.c_str(), 0) == 0) { + ASSERT_EQ(0, host_fs->unlink(src_path.c_str())); + ASSERT_EQ(0, host_fs->unlink(fn_idx.c_str())); + ASSERT_EQ(0, host_fs->unlink(fn_meta.c_str())); + ASSERT_EQ(0, host_fs->unlink(sha256_path.c_str())); + } + delete host_fs; + } + + int prepare_host_fs() { + host_fs = photon::fs::new_localfs_adaptor(); + if (!host_fs) + return -1; + if (host_fs->access(workdir.c_str(), 0) != 0) { + if (host_fs->mkdir(workdir.c_str(), 0755)) + return -1; + } + return 0; + } + + int download(const std::string &url, const std::string &out) + { + std::string cmd = "wget -O" + out + " " + url; + auto ret = system(cmd.c_str()); + if (ret != 0) + LOG_ERRNO_RETURN(0, -1, "download failed: `", url.c_str()); + return 0; + } + +}; + +TEST_F(ErofsPax, pax_test) { + + // create device + auto src_file = host_fs->open(src_path.c_str(), O_RDONLY, 0666); + ASSERT_NE(nullptr, src_file); + DEFER(delete src_file); + auto fmeta = host_fs->open(fn_idx.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRWXU); + auto findex = host_fs->open(fn_meta.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRWXU); + LSMT::WarpFileArgs args(findex, fmeta, src_file); + args.virtual_size = IMAGE_SIZE; + auto wrap_file = create_warpfile(args, false); + ASSERT_NE(wrap_file, nullptr); + + // create erofs image + auto tar = new LibErofs(wrap_file, 4096, true); + ASSERT_EQ(0, tar->extract_tar(src_file, true, true)); + delete tar; + + // setup erofs fs + auto erofs_fs = create_erofs_fs(wrap_file, 4096); + ASSERT_NE(erofs_fs, nullptr); + DEFER(delete erofs_fs); + + // traverse the fs tree + std::string res; + std::string std_res = "sha256:4d7ad1fb36abd862a7caa9da01233aed7c762bbb8d900dc5baf60c5b1ddd8496"; + std::vector items; + items.emplace_back("/"); + while (!items.empty()) { + std::string tmp = items.front(); + struct stat st; + + items.erase(items.begin()); + res += tmp; + ASSERT_EQ(0, erofs_fs->stat(tmp.c_str(), &st)); + if (S_ISDIR(st.st_mode)) { + auto dir = erofs_fs->opendir(tmp.c_str()); + while (dir->next()) { + dirent *dent = dir->get(); + items.emplace_back(tmp + "/" + dent->d_name); + } + dir->closedir(); + delete dir; + } + } + + auto sha256_f = host_fs->open(sha256_path.c_str(), O_RDWR | O_CREAT | O_TRUNC); + ASSERT_NE(sha256_f, nullptr); + sha256_f->pwrite(res.c_str(), res.size(), 0); + sha256_f->lseek(0, SEEK_SET); + auto sha256file = new_sha256_file(sha256_f, true); + ASSERT_STREQ(std_res.c_str(), sha256file->sha256_checksum().c_str()); + delete sha256_f, sha256file; +} + + + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv);