audio: send 486 Busy Here when call is in progress and another call invitation arrives.
[siplcs.git] / src / core / sipe-xml-tests.c
bloba83ed6e1c8e5f804de379394496c3e3657ea52ff
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-digest.h"
36 #include "sipe-xml.h"
37 #include "sipe-utils.h"
39 /* stub functions for backend API */
40 void sipe_backend_debug(sipe_debug_level level,
41 const gchar *format,
42 ...)
44 va_list args;
45 gchar *msg;
46 va_start(args, format);
47 msg = g_strdup_vprintf(format, args);
48 va_end(args);
50 printf("DEBUG %d: %s", level, msg);
51 g_free(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); }
59 /* test helpers */
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)) {
71 succeeded++;
72 } else {
73 printf("[%s]\nXML parse FAILED: %p\n",
74 teststring, xml);
75 failed++;
77 return(xml);
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)) {
85 succeeded++;
86 } else {
87 printf("[%s]\nXML name FAILED: '%s' expected: '%s'\n",
88 teststring, name ? name : "(nil)", s ? s : "(nil)");
89 failed++;
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)) {
99 succeeded++;
100 } else {
101 printf("[%s]\nXML child FAILED: %p '%s'\n",
102 teststring, xml, s ? s : "(nil)");
103 failed++;
105 return(child);
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)) {
113 succeeded++;
114 } else {
115 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
116 teststring, data ? data : "(nil)", s ? s : "(nil)");
117 failed++;
119 g_free(data);
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)) {
128 succeeded++;
129 } else {
130 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
131 teststring, key ? key : "(nil)",
132 attr ? attr : "(nil)", value ? value : "(nil)");
133 failed++;
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)) {
143 succeeded++;
144 } else {
145 printf("[%s]\nXML int attr FAILED: '%s': %d expected: %d/%d\n",
146 teststring, key ? key : "(nil)",
147 attr, value, fallback);
148 failed++;
152 static void assert_stringify(const sipe_xml *xml,
153 int expected, ...)
155 va_list args;
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)) {
162 succeeded++;
163 break;
164 } else {
165 printf("XML stringify alternative FAILED: '%s' (trying next...)\n",
166 alternative ? alternative : "(nil)");
169 va_end(args);
171 if (expected < 0) {
172 printf("[%s]\nXML stringify all alternatives FAILED: '%s'\n",
173 teststring, string ? string : "(nil)");
174 failed++;
177 g_free(string);
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;
189 m[0] = n_bytes;
190 return(m + 1);
193 static void test_free(gpointer mem)
195 gsize *m = mem;
196 if (!m) return;
197 m--;
198 allocated -= m[0];
199 free(m);
202 static gpointer test_realloc(gpointer mem, gsize n_bytes)
204 guint8 *n = NULL;
205 if (n_bytes) {
206 n = test_malloc(n_bytes);
207 if (mem && n) {
208 memcpy(n, mem, n_bytes);
211 test_free(mem);
212 return(n);
215 static GMemVTable memory_leak_check = {
216 &test_malloc,
217 &test_realloc,
218 &test_free,
219 NULL,
220 NULL,
221 NULL,
224 int main(SIPE_UNUSED_PARAMETER int argc, SIPE_UNUSED_PARAMETER char **argv)
226 sipe_xml *xml;
227 const sipe_xml *child1, *child2;
229 #if 0
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);
235 #else
236 (void) memory_leak_check;
237 #endif
239 /* empty XML */
240 xml = assert_parse(NULL, FALSE);
241 assert_stringify(xml, 1, NULL);
242 sipe_xml_free(xml);
243 xml = assert_parse("", FALSE);
244 sipe_xml_free(xml);
245 xml = assert_parse("<?xml version=\"1.0\" ?>", FALSE);
246 sipe_xml_free(xml);
248 /* one node */
249 xml = assert_parse("<test></test>", TRUE);
250 assert_name(xml, "test");
251 assert_data(xml, NULL);
252 assert_stringify(xml, 1, "<test/>");
253 sipe_xml_free(xml);
254 xml = assert_parse("<test/>", TRUE);
255 assert_name(xml, "test");
256 assert_data(xml, NULL);
257 assert_stringify(xml, 1, teststring);
258 sipe_xml_free(xml);
259 xml = assert_parse("<test>a</test>", TRUE);
260 assert_name(xml, "test");
261 assert_data(xml, "a");
262 assert_stringify(xml, 1, teststring);
263 sipe_xml_free(xml);
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);
268 sipe_xml_free(xml);
270 /* child node */
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);
281 sipe_xml_free(xml);
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);
292 sipe_xml_free(xml);
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);
307 sipe_xml_free(xml);
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);
331 sipe_xml_free(xml);
333 /* attributes */
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);
341 sipe_xml_free(xml);
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>");
353 sipe_xml_free(xml);
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");
369 sipe_xml_free(xml);
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");
381 sipe_xml_free(xml);
383 /* broken XML */
384 xml = assert_parse("t", FALSE);
385 sipe_xml_free(xml);
386 xml = assert_parse("<>", FALSE);
387 sipe_xml_free(xml);
388 xml = assert_parse("<></>", FALSE);
389 sipe_xml_free(xml);
390 xml = assert_parse("<test>", FALSE);
391 sipe_xml_free(xml);
392 xml = assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE);
393 sipe_xml_free(xml);
395 if (allocated) {
396 printf("MEMORY LEAK: %" G_GSIZE_FORMAT " still allocated\n", allocated);
397 failed++;
398 } else {
399 printf("MEMORY LEAK CHECK OK\n");
400 succeeded++;
403 printf("Result: %d PASSED %d FAILED\n", succeeded, failed);
404 return(failed);
408 Local Variables:
409 mode: c
410 c-file-style: "bsd"
411 indent-tabs-mode: t
412 tab-width: 8
413 End: