From 131beea9d136a3560067209ce40bc03ec8995db5 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Fri, 12 Jan 2024 20:47:59 +0100 Subject: [PATCH] test(filesystem): add readlink tests --- CMakeLists.txt | 1 + test/filesystem_test.cpp | 180 +++++++++++++++++++++++++++++++++++++++ test/unixlink.dwarfs | Bin 0 -> 1334 bytes test/winlink.dwarfs | Bin 0 -> 1267 bytes 4 files changed, 181 insertions(+) create mode 100644 test/filesystem_test.cpp create mode 100644 test/unixlink.dwarfs create mode 100644 test/winlink.dwarfs diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ff260b9..7b2d8e01 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -617,6 +617,7 @@ if(WITH_TESTS) entry_test error_test file_access_test + filesystem_test fragment_category_test incompressible_categorizer_test integral_value_parser_test diff --git a/test/filesystem_test.cpp b/test/filesystem_test.cpp new file mode 100644 index 00000000..7527e591 --- /dev/null +++ b/test/filesystem_test.cpp @@ -0,0 +1,180 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/** + * \author Marcus Holland-Moritz (github@mhxnet.de) + * \copyright Copyright (c) Marcus Holland-Moritz + * + * This file is part of dwarfs. + * + * dwarfs is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dwarfs is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dwarfs. If not, see . + */ + +#include + +#include +#include + +#include + +#include "dwarfs/filesystem_v2.h" +#include "dwarfs/mmap.h" + +#include "test_logger.h" + +using namespace dwarfs; + +namespace fs = std::filesystem; + +namespace { + +auto test_dir = fs::path(TEST_DATA_DIR).make_preferred(); + +} // namespace + +TEST(filesystem, metadata_symlink_win) { + test::test_logger lgr; + + auto mm = std::make_shared(test_dir / "winlink.dwarfs"); + filesystem_v2 fs(lgr, mm); + + auto i1 = fs.find("link.txt"); + auto i2 = fs.find("dir/link.txt"); + auto i3 = fs.find("subdir/test.txt"); + + ASSERT_TRUE(i1); + ASSERT_TRUE(i2); + ASSERT_TRUE(i3); + + EXPECT_TRUE(i1->is_symlink()); + EXPECT_TRUE(i2->is_symlink()); + EXPECT_TRUE(i3->is_regular_file()); + + // readlink_mode::preferred (default) + { + std::string buf1, buf2; + EXPECT_EQ(0, fs.readlink(*i1, &buf1)); + EXPECT_EQ(0, fs.readlink(*i2, &buf2)); + +#if defined(_WIN32) + EXPECT_EQ("subdir\\test.txt", buf1); + EXPECT_EQ("..\\subdir\\test.txt", buf2); +#else + EXPECT_EQ("subdir/test.txt", buf1); + EXPECT_EQ("../subdir/test.txt", buf2); +#endif + } + + { + std::string buffer; + EXPECT_EQ(0, fs.readlink(*i1, &buffer, readlink_mode::raw)); + EXPECT_EQ("subdir\\test.txt", buffer); + EXPECT_EQ(0, fs.readlink(*i2, &buffer, readlink_mode::raw)); + EXPECT_EQ("..\\subdir\\test.txt", buffer); + } + + { + std::string buffer; + EXPECT_EQ(0, fs.readlink(*i1, &buffer, readlink_mode::unix)); + EXPECT_EQ("subdir/test.txt", buffer); + EXPECT_EQ(0, fs.readlink(*i2, &buffer, readlink_mode::unix)); + EXPECT_EQ("../subdir/test.txt", buffer); + } + + // test error case + { + std::string buffer; + EXPECT_EQ(-EINVAL, fs.readlink(*i3, &buffer)); + } + + // also test expected interface + { + auto r = fs.readlink(*i1, readlink_mode::unix); + EXPECT_TRUE(r); + EXPECT_EQ("subdir/test.txt", *r); + } + + { + auto r = fs.readlink(*i3); + EXPECT_FALSE(r); + EXPECT_EQ(-EINVAL, r.error()); + } +} + +TEST(filesystem, metadata_symlink_unix) { + test::test_logger lgr; + + auto mm = std::make_shared(test_dir / "unixlink.dwarfs"); + filesystem_v2 fs(lgr, mm); + + auto i1 = fs.find("link.txt"); + auto i2 = fs.find("dir/link.txt"); + auto i3 = fs.find("subdir/test.txt"); + + ASSERT_TRUE(i1); + ASSERT_TRUE(i2); + ASSERT_TRUE(i3); + + EXPECT_TRUE(i1->is_symlink()); + EXPECT_TRUE(i2->is_symlink()); + EXPECT_TRUE(i3->is_regular_file()); + + // readlink_mode::preferred (default) + { + std::string buf1, buf2; + EXPECT_EQ(0, fs.readlink(*i1, &buf1)); + EXPECT_EQ(0, fs.readlink(*i2, &buf2)); + +#if defined(_WIN32) + EXPECT_EQ("subdir\\test.txt", buf1); + EXPECT_EQ("..\\subdir\\test.txt", buf2); +#else + EXPECT_EQ("subdir/test.txt", buf1); + EXPECT_EQ("../subdir/test.txt", buf2); +#endif + } + + { + std::string buffer; + EXPECT_EQ(0, fs.readlink(*i1, &buffer, readlink_mode::raw)); + EXPECT_EQ("subdir/test.txt", buffer); + EXPECT_EQ(0, fs.readlink(*i2, &buffer, readlink_mode::raw)); + EXPECT_EQ("../subdir/test.txt", buffer); + } + + { + std::string buffer; + EXPECT_EQ(0, fs.readlink(*i1, &buffer, readlink_mode::unix)); + EXPECT_EQ("subdir/test.txt", buffer); + EXPECT_EQ(0, fs.readlink(*i2, &buffer, readlink_mode::unix)); + EXPECT_EQ("../subdir/test.txt", buffer); + } + + // test error case + { + std::string buffer; + EXPECT_EQ(-EINVAL, fs.readlink(*i3, &buffer)); + } + + // also test expected interface + { + auto r = fs.readlink(*i1, readlink_mode::unix); + EXPECT_TRUE(r); + EXPECT_EQ("subdir/test.txt", *r); + } + + { + auto r = fs.readlink(*i3); + EXPECT_FALSE(r); + EXPECT_EQ(-EINVAL, r.error()); + } +} diff --git a/test/unixlink.dwarfs b/test/unixlink.dwarfs new file mode 100644 index 0000000000000000000000000000000000000000..2a208f4e2e953e0d2592c2a83f3247b824d91b6a GIT binary patch literal 1334 zcmZuxZAepL6h8Oe-Bs4Sj&WMci7ivJq1QFm5}7({naC6|q9~;NEYT^>mD;C4AW{A3 z3yQK&q6mMP)sKDz1xZB(K~&J6q%R0Di0DUIo%elbj_ShQeeQFf_c`Z1=e-NBtEjJB z!^}hB2aD?$=63kbF*bb|ul-n5*D?9EXaAQ?pI`M@{Oagv-^j}7#l4WY%!y~bwKr}F zCrozVn7%S~_h@>>qnkrxYdm|NoSHaRu`6x7WXaWnadpaLq=6ExIh(2`KAGioal zwuIVDYHp&TMXGgS#Z7I5RFTh4$Sx2Pa=2JTMH|U=w4;ej9YVnK>p1I(($PslnNBCQ zT&al?X=MflQCMdZ(nVEz(jGFTN0tckRPO)EvZO4Vf^OlKL+Ib6*;1BA!7?GsC*%>x zLx{N)+5&GLB`P37rL9UrMGsX)v_Z5*(7Xe@d7=YeqV0+5XFFg>D<1_h$;vCO{6sqi ztqUt1=kN>xqBD}zeyJ`dI+R4`NxI~Z3GV@o64SUzkZ7COSQ#OQs4SQ?8<3TSC};@N z5+XD#!+Cu53ZlJ9xk55mnCMJF2NMOSb9E)rn8+|YV7?~`R{gOGiwu==+SNq+k}_1u z3=yLBB$^)uGO3*##Fr+QgNBhRGFz!t8al`;6`fcJR{AZa^O%V>U_&=<1Uu$nJ2){9 z2Otac@sma_fy{p5B_s4s;HE|XFd-1o;BPSKZD3WB+ N4ExvXAo3lE>o@M^&oBT0 literal 0 HcmV?d00001 diff --git a/test/winlink.dwarfs b/test/winlink.dwarfs new file mode 100644 index 0000000000000000000000000000000000000000..d753cd4b6ca71695b0e49c2c5bde2186aba09a3a GIT binary patch literal 1267 zcmZvcUr1AN6vxl~ZQjP5Yp&xOPUIpgw)8fq%|D5$ERmcDl?o!&HZ@H*V_T_2WDk;9 zZ$6|+kp%V9gFzvcB?w{~6%@XRNDqNV&|5)4(7C_grX##??>+Z(zQ6A|zu)iP>usw& zRezeL%>JF2m`@#BENh(`mo}~q^_iOG0~0Lpdvf{@oIT_8y;yk;!PPGiKjip5))kGV z8NG3r`P$g@^{=k}?s1=YCeyH9Kl^rhc3}P2Md#9?<70RBSN&+W4yKeuB^tet&S9Of zIl(06$n!x4pwR(#07enxk)9%%M>3BjpNx70O=KWoa_D>v0VU8Q-9biKB#m*kKA|=d zu*B6Vq}EJiGzztul6qdnLXnwAR*I|+=F_8@L}s+0POFN0`dEdJjglrl+P)*R(Kz8D z^A^b?lakCKk+s${fh++RNl=I`{4RltZX9Lc!YW*1;6^Y0WKfFr_*e%XQqGAtxd9R%E^8@% zu+9RJ>YvwDoZM z=1dpB_T1Hqg;9I^n-6)n?f36GWPe)0(@!hwO~6>FHZ&8uG2&fQ`+EbOgZ@Z&RJr0( z%T#yKQFlqvh2o0tGEYUIG#D)E?2W{Rm4UDli1@?79wpQ>bYyTK(nq|%H{7SjhGNmn d0Zm1}9_{J;lSxTcI|S@V(ElDsP-~qZ{{TXo#MA%) literal 0 HcmV?d00001