BUG#: 6512
authorkumpf <kumpf>
Fri, 8 Jun 2007 17:56:02 +0000 (17:56 +0000)
committerkumpf <kumpf>
Fri, 8 Jun 2007 17:56:02 +0000 (17:56 +0000)
TITLE: Add unit tests for Privilege Separation
DESCRIPTION: Add Executor unit test cases.

src/Executor/LocalAuth.c
src/Executor/Policy.c
src/Executor/Policy.h
src/Executor/Strlcat.h
src/Executor/tests/Makefile
src/Executor/tests/Policy/Makefile [new file with mode: 0644]
src/Executor/tests/Policy/TestExecutorPolicy.c [new file with mode: 0644]
src/Executor/tests/Strlcat/TestStrlcat.c
src/Executor/tests/Strlcpy/TestStrlcpy.c
src/Executor/tests/User/TestExecutorUser.c
src/Pegasus/ProviderManagerService/OOPProviderManagerRouter.cpp

index 60aa8851f4aab12c3ef8a20fd20f599e7d87f99a..093f73a2888a0deb547369341a68b3be38b15189 100644 (file)
@@ -261,8 +261,9 @@ int FinishLocalAuthentication(
 
     int rc = CheckLocalAuthToken(challengeFilePath, response);
 
-    if (challengeFilePath)
-        unlink((char*)challengeFilePath);
+    /* Clean up the file now that the authentication is complete. */
+
+    unlink(challengeFilePath);
 
     return rc;
 }
index 5dbf194ed99ab6f2b11e8e340d544b38519e54a0..297e821a6c2f7e58c866e9afa8f9f788620898ac 100644 (file)
 
 #define ARG(X) #X, X
 
-/*
-**==============================================================================
-**
-** Policy
-**
-**     This structure defines a policy rule.
-**
-**==============================================================================
-*/
-
-struct Policy
-{
-    enum ExecutorMessageCode messageCode;
-    const char* arg1;
-    const char* arg2;
-};
-
 /*
 **==============================================================================
 **
@@ -206,7 +189,7 @@ static const size_t _staticPolicyTableSize =
 **==============================================================================
 */
 
