#include <fstream>
#include <sstream>
#include <algorithm>
+#include <unistd.h>
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
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<uid_t>(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)