comparison libpurple/tests/test_xmlnode.c @ 32672:3828a61c44da

A boring and large patch so I can merge heads.
author Elliott Sales de Andrade <qulogic@pidgin.im>
date Fri, 23 Dec 2011 08:21:58 +0000
parents c8f91310bfbf
children
comparison
equal deleted inserted replaced
32671:0e69949b3e61 32672:3828a61c44da
19 fail_if(xmlnode_from_str(malicious_xml_doc, -1), 19 fail_if(xmlnode_from_str(malicious_xml_doc, -1),
20 "xmlnode_from_str() returned an XML tree, but we didn't want it to"); 20 "xmlnode_from_str() returned an XML tree, but we didn't want it to");
21 } 21 }
22 END_TEST 22 END_TEST
23 23
24 #define check_doc_structure(x) { \
25 xmlnode *ping, *child1, *child2; \
26 fail_if(x == NULL, "Failed to parse document"); \
27 ping = xmlnode_get_child(x, "ping"); \
28 fail_if(ping == NULL, "Failed to find 'ping' child"); \
29 child1 = xmlnode_get_child(ping, "child1"); \
30 fail_if(child1 == NULL, "Failed to find 'child1'"); \
31 child2 = xmlnode_get_child(child1, "child2"); \
32 fail_if(child2 == NULL, "Failed to find 'child2'"); \
33 xmlnode_new_child(child2, "a"); \
34 \
35 assert_string_equal("jabber:client", xmlnode_get_namespace(x)); \
36 /* NOTE: xmlnode_get_namespace() returns the namespace of the element, not the
37 * current default namespace. See http://www.w3.org/TR/xml-names/#defaulting and
38 * http://www.w3.org/TR/xml-names/#dt-defaultNS.
39 */ \
40 assert_string_equal("urn:xmpp:ping", xmlnode_get_namespace(ping)); \
41 assert_string_equal("jabber:client", xmlnode_get_namespace(child1)); \
42 assert_string_equal("urn:xmpp:ping", xmlnode_get_namespace(child2)); \
43 /*
44 * This fails (well, actually crashes [the ns is NULL]) unless
45 * xmlnode_new_child() actually sets the element namespace.
46 assert_string_equal("jabber:client", xmlnode_get_namespace(xmlnode_get_child(child2, "a")));
47 */ \
48 \
49 assert_string_equal("jabber:client", xmlnode_get_default_namespace(x)); \
50 assert_string_equal("jabber:client", xmlnode_get_default_namespace(ping)); \
51 assert_string_equal("jabber:client", xmlnode_get_default_namespace(child1)); \
52 assert_string_equal("jabber:client", xmlnode_get_default_namespace(child2)); \
53 }
54
55 START_TEST(test_xmlnode_prefixes)
56 {
57 const char *xml_doc =
58 "<iq type='get' xmlns='jabber:client' xmlns:ping='urn:xmpp:ping'>"
59 "<ping:ping>"
60 "<child1>"
61 "<ping:child2></ping:child2>" /* xmlns='jabber:child' */
62 "</child1>"
63 "</ping:ping>"
64 "</iq>";
65 char *str;
66 xmlnode *xml, *reparsed;
67
68 xml = xmlnode_from_str(xml_doc, -1);
69 check_doc_structure(xml);
70
71 /* Check that xmlnode_from_str(xmlnode_to_str(xml, NULL), -1) is idempotent. */
72 str = xmlnode_to_str(xml, NULL);
73 fail_if(str == NULL, "Failed to serialize XMLnode");
74 reparsed = xmlnode_from_str(str, -1);
75 fail_if(reparsed == NULL, "Failed to reparse xml document");
76 check_doc_structure(reparsed);
77
78 g_free(str);
79 xmlnode_free(xml);
80 xmlnode_free(reparsed);
81 }
82 END_TEST
83
84
85 START_TEST(test_strip_prefixes)
86 {
87 const char *xml_doc = "<message xmlns='jabber:client' from='user@gmail.com/resource' to='another_user@darkrain42.org' type='chat' id='purple'>"
88 "<cha:active xmlns:cha='http://jabber.org/protocol/chatstates'/>"
89 "<body>xvlc xvlc</body>"
90 "<im:html xmlns:im='http://jabber.org/protocol/xhtml-im'>"
91 "<xht:body xmlns:xht='http://www.w3.org/1999/xhtml'>"
92 "<xht:p>xvlc <xht:span style='font-weight: bold;'>xvlc</xht:span></xht:p>"
93 "</xht:body>"
94 "</im:html>"
95 "</message>";
96 const char *out = "<message xmlns='jabber:client' from='user@gmail.com/resource' to='another_user@darkrain42.org' type='chat' id='purple'>"
97 "<active xmlns:cha='http://jabber.org/protocol/chatstates' xmlns='http://jabber.org/protocol/chatstates'/>"
98 "<body>xvlc xvlc</body>"
99 "<html xmlns:im='http://jabber.org/protocol/xhtml-im' xmlns='http://jabber.org/protocol/xhtml-im'>"
100 "<body xmlns:xht='http://www.w3.org/1999/xhtml' xmlns='http://www.w3.org/1999/xhtml'>"
101 "<p>xvlc <span style='font-weight: bold;'>xvlc</span></p>"
102 "</body>"
103 "</html>"
104 "</message>";
105 char *str;
106 xmlnode *xml;
107
108 xml = xmlnode_from_str(xml_doc, -1);
109 fail_if(xml == NULL, "Failed to parse XML");
110
111 xmlnode_strip_prefixes(xml);
112 str = xmlnode_to_str(xml, NULL);
113 assert_string_equal_free(out, str);
114
115 xmlnode_free(xml);
116 }
117 END_TEST
118
24 Suite * 119 Suite *
25 xmlnode_suite(void) 120 xmlnode_suite(void)
26 { 121 {
27 Suite *s = suite_create("Utility Functions"); 122 Suite *s = suite_create("Utility Functions");
28 123
29 TCase *tc = tcase_create("xmlnode"); 124 TCase *tc = tcase_create("xmlnode");
30 tcase_add_test(tc, test_xmlnode_billion_laughs_attack); 125 tcase_add_test(tc, test_xmlnode_billion_laughs_attack);
126 tcase_add_test(tc, test_xmlnode_prefixes);
127 tcase_add_test(tc, test_strip_prefixes);
128
31 suite_add_tcase(s, tc); 129 suite_add_tcase(s, tc);
32 130
33 return s; 131 return s;
34 } 132 }