fix previous commit
[siplcs.git] / src / core / sipe-xml-tests.c
blobf42efa4bd2a1d0143c86193471f7d379316d5f49
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 "glib.h"
30 #include "sipe.h"
31 #include "sipe-xml.h"
32 #include "sipe-utils.h"
33 #include "sipe-backend-debug.h"
34 #include "sipe-common.h"
36 /* stub functions for backend API */
37 void sipe_backend_debug(sipe_debug_level level,
38 const gchar *format,
39 ...)
41 va_list args;
42 gchar *msg;
43 va_start(args, format);
44 msg = g_strdup_vprintf(format, args);
45 va_end(args);
47 printf("DEBUG %d: %s", level, msg);
48 g_free(msg);
51 /* test helpers */
52 static guint succeeded = 0;
53 static guint failed = 0;
54 static const gchar *teststring;
56 static sipe_xml *assert_parse(const gchar *s, gboolean ok)
58 sipe_xml *xml = sipe_xml_parse(s, s ? strlen(s) : 0);
60 teststring = s ? s : "(nil)";
62 if ((ok && xml) || (!ok && !xml)) {
63 succeeded++;
64 } else {
65 printf("[%s]\nXML parse FAILED: %p\n",
66 teststring, xml);
67 failed++;
69 return(xml);
72 static sipe_xml *assert_child(const sipe_xml *xml, const gchar *s, gboolean ok)
74 sipe_xml *child = sipe_xml_get_child(xml, s);
76 if ((ok && child) || (!ok && !child)) {
77 succeeded++;
78 } else {
79 printf("[%s]\nXML child FAILED: %p '%s'\n",
80 teststring, xml, s ? s : "(nil)");
81 failed++;
83 return(child);
86 static void assert_data(const sipe_xml *xml, const gchar *s)
88 gchar *data = sipe_xml_get_data(xml);
90 if (sipe_strequal(s, data)) {
91 succeeded++;
92 } else {
93 printf("[%s]\nXML data FAILED: '%s' expected: '%s'\n",
94 teststring, data ? data : "(nil)", s ? s : "(nil)");
95 failed++;
97 g_free(data);
100 static void assert_attribute(const sipe_xml *xml,
101 const gchar *key, const gchar *value)
103 const gchar *attr = sipe_xml_get_attribute(xml, key);
105 if (sipe_strequal(value, attr)) {
106 succeeded++;
107 } else {
108 printf("[%s]\nXML attr FAILED: '%s': '%s' expected: '%s'\n",
109 teststring, key ? key : "(nil)",
110 attr ? attr : "(nil)", value ? value : "(nil)");
111 failed++;
115 /* memory leak check */
116 static gsize allocated = 0;
118 static gpointer test_malloc(gsize n_bytes)
120 gsize *m = malloc(sizeof(gsize) + n_bytes);
121 if (!m) return(NULL);
122 allocated += n_bytes;
123 m[0] = n_bytes;
124 return(m + 1);
127 static void test_free(gpointer mem)
129 gsize *m = mem;
130 if (!m) return;
131 m--;
132 allocated -= m[0];
133 free(m);
136 static gpointer test_realloc(gpointer mem, gsize n_bytes)
138 guint8 *n = NULL;
139 if (n_bytes) {
140 n = test_malloc(n_bytes);
141 if (mem && n) {
142 memcpy(n, mem, n_bytes);
145 test_free(mem);
146 return(n);
149 static GMemVTable memory_leak_check = {
150 &test_malloc,
151 &test_realloc,
152 &test_free,
153 NULL,
154 NULL,
155 NULL,
158 int main(SIPE_UNUSED_PARAMETER int argc, SIPE_UNUSED_PARAMETER char **argv)
160 sipe_xml *xml, *child1, *child2;
162 #if 0
164 * No idea why the memory leak checks work on some platforms
165 * but fail on others :-( Disable for now...
167 g_mem_set_vtable(&memory_leak_check);
168 #else
169 (void) memory_leak_check;
170 #endif
172 /* empty XML */
173 xml = assert_parse(NULL, FALSE);
174 sipe_xml_free(xml);
175 xml = assert_parse("", FALSE);
176 sipe_xml_free(xml);
177 xml = assert_parse("<?xml version=\"1.0\" ?>", FALSE);
178 sipe_xml_free(xml);
180 /* one node */
181 xml = assert_parse("<test></test>", TRUE);
182 assert_data(xml, NULL);
183 sipe_xml_free(xml);
184 xml = assert_parse("<test>a</test>", TRUE);
185 assert_data(xml, "a");
186 sipe_xml_free(xml);
187 xml = assert_parse("<test>a\nb</test>", TRUE);
188 assert_data(xml, "a\nb");
189 sipe_xml_free(xml);
191 /* child node */
192 xml = assert_parse("<test>a<child>b</child></test>", TRUE);
193 assert_data(xml, "a");
194 child1 = assert_child(xml, NULL, FALSE);
195 child1 = assert_child(xml, "child", TRUE);
196 assert_data(child1, "b");
197 child1 = assert_child(xml, "shouldnotmatch", FALSE);
198 assert_data(child1, NULL);
199 sipe_xml_free(xml);
201 xml = assert_parse("<test>a<child/></test>", TRUE);
202 assert_data(xml, "a");
203 child1 = assert_child(xml, "child", TRUE);
204 assert_data(child1, NULL);
205 child1 = assert_child(xml, "shouldnotmatch", FALSE);
206 assert_data(child1, NULL);
207 sipe_xml_free(xml);
209 xml = assert_parse("<test>a<child>b<inner>c</inner></child></test>", TRUE);
210 assert_data(xml, "a");
211 child1 = assert_child(xml, "child", TRUE);
212 assert_data(child1, "b");
213 child1 = assert_child(child1, "inner", TRUE);
214 assert_data(child1, "c");
215 child1 = assert_child(xml, "child/inner", TRUE);
216 assert_data(child1, "c");
217 sipe_xml_free(xml);
219 xml = assert_parse("<test>a<child>b<inner>c<innerinner>d</innerinner></inner></child></test>", TRUE);
220 assert_data(xml, "a");
221 child1 = assert_child(xml, "child", TRUE);
222 assert_data(child1, "b");
223 child2 = assert_child(child1, "inner/innerinner", TRUE);
224 assert_data(child2, "d");
225 child1 = assert_child(child1, "inner", TRUE);
226 assert_data(child1, "c");
227 child1 = assert_child(child1, "innerinner", TRUE);
228 assert_data(child1, "d");
229 child1 = assert_child(xml, "child/inner", TRUE);
230 assert_data(child1, "c");
231 child1 = assert_child(xml, "child/inner/innerinner", TRUE);
232 assert_data(child1, "d");
233 sipe_xml_free(xml);
235 /* attributes */
236 xml = assert_parse("<test a=\"\">a</test>", TRUE);
237 assert_data(xml, "a");
238 assert_attribute(xml, NULL, NULL);
239 assert_attribute(xml, "a", "");
240 assert_attribute(xml, "b", NULL);
241 sipe_xml_free(xml);
243 xml = assert_parse("<test a=\"1\" b=\"abc\">a</test>", TRUE);
244 assert_data(xml, "a");
245 assert_attribute(xml, "a", "1");
246 assert_attribute(xml, "b", "abc");
247 assert_attribute(xml, "c", NULL);
248 sipe_xml_free(xml);
250 /* broken XML */
251 xml = assert_parse("t", FALSE);
252 sipe_xml_free(xml);
253 xml = assert_parse("<>", FALSE);
254 sipe_xml_free(xml);
255 xml = assert_parse("<></>", FALSE);
256 sipe_xml_free(xml);
257 xml = assert_parse("<test>", FALSE);
258 sipe_xml_free(xml);
260 if (allocated) {
261 printf("MEMORY LEAK: %" G_GSIZE_FORMAT " still allocated\n", allocated);
262 failed++;
263 } else {
264 printf("MEMORY LEAK CHECK OK\n");
265 succeeded++;
268 printf("Result: %d PASSED %d FAILED\n", succeeded, failed);
269 return(failed);
273 Local Variables:
274 mode: c
275 c-file-style: "bsd"
276 indent-tabs-mode: t
277 tab-width: 8
278 End: