[PolicyKit-gnome] Add test-case for fd.o bug 23867



commit ad5fe38a1f7a7a670c3d8e9384b9cd0d037c9222
Author: David Zeuthen <davidz redhat com>
Date:   Fri Sep 11 15:32:40 2009 -0400

    Add test-case for fd.o bug 23867
    
    https://bugs.freedesktop.org/show_bug.cgi?id=23867

 configure.ac               |    4 ++
 polkitgtk/Makefile.am      |    2 +
 polkitgtk/example.c        |  109 ++++++++++++++++++++++++++++++++++++++++++-
 polkitgtk/polkitgtktypes.h |    1 +
 4 files changed, 113 insertions(+), 3 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index f6c55b9..5e968e7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -134,6 +134,10 @@ PKG_CHECK_MODULES(POLKIT_GOBJECT, polkit-gobject-1 >= $POLKIT_GOBJECT_REQUIRED)
 AC_SUBST(POLKIT_GOBJECT_CFLAGS)
 AC_SUBST(POLKIT_GOBJECT_LIBS)
 
+PKG_CHECK_MODULES(DBUS_GLIB, dbus-glib-1)
+AC_SUBST(DBUS_GLIB_CFLAGS)
+AC_SUBST(DBUS_GLIB_LIBS)
+
 AC_ARG_ENABLE([examples],
               AC_HELP_STRING([--enable-examples], [Build the example programs]),,
               [enable_examples=yes])
diff --git a/polkitgtk/Makefile.am b/polkitgtk/Makefile.am
index d8f8d20..7b1cb8b 100644
--- a/polkitgtk/Makefile.am
+++ b/polkitgtk/Makefile.am
@@ -56,6 +56,7 @@ example_CFLAGS = 					\
 	-I$(top_srcdir)					\
 	-I$(top_builddir)				\
 	$(POLKIT_GOBJECT_CFLAGS)			\
+	$(DBUS_GLIB_CFLAGS)				\
 	$(GTK_CFLAGS)					\
 	$(WARN_CFLAGS)					\
 	$(AM_CFLAGS)					\
@@ -63,6 +64,7 @@ example_CFLAGS = 					\
 
 example_LDADD = 					\
 	$(POLKIT_GOBJECT_LIBS)				\
+	$(DBUS_GLIB_LIBS)				\
 	$(GTK_LIBS)					\
 	$(INTLLIBS)					\
 	libpolkit-gtk-1.la				\
diff --git a/polkitgtk/example.c b/polkitgtk/example.c
index 1cedbc5..c7aacb3 100644
--- a/polkitgtk/example.c
+++ b/polkitgtk/example.c
@@ -19,8 +19,73 @@
  * Author: David Zeuthen <davidz redhat com>
  */
 
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
 #include <polkitgtk/polkitgtk.h>
 
+static PolkitAuthority *authority = NULL;
+const gchar *action_id = NULL;
+static PolkitSubject *system_bus_name_subject = NULL;
+static PolkitSubject *unix_process_subject = NULL;
+static GtkWidget *system_bus_name_authorized_label = NULL;
+static GtkWidget *unix_process_authorized_label = NULL;
+
+static void
+update_one (PolkitSubject *subject,
+            GtkWidget     *label)
+{
+  PolkitAuthorizationResult *result;
+  GError *error;
+  GString *s;
+  gchar *subject_str;
+
+  s = g_string_new (NULL);
+  subject_str = polkit_subject_to_string (subject);
+  g_string_append_printf (s, "Result for subject `%s': ", subject_str);
+  g_free (subject_str);
+
+  error = NULL;
+  result = polkit_authority_check_authorization_sync (authority,
+                                                      subject,
+                                                      action_id,
+                                                      NULL,
+                                                      POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE,
+                                                      NULL,
+                                                      &error);
+  if (result == NULL)
+    {
+      g_string_append_printf (s,
+                              "failed: %s", error->message);
+      g_error_free (error);
+    }
+  else
+    {
+      g_string_append_printf (s,
+                              "authorized=%d challenge=%d retains=%d",
+                              polkit_authorization_result_get_is_authorized (result),
+                              polkit_authorization_result_get_is_challenge (result),
+                              polkit_authorization_result_get_retains_authorization (result));
+      g_object_unref (result);
+    }
+
+  gtk_label_set_text (GTK_LABEL (label), s->str);
+  g_string_free (s, TRUE);
+}
+
+static void
+update_labels (void)
+{
+  update_one (system_bus_name_subject, system_bus_name_authorized_label);
+  update_one (unix_process_subject, unix_process_authorized_label);
+}
+
+static void
+on_authority_changed (PolkitAuthority *authority,
+                      gpointer         user_data)
+{
+  update_labels ();
+}
+
 static void
 on_button_changed (PolkitLockButton *button,
                    gpointer          user_data)
@@ -34,11 +99,13 @@ on_button_changed (PolkitLockButton *button,
 int
 main (int argc, char *argv[])
 {
+  DBusGConnection *bus;
   GtkWidget *window;
   GtkWidget *label;
   GtkWidget *button;
   GtkWidget *entry;
   GtkWidget *vbox;
+  GError *error;
   gchar *s;
 
   gtk_init (&argc, &argv);
@@ -48,6 +115,17 @@ main (int argc, char *argv[])
       g_printerr ("usage: %s <action_id>\n", argv[0]);
       goto out;
     }
+  action_id = argv[1];
+
+  error = NULL;
+  bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
+  if (bus == NULL)
+    {
+      g_printerr ("Failed connecting to system bus: %s\n",
+                  error->message);
+      g_error_free (error);
+      goto out;
+    }
 
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
@@ -56,22 +134,47 @@ main (int argc, char *argv[])
   gtk_container_set_border_width (GTK_CONTAINER (window), 12);
   gtk_container_add (GTK_CONTAINER (window), vbox);
 
-  s = g_strdup_printf ("Showing PolkitLockButton for action id: %s", argv[1]);
+  s = g_strdup_printf ("Showing PolkitLockButton for action id: %s", action_id);
   label = gtk_label_new (s);
-  gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
+  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+  gtk_label_set_line_wrap (GTK_LABEL (label), FALSE);
   gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
   g_free (s);
 
+  label = gtk_label_new (NULL);
+  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+  gtk_label_set_line_wrap (GTK_LABEL (label), FALSE);
+  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
+  system_bus_name_authorized_label = label;
+
+  label = gtk_label_new (NULL);
+  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+  gtk_label_set_line_wrap (GTK_LABEL (label), FALSE);
+  gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
+  unix_process_authorized_label = label;
+
   entry = gtk_entry_new ();
   gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
 
-  button = polkit_lock_button_new (argv[1]);
+  button = polkit_lock_button_new (action_id);
   g_signal_connect (button,
                     "changed",
                     G_CALLBACK (on_button_changed),
                     entry);
   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
 
+
+  system_bus_name_subject = polkit_system_bus_name_new (dbus_bus_get_unique_name (dbus_g_connection_get_connection (bus)));
+  unix_process_subject = polkit_unix_process_new (getpid ());
+
+  authority = polkit_authority_get ();
+  g_signal_connect (authority,
+                    "changed",
+                    G_CALLBACK (on_authority_changed),
+                    NULL);
+
+  update_labels ();
+
   gtk_widget_set_sensitive (entry,
                             polkit_lock_button_get_is_authorized (POLKIT_LOCK_BUTTON (button)));
 
diff --git a/polkitgtk/polkitgtktypes.h b/polkitgtk/polkitgtktypes.h
index d31d975..36cfd53 100644
--- a/polkitgtk/polkitgtktypes.h
+++ b/polkitgtk/polkitgtktypes.h
@@ -27,6 +27,7 @@
 #define __POLKIT_GTK_TYPES_H
 
 #include <gtk/gtk.h>
+#include <polkit/polkit.h>
 
 struct _PolkitLockButton;
 typedef struct _PolkitLockButton PolkitLockButton;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]