update ChangeLog
[siplcs.git] / src / core / sipe-xml-tests.c
blob6b9a6a8df7795b67893426a48f67ca2f2750141d
1 /**
2 * @file sipe-xml-tests.c
4 * pidgin-sipe
6 * Copyright (C) 2010-12 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-digest.h"
36 #include "sipe-xml.h"
37 #include "sipe-utils.h"
39 /* stub functions for backend API */
40 void sipe_backend_debug_literal(sipe_debug_level level,
41 const gchar *msg)
43 printf("DEBUG %d: %s", level, msg);
45 void sipe_backend_debug(sipe_debug_level level,
46 const gchar *format,
47 ...)
49 va_list args;
50 gchar *msg;
51 va_start(args, format);
52 msg = g_strdup_vprintf(format, args);
53 va_end(args);
55 sipe_backend_debug_literal(level, msg);
56 g_free(msg);
58 gboolean sipe_backend_debug_enabled(void)
60 return TRUE;
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(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public) { return(NULL); }
68 /* test helpers */
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)) {
80 succeeded++;
81 } else {
82 printf("[%s]\nXML parse FAILED: %p\n",
83 teststring, xml);
84 failed++;
86 return(xml);
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)) {
94 succeeded++;
95 } else {
96 printf("[%s]\nXML name FAILED: '%s' expected: '%s'\n",
97 teststring, name ? name : "(nil)", s ? s : "(nil)");
98 failed++;
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)) {
108 succeeded++;
109 } else {
110 printf("[%s]\nXML child FAILED: %p '%s'\n",
111 teststring, xml, s ? s : "(nil)");
112 failed++;
114 return(child);
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)) {
122 succeeded++;
123 } else {
124 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
125 teststring, data ? data : "(nil)", s ? s : "(nil)");
126 failed++;
128 g_free(data);
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)) {
137 succeeded++;
138 } else {
139 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
140 teststring, key ? key : "(nil)",
141 attr ? attr : "(nil)", value ? value : "(nil)");
142 failed++;
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)) {
152 succeeded++;
153 } else {
154 printf("[%s]\nXML int attr FAILED: '%s': %d expected: %d/%d\n",
155 teststring, key ? key : "(nil)",
156 attr, value, fallback);
157 failed++;
161 static void assert_stringify(const sipe_xml *xml,
162 int expected, ...)
164 va_list args;
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)) {
171 succeeded++;
172 break;
173 } else {
174 printf("XML stringify alternative FAILED: '%s' (trying next...)\n",
175 alternative ? alternative : "(nil)");
178 va_end(args);
180 if (expected < 0) {
181 printf("[%s]\nXML stringify all alternatives FAILED: '%s'\n",
182 teststring, string ? string : "(nil)");
183 failed++;
186 g_free(string);
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;
198 m[0] = n_bytes;
199 return(m + 1);
202 static void test_free(gpointer mem)
204 gsize *m = mem;
205 if (!m) return;
206 m--;
207 allocated -= m[0];
208 free(m);
211 static gpointer test_realloc(gpointer mem, gsize n_bytes)
213 guint8 *n = NULL;
214 if (n_bytes) {
215 n = test_malloc(n_bytes);
216 if (mem && n) {
217 memcpy(n, mem, n_bytes);
220 test_free(mem);
221 return(n);
224 static GMemVTable memory_leak_check = {
225 &test_malloc,
226 &test_realloc,
227 &test_free,
228 NULL,
229 NULL,
230 NULL,
233 int main(SIPE_UNUSED_PARAMETER int argc, SIPE_UNUSED_PARAMETER char **argv)
235 sipe_xml *xml;
236 const sipe_xml *child1, *child2;
238 #if 0
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);
244 #else
245 (void) memory_leak_check;
246 #endif
248 /* empty XML */
249 xml = assert_parse(NULL, FALSE);
250 assert_stringify(xml, 1, NULL);
251 sipe_xml_free(xml);
252 xml = assert_parse("", FALSE);
253 sipe_xml_free(xml);
254 xml = assert_parse("<?xml version=\"1.0\" ?>", FALSE);
255 sipe_xml_free(xml);
257 /* one node */
258 xml = assert_parse("<test></test>", TRUE);
259 assert_name(xml, "test");
260 assert_data(xml, NULL);
261 assert_stringify(xml, 1, "<test/>");
262 sipe_xml_free(xml);
263 xml = assert_parse("<test/>", TRUE);
264 assert_name(xml, "test");
265 assert_data(xml, NULL);
266 assert_stringify(xml, 1, teststring);
267 sipe_xml_free(xml);
268 xml = assert_parse("<test>a</test>", TRUE);
269 assert_name(xml, "test");
270 assert_data(xml, "a");
271 assert_stringify(xml, 1, teststring);
272 sipe_xml_free(xml);
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);
277 sipe_xml_free(xml);
279 /* child node */
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);
290 sipe_xml_free(xml);
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);
301 sipe_xml_free(xml);
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);
316 sipe_xml_free(xml);
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);
340 sipe_xml_free(xml);
342 /* attributes */
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);
350 sipe_xml_free(xml);
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>");
362 sipe_xml_free(xml);
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");
378 sipe_xml_free(xml);
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");
390 sipe_xml_free(xml);
392 /* broken XML */
393 xml = assert_parse("t", FALSE);
394 sipe_xml_free(xml);
395 xml = assert_parse("<>", FALSE);
396 sipe_xml_free(xml);
397 xml = assert_parse("<></>", FALSE);
398 sipe_xml_free(xml);
399 xml = assert_parse("<test>", FALSE);
400 sipe_xml_free(xml);
401 xml = assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE);
402 sipe_xml_free(xml);
404 if (allocated) {
405 printf("MEMORY LEAK: %" G_GSIZE_FORMAT " still allocated\n", allocated);
406 failed++;
407 } else {
408 printf("MEMORY LEAK CHECK OK\n");
409 succeeded++;
412 printf("Result: %d PASSED %d FAILED\n", succeeded, failed);
413 return(failed);
417 Local Variables:
418 mode: c
419 c-file-style: "bsd"
420 indent-tabs-mode: t
421 tab-width: 8
422 End: