From 9c76c7818304be90bf0d7cdc1e31d9a049b5c371 Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Tue, 30 Dec 2025 14:44:21 +0100 Subject: [PATCH] test: Add unit tests for chown() with User and Group parameters Test the sentinel value handling for uid_t and gid_t types that was fixed in the previous commit. Added tests: - TestChownWithInvalidUid: Verify chown fails when User has invalid uid - TestChownWithValidUidAndInvalidGid: Verify chown succeeds when gid falls back to valid user.Gid - TestChownWithUserAndGroupObjects: Verify chown works with valid User and Group objects --- test/test_filefunc.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 61 insertions(+), 0 deletions(-) diff --git a/test/test_filefunc.cpp b/test/test_filefunc.cpp index 583de35..6ad516d 100644 --- a/test/test_filefunc.cpp +++ b/test/test_filefunc.cpp @@ -35,6 +35,7 @@ on this file might be covered by the GNU General Public License. #include #include #include +#include #define BOOST_TEST_DYN_LINK #include @@ -302,6 +303,66 @@ BOOST_AUTO_TEST_CASE(TestFileModes1) BOOST_CHECK_EQUAL( true, res ); } // eo TestFileModes1() +BOOST_AUTO_TEST_CASE(TestChownWithInvalidUid) +{ + std::string path= get_check_file_path("ChownInvalidUid"); + + write_file(path,"42"); + + // Create User with invalid uid (equals sentinel value) + User invalid_user; + BOOST_CHECK_EQUAL( false, invalid_user.is_valid() ); + + // Create valid Group + Group group((gid_t)0); + + // Should fail because uid is invalid + bool res = chown(path, invalid_user, group); + BOOST_CHECK_EQUAL( false, res ); +} // eo TestChownWithInvalidUid() + +BOOST_AUTO_TEST_CASE(TestChownWithValidUidAndInvalidGid) +{ + std::string path= get_check_file_path("ChownValidUidInvalidGid"); + + write_file(path,"42"); + + // Get valid user from current process (use getuid() to avoid requiring root) + User valid_user(static_cast(getuid())); + + // Create Group with invalid gid (equals sentinel value) + Group invalid_group; + BOOST_CHECK_EQUAL( false, invalid_group.is_valid() ); + + // Should succeed because gid is invalid but falls back to user.Gid which is valid + bool res = chown(path, valid_user, invalid_group); + BOOST_CHECK_EQUAL( true, res ); +} // eo TestChownWithValidUidAndInvalidGid() + +BOOST_AUTO_TEST_CASE(TestChownWithUserAndGroupObjects) +{ + // This test requires root privileges to change ownership + // Skip if not running as root + if (getuid() != 0) + { + BOOST_TEST_MESSAGE("Skipping TestChownWithUserAndGroupObjects - not running as root"); + return; + } + + std::string path= get_check_file_path("ChownUserGroup"); + + write_file(path,"42"); + + User user_root((uid_t)0); + Group group_root((gid_t)0); + + // Both uid and gid are valid + bool res = chown(path, user_root, group_root); + + // Should succeed + BOOST_CHECK_EQUAL( true, res ); +} // eo TestChownWithUserAndGroupObjects() + BOOST_AUTO_TEST_CASE(TestPidOf1) -- 1.7.1