34 #include <libxml/tree.h> |
34 #include <libxml/tree.h> |
35 |
35 |
36 #include "testutils.h" |
36 #include "testutils.h" |
37 |
37 |
38 #include "../public/webdav.h" |
38 #include "../public/webdav.h" |
|
39 #include "../util/writer.h" |
|
40 |
|
41 #include "../webdav/webdav.h" |
|
42 #include "../webdav/xml.h" |
39 |
43 |
40 typedef struct Test1Data { |
44 typedef struct Test1Data { |
41 int beginCounter; |
45 int beginCounter; |
42 int endCounter; |
46 int endCounter; |
43 int elmCounter; |
47 int elmCounter; |
44 int endElmCounter; |
48 int endElmCounter; |
45 int textErr; |
49 int textErr; |
46 int err; |
50 int err; |
47 int endErr; |
51 int endErr; |
|
52 int nodesWithAttributesCounter; |
48 xmlNode *prev; |
53 xmlNode *prev; |
49 } Test1Data; |
54 } Test1Data; |
50 |
55 |
51 static int test1_begin(xmlNode *node, void *userdata) { |
56 static int test1_begin(xmlNode *node, void *userdata) { |
52 Test1Data *data = userdata; |
57 Test1Data *data = userdata; |
124 } |
129 } |
125 |
130 |
126 return 0; |
131 return 0; |
127 } |
132 } |
128 |
133 |
|
134 static int test2_begin(xmlNode *node, void *userdata) { |
|
135 Test1Data *data = userdata; |
|
136 data->beginCounter++; |
|
137 if(node->type == XML_ELEMENT_NODE) { |
|
138 data->elmCounter++; |
|
139 if(node->properties) { |
|
140 data->nodesWithAttributesCounter++; |
|
141 } |
|
142 } |
|
143 return 0; |
|
144 } |
|
145 |
|
146 static int test2_end(xmlNode *node, void *userdata) { |
|
147 Test1Data *data = userdata; |
|
148 data->endCounter++; |
|
149 if(node->type == XML_ELEMENT_NODE) { |
|
150 data->endElmCounter++; |
|
151 } |
|
152 return 0; |
|
153 } |
|
154 |
129 UCX_TEST(test_wsxml_iterator) { |
155 UCX_TEST(test_wsxml_iterator) { |
130 Session *sn = testutil_session(); |
156 Session *sn = testutil_session(); |
131 |
157 |
132 UCX_TEST_BEGIN; |
158 UCX_TEST_BEGIN; |
133 |
159 |
134 xmlDoc *doc = xmlReadMemory( |
160 xmlDoc *doc = xmlReadMemory( |
135 XML_TESTDATA1, strlen(XML_TESTDATA1), NULL, NULL, 0); |
161 XML_TESTDATA1, strlen(XML_TESTDATA1), NULL, NULL, 0); |
136 xmlDoc *doc2 = xmlReadMemory( |
162 xmlDoc *doc2 = xmlReadMemory( |
137 XML_TESTDATA2, strlen(XML_TESTDATA2), NULL, NULL, 0); |
163 XML_TESTDATA2, strlen(XML_TESTDATA2), NULL, NULL, 0); |
|
164 xmlDoc *doc6 = xmlReadMemory( |
|
165 XML_TESTDATA6, strlen(XML_TESTDATA6), NULL, NULL, 0); |
138 UCX_TEST_ASSERT(doc, "doc is NULL"); |
166 UCX_TEST_ASSERT(doc, "doc is NULL"); |
139 UCX_TEST_ASSERT(doc2, "doc2 is NULL"); |
167 UCX_TEST_ASSERT(doc2, "doc2 is NULL"); |
|
168 UCX_TEST_ASSERT(doc6, "doc6 is NULL"); |
140 |
169 |
141 xmlNode *root = xmlDocGetRootElement(doc); |
170 xmlNode *root = xmlDocGetRootElement(doc); |
142 |
171 |
143 // Test 1: iterate over complete document |
172 // Test 1: iterate over complete document |
144 Test1Data testdata; |
173 Test1Data testdata; |
159 UCX_TEST_ASSERT(!testdata.err, "test2: wrong element order (begin)"); |
188 UCX_TEST_ASSERT(!testdata.err, "test2: wrong element order (begin)"); |
160 UCX_TEST_ASSERT(!testdata.endErr, "test2: wrong element order (end)"); |
189 UCX_TEST_ASSERT(!testdata.endErr, "test2: wrong element order (end)"); |
161 UCX_TEST_ASSERT(!testdata.textErr, "test2: text order error"); |
190 UCX_TEST_ASSERT(!testdata.textErr, "test2: text order error"); |
162 UCX_TEST_ASSERT(testdata.beginCounter == testdata.endCounter, "test2: begin/end counter not equal"); |
191 UCX_TEST_ASSERT(testdata.beginCounter == testdata.endCounter, "test2: begin/end counter not equal"); |
163 |
192 |
|
193 // Test 3: iterate over document with all kinds of node types |
|
194 xmlNode *root6 = xmlDocGetRootElement(doc6); |
|
195 ZERO(&testdata, sizeof(Test1Data)); |
|
196 ret = wsxml_iterator(sn->pool, root6, test2_begin, test2_end, &testdata); |
|
197 UCX_TEST_ASSERT(ret == 0, "test3: wsxml_iterator failed"); |
|
198 UCX_TEST_ASSERT(testdata.elmCounter == testdata.endElmCounter, "test3: begin/end counter not equal"); |
|
199 UCX_TEST_ASSERT(testdata.elmCounter == 12, "test3: wrong elm counter"); |
|
200 UCX_TEST_ASSERT(testdata.nodesWithAttributesCounter == 5, "test3: wrong entity ref counter"); |
|
201 |
164 xmlFreeDoc(doc); |
202 xmlFreeDoc(doc); |
165 xmlFreeDoc(doc2); |
203 xmlFreeDoc(doc2); |
|
204 xmlFreeDoc(doc6); |
166 UCX_TEST_END; |
205 UCX_TEST_END; |
167 } |
206 } |
168 |
207 |
169 // checks if the namespace list contains the test namespaces x1, x2, x3 and x4 |
208 // checks if the namespace list contains the test namespaces x1, x2, x3 and x4 |
170 static void check_ns_list(WebdavNSList *list, int *x1, int *x2, int *x3, int *x4) { |
209 static void check_ns_list(WebdavNSList *list, int *x1, int *x2, int *x3, int *x4) { |
255 UCX_TEST_ASSERT(x1, "ns3: x1 missing"); |
294 UCX_TEST_ASSERT(x1, "ns3: x1 missing"); |
256 UCX_TEST_ASSERT(x2, "ns3: x2 missing"); |
295 UCX_TEST_ASSERT(x2, "ns3: x2 missing"); |
257 UCX_TEST_ASSERT(!x3, "ns3: x3"); |
296 UCX_TEST_ASSERT(!x3, "ns3: x3"); |
258 UCX_TEST_ASSERT(!x4, "ns3: x4"); |
297 UCX_TEST_ASSERT(!x4, "ns3: x4"); |
259 |
298 |
|
299 xmlFreeDoc(doc3); |
|
300 xmlFreeDoc(doc4); |
|
301 xmlFreeDoc(doc5); |
260 UCX_TEST_END; |
302 UCX_TEST_END; |
261 } |
303 } |
|
304 |
|
305 UCX_TEST(test_wsxml_write_nodes) { |
|
306 Session *sn = testutil_session(); |
|
307 TestIOStream *st = testutil_iostream(2048, TRUE); |
|
308 |
|
309 UCX_TEST_BEGIN; |
|
310 xmlDoc *doc = xmlReadMemory( |
|
311 XML_TESTDATA6, strlen(XML_TESTDATA6), NULL, NULL, 0); |
|
312 UCX_TEST_ASSERT(doc, "xml parser error"); |
|
313 xmlNode *root = xmlDocGetRootElement(doc); |
|
314 |
|
315 Writer writer; |
|
316 char buffer[1024]; |
|
317 writer_init(&writer, st, buffer, 1024); |
|
318 |
|
319 int err = wsxml_write_nodes(sn->pool, &writer, NULL, root); |
|
320 writer_flush(&writer); |
|
321 UCX_TEST_ASSERT(err == 0, "wsxml_write_nodes error"); |
|
322 UCX_TEST_ASSERT(st->buf->pos > 0, "buffer is empty"); |
|
323 |
|
324 //printf("\n\n"); |
|
325 //printf("%.*s\n", (int)st->buf->size, st->buf->space); |
|
326 //printf("\n\n"); |
|
327 |
|
328 xmlDoc *genDoc = xmlReadMemory( |
|
329 st->buf->space, st->buf->size, NULL, NULL, 0); |
|
330 UCX_TEST_ASSERT(genDoc, "generated doc is not valid xml"); |
|
331 |
|
332 xmlFreeDoc(doc); |
|
333 xmlFreeDoc(genDoc); |
|
334 |
|
335 UCX_TEST_END; |
|
336 testutil_iostream_destroy(st); |
|
337 } |