test: run test_clonepath_modes where gid 1 is unmapped, and clean up

Root in a user namespace may not have gid 1 mapped, so the chown to it
failed and the test aborted. Use a distinct group only when gid 1 is
available and differs from the caller's, and only check setgid group
inheritance then; permissions, ownership and existing destinations
are still checked on single-group hosts. TEST_ASSERT aborts without
unwinding and left the fixture in /tmp, so the body checks and
returns and the caller always removes it.
This commit is contained in:
Antonio SJ Musumeci 2026-11-29 23:10:56 -05:00
parent e47e82b3f9
commit 7e29c3f72a

View file

@ -4211,44 +4211,82 @@ test_rapidhash_withSeed_preserves_default_output()
}
}
static
void
_test_clonepath_modes(const std::filesystem::path &root_)
{
const auto src = root_ / "src";
const auto dst = root_ / "dst";
std::error_code ec;
std::filesystem::create_directories(src / "d" / "child",ec);
if(!TEST_CHECK(!ec))
return;
std::filesystem::create_directory(dst,ec);
if(!TEST_CHECK(!ec))
return;
// Namespace root may not have gid 1 mapped. Use a distinct group only
// when gid 1 is both available and different from the caller's group;
// otherwise this run tests caller-group preservation, not distinct-group
// inheritance.
const gid_t caller_group = ::getegid();
gid_t group = caller_group;
if((caller_group != 1) && (::geteuid() == 0) &&
(::chown((src / "d").c_str(),::geteuid(),1) == 0))
group = 1;
const bool distinct_group = (group != caller_group);
TEST_CASE(distinct_group ? "distinct group inheritance" : "caller group fallback");
if(!TEST_CHECK(::chown((src / "d").c_str(),::geteuid(),group) == 0))
return;
if(!TEST_CHECK(::chmod((src / "d").c_str(),02770) == 0))
return;
if(!TEST_CHECK(::chmod((src / "d" / "child").c_str(),0770) == 0))
return;
TEST_CHECK(fs::clonepath(src.string(),dst.string(),"d/child",true) == 0);
struct stat st;
if(!TEST_CHECK(::stat((dst / "d").c_str(),&st) == 0))
return;
TEST_CHECK((st.st_mode & 07777) == 02770);
TEST_CHECK(st.st_gid == group);
if(!TEST_CHECK(::stat((dst / "d" / "child").c_str(),&st) == 0))
return;
TEST_CHECK((st.st_mode & 07777) == 0770);
const auto file = dst / "d" / "file";
const int fd = ::open(file.c_str(),O_CREAT|O_EXCL|O_WRONLY,0600);
if(!TEST_CHECK(fd >= 0))
return;
const bool have_stat = TEST_CHECK(::fstat(fd,&st) == 0);
// With no distinct group this would also pass if setgid inheritance
// were broken, so only claim that check when the groups differ.
if(distinct_group && have_stat)
TEST_CHECK(st.st_gid == group);
::close(fd);
// Existing destination directories must remain untouched.
if(!TEST_CHECK(::chmod((dst / "d").c_str(),0700) == 0))
return;
TEST_CHECK(fs::clonepath(src.string(),dst.string(),"d",true) == 0);
if(!TEST_CHECK(::stat((dst / "d").c_str(),&st) == 0))
return;
TEST_CHECK((st.st_mode & 07777) == 0700);
}
void
test_clonepath_modes()
{
char tmp[] = "/tmp/mergerfs-clonepath-XXXXXX";
TEST_ASSERT(::mkdtemp(tmp) != NULL);
const std::filesystem::path root(tmp);
const auto src = root / "src";
const auto dst = root / "dst";
std::filesystem::create_directories(src / "d" / "child");
std::filesystem::create_directory(dst);
// Use a different group when privileged to verify actual inheritance.
const gid_t group = (::geteuid() == 0) ? 1 : ::getegid();
TEST_ASSERT(::chown((src / "d").c_str(),::geteuid(),group) == 0);
TEST_ASSERT(::chmod((src / "d").c_str(),02770) == 0);
TEST_ASSERT(::chmod((src / "d" / "child").c_str(),0770) == 0);
TEST_CHECK(fs::clonepath(src.string(),dst.string(),"d/child",true) == 0);
// TEST_ASSERT aborts the test without unwinding, so check-and-return
// in the body and always clean up here.
::_test_clonepath_modes(root);
struct stat st;
TEST_ASSERT(::stat((dst / "d").c_str(),&st) == 0);
TEST_CHECK((st.st_mode & 07777) == 02770);
TEST_CHECK(st.st_gid == group);
TEST_ASSERT(::stat((dst / "d" / "child").c_str(),&st) == 0);
TEST_CHECK((st.st_mode & 07777) == 0770);
const auto file = dst / "d" / "file";
const int fd = ::open(file.c_str(),O_CREAT|O_EXCL|O_WRONLY,0600);
TEST_ASSERT(fd >= 0);
TEST_ASSERT(::fstat(fd,&st) == 0);
TEST_CHECK(st.st_gid == group);
::close(fd);
// Existing destination directories must remain untouched.
TEST_ASSERT(::chmod((dst / "d").c_str(),0700) == 0);
TEST_CHECK(fs::clonepath(src.string(),dst.string(),"d",true) == 0);
TEST_ASSERT(::stat((dst / "d").c_str(),&st) == 0);
TEST_CHECK((st.st_mode & 07777) == 0700);
std::filesystem::remove_all(root);
std::error_code ec;
std::filesystem::remove_all(root,ec);
TEST_CHECK(!ec);
}
TEST_LIST =