libi2ncommon: (tomj) fix i2n::User and i2n::Group usage of uid/gid -1 (-1 is reserved...
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Thu, 4 Dec 2008 14:14:35 +0000 (14:14 +0000)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Thu, 4 Dec 2008 14:14:35 +0000 (14:14 +0000)
src/userfunc.cpp
test/test_filefunc.cpp

index 69ac1f9..9b6196e 100644 (file)
@@ -82,8 +82,8 @@ bool copy_data ( struct group* from , Group& to)
 */
 
 User::User()
-: Uid (-1)
-, Gid (-1)
+: Uid ((uid_t)-1)
+, Gid ((gid_t)-1)
 {
 } // User::User()
 
@@ -127,9 +127,8 @@ void User::clear()
 {
    Name.clear();
    Password.clear();
-    // FIXME: Uid and Gid is unsigned
-   Uid= -1;
-   Gid= -1;
+   Uid= (uid_t)-1;
+   Gid= (gid_t)-1;
    Gecos.clear();
    Homedir.clear();
    Shell.clear();
@@ -145,8 +144,7 @@ void User::clear()
  */
 bool User::is_valid() const
 {
-    // FIXME: Uid and Gid is unsigned, so the first expression is always true!
-   return (Uid >= 0) and (Gid >= 0)
+   return (Uid != (uid_t)-1) and (Gid != (gid_t)-1)
           and not Name.empty()
           ;
 } // eo User::is_valid() const
@@ -158,7 +156,7 @@ bool User::is_valid() const
 
 
 Group::Group()
-: Gid (-1)  // FIXME: Gid is unsigned
+: Gid ((gid_t)-1)
 {
 } // eo Group::Group()
 
@@ -201,8 +199,7 @@ void Group::clear()
 {
    Name.clear();
    Password.clear();
-    // FIXME: Gid is unsigned
-   Gid= -1;
+   Gid= (gid_t)-1;
    Members.clear();
 } // eo Group::clear()
 
@@ -216,8 +213,7 @@ void Group::clear()
  */
 bool Group::is_valid() const
 {
-    // FIXME: Gid is unsigned, so the first expression is always true!
-   return (Gid >= 0) and not Name.empty();
+   return (Gid != (gid_t)-1) and not Name.empty();
 } // eo Group::is_valid() const
 
 /**
index 0acdca3..92cc923 100644 (file)
@@ -53,6 +53,7 @@ class TestFileFunc : public TestFixture
    CPPUNIT_TEST(NormalizePath1);
    CPPUNIT_TEST(NormalizePath2);
    CPPUNIT_TEST(TestUserAndGroupStuff1);
+   CPPUNIT_TEST(TestUserAndGroupStuff2);
    CPPUNIT_TEST(TestFileModes1);
    CPPUNIT_TEST(TestPidOf1);
    CPPUNIT_TEST(TestCopyFileSourceFail);
@@ -254,7 +255,15 @@ public:
 
    } // TestUserAndGroupStuff1()
 
+   void TestUserAndGroupStuff2()
+   {
+      // Test if is_valid() works correctly
+      User user_root;
+      CPPUNIT_ASSERT_EQUAL( false, user_root.is_valid() );
 
+      Group group_root;
+      CPPUNIT_ASSERT_EQUAL( false, group_root.is_valid() );
+   }
 
    void TestFileModes1()
    {