-static int CheckPolicy(
+int CheckPolicy(
     const struct Policy* policyTable,
     size_t policyTableSize,
     enum ExecutorMessageCode messageCode,
index d1fed3f79d585f96ef1ef5ff3e9b1a29948125a3..39454963a125e7dca038afeaff8b9b4ddb4371d2 100644 (file)
 #ifndef _Executor_Policy_h
 #define _Executor_Policy_h
 
+#include <stdlib.h>
+#include "Defines.h"
+#include "Messages.h"
+
+/*
+**==============================================================================
+**
+** Policy
+**
+**     This structure defines a policy rule.
+**
+**==============================================================================
+*/
+
+struct Policy
+{
+    enum ExecutorMessageCode messageCode;
+    const char* arg1;
+    const char* arg2;
+};
+
+EXECUTOR_LINKAGE
+int CheckPolicy(
+    const struct Policy* policyTable,
+    size_t policyTableSize,
+    enum ExecutorMessageCode messageCode,
+    const char* arg1,
+    const char* arg2);
+
+EXECUTOR_LINKAGE
 int CheckOpenFilePolicy(const char* path, int mode);
 
+EXECUTOR_LINKAGE
 int CheckRemoveFilePolicy(const char* path);
 
+EXECUTOR_LINKAGE
 int CheckRenameFilePolicy(const char* oldPath, const char* newPath);
 
+EXECUTOR_LINKAGE
 void DumpPolicy(int expandMacros);
 
 #endif /* _Executor_Policy_h */
index 8ba149b708293283093dea44892ca3291eaea417..8faece8d562d4bd2b766f702f5284f969cf09a25 100644 (file)
@@ -65,17 +65,26 @@ static size_t Strlcat(char* dest, const char* src, size_t size)
     /* If no-null terminator found, return size. */
 
     if (i == size)
-        return size;
+    {
+        int j = 0;
+        while (src[j])
+        {
+            j++;
+        }
+        return size + j;
+    }
 
     /* Copy src characters to dest. */
 
     for (j = 0; src[j] && i + 1 < size; i++, j++)
         dest[i] = src[j];
 
-    /* Null terminate size non-zero. */
+    /* Null terminate the destination.  We are guaranteed that size is
+     * non-zero, because the (i == size) condition above is always true
+     * when size is zero.
+     */
 
-    if (size > 0)
-        dest[i] = '\0';
+    dest[i] = '\0';
 
     while (src[j])
     {
index ca1e05f95484359c0ff8c003ce7825298392a3c1..21492284befa8d340323a8064a9ea3690bb75d9d 100644 (file)
@@ -41,6 +41,7 @@ DIRS = \
     Options \
     PasswordFile \
     Path \
+    Policy \
     Process \
     Random \
     Socket \
diff --git a/src/Executor/tests/Policy/Makefile b/src/Executor/tests/Policy/Makefile
new file mode 100644 (file)
index 0000000..eecba32
--- /dev/null
@@ -0,0 +1,49 @@
+#//%2006////////////////////////////////////////////////////////////////////////
+#//
+#// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
+#// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
+#// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
+#// IBM Corp.; EMC Corporation, The Open Group.
+#// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
+#// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
+#// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
+#// EMC Corporation; VERITAS Software Corporation; The Open Group.
+#// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
+#// EMC Corporation; Symantec Corporation; The Open Group.
+#//
+#// Permission is hereby granted, free of charge, to any person obtaining a copy
+#// of this software and associated documentation files (the "Software"), to
+#// deal in the Software without restriction, including without limitation the
+#// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+#// sell copies of the Software, and to permit persons to whom the Software is
+#// furnished to do so, subject to the following conditions:
+#//
+#// THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
+#// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
+#// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
+#// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+#// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+#// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+#// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+#// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#//
+#//=============================================================================
+
+ROOT = $(PEGASUS_ROOT)
+DIR = Executor/tests/Policy
+
+include $(ROOT)/mak/config.mak
+
+PROGRAM = TestExecutorPolicy
+SOURCES = TestExecutorPolicy.c
+EXTRA_INCLUDES += -I../../..
+
+LIBRARIES += pegexecutor
+
+include ../common.mak
+include $(ROOT)/mak/program.mak
+
+tests:
+       $(PROGRAM)
+
+poststarttests:
diff --git a/src/Executor/tests/Policy/TestExecutorPolicy.c b/src/Executor/tests/Policy/TestExecutorPolicy.c
new file mode 100644 (file)
index 0000000..1b16600
--- /dev/null
@@ -0,0 +1,142 @@
+/*
+//%2006////////////////////////////////////////////////////////////////////////
+//
+// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
+// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
+// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
+// IBM Corp.; EMC Corporation, The Open Group.
+// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
+// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
+// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
+// EMC Corporation; VERITAS Software Corporation; The Open Group.
+// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
+// EMC Corporation; Symantec Corporation; The Open Group.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to
+// deal in the Software without restriction, including without limitation the
+// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+// sell copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
+// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
+// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
+// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+//==============================================================================
+//
+//%/////////////////////////////////////////////////////////////////////////////
+*/
+
+#include <Executor/Policy.h>
+#include <Executor/Macro.h>
+#include <stdio.h>
+#include <assert.h>
+
+static struct Policy _testPolicyTable[] =
+{
+    {
+        EXECUTOR_PING_MESSAGE,
+        NULL,
+        NULL
+    },
+    {
+        EXECUTOR_RENAME_FILE_MESSAGE,
+        "${file1}",
+        "${file2}"
+    },
+    {
+        EXECUTOR_RENAME_FILE_MESSAGE,
+        "file1",
+        "${file2}"
+    },
+    {
+        EXECUTOR_RENAME_FILE_MESSAGE,
+        "file1",
+        "file2"
+    }
+};
+
+static const size_t _testPolicyTableSize =
+    sizeof(_testPolicyTable) / sizeof(_testPolicyTable[0]);
+
+void testCheckPolicy()
+{
+    /* Test non-existent policy */
+    assert(CheckPolicy(
+        _testPolicyTable,
+        _testPolicyTableSize,
+        EXECUTOR_REAP_PROVIDER_AGENT,
+        NULL,
+        NULL) != 0);
+
+    /* Test policy with no arguments */
+    assert(CheckPolicy(
+        _testPolicyTable,
+        _testPolicyTableSize,
+        EXECUTOR_PING_MESSAGE,
+        NULL,
+        NULL) == 0);
+
+    /* Test policies with invalid macro expansion in first argument and
+     * non-match in first argument
+     */
+    assert(CheckPolicy(
+        _testPolicyTable,
+        _testPolicyTableSize,
+        EXECUTOR_RENAME_FILE_MESSAGE,
+        "MyFile",
+        "file2") != 0);
+
+    /* Test policies with invalid macro expansion in second argument and
+     * non-match in second argument
+     */
+    assert(CheckPolicy(
+        _testPolicyTable,
+        _testPolicyTableSize,
+        EXECUTOR_RENAME_FILE_MESSAGE,
+        "file1",
+        "MyFile") != 0);
+
+    /* Test policy with successful match in both arguments */
+    assert(CheckPolicy(
+        _testPolicyTable,
+        _testPolicyTableSize,
+        EXECUTOR_RENAME_FILE_MESSAGE,
+        "file1",
+        "file2") == 0);
+}
+
+void testFilePolicies()
+{
+    const char* currentConfigFile = "MyConfigFile";
+    const char* currentConfigFileBak = "MyConfigFile.bak";
+    const char* noAccessFile = "NoAccessFile";
+
+    /* Define a macro used in the static policy table */
+    DefineMacro("currentConfigFilePath", currentConfigFile);
+
+    assert(CheckOpenFilePolicy(currentConfigFile, 'w') == 0);
+    assert(CheckOpenFilePolicy(noAccessFile, 'w') != 0);
+
+    assert(CheckRemoveFilePolicy(currentConfigFile) == 0);
+    assert(CheckRemoveFilePolicy(noAccessFile) != 0);
+
+    assert(CheckRenameFilePolicy(currentConfigFile, currentConfigFileBak) == 0);
+    assert(CheckRenameFilePolicy(currentConfigFile, noAccessFile) != 0);
+}
+
+int main()
+{
+    testCheckPolicy();
+    testFilePolicies();
+
+    printf("+++++ passed all tests\n");
+
+    return 0;
+}
index 785490d2d82a652b33c93ed63a9abb48510d7850..dea25dc5dd3cda7d7493452a5441f2b963168168 100644 (file)
@@ -73,7 +73,7 @@ int main()
         memset(buf, 'X', sizeof(buf));
 
         n = Strlcat(buf, "abc", sizeof(buf));
-        assert(n == sizeof(buf));
+        assert(n == sizeof(buf) + 3);
         assert(memcmp(buf, "XXXXXXXX", 8) == 0);
     }
 
@@ -92,6 +92,17 @@ int main()
         assert(strcmp(buf, "1234567") == 0);
     }
 
+    {
+        char buf[8];
+        size_t n;
+        memset(buf, 'X', sizeof(buf));
+
+        *buf = '\0';
+        n = Strlcat(buf, "1234", 0);
+        assert(n == 4);
+        assert(strlen(buf) == 0);
+    }
+
     printf("+++++ passed all tests\n");
 
     return 0;
index 02a78844467497c7fcd8fb397fccaa18148a14cf..623687103778453bd9d0546ce6e75cd7491597bf 100644 (file)
@@ -100,6 +100,17 @@ int main()
         assert(strcmp(buffer, "1") == 0);
     }
 
+    {
+        char buffer[1024];
+        size_t n;
+        strcpy(buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
+
+        n = Strlcpy(buffer, "1234567890", 0);
+        assert(n == 10);
+        assert(strcmp(
+            buffer, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") == 0);
+    }
+
     printf("+++++ passed all tests\n");
 
     return 0;
index 6fc625a6dd67cf2b118fe9875ba2a53901594686..c1bffb507e7899bc38b2ad4a17c0edd33b117314 100644 (file)
@@ -50,6 +50,11 @@ int main()
     assert(GetUserName(uid, username) == 0);
     assert(strcmp(username, "root") == 0);
 
+    assert(GetUserInfo("xinvaliduserx", &uid, &gid) != 0);
+
+    assert(GetUserName(-1492, username) != 0);
+    assert(strlen(username) == 0);
+
     printf("+++++ passed all tests\n");
     return 0;
 }
index aabe29c575a6d03791e3c587e0b47c5c8390e62b..8bdfeda25f8ece51c79368bec3ff89486f1b7bb8 100644 (file)
@@ -416,10 +416,6 @@ void ProviderAgentContainer::_startAgentProcess()
             _moduleName));
     }
 
-    // Set the session key to be used for requests emanating from this read
-    // pipe (i.e., the provider agent). Examples include requests made by the
-    // provider with the CIMOMHandle or indications delivered by the provider.
-
 # if defined(PEGASUS_HAS_SIGNALS)
     _pid = pid;
 # endif