xml: added namespace tests
[siplcs.git] / src / core / sipe-xml-tests.c
blob2a88f64a5bb17edc9444cfd73526ce54cb467efb
1 /**
2 * @file sipe-xml-tests.c
4 * pidgin-sipe
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 */
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <time.h>
31 #include <glib.h>
33 #include "sipe-common.h"
34 #include "sipe-backend.h"
35 #include "sipe-xml.h"
36 #include "sipe-utils.h"
38 /* stub functions for backend API */
39 void sipe_backend_debug(sipe_debug_level level,
40 const gchar *format,
41 ...)
43 va_list args;
44 gchar *msg;
45 va_start(args, format);
46 msg = g_strdup_vprintf(format, args);
47 va_end(args);
49 printf("DEBUG %d: %s", level, msg);
50 g_free(msg);
53 /* test helpers */
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)) {
65 succeeded++;
66 } else {
67 printf("[%s]\nXML parse FAILED: %p\n",
68 teststring, xml);
69 failed++;
71 return(xml);
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)) {
79 succeeded++;
80 } else {
81 printf("[%s]\nXML name FAILED: '%s' expected: '%s'\n",
82 teststring, name ? name : "(nil)", s ? s : "(nil)");
83 failed++;
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)) {
93 succeeded++;
94 } else {
95 printf("[%s]\nXML child FAILED: %p '%s'\n",
96 teststring, xml, s ? s : "(nil)");
97 failed++;
99 return(child);
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)) {
107 succeeded++;
108 } else {
109 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
110 teststring, data ? data : "(nil)", s ? s : "(nil)");
111 failed++;
113 g_free(data);
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)) {
122 succeeded++;
123 } else {
124 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
125 teststring, key ? key : "(nil)",
126 attr ? attr : "(nil)", value ? value : "(nil)");
127 failed++;
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)) {
137 succeeded++;
138 } else {
139 printf("[%s]\nXML int attr FAILED: '%s': %d expected: %d/%d\n",
140 teststring, key ? key : "(nil)",
141 attr, value, fallback);
142 failed++;
146 static void assert_stringify(const sipe_xml *xml,
147 int expected, ...)
149 va_list args;
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)) {
156 succeeded++;
157 break;
158 } else {
159 printf("XML stringify alternative FAILED: '%s' (trying next...)\n",
160 alternative ? alternative : "(nil)");
163 va_end(args);
165 if (expected < 0) {
166 printf("[%s]\nXML stringify all alternatives FAILED: '%s'\n",
167 teststring, string ? string : "(nil)");
168 failed++;
171 g_free(string);
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;
183 m[0] = n_bytes;
184 return(m + 1);
187 static void test_free(gpointer mem)
189 gsize *m = mem;
190 if (!m) return;
191 m--;
192 allocated -= m[0];
193 free(m);
196 static gpointer test_realloc(gpointer mem, gsize n_bytes)
198 guint8 *n = NULL;
199 if (n_bytes) {
200 n = test_malloc(n_bytes);
201 if (mem && n) {
202 memcpy(n, mem, n_bytes);
205 test_free(mem);
206 return(n);
209 static GMemVTable memory_leak_check = {
210 &test_malloc,
211 &test_realloc,
212 &test_free,
213 NULL,
214 NULL,
215 NULL,
218 int main(SIPE_UNUSED_PARAMETER int argc, SIPE_UNUSED_PARAMETER char **argv)
220 sipe_xml *xml;
221 const sipe_xml *child1, *child2;
223 #if 0
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);
229 #else
230 (void) memory_leak_check;
231 #endif
233 /* empty XML */
234 xml = assert_parse(NULL, FALSE);
235 assert_stringify(xml, 1, NULL);
236 sipe_xml_free(xml);
237 xml = assert_parse("", FALSE);
238 sipe_xml_free(xml);
239 xml = assert_parse("<?xml version=\"1.0\" ?>", FALSE);
240 sipe_xml_free(xml);
242 /* one node */
243 xml = assert_parse("<test></test>", TRUE);
244 assert_name(xml, "test");
245 assert_data(xml, NULL);
246 assert_stringify(xml, 1, "<test/>");
247 sipe_xml_free(xml);
248 xml = assert_parse("<test/>", TRUE);
249 assert_name(xml, "test");
250 assert_data(xml, NULL);
251 assert_stringify(xml, 1, teststring);
252 sipe_xml_free(xml);
253 xml = assert_parse("<test>a</test>", TRUE);
254 assert_name(xml, "test");
255 assert_data(xml, "a");
256 assert_stringify(xml, 1, teststring);
257 sipe_xml_free(xml);
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);
262 sipe_xml_free(xml);
264 /* child node */
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);
275 sipe_xml_free(xml);
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);
286 sipe_xml_free(xml);
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);
301 sipe_xml_free(xml);
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);
325 sipe_xml_free(xml);
327 /* attributes */
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);
335 sipe_xml_free(xml);
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>");
347 sipe_xml_free(xml);
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");
363 sipe_xml_free(xml);
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");
375 sipe_xml_free(xml);
377 /* broken XML */
378 xml = assert_parse("t", FALSE);
379 sipe_xml_free(xml);
380 xml = assert_parse("<>", FALSE);
381 sipe_xml_free(xml);
382 xml = assert_parse("<></>", FALSE);
383 sipe_xml_free(xml);
384 xml = assert_parse("<test>", FALSE);
385 sipe_xml_free(xml);
386 xml = assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE);
387 sipe_xml_free(xml);
389 if (allocated) {
390 printf("MEMORY LEAK: %" G_GSIZE_FORMAT " still allocated\n", allocated);
391 failed++;
392 } else {
393 printf("MEMORY LEAK CHECK OK\n");
394 succeeded++;
397 printf("Result: %d PASSED %d FAILED\n", succeeded, failed);
398 return(failed);
402 Local Variables:
403 mode: c
404 c-file-style: "bsd"
405 indent-tabs-mode: t
406 tab-width: 8
407 End: