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
);
53 void sipe_backend_digest_sha1(SIPE_UNUSED_PARAMETER
const guchar
*data
,
54 SIPE_UNUSED_PARAMETER gsize length
,
55 SIPE_UNUSED_PARAMETER guchar
*digest
) {}
56 const gchar
*sipe_backend_network_ip_address(void) { return(NULL
); }
59 static guint succeeded
= 0;
60 static guint failed
= 0;
61 static const gchar
*teststring
;
63 static sipe_xml
*assert_parse(const gchar
*s
, gboolean ok
)
65 sipe_xml
*xml
= sipe_xml_parse(s
, s
? strlen(s
) : 0);
67 teststring
= s
? s
: "(nil)";
69 if ((ok
&& xml
) || (!ok
&& !xml
)) {
72 printf("[%s]\nXML parse FAILED: %p\n",
79 static void assert_name(const sipe_xml
*xml
, const gchar
*s
)
81 const gchar
*name
= sipe_xml_name(xml
);
83 if (sipe_strequal(name
, s
)) {
86 printf("[%s]\nXML name FAILED: '%s' expected: '%s'\n",
87 teststring
, name
? name
: "(nil)", s
? s
: "(nil)");
93 static const sipe_xml
*assert_child(const sipe_xml
*xml
, const gchar
*s
, gboolean ok
)
95 const sipe_xml
*child
= sipe_xml_child(xml
, s
);
97 if ((ok
&& child
) || (!ok
&& !child
)) {
100 printf("[%s]\nXML child FAILED: %p '%s'\n",
101 teststring
, xml
, s
? s
: "(nil)");
107 static void assert_data(const sipe_xml
*xml
, const gchar
*s
)
109 gchar
*data
= sipe_xml_data(xml
);
111 if (sipe_strequal(s
, data
)) {
114 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
115 teststring
, data
? data
: "(nil)", s
? s
: "(nil)");
121 static void assert_attribute(const sipe_xml
*xml
,
122 const gchar
*key
, const gchar
*value
)
124 const gchar
*attr
= sipe_xml_attribute(xml
, key
);
126 if (sipe_strequal(value
, attr
)) {
129 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
130 teststring
, key
? key
: "(nil)",
131 attr
? attr
: "(nil)", value
? value
: "(nil)");
136 static void assert_int_attribute(const sipe_xml
*xml
,
137 const gchar
*key
, gint value
, gint fallback
)
139 gint attr
= sipe_xml_int_attribute(xml
, key
, fallback
);
141 if ((attr
== value
) || (attr
== fallback
)) {
144 printf("[%s]\nXML int attr FAILED: '%s': %d expected: %d/%d\n",
145 teststring
, key
? key
: "(nil)",
146 attr
, value
, fallback
);
151 static void assert_stringify(const sipe_xml
*xml
,
155 gchar
*string
= sipe_xml_stringify(xml
);
157 va_start(args
, expected
);
158 while (expected
-- > 0) {
159 const gchar
*alternative
= va_arg(args
, const gchar
*);
160 if (sipe_strequal(string
, alternative
)) {
164 printf("XML stringify alternative FAILED: '%s' (trying next...)\n",
165 alternative
? alternative
: "(nil)");
171 printf("[%s]\nXML stringify all alternatives FAILED: '%s'\n",
172 teststring
, string
? string
: "(nil)");
180 /* memory leak check */
181 static gsize allocated
= 0;
183 static gpointer
test_malloc(gsize n_bytes
)
185 gsize
*m
= malloc(sizeof(gsize
) + n_bytes
);
186 if (!m
) return(NULL
);
187 allocated
+= n_bytes
;
192 static void test_free(gpointer mem
)
201 static gpointer
test_realloc(gpointer mem
, gsize n_bytes
)
205 n
= test_malloc(n_bytes
);
207 memcpy(n
, mem
, n_bytes
);
214 static GMemVTable memory_leak_check
= {
223 int main(SIPE_UNUSED_PARAMETER
int argc
, SIPE_UNUSED_PARAMETER
char **argv
)
226 const sipe_xml
*child1
, *child2
;
230 * No idea why the memory leak checks work on some platforms
231 * but fail on others :-( Disable for now...
233 g_mem_set_vtable(&memory_leak_check
);
235 (void) memory_leak_check
;
239 xml
= assert_parse(NULL
, FALSE
);
240 assert_stringify(xml
, 1, NULL
);
242 xml
= assert_parse("", FALSE
);
244 xml
= assert_parse("<?xml version=\"1.0\" ?>", FALSE
);
248 xml
= assert_parse("<test></test>", TRUE
);
249 assert_name(xml
, "test");
250 assert_data(xml
, NULL
);
251 assert_stringify(xml
, 1, "<test/>");
253 xml
= assert_parse("<test/>", TRUE
);
254 assert_name(xml
, "test");
255 assert_data(xml
, NULL
);
256 assert_stringify(xml
, 1, teststring
);
258 xml
= assert_parse("<test>a</test>", TRUE
);
259 assert_name(xml
, "test");
260 assert_data(xml
, "a");
261 assert_stringify(xml
, 1, teststring
);
263 xml
= assert_parse("<test>a\nb</test>", TRUE
);
264 assert_name(xml
, "test");
265 assert_data(xml
, "a\nb");
266 assert_stringify(xml
, 1, teststring
);
270 xml
= assert_parse("<test>a<child>b</child></test>", TRUE
);
271 assert_name(xml
, "test");
272 assert_data(xml
, "a");
273 child1
= assert_child(xml
, NULL
, FALSE
);
274 child1
= assert_child(xml
, "child", TRUE
);
275 assert_name(child1
, "child");
276 assert_data(child1
, "b");
277 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
278 assert_data(child1
, NULL
);
279 assert_stringify(xml
, 1, teststring
);
282 xml
= assert_parse("<test>a<child/></test>", TRUE
);
283 assert_name(xml
, "test");
284 assert_data(xml
, "a");
285 child1
= assert_child(xml
, "child", TRUE
);
286 assert_name(child1
, "child");
287 assert_data(child1
, NULL
);
288 child1
= assert_child(xml
, "shouldnotmatch", FALSE
);
289 assert_data(child1
, NULL
);
290 assert_stringify(xml
, 1, teststring
);
293 xml
= assert_parse("<test>a<child>b<inner>c</inner></child></test>", TRUE
);
294 assert_name(xml
, "test");
295 assert_data(xml
, "a");
296 child1
= assert_child(xml
, "child", TRUE
);
297 assert_name(child1
, "child");
298 assert_data(child1
, "b");
299 child1
= assert_child(child1
, "inner", TRUE
);
300 assert_name(child1
, "inner");
301 assert_data(child1
, "c");
302 child1
= assert_child(xml
, "child/inner", TRUE
);
303 assert_name(child1
, "inner");
304 assert_data(child1
, "c");
305 assert_stringify(xml
, 1, teststring
);
308 xml
= assert_parse("<test>a<child>b<inner>c<innerinner>d</innerinner></inner></child></test>", TRUE
);
309 assert_name(xml
, "test");
310 assert_data(xml
, "a");
311 child1
= assert_child(xml
, "child", TRUE
);
312 assert_name(child1
, "child");
313 assert_data(child1
, "b");
314 child2
= assert_child(child1
, "inner/innerinner", TRUE
);
315 assert_name(child2
, "innerinner");
316 assert_data(child2
, "d");
317 child1
= assert_child(child1
, "inner", TRUE
);
318 assert_name(child1
, "inner");
319 assert_data(child1
, "c");
320 child1
= assert_child(child1
, "innerinner", TRUE
);
321 assert_name(child1
, "innerinner");
322 assert_data(child1
, "d");
323 child1
= assert_child(xml
, "child/inner", TRUE
);
324 assert_name(child1
, "inner");
325 assert_data(child1
, "c");
326 child1
= assert_child(xml
, "child/inner/innerinner", TRUE
);
327 assert_name(child1
, "innerinner");
328 assert_data(child1
, "d");
329 assert_stringify(xml
, 1, teststring
);
333 xml
= assert_parse("<test a=\"\">a</test>", TRUE
);
334 assert_name(xml
, "test");
335 assert_data(xml
, "a");
336 assert_attribute(xml
, NULL
, NULL
);
337 assert_attribute(xml
, "a", "");
338 assert_attribute(xml
, "b", NULL
);
339 assert_stringify(xml
, 1, teststring
);
342 xml
= assert_parse("<test a=\"1\" b=\"abc\">a</test>", TRUE
);
343 assert_name(xml
, "test");
344 assert_data(xml
, "a");
345 assert_attribute(xml
, "a", "1");
346 assert_int_attribute(xml
, "a", 1, 0);
347 assert_attribute(xml
, "b", "abc");
348 assert_attribute(xml
, "c", NULL
);
349 assert_int_attribute(xml
, "d", 100, 200);
350 /* the attribute order depends on glib hashing :-( */
351 assert_stringify(xml
, 2, teststring
, "<test b=\"abc\" a=\"1\">a</test>");
354 /* attributes with namespace */
355 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
);
356 assert_name(xml
, "row");
357 assert_data(xml
, NULL
);
358 assert_attribute(xml
, "uri", "sip:");
359 assert_attribute(xml
, "displayName", "X");
360 assert_attribute(xml
, "title", "Y");
361 assert_attribute(xml
, "office", "Z");
362 assert_attribute(xml
, "phone", "0");
363 assert_attribute(xml
, "company", "A");
364 assert_attribute(xml
, "city", "B");
365 assert_attribute(xml
, "state", "C");
366 assert_attribute(xml
, "country", "D");
367 assert_attribute(xml
, "email", "E");
370 xml
= assert_parse("<state xsi:type=\"aggregateState\" lastActive=\"date\" xmlns:xsi=\"http://one\" xmlns=\"http://two\"><availability>15500</availability></state>", TRUE
);
371 assert_name(xml
, "state");
372 assert_data(xml
, NULL
);
373 assert_attribute(xml
, "type", "aggregateState");
374 assert_attribute(xml
, "lastActive", "date");
375 assert_attribute(xml
, "xsi", "http://one");
376 assert_attribute(xml
, "xmlns", "http://two");
377 child1
= assert_child(xml
, "availability", TRUE
);
378 assert_name(child1
, "availability");
379 assert_data(child1
, "15500");
383 xml
= assert_parse("t", FALSE
);
385 xml
= assert_parse("<>", FALSE
);
387 xml
= assert_parse("<></>", FALSE
);
389 xml
= assert_parse("<test>", FALSE
);
391 xml
= assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE
);
395 printf("MEMORY LEAK: %" G_GSIZE_FORMAT
" still allocated\n", allocated
);
398 printf("MEMORY LEAK CHECK OK\n");
402 printf("Result: %d PASSED %d FAILED\n", succeeded
, failed
);