interface cleanup: flatten interface dependencies
[siplcs.git] / src / core / sipe-xml-tests.c
blob19ba8d79b7da24f21e0405b6ea696a69dd4908c2
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 "xmlnode.h"
35 #include "sipe-common.h"
36 #include "sipe-backend-debug.h"
37 #include "sipe-xml.h"
38 #include "sipe-utils.h"
40 /* stub functions for backend API */
41 void sipe_backend_debug(sipe_debug_level level,
42 const gchar *format,
43 ...)
45 va_list args;
46 gchar *msg;
47 va_start(args, format);
48 msg = g_strdup_vprintf(format, args);
49 va_end(args);
51 printf("DEBUG %d: %s", level, msg);
52 g_free(msg);
55 /* test helpers */
56 static guint succeeded = 0;
57 static guint failed = 0;
58 static const gchar *teststring;
60 static sipe_xml *assert_parse(const gchar *s, gboolean ok)
62 sipe_xml *xml = sipe_xml_parse(s, s ? strlen(s) : 0);
64 teststring = s ? s : "(nil)";
66 if ((ok && xml) || (!ok && !xml)) {
67 succeeded++;
68 } else {
69 printf("[%s]\nXML parse FAILED: %p\n",
70 teststring, xml);
71 failed++;
73 return(xml);
76 static const sipe_xml *assert_child(const sipe_xml *xml, const gchar *s, gboolean ok)
78 const sipe_xml *child = sipe_xml_child(xml, s);
80 if ((ok && child) || (!ok && !child)) {
81 succeeded++;
82 } else {
83 printf("[%s]\nXML child FAILED: %p '%s'\n",
84 teststring, xml, s ? s : "(nil)");
85 failed++;
87 return(child);
90 static void assert_data(const sipe_xml *xml, const gchar *s)
92 gchar *data = sipe_xml_data(xml);
94 if (sipe_strequal(s, data)) {
95 succeeded++;
96 } else {
97 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
98 teststring, data ? data : "(nil)", s ? s : "(nil)");
99 failed++;
101 g_free(data);
104 static void assert_attribute(const sipe_xml *xml,
105 const gchar *key, const gchar *value)
107 const gchar *attr = sipe_xml_attribute(xml, key);
109 if (sipe_strequal(value, attr)) {
110 succeeded++;
111 } else {
112 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
113 teststring, key ? key : "(nil)",
114 attr ? attr : "(nil)", value ? value : "(nil)");
115 failed++;
119 static void assert_int_attribute(const sipe_xml *xml,
120 const gchar *key, gint value, gint fallback)
122 gint attr = sipe_xml_int_attribute(xml, key, fallback);
124 if ((attr == value) || (attr == fallback)) {
125 succeeded++;
126 } else {
127 printf("[%s]\nXML int attr FAILED: '%s': %d expected: %d/%d\n",
128 teststring, key ? key : "(nil)",
129 attr, value, fallback);
130 failed++;
134 static void assert_stringify(const sipe_xml *xml,
135 const gchar *expected)
137 gchar *string = sipe_xml_stringify(xml);
139 if (sipe_strequal(string, expected)) {
140 succeeded++;
141 } else {
142 printf("[%s]\nXML stringify FAILED: '%s' expected: '%s'\n",
143 teststring, string ? string : "(nil)", expected);
144 failed++;
146 g_free(string);
150 /* memory leak check */
151 static gsize allocated = 0;
153 static gpointer test_malloc(gsize n_bytes)
155 gsize *m = malloc(sizeof(gsize) + n_bytes);
156 if (!m) return(NULL);
157 allocated += n_bytes;
158 m[0] = n_bytes;
159 return(m + 1);
162 static void test_free(gpointer mem)
164 gsize *m = mem;
165 if (!m) return;
166 m--;
167 allocated -= m[0];
168 free(m);
171 static gpointer test_realloc(gpointer mem, gsize n_bytes)
173 guint8 *n = NULL;
174 if (n_bytes) {
175 n = test_malloc(n_bytes);
176 if (mem && n) {
177 memcpy(n, mem, n_bytes);
180 test_free(mem);
181 return(n);
184 static GMemVTable memory_leak_check = {
185 &test_malloc,
186 &test_realloc,
187 &test_free,
188 NULL,
189 NULL,
190 NULL,
193 int main(SIPE_UNUSED_PARAMETER int argc, SIPE_UNUSED_PARAMETER char **argv)
195 sipe_xml *xml;
196 const sipe_xml *child1, *child2;
198 #if 0
200 * No idea why the memory leak checks work on some platforms
201 * but fail on others :-( Disable for now...
203 g_mem_set_vtable(&memory_leak_check);
204 #else
205 (void) memory_leak_check;
206 #endif
208 /* empty XML */
209 xml = assert_parse(NULL, FALSE);
210 sipe_xml_free(xml);
211 xml = assert_parse("", FALSE);
212 sipe_xml_free(xml);
213 xml = assert_parse("<?xml version=\"1.0\" ?>", FALSE);
214 sipe_xml_free(xml);
216 /* one node */
217 xml = assert_parse("<test></test>", TRUE);
218 assert_data(xml, NULL);
219 assert_stringify(xml, "<test/>");
220 sipe_xml_free(xml);
221 xml = assert_parse("<test/>", TRUE);
222 assert_data(xml, NULL);
223 assert_stringify(xml, teststring);
224 sipe_xml_free(xml);
225 xml = assert_parse("<test>a</test>", TRUE);
226 assert_data(xml, "a");
227 assert_stringify(xml, teststring);
228 sipe_xml_free(xml);
229 xml = assert_parse("<test>a\nb</test>", TRUE);
230 assert_data(xml, "a\nb");
231 assert_stringify(xml, teststring);
232 sipe_xml_free(xml);
234 /* child node */
235 xml = assert_parse("<test>a<child>b</child></test>", TRUE);
236 assert_data(xml, "a");
237 child1 = assert_child(xml, NULL, FALSE);
238 child1 = assert_child(xml, "child", TRUE);
239 assert_data(child1, "b");
240 child1 = assert_child(xml, "shouldnotmatch", FALSE);
241 assert_data(child1, NULL);
242 assert_stringify(xml, teststring);
243 sipe_xml_free(xml);
245 xml = assert_parse("<test>a<child/></test>", TRUE);
246 assert_data(xml, "a");
247 child1 = assert_child(xml, "child", TRUE);
248 assert_data(child1, NULL);
249 child1 = assert_child(xml, "shouldnotmatch", FALSE);
250 assert_data(child1, NULL);
251 assert_stringify(xml, teststring);
252 sipe_xml_free(xml);
254 xml = assert_parse("<test>a<child>b<inner>c</inner></child></test>", TRUE);
255 assert_data(xml, "a");
256 child1 = assert_child(xml, "child", TRUE);
257 assert_data(child1, "b");
258 child1 = assert_child(child1, "inner", TRUE);
259 assert_data(child1, "c");
260 child1 = assert_child(xml, "child/inner", TRUE);
261 assert_data(child1, "c");
262 assert_stringify(xml, teststring);
263 sipe_xml_free(xml);
265 xml = assert_parse("<test>a<child>b<inner>c<innerinner>d</innerinner></inner></child></test>", TRUE);
266 assert_data(xml, "a");
267 child1 = assert_child(xml, "child", TRUE);
268 assert_data(child1, "b");
269 child2 = assert_child(child1, "inner/innerinner", TRUE);
270 assert_data(child2, "d");
271 child1 = assert_child(child1, "inner", TRUE);
272 assert_data(child1, "c");
273 child1 = assert_child(child1, "innerinner", TRUE);
274 assert_data(child1, "d");
275 child1 = assert_child(xml, "child/inner", TRUE);
276 assert_data(child1, "c");
277 child1 = assert_child(xml, "child/inner/innerinner", TRUE);
278 assert_data(child1, "d");
279 assert_stringify(xml, teststring);
280 sipe_xml_free(xml);
282 /* attributes */
283 xml = assert_parse("<test a=\"\">a</test>", TRUE);
284 assert_data(xml, "a");
285 assert_attribute(xml, NULL, NULL);
286 assert_attribute(xml, "a", "");
287 assert_attribute(xml, "b", NULL);
288 assert_stringify(xml, teststring);
289 sipe_xml_free(xml);
291 xml = assert_parse("<test a=\"1\" b=\"abc\">a</test>", TRUE);
292 assert_data(xml, "a");
293 assert_attribute(xml, "a", "1");
294 assert_int_attribute(xml, "a", 1, 0);
295 assert_attribute(xml, "b", "abc");
296 assert_attribute(xml, "c", NULL);
297 assert_int_attribute(xml, "d", 100, 200);
298 /* the attribute order depends on glib hashing :-( */
299 assert_stringify(xml, "<test b=\"abc\" a=\"1\">a</test>");
300 sipe_xml_free(xml);
302 /* broken XML */
303 xml = assert_parse("t", FALSE);
304 sipe_xml_free(xml);
305 xml = assert_parse("<>", FALSE);
306 sipe_xml_free(xml);
307 xml = assert_parse("<></>", FALSE);
308 sipe_xml_free(xml);
309 xml = assert_parse("<test>", FALSE);
310 sipe_xml_free(xml);
311 xml = assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE);
312 sipe_xml_free(xml);
314 if (allocated) {
315 printf("MEMORY LEAK: %" G_GSIZE_FORMAT " still allocated\n", allocated);
316 failed++;
317 } else {
318 printf("MEMORY LEAK CHECK OK\n");
319 succeeded++;
322 printf("Result: %d PASSED %d FAILED\n", succeeded, failed);
323 return(failed);
327 Local Variables:
328 mode: c
329 c-file-style: "bsd"
330 indent-tabs-mode: t
331 tab-width: 8
332 End: