[telepathy-gabble/telepathy-gabble-0.8] util: make lm_message_node_get_namespace chain up to parents' xmlns, and add a test for it

Jonny Lamb jonny.lamb at collabora.co.uk
Mon Dec 7 04:13:01 PST 2009


Signed-off-by: Jonny Lamb <jonny.lamb at collabora.co.uk>
---
 .gitignore        |    1 +
 src/util.c        |   20 +++++++++++++++--
 tests/Makefile.am |    6 +++-
 tests/test-ns.c   |   59 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 81 insertions(+), 5 deletions(-)
 create mode 100644 tests/test-ns.c

diff --git a/.gitignore b/.gitignore
index 47f8cab..84f589b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -74,6 +74,7 @@ cscope.out
 /tests/test-jid-decode
 /tests/test-parse-message
 /tests/test-presence
+/tests/test-ns
 /tests/twisted/gabbleconfig.py
 /tests/twisted/telepathy-gabble-debug
 /tests/twisted/tools/core.*
diff --git a/src/util.c b/src/util.c
index e0e6e18..d5308d9 100644
--- a/src/util.c
+++ b/src/util.c
@@ -191,13 +191,14 @@ find_namespace_of_prefix (LmMessageNode *node,
   return node_ns;
 }
 
-const gchar *
-lm_message_node_get_namespace (LmMessageNode *node)
+static const gchar *
+get_node_namespace (LmMessageNode *node,
+    gboolean check_prefix)
 {
   const gchar *node_ns = NULL;
   gchar *x = strchr (node->name, ':');
 
-  if (x != NULL)
+  if (check_prefix && x != NULL)
     {
       gchar *prefix = g_strndup (node->name, (x - node->name));
 
@@ -207,12 +208,25 @@ lm_message_node_get_namespace (LmMessageNode *node)
   else
     {
       node_ns = lm_message_node_get_attribute (node, "xmlns");
+
+      /* Chain up to the parent to get its namespace, as child nodes have the
+       * same namespace as their parent, if not explicitly set otherwise.
+       * However, make sure we don't check the parent's prefix as that doesn't
+       * get inherited by children. */
+      if (node_ns == NULL && node->parent != NULL)
+        node_ns = get_node_namespace (node->parent, FALSE);
     }
 
   return node_ns;
 }
 
 const gchar *
+lm_message_node_get_namespace (LmMessageNode *node)
+{
+  return get_node_namespace (node, TRUE);
+}
+
+const gchar *
 lm_message_node_get_name (LmMessageNode *node)
 {
   gchar *x = strchr (node->name, ':');
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 77da001..9cdd5b3 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -11,7 +11,8 @@ noinst_PROGRAMS = \
 	test-handles \
 	test-jid-decode \
 	test-parse-message \
-	test-presence
+	test-presence \
+	test-ns
 
 LDADD = $(top_builddir)/src/libgabble-convenience.la
 
@@ -42,7 +43,8 @@ check_c_sources = \
 	test-presence.c \
 	test-jid-decode.c \
 	test-handles.c \
-	test-parse-message.c
+	test-parse-message.c \
+	test-ns
 
 include $(top_srcdir)/tools/check-coding-style.mk
 check-local: check-coding-style
diff --git a/tests/test-ns.c b/tests/test-ns.c
new file mode 100644
index 0000000..034362c
--- /dev/null
+++ b/tests/test-ns.c
@@ -0,0 +1,59 @@
+#include "src/util.h"
+
+static gboolean
+one (void)
+{
+  LmMessageNode *baz;
+  LmMessage *stanza;
+
+  stanza = lm_message_build ("foo", LM_MESSAGE_TYPE_IQ,
+    '@', "xmlns", "bar",
+      '(', "baz", "", ')',
+    NULL);
+
+  g_assert (lm_message_node_has_namespace (stanza->node, "bar", NULL));
+
+  baz = lm_message_node_get_child (stanza->node, "baz");
+
+  g_assert (lm_message_node_has_namespace (baz, "bar", NULL));
+  g_assert (lm_message_node_get_child_with_namespace (stanza->node, "baz", "bar") == baz);
+
+  lm_message_unref (stanza);
+  return TRUE;
+}
+
+static gboolean
+two (void)
+{
+  LmMessageNode *foo, *baz;
+  LmMessage *stanza;
+
+  stanza = lm_message_build ("blah", LM_MESSAGE_TYPE_IQ,
+    '(', "bar:foo", "",
+      '@', "xmlns:bar", "zomg",
+      '(', "baz", "", ')',
+    ')',
+    NULL);
+
+  foo = lm_message_node_get_child_with_namespace (stanza->node, "foo", "zomg");
+  g_assert (foo != NULL);
+
+  baz = lm_message_node_get_child (foo, "baz");
+
+  g_assert (!lm_message_node_has_namespace (baz, "zomg", NULL));
+  g_assert (lm_message_node_get_child_with_namespace (foo, "baz", "zomg") == NULL);
+
+  lm_message_unref (stanza);
+  return TRUE;
+}
+
+int
+main (void)
+{
+  g_type_init ();
+
+  g_assert (one ());
+  g_assert (two ());
+
+  return 0;
+}
-- 
1.5.6.5




More information about the telepathy-commits mailing list