PolicyKit: Branch 'master'

David Zeuthen david at kemper.freedesktop.org
Sat Nov 3 12:56:05 PDT 2007


 polkit/polkit-error.c |   86 ++++++++++++++++++++++++++++++--------------------
 polkit/polkit-error.h |    8 +++-
 polkit/polkit-test.c  |    5 ++
 polkit/polkit-test.h  |    6 +--
 4 files changed, 65 insertions(+), 40 deletions(-)

New commits:
commit a82fa6258e196580f0e535bfeaaf76e2d4c5bc91
Author: David Zeuthen <davidz at redhat.com>
Date:   Sat Nov 3 15:53:41 2007 -0400

    add tests for PolKitError

diff --git a/polkit/polkit-error.c b/polkit/polkit-error.c
index e14111a..c70c698 100644
--- a/polkit/polkit-error.c
+++ b/polkit/polkit-error.c
@@ -49,6 +49,7 @@
 #include "polkit-types.h"
 #include "polkit-error.h"
 #include "polkit-debug.h"
+#include "polkit-test.h"
 
 /**
  * PolKitError:
@@ -78,6 +79,17 @@ polkit_error_is_set (PolKitError *error)
         return error != NULL;
 }
 
+static const char *error_names[POLKIT_ERROR_NUM_ERROR_CODES] = {
+        "OutOfMemory",
+        "PolicyFileInvalid",
+        "GeneralError",
+        "NotAuthorizedToReadAuthorizationsForOtherUsers",
+        "NotAuthorizedToRevokeAuthorizationsFromOtherUsers",
+        "NotAuthorizedToGrantAuthorization",
+        "AuthorizationAlreadyExists",
+        "NotSupported"
+};
+
 /**
  * polkit_error_get_error_name:
  * @error: the error
@@ -92,39 +104,10 @@ polkit_error_is_set (PolKitError *error)
 const char *
 polkit_error_get_error_name (PolKitError *error)
 {
-        const char *ret;
         g_return_val_if_fail (error != NULL, NULL);
+        g_return_val_if_fail (error->error_code >= 0 && error->error_code < POLKIT_ERROR_NUM_ERROR_CODES, NULL);
 
-        switch (error->error_code) {
-        case POLKIT_ERROR_OUT_OF_MEMORY:
-                ret = "OutOfMemory";
-                break;
-        case POLKIT_ERROR_POLICY_FILE_INVALID:
-                ret = "PolicyFileInvalid";
-                break;
-        case POLKIT_ERROR_GENERAL_ERROR:
-                ret = "GeneralError";
-                break;
-        case POLKIT_ERROR_NOT_AUTHORIZED_TO_READ_AUTHORIZATIONS_FOR_OTHER_USERS:
-                ret = "NotAuthorizedToReadAuthorizationsForOtherUsers";
-                break;
-        case POLKIT_ERROR_NOT_AUTHORIZED_TO_REVOKE_AUTHORIZATIONS_FROM_OTHER_USERS:
-                ret = "NotAuthorizedToRevokeAuthorizationsFromOtherUsers";
-                break;
-        case POLKIT_ERROR_NOT_AUTHORIZED_TO_GRANT_AUTHORIZATION:
-                ret = "NotAuthorizedToGrantAuthorization";
-                break;
-        case POLKIT_ERROR_AUTHORIZATION_ALREADY_EXISTS:
-                ret = "AuthorizationAlreadyExists";
-                break;
-        case POLKIT_ERROR_NOT_SUPPORTED:
-                ret = "NotSupported";
-                break;
-        default:
-                ret = NULL;
-        }
-
-        return ret;
+        return error_names[error->error_code];
 }
 
 /**
@@ -184,15 +167,17 @@ polkit_error_free (PolKitError *error)
  * @Varargs: printf style arguments
  * 
  * Sets an error. If OOM, the error will be set to a pre-allocated OOM error.
+ *
+ * Returns: TRUE if the error was set
  **/
