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(sipe_debug_level level
,
46 va_start(args
, format
);
47 msg
= g_strdup_vprintf(format
, args
);
50 printf("DEBUG %d: %s", level
, msg
);
54 void sipe_digest_sha1(SIPE_UNUSED_PARAMETER
const guchar
*data
,
55 SIPE_UNUSED_PARAMETER gsize length
,
56 SIPE_UNUSED_PARAMETER guchar
*digest
) {}
57 const gchar
*sipe_backend_network_ip_address(void) { return(NULL
); }
60 static guint succeeded
= 0;
61 static guint failed
= 0;
62 static const gchar
*teststring
;
64 static sipe_xml
*assert_parse(const gchar
*s
, gboolean ok
)
66 sipe_xml
*xml
= sipe_xml_parse(s
, s
? strlen(s
) : 0);
68 teststring
= s
? s
: "(nil)";
70 if ((ok
&& xml
) || (!ok
&& !xml
)) {
73 printf("[%s]\nXML parse FAILED: %p\n",
80 static void assert_name(const sipe_xml
*xml
, const gchar
*s
)
82 const gchar
*name
= sipe_xml_name(xml
);
84 if (sipe_strequal(name
, s
)) {
87 printf("[%s]\nXML name FAILED: '%s' expected: '%s'\n",
88 teststring
, name
? name
: "(nil)", s
? s
: "(nil)");
94 static const sipe_xml
*assert_child(const sipe_xml
*xml
, const gchar
*s
, gboolean ok
)
96 const sipe_xml
*child
= sipe_xml_child(xml
, s
);
98 if ((ok
&& child
) || (!ok
&& !child
)) {
101 printf("[%s]\nXML child FAILED: %p '%s'\n",
102 teststring
, xml
, s
? s
: "(nil)");
108 static void assert_data(const sipe_xml
*xml
, const gchar
*s
)
110 gchar
*data
= sipe_xml_data(xml
);
112 if (sipe_strequal(s
, data
)) {
115 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
116 teststring
, data
? data
: "(nil)", s
? s
: "(nil)");
122 static void assert_attribute(const sipe_xml
*xml
,
123 const gchar
*key
, const gchar
*value
)
125 const gchar
*attr
= sipe_xml_attribute(xml
, key
);
127 if (sipe_strequal(value
, attr
)) {
130 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
131 teststring
, key
? key
: "(nil)",
132 attr
? attr
: "(nil)", value
? value
: "(nil)");
137 static void assert_int_attribute(const sipe_xml
*xml
,
138 const gchar
*key
, gint value
, gint fallback
)
140 gint attr
= sipe_xml_int_attribute(xml
, key
, fallback
);
142 if ((attr
== value
) || (attr
== fallback
)) {
145 printf("[%s]\nXML int attr FAILED: '%s': %d expected: %d/%d\n",
146 teststring
, key
? key
: "(nil)",
147 attr
, value
, fallback
);
152 static void assert_stringify(const sipe_xml
*xml
,
156 gchar
*string
= sipe_xml_stringify(xml
);
158 va_start(args
, expected
);
159 while (expected
-- > 0) {
160 const gchar
*alternative
= va_arg(args
, const gchar
*);
161 if (sipe_strequal(string
, alternative
)) {
165 printf("XML stringify alternative FAILED: '%s' (trying next...)\n",
166 alternative
? alternative
: "(nil)");
172 printf("[%s]\nXML stringify all alternatives FAILED: '%s'\n",
173 teststring
, string
? string
: "(nil)");
181 /* memory leak check */
182 static gsize allocated
= 0;
184 static gpointer
test_malloc(gsize n_bytes
)
186 gsize
*m
= malloc(sizeof(gsize
) + n_bytes
);
187 if (!m
) return(NULL
);
188 allocated
+= n_bytes
;
193 static void test_free(gpointer mem
)
202 static gpointer
test_realloc(gpointer mem
, gsize n_bytes
)
206 n
= test_malloc(n_bytes
);
208 memcpy(n
, mem
, n_bytes
);
215 static GMemVTable memory_leak_check
= {
224 int main(SIPE_UNUSED_PARAMETER
int argc
, SIPE_UNUSED_PARAMETER
char **argv
)
227 const sipe_xml
*child1
, *child2
;
231 * No idea why the memory leak checks work on some platforms
232 * but fail on others :-( Disable for now...
234 g_mem_set_vtable(&memory_leak_check
);
236 (void) memory_leak_check
;
240 xml
= assert_parse(NULL
, FALSE
);
241 assert_stringify(xml
, 1, NULL
);
243 xml
= assert_parse("", FALSE
);
245 xml
= assert_parse("<?xml version=\"1.0\" ?>", FALSE
);
249 xml
= assert_parse("<test></test>", TRUE
);
250 assert_name(xml
, "test");
251 assert_data(xml
, NULL
);
252 assert_stringify(xml
, 1, "<test/>");
254 xml
= assert_parse("<test/>", TRUE
);
255 assert_name(xml
, "test");
256 assert_data(xml
, NULL
);
257 assert_stringify(xml
, 1, teststring
);
259 xml
= assert_parse("<test>a</test>", TRUE
);
260 assert_name(xml
, "test");
261 assert_data(xml
, "a");
262 assert_stringify(xml
, 1, teststring
);
264 xml
= assert_parse("<test>a\nb</test>", TRUE
);
265 assert_name(xml
, "test");
266 assert_data(xml
, "a\nb");
267 assert_stringify(xml
, 1, teststring
);
271 xml
= assert_parse("<test>a<child>b</child></test>", TRUE
);
272 assert_name(xml
, "test");
273 assert_data(xml
, "a");
274 child1
= assert_child(xml
, NULL
, FALSE
);
275 child1
= assert_child(xml
, "child", TRUE
);
276 assert_name(child1
, "child");
277 assert_data(child1
, "b");
278 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
279 assert_data(child1
, NULL
);
280 assert_stringify(xml
, 1, teststring
);
283 xml
= assert_parse("<test>a<child/></test>", TRUE
);
284 assert_name(xml
, "test");
285 assert_data(xml
, "a");
286 child1
= assert_child(xml
, "child", TRUE
);
287 assert_name(child1
, "child");
288 assert_data(child1
, NULL
);
289 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
290 assert_data(child1
, NULL
);
291 assert_stringify(xml
, 1, teststring
);
294 xml
= assert_parse("<test>a<child>b<inner>c</inner></child></test>", TRUE
);
295 assert_name(xml
, "test");
296 assert_data(xml
, "a");
297 child1
= assert_child(xml
, "child", TRUE
);
298 assert_name(child1
, "child");
299 assert_data(child1
, "b");
300 child1
= assert_child(child1
, "inner", TRUE
);
301 assert_name(child1
, "inner");
302 assert_data(child1
, "c");
303 child1
= assert_child(xml
, "child/inner", TRUE
);
304 assert_name(child1
, "inner");
305 assert_data(child1
, "c");
306 assert_stringify(xml
, 1, teststring
);
309 xml
= assert_parse("<test>a<child>b<inner>c<innerinner>d</innerinner></inner></child></test>", TRUE
);
310 assert_name(xml
, "test");
311 assert_data(xml
, "a");
312 child1
= assert_child(xml
, "child", TRUE
);
313 assert_name(child1
, "child");
314 assert_data(child1
, "b");
315 child2
= assert_child(child1
, "inner/innerinner", TRUE
);
316 assert_name(child2
, "innerinner");
317 assert_data(child2
, "d");
318 child1
= assert_child(child1
, "inner", TRUE
);
319 assert_name(child1
, "inner");
320 assert_data(child1
, "c");
321 child1
= assert_child(child1
, "innerinner", TRUE
);
322 assert_name(child1
, "innerinner");
323 assert_data(child1
, "d");
324 child1
= assert_child(xml
, "child/inner", TRUE
);
325 assert_name(child1
, "inner");
326 assert_data(child1
, "c");
327 child1
= assert_child(xml
, "child/inner/innerinner", TRUE
);
328 assert_name(child1
, "innerinner");
329 assert_data(child1
, "d");
330 assert_stringify(xml
, 1, teststring
);
334 xml
= assert_parse("<test a=\"\">a</test>", TRUE
);
335 assert_name(xml
, "test");
336 assert_data(xml
, "a");
337 assert_attribute(xml
, NULL
, NULL
);
338 assert_attribute(xml
, "a", "");
339 assert_attribute(xml
, "b", NULL
);
340 assert_stringify(xml
, 1, teststring
);
343 xml
= assert_parse("<test a=\"1\" b=\"abc\">a</test>", TRUE
);
344 assert_name(xml
, "test");
345 assert_data(xml
, "a");
346 assert_attribute(xml
, "a", "1");
347 assert_int_attribute(xml
, "a", 1, 0);
348 assert_attribute(xml
, "b", "abc");
349 assert_attribute(xml
, "c", NULL
);
350 assert_int_attribute(xml
, "d", 100, 200);
351 /* the attribute order depends on glib hashing :-( */
352 assert_stringify(xml
, 2, teststring
, "<test b=\"abc\" a=\"1\">a</test>");
355 /* attributes with namespace */
356 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
);
357 assert_name(xml
, "row");
358 assert_data(xml
, NULL
);
359 assert_attribute(xml
, "uri", "sip:");
360 assert_attribute(xml
, "displayName", "X");
361 assert_attribute(xml
, "title", "Y");
362 assert_attribute(xml
, "office", "Z");
363 assert_attribute(xml
, "phone", "0");
364 assert_attribute(xml
, "company", "A");
365 assert_attribute(xml
, "city", "B");
366 assert_attribute(xml
, "state", "C");
367 assert_attribute(xml
, "country", "D");
368 assert_attribute(xml
, "email", "E");
371 xml
= assert_parse("<state xsi:type=\"aggregateState\" lastActive=\"date\" xmlns:xsi=\"http://one\" xmlns=\"http://two\"><availability>15500</availability></state>", TRUE
);
372 assert_name(xml
, "state");
373 assert_data(xml
, NULL
);
374 assert_attribute(xml
, "type", "aggregateState");
375 assert_attribute(xml
, "lastActive", "date");
376 assert_attribute(xml
, "xsi", "http://one");
377 assert_attribute(xml
, "xmlns", "http://two");
378 child1
= assert_child(xml
, "availability", TRUE
);
379 assert_name(child1
, "availability");
380 assert_data(child1
, "15500");
384 xml
= assert_parse("t", FALSE
);
386 xml
= assert_parse("<>", FALSE
);
388 xml
= assert_parse("<></>", FALSE
);
390 xml
= assert_parse("<test>", FALSE
);
392 xml
= assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE
);
396 printf("MEMORY LEAK: %" G_GSIZE_FORMAT
" still allocated\n", allocated
);
399 printf("MEMORY LEAK CHECK OK\n");
403 printf("Result: %d PASSED %d FAILED\n", succeeded
, failed
);