Skip to content

Commit

Permalink
[EROFS]: test: add test for pax-header tar
Browse files Browse the repository at this point in the history
Signed-off-by: Hongzhen Luo <[email protected]>
  • Loading branch information
salvete committed Nov 13, 2024
1 parent 8ce880b commit 21ef9ae
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/overlaybd/tar/erofs/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
107 changes: 107 additions & 0 deletions src/overlaybd/tar/erofs/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
#include <photon/common/alog.h>
#include <photon/common/alog-stdstring.h>
#include <vector>
#include <dirent.h>
#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)
Expand Down Expand Up @@ -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<string> 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);
Expand Down

0 comments on commit 21ef9ae

Please sign in to comment.