2 * @file sipe-xml-tests.c
6 * Copyright (C) 2010 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(void) { 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)");
190 /* memory leak check */
191 static gsize allocated
= 0;
193 static gpointer
test_malloc(gsize n_bytes
)
195 gsize
*m
= malloc(sizeof(gsize
) + n_bytes
);
196 if (!m
) return(NULL
);
197 allocated
+= n_bytes
;
202 static void test_free(gpointer mem
)
211 static gpointer
test_realloc(gpointer mem
, gsize n_bytes
)
215 n
= test_malloc(n_bytes
);
217 memcpy(n
, mem
, n_bytes
);
224 static GMemVTable memory_leak_check
= {
233 int main(SIPE_UNUSED_PARAMETER
int argc
, SIPE_UNUSED_PARAMETER
char **argv
)
236 const sipe_xml
*child1
, *child2
;
240 * No idea why the memory leak checks work on some platforms
241 * but fail on others :-( Disable for now...
243 g_mem_set_vtable(&memory_leak_check
);
245 (void) memory_leak_check
;
249 xml
= assert_parse(NULL
, FALSE
);
250 assert_stringify(xml
, 1, NULL
);
252 xml
= assert_parse("", FALSE
);
254 xml
= assert_parse("<?xml version=\"1.0\" ?>", FALSE
);
258 xml
= assert_parse("<test></test>", TRUE
);
259 assert_name(xml
, "test");
260 assert_data(xml
, NULL
);
261 assert_stringify(xml
, 1, "<test/>");
263 xml
= assert_parse("<test/>", TRUE
);
264 assert_name(xml
, "test");
265 assert_data(xml
, NULL
);
266 assert_stringify(xml
, 1, teststring
);
268 xml
= assert_parse("<test>a</test>", TRUE
);
269 assert_name(xml
, "test");
270 assert_data(xml
, "a");
271 assert_stringify(xml
, 1, teststring
);
273 xml
= assert_parse("<test>a\nb</test>", TRUE
);
274 assert_name(xml
, "test");
275 assert_data(xml
, "a\nb");
276 assert_stringify(xml
, 1, teststring
);
280 xml
= assert_parse("<test>a<child>b</child></test>", TRUE
);
281 assert_name(xml
, "test");
282 assert_data(xml
, "a");
283 child1
= assert_child(xml
, NULL
, FALSE
);
284 child1
= assert_child(xml
, "child", TRUE
);
285 assert_name(child1
, "child");
286 assert_data(child1
, "b");
287 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
288 assert_data(child1
, NULL
);
289 assert_stringify(xml
, 1, teststring
);
292 xml
= assert_parse("<test>a<child/></test>", TRUE
);
293 assert_name(xml
, "test");
294 assert_data(xml
, "a");
295 child1
= assert_child(xml
, "child", TRUE
);
296 assert_name(child1
, "child");
297 assert_data(child1
, NULL
);
298 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
299 assert_data(child1
, NULL
);
300 assert_stringify(xml
, 1, teststring
);
303 xml
= assert_parse("<test>a<child>b<inner>c</inner></child></test>", TRUE
);
304 assert_name(xml
, "test");
305 assert_data(xml
, "a");
306 child1
= assert_child(xml
, "child", TRUE
);
307 assert_name(child1
, "child");
308 assert_data(child1
, "b");
309 child1
= assert_child(child1
, "inner", TRUE
);
310 assert_name(child1
, "inner");
311 assert_data(child1
, "c");
312 child1
= assert_child(xml
, "child/inner", TRUE
);
313 assert_name(child1
, "inner");
314 assert_data(child1
, "c");
315 assert_stringify(xml
, 1, teststring
);
318 xml
= assert_parse("<test>a<child>b<inner>c<innerinner>d</innerinner></inner></child></test>", TRUE
);
319 assert_name(xml
, "test");
320 assert_data(xml
, "a");
321 child1
= assert_child(xml
, "child", TRUE
);
322 assert_name(child1
, "child");
323 assert_data(child1
, "b");
324 child2
= assert_child(child1
, "inner/innerinner", TRUE
);
325 assert_name(child2
, "innerinner");
326 assert_data(child2
, "d");
327 child1
= assert_child(child1
, "inner", TRUE
);
328 assert_name(child1
, "inner");
329 assert_data(child1
, "c");
330 child1
= assert_child(child1
, "innerinner", TRUE
);
331 assert_name(child1
, "innerinner");
332 assert_data(child1
, "d");
333 child1
= assert_child(xml
, "child/inner", TRUE
);
334 assert_name(child1
, "inner");
335 assert_data(child1
, "c");
336 child1
= assert_child(xml
, "child/inner/innerinner", TRUE
);
337 assert_name(child1
, "innerinner");
338 assert_data(child1
, "d");
339 assert_stringify(xml
, 1, teststring
);
343 xml
= assert_parse("<test a=\"\">a</test>", TRUE
);
344 assert_name(xml
, "test");
345 assert_data(xml
, "a");
346 assert_attribute(xml
, NULL
, NULL
);
347 assert_attribute(xml
, "a", "");
348 assert_attribute(xml
, "b", NULL
);
349 assert_stringify(xml
, 1, teststring
);
352 xml
= assert_parse("<test a=\"1\" b=\"abc\">a</test>", TRUE
);
353 assert_name(xml
, "test");
354 assert_data(xml
, "a");
355 assert_attribute(xml
, "a", "1");
356 assert_int_attribute(xml
, "a", 1, 0);
357 assert_attribute(xml
, "b", "abc");
358 assert_attribute(xml
, "c", NULL
);
359 assert_int_attribute(xml
, "d", 100, 200);
360 /* the attribute order depends on glib hashing :-( */
361 assert_stringify(xml
, 2, teststring
, "<test b=\"abc\" a=\"1\">a</test>");
364 /* attributes with namespace */
365 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
);
366 assert_name(xml
, "row");
367 assert_data(xml
, NULL
);
368 assert_attribute(xml
, "uri", "sip:");
369 assert_attribute(xml
, "displayName", "X");
370 assert_attribute(xml
, "title", "Y");
371 assert_attribute(xml
, "office", "Z");
372 assert_attribute(xml
, "phone", "0");
373 assert_attribute(xml
, "company", "A");
374 assert_attribute(xml
, "city", "B");
375 assert_attribute(xml
, "state", "C");
376 assert_attribute(xml
, "country", "D");
377 assert_attribute(xml
, "email", "E");
380 xml
= assert_parse("<state xsi:type=\"aggregateState\" lastActive=\"date\" xmlns:xsi=\"http://one\" xmlns=\"http://two\"><availability>15500</availability></state>", TRUE
);
381 assert_name(xml
, "state");
382 assert_data(xml
, NULL
);
383 assert_attribute(xml
, "type", "aggregateState");
384 assert_attribute(xml
, "lastActive", "date");
385 assert_attribute(xml
, "xsi", "http://one");
386 assert_attribute(xml
, "xmlns", "http://two");
387 child1
= assert_child(xml
, "availability", TRUE
);
388 assert_name(child1
, "availability");
389 assert_data(child1
, "15500");
393 xml
= assert_parse("t", FALSE
);
395 xml
= assert_parse("<>", FALSE
);
397 xml
= assert_parse("<></>", FALSE
);
399 xml
= assert_parse("<test>", FALSE
);
401 xml
= assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE
);
405 printf("MEMORY LEAK: %" G_GSIZE_FORMAT
" still allocated\n", allocated
);
408 printf("MEMORY LEAK CHECK OK\n");
412 printf("Result: %d PASSED %d FAILED\n", succeeded
, failed
);