add unit tests for PolKitSeat
authorDavid Zeuthen <davidz@redhat.com>
Sat, 3 Nov 2007 21:50:35 +0000 (17:50 -0400)
committerDavid Zeuthen <davidz@redhat.com>
Sat, 3 Nov 2007 21:50:35 +0000 (17:50 -0400)
polkit/polkit-seat.c
polkit/polkit-test.c
polkit/polkit-test.h

index b6e3269..0056da9 100644 (file)
@@ -40,6 +40,8 @@
 #include "polkit-debug.h"
 #include "polkit-seat.h"
 #include "polkit-utils.h"
+#include "polkit-test.h"
+#include "polkit-memory.h"
 
 /**
  * SECTION:polkit-seat
@@ -72,8 +74,11 @@ PolKitSeat *
 polkit_seat_new (void)
 {
         PolKitSeat *seat;
-        seat = g_new0 (PolKitSeat, 1);
+        seat = p_new0 (PolKitSeat, 1);
+        if (seat == NULL)
+                goto out;
         seat->refcount = 1;
+out:
         return seat;
 }
 
@@ -108,8 +113,8 @@ polkit_seat_unref (PolKitSeat *seat)
         seat->refcount--;
         if (seat->refcount > 0) 
                 return;
-        g_free (seat->ck_objref);
-        g_free (seat);
+        p_free (seat->ck_objref);
+        p_free (seat);
 }
 
 /**
@@ -127,9 +132,12 @@ polkit_seat_set_ck_objref (PolKitSeat *seat, const char *ck_objref)
         g_return_val_if_fail (seat != NULL, FALSE);
         g_return_val_if_fail (_pk_validate_identifier (ck_objref), FALSE);
         if (seat->ck_objref != NULL)
-                g_free (seat->ck_objref);
-        seat->ck_objref = g_strdup (ck_objref);
-        return TRUE;
+                p_free (seat->ck_objref);
+        seat->ck_objref = p_strdup (ck_objref);
+        if (seat->ck_objref == NULL)
+                return FALSE;
+        else
+                return TRUE;
 }
 
 /**
@@ -178,3 +186,46 @@ polkit_seat_validate (PolKitSeat *seat)
         g_return_val_if_fail (seat->ck_objref != NULL, FALSE);
         return TRUE;
 }
+
+#ifdef POLKIT_BUILD_TESTS
+
+static polkit_bool_t
+_run_test (void)
+{
+        char *str;
+        PolKitSeat *s;
+
+        s = polkit_seat_new ();
+        if (s == NULL) {
+                /* OOM */
+        } else {
+                if (! polkit_seat_set_ck_objref (s, "/someseat")) {
+                        /* OOM */
+                } else {
+                        g_assert (polkit_seat_get_ck_objref (s, &str) && strcmp (str, "/someseat") == 0);
+                        g_assert (polkit_seat_validate (s));
+                        polkit_seat_ref (s);
+                        g_assert (polkit_seat_validate (s));
+                        polkit_seat_unref (s);
+                        g_assert (polkit_seat_validate (s));
+                        polkit_seat_debug (s);
+                        if (! polkit_seat_set_ck_objref (s, "/someseat2")) {
+                                /* OOM */
+                        } else {
+                                g_assert (polkit_seat_get_ck_objref (s, &str) && strcmp (str, "/someseat2") == 0);
+                        }
+                }
+                polkit_seat_unref (s);
+        }
+
+        return TRUE;
+}
+
+PolKitTest _test_seat = {
+        "polkit_seat",
+        NULL,
+        NULL,
+        _run_test
+};
+
+#endif /* POLKIT_BUILD_TESTS */
index ac85456..c2008ae 100644 (file)
@@ -34,6 +34,7 @@ static PolKitTest *tests[] = {
         &_test_action,
         &_test_error,
         &_test_result,
+        &_test_seat,
 };
 
 int 
index 2493451..59116cb 100644 (file)
@@ -47,6 +47,7 @@ typedef struct {
 extern PolKitTest _test_action;
 extern PolKitTest _test_error;
 extern PolKitTest _test_result;
+extern PolKitTest _test_seat;
 
 POLKIT_END_DECLS