core cleanup: rename core & backend API headers
[siplcs.git] / src / core / sipe-xml-tests.c
blob180dabc97c3f731a1ba6d1fc89eaf9766abcad91
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 const sipe_xml *assert_child(const sipe_xml *xml, const gchar *s, gboolean ok)
76 const sipe_xml *child = sipe_xml_child(xml, s);
78 if ((ok && child) || (!ok && !child)) {
79 succeeded++;
80 } else {
81 printf("[%s]\nXML child FAILED: %p '%s'\n",
82 teststring, xml, s ? s : "(nil)");
83 failed++;
85 return(child);
88 static void assert_data(const sipe_xml *xml, const gchar *s)
90 gchar *data = sipe_xml_data(xml);
92 if (sipe_strequal(s, data)) {
93 succeeded++;
94 } else {
95 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
96 teststring, data ? data : "(nil)", s ? s : "(nil)");
97 failed++;
99 g_free(data);
102 static void assert_attribute(const sipe_xml *xml,
103 const gchar *key, const gchar *value)
105 const gchar *attr = sipe_xml_attribute(xml, key);
107 if (sipe_strequal(value, attr)) {
108 succeeded++;
109 } else {
110 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
111 teststring, key ? key : "(nil)",
112 attr ? attr : "(nil)", value ? value : "(nil)");
113 failed++;
117 static void assert_int_attribute(const sipe_xml *xml,
118 const gchar *key, gint value, gint fallback)
120 gint attr = sipe_xml_int_attribute(xml, key, fallback);
122 if ((attr == value) || (attr == fallback)) {
123 succeeded++;
124 } else {
125 printf("[%s]\nXML int attr FAILED: '%s': %d expected: %d/%d\n",
126 teststring, key ? key : "(nil)",
127 attr, value, fallback);
128 failed++;
132 static void assert_stringify(const sipe_xml *xml,
133 int expected, ...)
135 va_list args;
136 gchar *string = sipe_xml_stringify(xml);
138 va_start(args, expected);
139 while (expected-- > 0) {
140 const gchar *alternative = va_arg(args, const gchar *);
141 if (sipe_strequal(string, alternative)) {
142 succeeded++;
143 break;
144 } else {
145 printf("XML stringify alternative FAILED: '%s' (trying next...)\n",
146 alternative ? alternative : "(nil)");
149 va_end(args);
151 if (expected < 0) {
152 printf("[%s]\nXML stringify all alternatives FAILED: '%s'\n",
153 teststring, string ? string : "(nil)");
154 failed++;
157 g_free(string);
161 /* memory leak check */
162 static gsize allocated = 0;
164 static gpointer test_malloc(gsize n_bytes)
166 gsize *m = malloc(sizeof(gsize) + n_bytes);
167 if (!m) return(NULL);
168 allocated += n_bytes;
169 m[0] = n_bytes;
170 return(m + 1);
173 static void test_free(gpointer mem)
175 gsize *m = mem;
176 if (!m) return;
177 m--;
178 allocated -= m[0];
179 free(m);
182 static gpointer test_realloc(gpointer mem, gsize n_bytes)
184 guint8 *n = NULL;
185 if (n_bytes) {
186 n = test_malloc(n_bytes);
187 if (mem && n) {
188 memcpy(n, mem, n_bytes);
191 test_free(mem);
192 return(n);
195 static GMemVTable memory_leak_check = {
196 &test_malloc,
197 &test_realloc,
198 &test_free,
199 NULL,
200 NULL,
201 NULL,
204 int main(SIPE_UNUSED_PARAMETER int argc, SIPE_UNUSED_PARAMETER char **argv)
206 sipe_xml *xml;
207 const sipe_xml *child1, *child2;
209 #if 0
211 * No idea why the memory leak checks work on some platforms
212 * but fail on others :-( Disable for now...
214 g_mem_set_vtable(&memory_leak_check);
215 #else
216 (void) memory_leak_check;
217 #endif
219 /* empty XML */
220 xml = assert_parse(NULL, FALSE);
221 assert_stringify(xml, 1, NULL);
222 sipe_xml_free(xml);
223 xml = assert_parse("", FALSE);
224 sipe_xml_free(xml);
225 xml = assert_parse("<?xml version=\"1.0\" ?>", FALSE);
226 sipe_xml_free(xml);
228 /* one node */
229 xml = assert_parse("<test></test>", TRUE);
230 assert_data(xml, NULL);
231 assert_stringify(xml, 1, "<test/>");
232 sipe_xml_free(xml);
233 xml = assert_parse("<test/>", TRUE);
234 assert_data(xml, NULL);
235 assert_stringify(xml, 1, teststring);
236 sipe_xml_free(xml);
237 xml = assert_parse("<test>a</test>", TRUE);
238 assert_data(xml, "a");
239 assert_stringify(xml, 1, teststring);
240 sipe_xml_free(xml);
241 xml = assert_parse("<test>a\nb</test>", TRUE);
242 assert_data(xml, "a\nb");
243 assert_stringify(xml, 1, teststring);
244 sipe_xml_free(xml);
246 /* child node */
247 xml = assert_parse("<test>a<child>b</child></test>", TRUE);
248 assert_data(xml, "a");
249 child1 = assert_child(xml, NULL, FALSE);
250 child1 = assert_child(xml, "child", TRUE);
251 assert_data(child1, "b");
252 child1 = assert_child(xml, "shouldnotmatch", FALSE);
253 assert_data(child1, NULL);
254 assert_stringify(xml, 1, teststring);
255 sipe_xml_free(xml);
257 xml = assert_parse("<test>a<child/></test>", TRUE);
258 assert_data(xml, "a");
259 child1 = assert_child(xml, "child", TRUE);
260 assert_data(child1, NULL);
261 child1 = assert_child(xml, "shouldnotmatch", FALSE);
262 assert_data(child1, NULL);
263 assert_stringify(xml, 1, teststring);
264 sipe_xml_free(xml);
266 xml = assert_parse("<test>a<child>b<inner>c</inner></child></test>", TRUE);
267 assert_data(xml, "a");
268 child1 = assert_child(xml, "child", TRUE);
269 assert_data(child1, "b");
270 child1 = assert_child(child1, "inner", TRUE);
271 assert_data(child1, "c");
272 child1 = assert_child(xml, "child/inner", TRUE);
273 assert_data(child1, "c");
274 assert_stringify(xml, 1, teststring);
275 sipe_xml_free(xml);
277 xml = assert_parse("<test>a<child>b<inner>c<innerinner>d</innerinner></inner></child></test>", TRUE);
278 assert_data(xml, "a");
279 child1 = assert_child(xml, "child", TRUE);
280 assert_data(child1, "b");
281 child2 = assert_child(child1, "inner/innerinner", TRUE);
282 assert_data(child2, "d");
283 child1 = assert_child(child1, "inner", TRUE);
284 assert_data(child1, "c");
285 child1 = assert_child(child1, "innerinner", TRUE);
286 assert_data(child1, "d");
287 child1 = assert_child(xml, "child/inner", TRUE);
288 assert_data(child1, "c");
289 child1 = assert_child(xml, "child/inner/innerinner", TRUE);
290 assert_data(child1, "d");
291 assert_stringify(xml, 1, teststring);
292 sipe_xml_free(xml);
294 /* attributes */
295 xml = assert_parse("<test a=\"\">a</test>", TRUE);
296 assert_data(xml, "a");
297 assert_attribute(xml, NULL, NULL);
298 assert_attribute(xml, "a", "");
299 assert_attribute(xml, "b", NULL);
300 assert_stringify(xml, 1, teststring);
301 sipe_xml_free(xml);
303 xml = assert_parse("<test a=\"1\" b=\"abc\">a</test>", TRUE);
304 assert_data(xml, "a");
305 assert_attribute(xml, "a", "1");
306 assert_int_attribute(xml, "a", 1, 0);
307 assert_attribute(xml, "b", "abc");
308 assert_attribute(xml, "c", NULL);
309 assert_int_attribute(xml, "d", 100, 200);
310 /* the attribute order depends on glib hashing :-( */
311 assert_stringify(xml, 2, teststring, "<test b=\"abc\" a=\"1\">a</test>");
312 sipe_xml_free(xml);
314 /* broken XML */
315 xml = assert_parse("t", FALSE);
316 sipe_xml_free(xml);
317 xml = assert_parse("<>", FALSE);
318 sipe_xml_free(xml);
319 xml = assert_parse("<></>", FALSE);
320 sipe_xml_free(xml);
321 xml = assert_parse("<test>", FALSE);
322 sipe_xml_free(xml);
323 xml = assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE);
324 sipe_xml_free(xml);
326 if (allocated) {
327 printf("MEMORY LEAK: %" G_GSIZE_FORMAT " still allocated\n", allocated);
328 failed++;
329 } else {
330 printf("MEMORY LEAK CHECK OK\n");
331 succeeded++;
334 printf("Result: %d PASSED %d FAILED\n", succeeded, failed);
335 return(failed);
339 Local Variables:
340 mode: c
341 c-file-style: "bsd"
342 indent-tabs-mode: t
343 tab-width: 8
344 End: