src/server/test/xml.c

branch
webdav
changeset 224
0de1ec82628e
child 225
e4f3e1433098
equal deleted inserted replaced
223:bbaec8415c10 224:0de1ec82628e
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2020 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "xml.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include <libxml/tree.h>
35
36 #include "testutils.h"
37
38 #include "../public/webdav.h"
39
40 typedef struct Test1Data {
41 int beginCounter;
42 int endCounter;
43 int elmCounter;
44 int endElmCounter;
45 int textErr;
46 int err;
47 int endErr;
48 xmlNode *prev;
49 } Test1Data;
50
51 static int test1_begin(xmlNode *node, void *userdata) {
52 Test1Data *data = userdata;
53 data->beginCounter++;
54
55 if(node->type == XML_ELEMENT_NODE) {
56 data->elmCounter++;
57 const char *name = (const char*)node->name;
58
59 if(!strcmp(name, "ignore") || !strcmp(name, "ignore")) {
60 data->err = 1;
61 return 1;
62 }
63
64 switch(data->elmCounter) {
65 case 1: if(strcmp(name, "test")){ data->err = 1; return 1; } break;
66 case 2: if(strcmp(name, "elm1")) { data->err = 1; return 1; } break;
67 case 3: if(strcmp(name, "elm2")) { data->err = 1; return 1; } break;
68 case 4: if(strcmp(name, "c")) { data->err = 1; return 1; } break;
69 case 5: if(strcmp(name, "a")) { data->err = 1; return 1; } break;
70 case 6: if(strcmp(name, "d")) { data->err = 1; return 1; } break;
71 case 7: if(strcmp(name, "e")) { data->err = 1; return 1; } break;
72 case 8: if(strcmp(name, "b")) { data->err = 1; return 1; } break;
73 case 9: if(strcmp(name, "x")) { data->err = 1; return 1; } break;
74 case 10: if(strcmp(name, "z")) { data->err = 1; return 1; } break;
75 case 11: if(strcmp(name, "nextelm")) { data->err = 1; return 1; } break;
76 }
77 } else if(node->type == XML_TEXT_NODE) {
78 const char *text = (const char*)node->content;
79 if(!strcmp(text, "teststr")) {
80 if(strcmp((const char*)data->prev->name, "elm1")) {
81 data->textErr = 1;
82 return 1;
83 }
84 } else if(!strcmp(text, "hello") || !strcmp(text, "world")) {
85 if(strcmp((const char*)data->prev->name, "a")) {
86 data->textErr = 1;
87 return 1;
88 }
89 }
90 }
91
92 if(node->type == XML_ELEMENT_NODE) {
93 data->prev = node;
94 }
95 return 0;
96 }
97
98 static int test1_end(xmlNode *node, void *userdata) {
99 Test1Data *data = userdata;
100 data->endCounter++;
101
102 if(node->type == XML_ELEMENT_NODE) {
103 data->endElmCounter++;
104 const char *name = (const char*)node->name;
105
106 if(!strcmp(name, "ignore") || !strcmp(name, "ignore")) {
107 data->err = 1;
108 return 1;
109 }
110
111 switch(data->endElmCounter) {
112 case 1: if(strcmp(name, "elm1")){ data->endErr = 1; return 1; } break;
113 case 2: if(strcmp(name, "elm2")){ data->endErr = 1; return 1; } break;
114 case 3: if(strcmp(name, "a")){ data->endErr = 1; return 1; } break;
115 case 4: if(strcmp(name, "b")){ data->endErr = 1; return 1; } break;
116 case 5: if(strcmp(name, "e")){ data->endErr = 1; return 1; } break;
117 case 6: if(strcmp(name, "d")){ data->endErr = 1; return 1; } break;
118 case 7: if(strcmp(name, "c")){ data->endErr = 1; return 1; } break;
119 case 8: if(strcmp(name, "z")){ data->endErr = 1; return 1; } break;
120 case 9: if(strcmp(name, "x")){ data->endErr = 1; return 1; } break;
121 case 10: if(strcmp(name, "test")){ data->endErr = 1; return 1; } break;
122 case 11: if(strcmp(name, "nextelm")) { data->endErr = 1; return 1; } break;
123 }
124 }
125
126 return 0;
127 }
128
129 UCX_TEST(test_wsxml_iterator) {
130 Session *sn = testutil_session();
131
132 UCX_TEST_BEGIN;
133
134 xmlDoc *doc = xmlReadMemory(
135 XML_TESTDATA1, strlen(XML_TESTDATA1), NULL, NULL, 0);
136 xmlDoc *doc2 = xmlReadMemory(
137 XML_TESTDATA2, strlen(XML_TESTDATA2), NULL, NULL, 0);
138 UCX_TEST_ASSERT(doc, "doc is NULL");
139 UCX_TEST_ASSERT(doc2, "doc2 is NULL");
140
141 xmlNode *root = xmlDocGetRootElement(doc);
142
143 // Test 1: iterate over complete document
144 Test1Data testdata;
145 ZERO(&testdata, sizeof(Test1Data));
146 int ret = wsxml_iterator(sn->pool, root, test1_begin, test1_end, &testdata);
147 UCX_TEST_ASSERT(ret == 0, "wsxml_iterator failed");
148 UCX_TEST_ASSERT(!testdata.err, "wrong element order (begin)");
149 UCX_TEST_ASSERT(!testdata.endErr, "wrong element order (end)");
150 UCX_TEST_ASSERT(!testdata.textErr, "text order error");
151 UCX_TEST_ASSERT(testdata.beginCounter == testdata.endCounter, "begin/end counter not equal");
152
153 // Test 2: iterate over sub-document
154 ZERO(&testdata, sizeof(Test1Data));
155 xmlNode *root2 = xmlDocGetRootElement(doc2);
156 xmlNode *sub = root2->children->children;
157 ret = wsxml_iterator(sn->pool, sub, test1_begin, test1_end, &testdata);
158 UCX_TEST_ASSERT(ret == 0, "test2: wsxml_iterator failed");
159 UCX_TEST_ASSERT(!testdata.err, "test2: wrong element order (begin)");
160 UCX_TEST_ASSERT(!testdata.endErr, "test2: wrong element order (end)");
161 UCX_TEST_ASSERT(!testdata.textErr, "test2: text order error");
162 UCX_TEST_ASSERT(testdata.beginCounter == testdata.endCounter, "test2: begin/end counter not equal");
163
164 xmlFreeDoc(doc);
165 xmlFreeDoc(doc2);
166 UCX_TEST_END;
167 }

mercurial