-void
+polkit_bool_t
 polkit_error_set_error (PolKitError **error, PolKitErrorCode error_code, const char *format, ...)
 {
         va_list args;
         PolKitError *e;
 
-        if (error == NULL)
-                return;
+        g_return_val_if_fail (error != NULL, FALSE);
+        g_return_val_if_fail (error_code >= 0 && error_code < POLKIT_ERROR_NUM_ERROR_CODES, FALSE);
 
         e = g_new0 (PolKitError, 1);
         e->is_static = FALSE;
@@ -202,4 +187,37 @@ polkit_error_set_error (PolKitError **error, PolKitErrorCode error_code, const c
         va_end (args);
 
         *error = e;
+
+        return TRUE;
 }
+
+#ifdef POLKIT_BUILD_TESTS
+
+polkit_bool_t
+_test_polkit_error (void)
+{
+        unsigned int n;
+        PolKitError *e;
+        char s[256];
+
+        e = NULL;
+        g_assert (! polkit_error_is_set (e));
+        g_assert (! polkit_error_set_error (&e, -1, "Testing"));
+        g_assert (! polkit_error_set_error (&e, POLKIT_ERROR_NUM_ERROR_CODES, "Testing"));
+
+        for (n = 0; n < POLKIT_ERROR_NUM_ERROR_CODES; n++) {
+                polkit_error_set_error (&e, n, "Testing error code %d", n);
+                g_assert (polkit_error_is_set (e));
+                g_assert (polkit_error_get_error_code (e) == n);
+                g_assert (strcmp (polkit_error_get_error_name (e), error_names[n]) == 0);
+
+                snprintf (s, sizeof (s), "Testing error code %d", n);
+                g_assert (strcmp (polkit_error_get_error_message (e), s) == 0);
+
+                polkit_error_free (e);
+        }
+
+        return TRUE;
+}
+
+#endif /* POLKIT_BUILD_TESTS */
diff --git a/polkit/polkit-error.h b/polkit/polkit-error.h
index 7a29617..472d670 100644
--- a/polkit/polkit-error.h
+++ b/polkit/polkit-error.h
@@ -54,6 +54,8 @@ POLKIT_BEGIN_DECLS
  * similar authorization already (modulo time of grant and who granted).
  * @POLKIT_ERROR_NOT_SUPPORTED: The operation is not supported by the
  * authorization database backend
+ * @POLKIT_ERROR_NUM_ERROR_CODES: Number of error codes. This may change
+ * from version to version; do not rely on it.
  *
  * Errors returned by PolicyKit
  */
@@ -66,7 +68,9 @@ typedef enum
         POLKIT_ERROR_NOT_AUTHORIZED_TO_REVOKE_AUTHORIZATIONS_FROM_OTHER_USERS,
         POLKIT_ERROR_NOT_AUTHORIZED_TO_GRANT_AUTHORIZATION,
         POLKIT_ERROR_AUTHORIZATION_ALREADY_EXISTS,
-        POLKIT_ERROR_NOT_SUPPORTED
+        POLKIT_ERROR_NOT_SUPPORTED,
+
+        POLKIT_ERROR_NUM_ERROR_CODES
 } PolKitErrorCode;
 
 struct _PolKitError;
@@ -77,7 +81,7 @@ const char      *polkit_error_get_error_name (PolKitError *error);
 PolKitErrorCode  polkit_error_get_error_code (PolKitError *error);
 const char      *polkit_error_get_error_message (PolKitError *error);
 void             polkit_error_free (PolKitError *error);
-void             polkit_error_set_error (PolKitError **error, PolKitErrorCode error_code, const char *format, ...) __attribute__((__format__ (__printf__, 3, 4)));
+polkit_bool_t    polkit_error_set_error (PolKitError **error, PolKitErrorCode error_code, const char *format, ...) __attribute__((__format__ (__printf__, 3, 4)));
 
 POLKIT_END_DECLS
 
diff --git a/polkit/polkit-test.c b/polkit/polkit-test.c
index a5aca18..fbd7a3e 100644
--- a/polkit/polkit-test.c
+++ b/polkit/polkit-test.c
@@ -23,6 +23,8 @@
  *
  **************************************************************************/
 
+#include <stdio.h>
+#include <stdlib.h>
 #include <polkit/polkit-test.h>
 
 int 
@@ -36,6 +38,9 @@ main (int argc, char *argv[])
         if (!_test_polkit_action ())
                 goto out;
 
+        if (!_test_polkit_error ())
+                goto out;
+
         ret = 0;
 out:
 
diff --git a/polkit/polkit-test.h b/polkit/polkit-test.h
index c9ec6a3..5ca3e54 100644
--- a/polkit/polkit-test.h
+++ b/polkit/polkit-test.h
@@ -34,11 +34,9 @@
 
 POLKIT_BEGIN_DECLS
 
-
-#endif /* POLKIT_BUILD_TESTS */
-
-#ifdef POLKIT_BUILD_TESTS
 polkit_bool_t _test_polkit_action (void);
+polkit_bool_t _test_polkit_error (void);
+
 POLKIT_END_DECLS
 
 #endif /* POLKIT_TEST_H */


More information about the hal-commit mailing list