l10n: Updates to Portuguese (Brazilian) (pt_BR) translation
[siplcs.git] / src / core / sipe-xml-tests.c
blobecc39545393d2a2be2669bb4db93d3e771439004
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 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); }
58 /* test helpers */
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)) {
70 succeeded++;
71 } else {
72 printf("[%s]\nXML parse FAILED: %p\n",
73 teststring, xml);
74 failed++;
76 return(xml);
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)) {
84 succeeded++;
85 } else {
86 printf("[%s]\nXML name FAILED: '%s' expected: '%s'\n",
87 teststring, name ? name : "(nil)", s ? s : "(nil)");
88 failed++;
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)) {
98 succeeded++;
99 } else {
100 printf("[%s]\nXML child FAILED: %p '%s'\n",
101 teststring, xml, s ? s : "(nil)");
102 failed++;
104 return(child);
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)) {
112 succeeded++;
113 } else {
114 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
115 teststring, data ? data : "(nil)", s ? s : "(nil)");
116 failed++;
118 g_free(data);
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)) {
127 succeeded++;
128 } else {
129 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
130 teststring, key ? key : "(nil)",
131 attr ? attr : "(nil)", value ? value : "(nil)");
132 failed++;
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)) {
142 succeeded++;
143 } else {
144 printf("[%s]\nXML int attr FAILED: '%s': %d expected: %d/%d\n",
145 teststring, key ? key : "(nil)",
146 attr, value, fallback);
147 failed++;
151 static void assert_stringify(const sipe_xml *xml,
152 int expected, ...)
154 va_list args;
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)) {
161 succeeded++;
162 break;
163 } else {
164 printf("XML stringify alternative FAILED: '%s' (trying next...)\n",
165 alternative ? alternative : "(nil)");
168 va_end(args);
170 if (expected < 0) {
171 printf("[%s]\nXML stringify all alternatives FAILED: '%s'\n",
172 teststring, string ? string : "(nil)");
173 failed++;
176 g_free(string);
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;
188 m[0] = n_bytes;
189 return(m + 1);
192 static void test_free(gpointer mem)
194 gsize *m = mem;
195 if (!m) return;
196 m--;
197 allocated -= m[0];
198 free(m);
201 static gpointer test_realloc(gpointer mem, gsize n_bytes)
203 guint8 *n = NULL;
204 if (n_bytes) {
205 n = test_malloc(n_bytes);
206 if (mem && n) {
207 memcpy(n, mem, n_bytes);
210 test_free(mem);
211 return(n);
214 static GMemVTable memory_leak_check = {
215 &test_malloc,
216 &test_realloc,
217 &test_free,
218 NULL,
219 NULL,
220 NULL,
223 int main(SIPE_UNUSED_PARAMETER int argc, SIPE_UNUSED_PARAMETER char **argv)
225 sipe_xml *xml;
226 const sipe_xml *child1, *child2;
228 #if 0
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);
234 #else
235 (void) memory_leak_check;
236 #endif
238 /* empty XML */
239 xml = assert_parse(NULL, FALSE);
240 assert_stringify(xml, 1, NULL);
241 sipe_xml_free(xml);
242 xml = assert_parse("", FALSE);
243 sipe_xml_free(xml);
244 xml = assert_parse("<?xml version=\"1.0\" ?>", FALSE);
245 sipe_xml_free(xml);
247 /* one node */
248 xml = assert_parse("<test></test>", TRUE);
249 assert_name(xml, "test");
250 assert_data(xml, NULL);
251 assert_stringify(xml, 1, "<test/>");
252 sipe_xml_free(xml);
253 xml = assert_parse("<test/>", TRUE);
254 assert_name(xml, "test");
255 assert_data(xml, NULL);
256 assert_stringify(xml, 1, teststring);
257 sipe_xml_free(xml);
258 xml = assert_parse("<test>a</test>", TRUE);
259 assert_name(xml, "test");
260 assert_data(xml, "a");
261 assert_stringify(xml, 1, teststring);
262 sipe_xml_free(xml);
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);
267 sipe_xml_free(xml);
269 /* child node */
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);
280 sipe_xml_free(xml);
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);
291 sipe_xml_free(xml);
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);
306 sipe_xml_free(xml);
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);
330 sipe_xml_free(xml);
332 /* attributes */
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);
340 sipe_xml_free(xml);
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>");
352 sipe_xml_free(xml);
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");
368 sipe_xml_free(xml);
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");
380 sipe_xml_free(xml);
382 /* broken XML */
383 xml = assert_parse("t", FALSE);
384 sipe_xml_free(xml);
385 xml = assert_parse("<>", FALSE);
386 sipe_xml_free(xml);
387 xml = assert_parse("<></>", FALSE);
388 sipe_xml_free(xml);
389 xml = assert_parse("<test>", FALSE);
390 sipe_xml_free(xml);
391 xml = assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE);
392 sipe_xml_free(xml);
394 if (allocated) {
395 printf("MEMORY LEAK: %" G_GSIZE_FORMAT " still allocated\n", allocated);
396 failed++;
397 } else {
398 printf("MEMORY LEAK CHECK OK\n");
399 succeeded++;
402 printf("Result: %d PASSED %d FAILED\n", succeeded, failed);
403 return(failed);
407 Local Variables:
408 mode: c
409 c-file-style: "bsd"
410 indent-tabs-mode: t
411 tab-width: 8
412 End: