telepathy-idle: muc-channel: validate Password properties to simplify {, un}setting branch

Jonny Lamb jonny at kemper.freedesktop.org
Wed May 16 10:45:09 PDT 2012


Module: telepathy-idle
Branch: master
Commit: 33f0ab025272f54e613f84c68b09a4c2046b7409
URL:    http://cgit.freedesktop.org/telepathy/telepathy-idle/commit/?id=33f0ab025272f54e613f84c68b09a4c2046b7409

Author: Jonny Lamb <jonny.lamb at collabora.co.uk>
Date:   Wed May 16 17:40:02 2012 +0100

muc-channel: validate Password properties to simplify {,un}setting branch

This is much better. I should have done this before. I also added some
tests to make sure bad args are rejected.

Signed-off-by: Jonny Lamb <jonny.lamb at collabora.co.uk>

---

 src/idle-muc-channel.c                |   84 +++++++++++++++++++++------------
 tests/twisted/messages/room-config.py |   23 +++++++++
 2 files changed, 76 insertions(+), 31 deletions(-)

diff --git a/src/idle-muc-channel.c b/src/idle-muc-channel.c
index 5b9a486..0b918f0 100644
--- a/src/idle-muc-channel.c
+++ b/src/idle-muc-channel.c
@@ -1584,9 +1584,37 @@ idle_muc_channel_update_configuration_async (
   IdleMUCChannelPrivate *priv = self->priv;
   GSimpleAsyncResult *result = g_simple_async_result_new ((GObject *) self,
       callback, user_data, idle_muc_channel_update_configuration_async);
-  const gchar *password;
+  gboolean present = FALSE;
 
-  /* do the quick ones */
+  /* first, do sanity checking for Password & PasswordProtected */
+  if (tp_asv_get_boolean (validated_properties,
+          GUINT_TO_POINTER (TP_BASE_ROOM_CONFIG_PASSWORD_PROTECTED), NULL)
+      && tp_str_empty (tp_asv_get_string (validated_properties,
+              GUINT_TO_POINTER (TP_BASE_ROOM_CONFIG_PASSWORD))))
+    {
+      g_simple_async_result_set_error (result, TP_ERROR,
+          TP_ERROR_INVALID_ARGUMENT,
+          "PasswordProtected=True but no password given");
+      g_simple_async_result_complete_in_idle (result);
+      g_object_unref (result);
+      return;
+    }
+
+  if (!tp_asv_get_boolean (validated_properties,
+          GUINT_TO_POINTER (TP_BASE_ROOM_CONFIG_PASSWORD_PROTECTED), &present)
+      && present
+      && tp_asv_get_string (validated_properties,
+          GUINT_TO_POINTER (TP_BASE_ROOM_CONFIG_PASSWORD)) != NULL)
+    {
+      g_simple_async_result_set_error (result, TP_ERROR,
+          TP_ERROR_INVALID_ARGUMENT,
+          "PasswordProtected=False but then a password given, madness!");
+      g_simple_async_result_complete_in_idle (result);
+      g_object_unref (result);
+      return;
+    }
+
+  /* okay go and do the quick ones */
   do_quick_boolean (self, validated_properties,
       TP_BASE_ROOM_CONFIG_INVITE_ONLY, "invite-only", 'i');
   do_quick_boolean (self, validated_properties,
@@ -1596,6 +1624,7 @@ idle_muc_channel_update_configuration_async (
 
   /* now the rest */
 
+  /* limit */
   if (g_hash_table_lookup (validated_properties,
           GUINT_TO_POINTER (TP_BASE_ROOM_CONFIG_LIMIT)) != NULL)
     {
@@ -1623,48 +1652,41 @@ idle_muc_channel_update_configuration_async (
       g_free (cmd);
     }
 
-  /* if we got a new password, save it away for a rainy day */
-  password = tp_asv_get_string (validated_properties,
-      GUINT_TO_POINTER (TP_BASE_ROOM_CONFIG_PASSWORD));
-
+  /* set a new password */
   if (g_hash_table_lookup (validated_properties,
-          GUINT_TO_POINTER (TP_BASE_ROOM_CONFIG_PASSWORD_PROTECTED)) != NULL)
+          GUINT_TO_POINTER (TP_BASE_ROOM_CONFIG_PASSWORD)) != NULL)
     {
-      /* okay we want to require or not require a password ... */
-      gboolean required = tp_asv_get_boolean (validated_properties,
-          GUINT_TO_POINTER (TP_BASE_ROOM_CONFIG_PASSWORD_PROTECTED), NULL);
-      gchar *cmd = NULL;
-
-      /* only set the password if we want to and if we have one
-       * (ie. not the default value */
-      if (required && password != NULL)
-        cmd = g_strdup_printf ("MODE %s +k %s", priv->channel_name, password);
-      else if (!required)
-        cmd = g_strdup_printf ("MODE %s -k", priv->channel_name);
-      /* deliberately unhandled case here is:
-       *              required && (password == NULL) */
-
-      if (cmd != NULL)
-        send_command (self, cmd);
-
-      g_free (cmd);
-    }
-  else if (password != NULL)
-    {
-      /* we didn't set PasswordProtected but we did get the password,
-       * and the flags say we can set the password. let's do so */
+      const gchar *password = tp_asv_get_string (validated_properties,
+          GUINT_TO_POINTER (TP_BASE_ROOM_CONFIG_PASSWORD));
       gchar *cmd;
 
+      /* we've already validated this; either PasswordProtected was
+       * not included, or it's TRUE */
+
       cmd = g_strdup_printf ("MODE %s +k %s", priv->channel_name, password);
       send_command (self, cmd);
       g_free (cmd);
 
-      /* so set PasswordProtected now */
       g_object_set (priv->room_config,
           "password-protected", TRUE,
           NULL);
     }
 
+  /* unset a password */
+  if (!tp_asv_get_boolean (validated_properties,
+          GUINT_TO_POINTER (TP_BASE_ROOM_CONFIG_PASSWORD_PROTECTED), &present)
+      && present)
+    {
+      gchar *cmd;
+
+      /* we've already validated this; PasswordProtected=FALSE so no
+       * Password is given */
+
+      cmd = g_strdup_printf ("MODE %s -k", priv->channel_name);
+      send_command (self, cmd);
+      g_free (cmd);
+    }
+
   g_simple_async_result_complete_in_idle (result);
   g_object_unref (result);
 }
diff --git a/tests/twisted/messages/room-config.py b/tests/twisted/messages/room-config.py
index d8e5bdf..e18687d 100644
--- a/tests/twisted/messages/room-config.py
+++ b/tests/twisted/messages/room-config.py
@@ -240,6 +240,29 @@ def test_password(q, bus, conn, stream):
                                      []])
                   )
 
+    # get rid of it
+    call_async(q, chan.RoomConfig1, 'UpdateConfiguration',
+               {'PasswordProtected': False})
+    q.expect('stream-MODE', data=['#test', '-k']),
+
+    # try some wacky values
+    call_async(q, chan.RoomConfig1, 'UpdateConfiguration',
+               {'PasswordProtected': True})
+    q.expect('dbus-error', method='UpdateConfiguration',
+             name=cs.INVALID_ARGUMENT)
+
+    call_async(q, chan.RoomConfig1, 'UpdateConfiguration',
+               {'PasswordProtected': True,
+                'Password': ''})
+    q.expect('dbus-error', method='UpdateConfiguration',
+             name=cs.INVALID_ARGUMENT)
+
+    call_async(q, chan.RoomConfig1, 'UpdateConfiguration',
+               {'PasswordProtected': False,
+                'Password': 'scumbagsteve'})
+    q.expect('dbus-error', method='UpdateConfiguration',
+             name=cs.INVALID_ARGUMENT)
+
 def test_modechanges(q, bus, conn, stream):
     chan = setup(q, bus, conn, stream)
 



More information about the telepathy-commits mailing list