core cleanup: sipe-csta module is purple free
[siplcs.git] / src / core / sipe-xml-tests.c
blobf4f4835d9827bc2abb7d8a2a97808693ea17fbe8
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-debug.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 const gchar *expected)
135 gchar *string = sipe_xml_stringify(xml);
137 if (sipe_strequal(string, expected)) {
138 succeeded++;
139 } else {
140 printf("[%s]\nXML stringify FAILED: '%s' expected: '%s'\n",
141 teststring, string ? string : "(nil)", expected);
142 failed++;
144 g_free(string);
148 /* memory leak check */
149 static gsize allocated = 0;
151 static gpointer test_malloc(gsize n_bytes)
153 gsize *m = malloc(sizeof(gsize) + n_bytes);
154 if (!m) return(NULL);
155 allocated += n_bytes;
156 m[0] = n_bytes;
157 return(m + 1);
160 static void test_free(gpointer mem)
162 gsize *m = mem;
163 if (!m) return;
164 m--;
165 allocated -= m[0];
166 free(m);
169 static gpointer test_realloc(gpointer mem, gsize n_bytes)
171 guint8 *n = NULL;
172 if (n_bytes) {
173 n = test_malloc(n_bytes);
174 if (mem && n) {
175 memcpy(n, mem, n_bytes);
178 test_free(mem);
179 return(n);
182 static GMemVTable memory_leak_check = {
183 &test_malloc,
184 &test_realloc,
185 &test_free,
186 NULL,
187 NULL,
188 NULL,
191 int main(SIPE_UNUSED_PARAMETER int argc, SIPE_UNUSED_PARAMETER char **argv)
193 sipe_xml *xml;
194 const sipe_xml *child1, *child2;
196 #if 0
198 * No idea why the memory leak checks work on some platforms
199 * but fail on others :-( Disable for now...
201 g_mem_set_vtable(&memory_leak_check);
202 #else
203 (void) memory_leak_check;
204 #endif
206 /* empty XML */
207 xml = assert_parse(NULL, FALSE);
208 assert_stringify(xml, NULL);
209 sipe_xml_free(xml);
210 xml = assert_parse("", FALSE);
211 sipe_xml_free(xml);
212 xml = assert_parse("<?xml version=\"1.0\" ?>", FALSE);
213 sipe_xml_free(xml);
215 /* one node */
216 xml = assert_parse("<test></test>", TRUE);
217 assert_data(xml, NULL);
218 assert_stringify(xml, "<test/>");
219 sipe_xml_free(xml);
220 xml = assert_parse("<test/>", TRUE);
221 assert_data(xml, NULL);
222 assert_stringify(xml, teststring);
223 sipe_xml_free(xml);
224 xml = assert_parse("<test>a</test>", TRUE);
225 assert_data(xml, "a");
226 assert_stringify(xml, teststring);
227 sipe_xml_free(xml);
228 xml = assert_parse("<test>a\nb</test>", TRUE);
229 assert_data(xml, "a\nb");
230 assert_stringify(xml, teststring);
231 sipe_xml_free(xml);
233 /* child node */
234 xml = assert_parse("<test>a<child>b</child></test>", TRUE);
235 assert_data(xml, "a");
236 child1 = assert_child(xml, NULL, FALSE);
237 child1 = assert_child(xml, "child", TRUE);
238 assert_data(child1, "b");
239 child1 = assert_child(xml, "shouldnotmatch", FALSE);
240 assert_data(child1, NULL);
241 assert_stringify(xml, teststring);
242 sipe_xml_free(xml);
244 xml = assert_parse("<test>a<child/></test>", TRUE);
245 assert_data(xml, "a");
246 child1 = assert_child(xml, "child", TRUE);
247 assert_data(child1, NULL);
248 child1 = assert_child(xml, "shouldnotmatch", FALSE);
249 assert_data(child1, NULL);
250 assert_stringify(xml, teststring);
251 sipe_xml_free(xml);
253 xml = assert_parse("<test>a<child>b<inner>c</inner></child></test>", TRUE);
254 assert_data(xml, "a");
255 child1 = assert_child(xml, "child", TRUE);
256 assert_data(child1, "b");
257 child1 = assert_child(child1, "inner", TRUE);
258 assert_data(child1, "c");
259 child1 = assert_child(xml, "child/inner", TRUE);
260 assert_data(child1, "c");
261 assert_stringify(xml, teststring);
262 sipe_xml_free(xml);
264 xml = assert_parse("<test>a<child>b<inner>c<innerinner>d</innerinner></inner></child></test>", TRUE);
265 assert_data(xml, "a");
266 child1 = assert_child(xml, "child", TRUE);
267 assert_data(child1, "b");
268 child2 = assert_child(child1, "inner/innerinner", TRUE);
269 assert_data(child2, "d");
270 child1 = assert_child(child1, "inner", TRUE);
271 assert_data(child1, "c");
272 child1 = assert_child(child1, "innerinner", TRUE);
273 assert_data(child1, "d");
274 child1 = assert_child(xml, "child/inner", TRUE);
275 assert_data(child1, "c");
276 child1 = assert_child(xml, "child/inner/innerinner", TRUE);
277 assert_data(child1, "d");
278 assert_stringify(xml, teststring);
279 sipe_xml_free(xml);
281 /* attributes */
282 xml = assert_parse("<test a=\"\">a</test>", TRUE);
283 assert_data(xml, "a");
284 assert_attribute(xml, NULL, NULL);
285 assert_attribute(xml, "a", "");
286 assert_attribute(xml, "b", NULL);
287 assert_stringify(xml, teststring);
288 sipe_xml_free(xml);
290 xml = assert_parse("<test a=\"1\" b=\"abc\">a</test>", TRUE);
291 assert_data(xml, "a");
292 assert_attribute(xml, "a", "1");
293 assert_int_attribute(xml, "a", 1, 0);
294 assert_attribute(xml, "b", "abc");
295 assert_attribute(xml, "c", NULL);
296 assert_int_attribute(xml, "d", 100, 200);
297 /* the attribute order depends on glib hashing :-( */
298 assert_stringify(xml, "<test b=\"abc\" a=\"1\">a</test>");
299 sipe_xml_free(xml);
301 /* broken XML */
302 xml = assert_parse("t", FALSE);
303 sipe_xml_free(xml);
304 xml = assert_parse("<>", FALSE);
305 sipe_xml_free(xml);
306 xml = assert_parse("<></>", FALSE);
307 sipe_xml_free(xml);
308 xml = assert_parse("<test>", FALSE);
309 sipe_xml_free(xml);
310 xml = assert_parse("<a a=\"1\" a=\"2\"></a>", FALSE);
311 sipe_xml_free(xml);
313 if (allocated) {
314 printf("MEMORY LEAK: %" G_GSIZE_FORMAT " still allocated\n", allocated);
315 failed++;
316 } else {
317 printf("MEMORY LEAK CHECK OK\n");
318 succeeded++;
321 printf("Result: %d PASSED %d FAILED\n", succeeded, failed);
322 return(failed);
326 Local Variables:
327 mode: c
328 c-file-style: "bsd"
329 indent-tabs-mode: t
330 tab-width: 8
331 End: