2 * @file sipe-xml-tests.c
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 */
33 #include "sipe-common.h"
34 #include "sipe-backend.h"
35 #include "sipe-digest.h"
37 #include "sipe-utils.h"
39 /* stub functions for backend API */
40 void sipe_backend_debug_literal(sipe_debug_level level
,
43 printf("DEBUG %d: %s", level
, msg
);
45 void sipe_backend_debug(sipe_debug_level level
,
51 va_start(args
, format
);
52 msg
= g_strdup_vprintf(format
, args
);
55 sipe_backend_debug_literal(level
, msg
);
58 gboolean
sipe_backend_debug_enabled(void)
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
); }
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
)) {
82 printf("[%s]\nXML parse FAILED: %p\n",
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
)) {
96 printf("[%s]\nXML name FAILED: '%s' expected: '%s'\n",
97 teststring
, name
? name
: "(nil)", s
? s
: "(nil)");
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
)) {
110 printf("[%s]\nXML child FAILED: %p '%s'\n",
111 teststring
, xml
, s
? s
: "(nil)");
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
)) {
124 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
125 teststring
, data
? data
: "(nil)", s
? s
: "(nil)");
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
)) {
139 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
140 teststring
, key
? key
: "(nil)",
141 attr
? attr
: "(nil)", value
? value
: "(nil)");
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
)) {
154 printf("[%s]\nXML int attr FAILED: '%s': %d expected: %d/%d\n",
155 teststring
, key
? key
: "(nil)",
156 attr
, value
, fallback
);
161 static void assert_stringify(const sipe_xml
*xml
,
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
)) {
174 printf("XML stringify alternative FAILED: '%s' (trying next...)\n",
175 alternative
? alternative
: "(nil)");
181 printf("[%s]\nXML stringify all alternatives FAILED: '%s'\n",
182 teststring
, string
? string
: "(nil)");
189 static void assert_raw(const gchar
*raw
,
191 gboolean include_tag
,
192 const char *expected
)
194 gchar
*string
= sipe_xml_extract_raw(raw
, tag
, include_tag
);
197 if (sipe_strequal(string
, expected
)) {
200 printf("[%s]\nXML raw extract FAILED: '%s' expected: '%s'\n",
201 raw
, string
, expected
);
205 printf("[%s]\nXML raw extract not found FAILED: '%s' expected: '%s'\n",
211 printf("[%s]\nXML raw extract no match FAILED: '%s' matched: '%s'\n",
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
;
235 static void test_free(gpointer mem
)
244 static gpointer
test_realloc(gpointer mem
, gsize n_bytes
)
248 n
= test_malloc(n_bytes
);
250 memcpy(n
, mem
, n_bytes
);
257 static GMemVTable memory_leak_check
= {
266 int main(SIPE_UNUSED_PARAMETER
int argc
, SIPE_UNUSED_PARAMETER
char **argv
)
269 const sipe_xml
*child1
, *child2
;
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
);
278 (void) memory_leak_check
;
282 xml
= assert_parse(NULL
, FALSE
);
283 assert_stringify(xml
, 1, NULL
);
285 xml
= assert_parse("", FALSE
);
287 xml
= assert_parse("<?xml version=\"1.0\" ?>", FALSE
);
291 xml
= assert_parse("<test></test>", TRUE
);
292 assert_name(xml
, "test");
293 assert_data(xml
, NULL
);
294 assert_stringify(xml
, 1, "<test/>");
296 xml
= assert_parse("<test/>", TRUE
);
297 assert_name(xml
, "test");
298 assert_data(xml
, NULL
);
299 assert_stringify(xml
, 1, teststring
);
301 xml
= assert_parse("<test>a</test>", TRUE
);
302 assert_name(xml
, "test");
303 assert_data(xml
, "a");
304 assert_stringify(xml
, 1, teststring
);
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
);
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
);
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
);
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
);
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
);
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
);
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>");
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");
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");
426 xml
= assert_parse("t", FALSE
);
428 xml
= assert_parse("<>", FALSE
);
430 xml
= assert_parse("<></>", FALSE
);
432 xml
= assert_parse("<test>", FALSE
);
434 xml
= assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE
);
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
);
477 printf("MEMORY LEAK: %" G_GSIZE_FORMAT
" still allocated\n", allocated
);
480 printf("MEMORY LEAK CHECK OK\n");
484 printf("Result: %d PASSED %d FAILED\n", succeeded
, failed
);