xml: ignore namespace in raw extract
[siplcs.git] / src / core / sipe-xml-tests.c
blob09227660833bba673ff3996ce1addafaec031155
1 /**
2 * @file sipe-xml-tests.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2015 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* Tests for sipe-xml.c */
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <time.h>
31 #include <glib.h>
33 #include "sipe-common.h"
34 #include "sipe-backend.h"
35 #include "sipe-digest.h"
36 #include "sipe-xml.h"
37 #include "sipe-utils.h"
39 /* stub functions for backend API */
40 void sipe_backend_debug_literal(sipe_debug_level level,
41 const gchar *msg)
43 printf("DEBUG %d: %s", level, msg);
45 void sipe_backend_debug(sipe_debug_level level,
46 const gchar *format,
47 ...)
49 va_list args;
50 gchar *msg;
51 va_start(args, format);
52 msg = g_strdup_vprintf(format, args);
53 va_end(args);
55 sipe_backend_debug_literal(level, msg);
56 g_free(msg);
58 gboolean sipe_backend_debug_enabled(void)
60 return TRUE;
63 void sipe_digest_sha1(SIPE_UNUSED_PARAMETER const guchar *data,
64 SIPE_UNUSED_PARAMETER gsize length,
65 SIPE_UNUSED_PARAMETER guchar *digest) {}
66 const gchar *sipe_backend_network_ip_address(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public) { return(NULL); }
68 /* test helpers */
69 static guint succeeded = 0;
70 static guint failed = 0;
71 static const gchar *teststring;
73 static sipe_xml *assert_parse(const gchar *s, gboolean ok)
75 sipe_xml *xml = sipe_xml_parse(s, s ? strlen(s) : 0);
77 teststring = s ? s : "(nil)";
79 if ((ok && xml) || (!ok && !xml)) {
80 succeeded++;
81 } else {
82 printf("[%s]\nXML parse FAILED: %p\n",
83 teststring, xml);
84 failed++;
86 return(xml);
89 static void assert_name(const sipe_xml *xml, const gchar *s)
91 const gchar *name = sipe_xml_name(xml);
93 if (sipe_strequal(name, s)) {
94 succeeded++;
95 } else {
96 printf("[%s]\nXML name FAILED: '%s' expected: '%s'\n",
97 teststring, name ? name : "(nil)", s ? s : "(nil)");
98 failed++;
103 static const sipe_xml *assert_child(const sipe_xml *xml, const gchar *s, gboolean ok)
105 const sipe_xml *child = sipe_xml_child(xml, s);
107 if ((ok && child) || (!ok && !child)) {
108 succeeded++;
109 } else {
110 printf("[%s]\nXML child FAILED: %p '%s'\n",
111 teststring, xml, s ? s : "(nil)");
112 failed++;
114 return(child);
117 static void assert_data(const sipe_xml *xml, const gchar *s)
119 gchar *data = sipe_xml_data(xml);
121 if (sipe_strequal(s, data)) {
122 succeeded++;
123 } else {
124 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
125 teststring, data ? data : "(nil)", s ? s : "(nil)");
126 failed++;
128 g_free(data);
131 static void assert_attribute(const sipe_xml *xml,
132 const gchar *key, const gchar *value)
134 const gchar *attr = sipe_xml_attribute(xml, key);
136 if (sipe_strequal(value, attr)) {
137 succeeded++;
138 } else {
139 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
140 teststring, key ? key : "(nil)",
141 attr ? attr : "(nil)", value ? value : "(nil)");
142 failed++;
146 static void assert_int_attribute(const sipe_xml *xml,
147 const gchar *key, gint value, gint fallback)
149 gint attr = sipe_xml_int_attribute(xml, key, fallback);
151 if ((attr == value) || (attr == fallback)) {
152 succeeded++;
153 } else {
154 printf("[%s]\nXML int attr FAILED: '%s': %d expected: %d/%d\n",
155 teststring, key ? key : "(nil)",
156 attr, value, fallback);
157 failed++;
161 static void assert_stringify(const sipe_xml *xml,
162 int expected, ...)
164 va_list args;
165 gchar *string = sipe_xml_stringify(xml);
167 va_start(args, expected);
168 while (expected-- > 0) {
169 const gchar *alternative = va_arg(args, const gchar *);
170 if (sipe_strequal(string, alternative)) {
171 succeeded++;
172 break;
173 } else {
174 printf("XML stringify alternative FAILED: '%s' (trying next...)\n",
175 alternative ? alternative : "(nil)");
178 va_end(args);
180 if (expected < 0) {
181 printf("[%s]\nXML stringify all alternatives FAILED: '%s'\n",
182 teststring, string ? string : "(nil)");
183 failed++;
186 g_free(string);
189 static void assert_raw(const gchar *raw,
190 const gchar *tag,
191 gboolean include_tag,
192 const char *expected)
194 gchar *string = sipe_xml_extract_raw(raw, tag, include_tag);
195 if (expected) {
196 if (string) {
197 if (sipe_strequal(string, expected)) {
198 succeeded++;
199 } else {
200 printf("[%s]\nXML raw extract FAILED: '%s' expected: '%s'\n",
201 raw, string, expected);
202 failed++;
204 } else {
205 printf("[%s]\nXML raw extract not found FAILED: '%s' expected: '%s'\n",
206 raw, tag, expected);
207 failed++;
209 } else {
210 if (string) {
211 printf("[%s]\nXML raw extract no match FAILED: '%s' matched: '%s'\n",
212 raw, tag, string);
213 failed++;
214 } else {
215 succeeded++;
219 g_free(string);
223 /* memory leak check */
224 static gsize allocated = 0;
226 static gpointer test_malloc(gsize n_bytes)
228 gsize *m = malloc(sizeof(gsize) + n_bytes);
229 if (!m) return(NULL);
230 allocated += n_bytes;
231 m[0] = n_bytes;
232 return(m + 1);
235 static void test_free(gpointer mem)
237 gsize *m = mem;
238 if (!m) return;
239 m--;
240 allocated -= m[0];
241 free(m);
244 static gpointer test_realloc(gpointer mem, gsize n_bytes)
246 guint8 *n = NULL;
247 if (n_bytes) {
248 n = test_malloc(n_bytes);
249 if (mem && n) {
250 memcpy(n, mem, n_bytes);
253 test_free(mem);
254 return(n);
257 static GMemVTable memory_leak_check = {
258 &test_malloc,
259 &test_realloc,
260 &test_free,
261 NULL,
262 NULL,
263 NULL,
266 int main(SIPE_UNUSED_PARAMETER int argc, SIPE_UNUSED_PARAMETER char **argv)
268 sipe_xml *xml;
269 const sipe_xml *child1, *child2;
271 #if 0
273 * No idea why the memory leak checks work on some platforms
274 * but fail on others :-( Disable for now...
276 g_mem_set_vtable(&memory_leak_check);
277 #else
278 (void) memory_leak_check;
279 #endif
281 /* empty XML */
282 xml = assert_parse(NULL, FALSE);
283 assert_stringify(xml, 1, NULL);
284 sipe_xml_free(xml);
285 xml = assert_parse("", FALSE);
286 sipe_xml_free(xml);
287 xml = assert_parse("<?xml version=\"1.0\" ?>", FALSE);
288 sipe_xml_free(xml);
290 /* one node */
291 xml = assert_parse("<test></test>", TRUE);
292 assert_name(xml, "test");
293 assert_data(xml, NULL);
294 assert_stringify(xml, 1, "<test/>");
295 sipe_xml_free(xml);
296 xml = assert_parse("<test/>", TRUE);
297 assert_name(xml, "test");
298 assert_data(xml, NULL);
299 assert_stringify(xml, 1, teststring);
300 sipe_xml_free(xml);
301 xml = assert_parse("<test>a</test>", TRUE);
302 assert_name(xml, "test");
303 assert_data(xml, "a");
304 assert_stringify(xml, 1, teststring);
305 sipe_xml_free(xml);
306 xml = assert_parse("<test>a\nb</test>", TRUE);
307 assert_name(xml, "test");
308 assert_data(xml, "a\nb");
309 assert_stringify(xml, 1, teststring);
310 sipe_xml_free(xml);
312 /* child node */
313 xml = assert_parse("<test>a<child>b</child></test>", TRUE);
314 assert_name(xml, "test");
315 assert_data(xml, "a");
316 child1 = assert_child(xml, NULL, FALSE);
317 child1 = assert_child(xml, "child", TRUE);
318 assert_name(child1, "child");
319 assert_data(child1, "b");
320 child1 = assert_child(xml, "shouldnotmatch", FALSE);
321 assert_data(child1, NULL);
322 assert_stringify(xml, 1, teststring);
323 sipe_xml_free(xml);
325 xml = assert_parse("<test>a<child/></test>", TRUE);
326 assert_name(xml, "test");
327 assert_data(xml, "a");
328 child1 = assert_child(xml, "child", TRUE);
329 assert_name(child1, "child");
330 assert_data(child1, NULL);
331 child1 = assert_child(xml, "shouldnotmatch", FALSE);
332 assert_data(child1, NULL);
333 assert_stringify(xml, 1, teststring);
334 sipe_xml_free(xml);
336 xml = assert_parse("<test>a<child>b<inner>c</inner></child></test>", TRUE);
337 assert_name(xml, "test");
338 assert_data(xml, "a");
339 child1 = assert_child(xml, "child", TRUE);
340 assert_name(child1, "child");
341 assert_data(child1, "b");
342 child1 = assert_child(child1, "inner", TRUE);
343 assert_name(child1, "inner");
344 assert_data(child1, "c");
345 child1 = assert_child(xml, "child/inner", TRUE);
346 assert_name(child1, "inner");
347 assert_data(child1, "c");
348 assert_stringify(xml, 1, teststring);
349 sipe_xml_free(xml);
351 xml = assert_parse("<test>a<child>b<inner>c<innerinner>d</innerinner></inner></child></test>", TRUE);
352 assert_name(xml, "test");
353 assert_data(xml, "a");
354 child1 = assert_child(xml, "child", TRUE);
355 assert_name(child1, "child");
356 assert_data(child1, "b");
357 child2 = assert_child(child1, "inner/innerinner", TRUE);
358 assert_name(child2, "innerinner");
359 assert_data(child2, "d");
360 child1 = assert_child(child1, "inner", TRUE);
361 assert_name(child1, "inner");
362 assert_data(child1, "c");
363 child1 = assert_child(child1, "innerinner", TRUE);
364 assert_name(child1, "innerinner");
365 assert_data(child1, "d");
366 child1 = assert_child(xml, "child/inner", TRUE);
367 assert_name(child1, "inner");
368 assert_data(child1, "c");
369 child1 = assert_child(xml, "child/inner/innerinner", TRUE);
370 assert_name(child1, "innerinner");
371 assert_data(child1, "d");
372 assert_stringify(xml, 1, teststring);
373 sipe_xml_free(xml);
375 /* attributes */
376 xml = assert_parse("<test a=\"\">a</test>", TRUE);
377 assert_name(xml, "test");
378 assert_data(xml, "a");
379 assert_attribute(xml, NULL, NULL);
380 assert_attribute(xml, "a", "");
381 assert_attribute(xml, "b", NULL);
382 assert_stringify(xml, 1, teststring);
383 sipe_xml_free(xml);
385 xml = assert_parse("<test a=\"1\" b=\"abc\">a</test>", TRUE);
386 assert_name(xml, "test");
387 assert_data(xml, "a");
388 assert_attribute(xml, "a", "1");
389 assert_int_attribute(xml, "a", 1, 0);
390 assert_attribute(xml, "b", "abc");
391 assert_attribute(xml, "c", NULL);
392 assert_int_attribute(xml, "d", 100, 200);
393 /* the attribute order depends on glib hashing :-( */
394 assert_stringify(xml, 2, teststring, "<test b=\"abc\" a=\"1\">a</test>");
395 sipe_xml_free(xml);
397 /* attributes with namespace */
398 xml = assert_parse("<m:row m:uri=\"sip:\" m:displayName=\"X\" m:title=\"Y\" m:office=\"Z\" m:phone=\"0\" m:company=\"A\" m:city=\"B\" m:state=\"C\" m:country=\"D\" m:email=\"E\" />", TRUE);
399 assert_name(xml, "row");
400 assert_data(xml, NULL);
401 assert_attribute(xml, "uri", "sip:");
402 assert_attribute(xml, "displayName", "X");
403 assert_attribute(xml, "title", "Y");
404 assert_attribute(xml, "office", "Z");
405 assert_attribute(xml, "phone", "0");
406 assert_attribute(xml, "company", "A");
407 assert_attribute(xml, "city", "B");
408 assert_attribute(xml, "state", "C");
409 assert_attribute(xml, "country", "D");
410 assert_attribute(xml, "email", "E");
411 sipe_xml_free(xml);
413 xml = assert_parse("<state xsi:type=\"aggregateState\" lastActive=\"date\" xmlns:xsi=\"http://one\" xmlns=\"http://two\"><availability>15500</availability></state>", TRUE);
414 assert_name(xml, "state");
415 assert_data(xml, NULL);
416 assert_attribute(xml, "type", "aggregateState");
417 assert_attribute(xml, "lastActive", "date");
418 assert_attribute(xml, "xsi", "http://one");
419 assert_attribute(xml, "xmlns", "http://two");
420 child1 = assert_child(xml, "availability", TRUE);
421 assert_name(child1, "availability");
422 assert_data(child1, "15500");
423 sipe_xml_free(xml);
425 /* broken XML */
426 xml = assert_parse("t", FALSE);
427 sipe_xml_free(xml);
428 xml = assert_parse("<>", FALSE);
429 sipe_xml_free(xml);
430 xml = assert_parse("<></>", FALSE);
431 sipe_xml_free(xml);
432 xml = assert_parse("<test>", FALSE);
433 sipe_xml_free(xml);
434 xml = assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE);
435 sipe_xml_free(xml);
437 /* XML raw extract */
438 assert_raw("<tag>data</tag>", "tag", FALSE, "data");
439 assert_raw("<tag>data</tag>", "tag", TRUE, "<tag>data</tag>");
440 assert_raw("<tag>data</tag>", "tag1", FALSE, NULL);
441 assert_raw("<tag>data</tag>", "tag1", TRUE, NULL);
442 assert_raw("<ns:tag>data</ns:tag>", "tag", FALSE, "data");
443 assert_raw("<ns:tag>data</ns:tag>", "tag", TRUE, "<ns:tag>data</ns:tag>");
444 assert_raw("<ns:tag>data</ns:tag>", "tag1", FALSE, NULL);
445 assert_raw("<ns:tag>data</ns:tag>", "tag1", TRUE, NULL);
446 assert_raw("<ns:tag>data</ns:tag>", "ns:tag", FALSE, "data");
447 assert_raw("<ns:tag>data</ns:tag>", "ns:tag", TRUE, "<ns:tag>data</ns:tag>");
448 assert_raw("<ns:tag>data</ns:tag>", "ns:tag1", FALSE, NULL);
449 assert_raw("<ns:tag>data</ns:tag>", "ns:tag1", TRUE, NULL);
451 assert_raw("<othertag>data</othertag><tag>data</tag>", "tag", FALSE, "data");
452 assert_raw("<othertag>data</othertag><tag>data</tag>", "tag", TRUE, "<tag>data</tag>");
453 assert_raw("<othertag>data</othertag><tag>data</tag><othertag>data</othertag>", "tag", FALSE, "data");
454 assert_raw("<othertag>data</othertag><tag>data</tag><othertag>data</othertag>", "tag", TRUE, "<tag>data</tag>");
455 assert_raw("<othertag>data</othertag><tag>data</tag>", "tag1", FALSE, NULL);
456 assert_raw("<othertag>data</othertag><tag>data</tag>", "tag1", TRUE, NULL);
457 assert_raw("<othertag>data</othertag><tag>data</tag><othertag>data</othertag>", "tag1", FALSE, NULL);
458 assert_raw("<othertag>data</othertag><tag>data</tag><othertag>data</othertag>", "tag1", TRUE, NULL);
459 assert_raw("<othertag>data</othertag><ns:tag>data</ns:tag>", "tag", FALSE, "data");
460 assert_raw("<othertag>data</othertag><ns:tag>data</ns:tag>", "tag", TRUE, "<ns:tag>data</ns:tag>");
461 assert_raw("<othertag>data</othertag><ns:tag>data</ns:tag><othertag>data</othertag>", "tag", FALSE, "data");
462 assert_raw("<othertag>data</othertag><ns:tag>data</ns:tag><othertag>data</othertag>", "tag", TRUE, "<ns:tag>data</ns:tag>");
463 assert_raw("<othertag>data</othertag><ns:tag>data</ns:tag>", "tag1", FALSE, NULL);
464 assert_raw("<othertag>data</othertag><ns:tag>data</ns:tag>", "tag1", TRUE, NULL);
465 assert_raw("<othertag>data</othertag><ns:tag>data</ns:tag><othertag>data</othertag>", "tag1", FALSE, NULL);
466 assert_raw("<othertag>data</othertag><ns:tag>data</ns:tag><othertag>data</othertag>", "tag1", TRUE, NULL);
468 /* broken XML raw extract */
469 assert_raw("tag>data</tag>", "tag", FALSE, NULL);
470 assert_raw(":tag>data</tag>", "tag", FALSE, NULL);
471 assert_raw("<tag>data</tag", "tag", FALSE, NULL);
472 assert_raw("<tag>data</tag1>", "tag", FALSE, NULL);
473 assert_raw("<ns:tag>data</tag1>", "tag", FALSE, NULL);
474 assert_raw("<ns:tag>data</ns:tag1>", "tag", FALSE, NULL);
476 if (allocated) {
477 printf("MEMORY LEAK: %" G_GSIZE_FORMAT " still allocated\n", allocated);
478 failed++;
479 } else {
480 printf("MEMORY LEAK CHECK OK\n");
481 succeeded++;
484 printf("Result: %d PASSED %d FAILED\n", succeeded, failed);
485 return(failed);
489 Local Variables:
490 mode: c
491 c-file-style: "bsd"
492 indent-tabs-mode: t
493 tab-width: 8
494 End: