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"
36 #include "sipe-utils.h"
38 /* stub functions for backend API */
39 void sipe_backend_debug(sipe_debug_level level
,
45 va_start(args
, format
);
46 msg
= g_strdup_vprintf(format
, args
);
49 printf("DEBUG %d: %s", level
, msg
);
54 static guint succeeded
= 0;
55 static guint failed
= 0;
56 static const gchar
*teststring
;
58 static sipe_xml
*assert_parse(const gchar
*s
, gboolean ok
)
60 sipe_xml
*xml
= sipe_xml_parse(s
, s
? strlen(s
) : 0);
62 teststring
= s
? s
: "(nil)";
64 if ((ok
&& xml
) || (!ok
&& !xml
)) {
67 printf("[%s]\nXML parse FAILED: %p\n",
74 static void assert_name(const sipe_xml
*xml
, const gchar
*s
)
76 const gchar
*name
= sipe_xml_name(xml
);
78 if (sipe_strequal(name
, s
)) {
81 printf("[%s]\nXML name FAILED: '%s' expected: '%s'\n",
82 teststring
, name
? name
: "(nil)", s
? s
: "(nil)");
88 static const sipe_xml
*assert_child(const sipe_xml
*xml
, const gchar
*s
, gboolean ok
)
90 const sipe_xml
*child
= sipe_xml_child(xml
, s
);
92 if ((ok
&& child
) || (!ok
&& !child
)) {
95 printf("[%s]\nXML child FAILED: %p '%s'\n",
96 teststring
, xml
, s
? s
: "(nil)");
102 static void assert_data(const sipe_xml
*xml
, const gchar
*s
)
104 gchar
*data
= sipe_xml_data(xml
);
106 if (sipe_strequal(s
, data
)) {
109 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
110 teststring
, data
? data
: "(nil)", s
? s
: "(nil)");
116 static void assert_attribute(const sipe_xml
*xml
,
117 const gchar
*key
, const gchar
*value
)
119 const gchar
*attr
= sipe_xml_attribute(xml
, key
);
121 if (sipe_strequal(value
, attr
)) {
124 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
125 teststring
, key
? key
: "(nil)",
126 attr
? attr
: "(nil)", value
? value
: "(nil)");
131 static void assert_int_attribute(const sipe_xml
*xml
,
132 const gchar
*key
, gint value
, gint fallback
)
134 gint attr
= sipe_xml_int_attribute(xml
, key
, fallback
);
136 if ((attr
== value
) || (attr
== fallback
)) {
139 printf("[%s]\nXML int attr FAILED: '%s': %d expected: %d/%d\n",
140 teststring
, key
? key
: "(nil)",
141 attr
, value
, fallback
);
146 static void assert_stringify(const sipe_xml
*xml
,
150 gchar
*string
= sipe_xml_stringify(xml
);
152 va_start(args
, expected
);
153 while (expected
-- > 0) {
154 const gchar
*alternative
= va_arg(args
, const gchar
*);
155 if (sipe_strequal(string
, alternative
)) {
159 printf("XML stringify alternative FAILED: '%s' (trying next...)\n",
160 alternative
? alternative
: "(nil)");
166 printf("[%s]\nXML stringify all alternatives FAILED: '%s'\n",
167 teststring
, string
? string
: "(nil)");
175 /* memory leak check */
176 static gsize allocated
= 0;
178 static gpointer
test_malloc(gsize n_bytes
)
180 gsize
*m
= malloc(sizeof(gsize
) + n_bytes
);
181 if (!m
) return(NULL
);
182 allocated
+= n_bytes
;
187 static void test_free(gpointer mem
)
196 static gpointer
test_realloc(gpointer mem
, gsize n_bytes
)
200 n
= test_malloc(n_bytes
);
202 memcpy(n
, mem
, n_bytes
);
209 static GMemVTable memory_leak_check
= {
218 int main(SIPE_UNUSED_PARAMETER
int argc
, SIPE_UNUSED_PARAMETER
char **argv
)
221 const sipe_xml
*child1
, *child2
;
225 * No idea why the memory leak checks work on some platforms
226 * but fail on others :-( Disable for now...
228 g_mem_set_vtable(&memory_leak_check
);
230 (void) memory_leak_check
;
234 xml
= assert_parse(NULL
, FALSE
);
235 assert_stringify(xml
, 1, NULL
);
237 xml
= assert_parse("", FALSE
);
239 xml
= assert_parse("<?xml version=\"1.0\" ?>", FALSE
);
243 xml
= assert_parse("<test></test>", TRUE
);
244 assert_name(xml
, "test");
245 assert_data(xml
, NULL
);
246 assert_stringify(xml
, 1, "<test/>");
248 xml
= assert_parse("<test/>", TRUE
);
249 assert_name(xml
, "test");
250 assert_data(xml
, NULL
);
251 assert_stringify(xml
, 1, teststring
);
253 xml
= assert_parse("<test>a</test>", TRUE
);
254 assert_name(xml
, "test");
255 assert_data(xml
, "a");
256 assert_stringify(xml
, 1, teststring
);
258 xml
= assert_parse("<test>a\nb</test>", TRUE
);
259 assert_name(xml
, "test");
260 assert_data(xml
, "a\nb");
261 assert_stringify(xml
, 1, teststring
);
265 xml
= assert_parse("<test>a<child>b</child></test>", TRUE
);
266 assert_name(xml
, "test");
267 assert_data(xml
, "a");
268 child1
= assert_child(xml
, NULL
, FALSE
);
269 child1
= assert_child(xml
, "child", TRUE
);
270 assert_name(child1
, "child");
271 assert_data(child1
, "b");
272 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
273 assert_data(child1
, NULL
);
274 assert_stringify(xml
, 1, teststring
);
277 xml
= assert_parse("<test>a<child/></test>", TRUE
);
278 assert_name(xml
, "test");
279 assert_data(xml
, "a");
280 child1
= assert_child(xml
, "child", TRUE
);
281 assert_name(child1
, "child");
282 assert_data(child1
, NULL
);
283 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
284 assert_data(child1
, NULL
);
285 assert_stringify(xml
, 1, teststring
);
288 xml
= assert_parse("<test>a<child>b<inner>c</inner></child></test>", TRUE
);
289 assert_name(xml
, "test");
290 assert_data(xml
, "a");
291 child1
= assert_child(xml
, "child", TRUE
);
292 assert_name(child1
, "child");
293 assert_data(child1
, "b");
294 child1
= assert_child(child1
, "inner", TRUE
);
295 assert_name(child1
, "inner");
296 assert_data(child1
, "c");
297 child1
= assert_child(xml
, "child/inner", TRUE
);
298 assert_name(child1
, "inner");
299 assert_data(child1
, "c");
300 assert_stringify(xml
, 1, teststring
);
303 xml
= assert_parse("<test>a<child>b<inner>c<innerinner>d</innerinner></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 child2
= assert_child(child1
, "inner/innerinner", TRUE
);
310 assert_name(child2
, "innerinner");
311 assert_data(child2
, "d");
312 child1
= assert_child(child1
, "inner", TRUE
);
313 assert_name(child1
, "inner");
314 assert_data(child1
, "c");
315 child1
= assert_child(child1
, "innerinner", TRUE
);
316 assert_name(child1
, "innerinner");
317 assert_data(child1
, "d");
318 child1
= assert_child(xml
, "child/inner", TRUE
);
319 assert_name(child1
, "inner");
320 assert_data(child1
, "c");
321 child1
= assert_child(xml
, "child/inner/innerinner", TRUE
);
322 assert_name(child1
, "innerinner");
323 assert_data(child1
, "d");
324 assert_stringify(xml
, 1, teststring
);
328 xml
= assert_parse("<test a=\"\">a</test>", TRUE
);
329 assert_name(xml
, "test");
330 assert_data(xml
, "a");
331 assert_attribute(xml
, NULL
, NULL
);
332 assert_attribute(xml
, "a", "");
333 assert_attribute(xml
, "b", NULL
);
334 assert_stringify(xml
, 1, teststring
);
337 xml
= assert_parse("<test a=\"1\" b=\"abc\">a</test>", TRUE
);
338 assert_name(xml
, "test");
339 assert_data(xml
, "a");
340 assert_attribute(xml
, "a", "1");
341 assert_int_attribute(xml
, "a", 1, 0);
342 assert_attribute(xml
, "b", "abc");
343 assert_attribute(xml
, "c", NULL
);
344 assert_int_attribute(xml
, "d", 100, 200);
345 /* the attribute order depends on glib hashing :-( */
346 assert_stringify(xml
, 2, teststring
, "<test b=\"abc\" a=\"1\">a</test>");
349 /* attributes with namespace */
350 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
);
351 assert_name(xml
, "row");
352 assert_data(xml
, NULL
);
353 assert_attribute(xml
, "uri", "sip:");
354 assert_attribute(xml
, "displayName", "X");
355 assert_attribute(xml
, "title", "Y");
356 assert_attribute(xml
, "office", "Z");
357 assert_attribute(xml
, "phone", "0");
358 assert_attribute(xml
, "company", "A");
359 assert_attribute(xml
, "city", "B");
360 assert_attribute(xml
, "state", "C");
361 assert_attribute(xml
, "country", "D");
362 assert_attribute(xml
, "email", "E");
365 xml
= assert_parse("<state xsi:type=\"aggregateState\" lastActive=\"date\" xmlns:xsi=\"http://one\" xmlns=\"http://two\"><availability>15500</availability></state>", TRUE
);
366 assert_name(xml
, "state");
367 assert_data(xml
, NULL
);
368 assert_attribute(xml
, "type", "aggregateState");
369 assert_attribute(xml
, "lastActive", "date");
370 assert_attribute(xml
, "xsi", "http://one");
371 assert_attribute(xml
, "xmlns", "http://two");
372 child1
= assert_child(xml
, "availability", TRUE
);
373 assert_name(child1
, "availability");
374 assert_data(child1
, "15500");
378 xml
= assert_parse("t", FALSE
);
380 xml
= assert_parse("<>", FALSE
);
382 xml
= assert_parse("<></>", FALSE
);
384 xml
= assert_parse("<test>", FALSE
);
386 xml
= assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE
);
390 printf("MEMORY LEAK: %" G_GSIZE_FORMAT
" still allocated\n", allocated
);
393 printf("MEMORY LEAK CHECK OK\n");
397 printf("Result: %d PASSED %d FAILED\n", succeeded
, failed
);