diff -r bbaec8415c10 -r 0de1ec82628e src/server/test/xml.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/test/xml.c Thu Jan 16 19:14:53 2020 +0100 @@ -0,0 +1,167 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2020 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "xml.h" + +#include +#include + +#include + +#include "testutils.h" + +#include "../public/webdav.h" + +typedef struct Test1Data { + int beginCounter; + int endCounter; + int elmCounter; + int endElmCounter; + int textErr; + int err; + int endErr; + xmlNode *prev; +} Test1Data; + +static int test1_begin(xmlNode *node, void *userdata) { + Test1Data *data = userdata; + data->beginCounter++; + + if(node->type == XML_ELEMENT_NODE) { + data->elmCounter++; + const char *name = (const char*)node->name; + + if(!strcmp(name, "ignore") || !strcmp(name, "ignore")) { + data->err = 1; + return 1; + } + + switch(data->elmCounter) { + case 1: if(strcmp(name, "test")){ data->err = 1; return 1; } break; + case 2: if(strcmp(name, "elm1")) { data->err = 1; return 1; } break; + case 3: if(strcmp(name, "elm2")) { data->err = 1; return 1; } break; + case 4: if(strcmp(name, "c")) { data->err = 1; return 1; } break; + case 5: if(strcmp(name, "a")) { data->err = 1; return 1; } break; + case 6: if(strcmp(name, "d")) { data->err = 1; return 1; } break; + case 7: if(strcmp(name, "e")) { data->err = 1; return 1; } break; + case 8: if(strcmp(name, "b")) { data->err = 1; return 1; } break; + case 9: if(strcmp(name, "x")) { data->err = 1; return 1; } break; + case 10: if(strcmp(name, "z")) { data->err = 1; return 1; } break; + case 11: if(strcmp(name, "nextelm")) { data->err = 1; return 1; } break; + } + } else if(node->type == XML_TEXT_NODE) { + const char *text = (const char*)node->content; + if(!strcmp(text, "teststr")) { + if(strcmp((const char*)data->prev->name, "elm1")) { + data->textErr = 1; + return 1; + } + } else if(!strcmp(text, "hello") || !strcmp(text, "world")) { + if(strcmp((const char*)data->prev->name, "a")) { + data->textErr = 1; + return 1; + } + } + } + + if(node->type == XML_ELEMENT_NODE) { + data->prev = node; + } + return 0; +} + +static int test1_end(xmlNode *node, void *userdata) { + Test1Data *data = userdata; + data->endCounter++; + + if(node->type == XML_ELEMENT_NODE) { + data->endElmCounter++; + const char *name = (const char*)node->name; + + if(!strcmp(name, "ignore") || !strcmp(name, "ignore")) { + data->err = 1; + return 1; + } + + switch(data->endElmCounter) { + case 1: if(strcmp(name, "elm1")){ data->endErr = 1; return 1; } break; + case 2: if(strcmp(name, "elm2")){ data->endErr = 1; return 1; } break; + case 3: if(strcmp(name, "a")){ data->endErr = 1; return 1; } break; + case 4: if(strcmp(name, "b")){ data->endErr = 1; return 1; } break; + case 5: if(strcmp(name, "e")){ data->endErr = 1; return 1; } break; + case 6: if(strcmp(name, "d")){ data->endErr = 1; return 1; } break; + case 7: if(strcmp(name, "c")){ data->endErr = 1; return 1; } break; + case 8: if(strcmp(name, "z")){ data->endErr = 1; return 1; } break; + case 9: if(strcmp(name, "x")){ data->endErr = 1; return 1; } break; + case 10: if(strcmp(name, "test")){ data->endErr = 1; return 1; } break; + case 11: if(strcmp(name, "nextelm")) { data->endErr = 1; return 1; } break; + } + } + + return 0; +} + +UCX_TEST(test_wsxml_iterator) { + Session *sn = testutil_session(); + + UCX_TEST_BEGIN; + + xmlDoc *doc = xmlReadMemory( + XML_TESTDATA1, strlen(XML_TESTDATA1), NULL, NULL, 0); + xmlDoc *doc2 = xmlReadMemory( + XML_TESTDATA2, strlen(XML_TESTDATA2), NULL, NULL, 0); + UCX_TEST_ASSERT(doc, "doc is NULL"); + UCX_TEST_ASSERT(doc2, "doc2 is NULL"); + + xmlNode *root = xmlDocGetRootElement(doc); + + // Test 1: iterate over complete document + Test1Data testdata; + ZERO(&testdata, sizeof(Test1Data)); + int ret = wsxml_iterator(sn->pool, root, test1_begin, test1_end, &testdata); + UCX_TEST_ASSERT(ret == 0, "wsxml_iterator failed"); + UCX_TEST_ASSERT(!testdata.err, "wrong element order (begin)"); + UCX_TEST_ASSERT(!testdata.endErr, "wrong element order (end)"); + UCX_TEST_ASSERT(!testdata.textErr, "text order error"); + UCX_TEST_ASSERT(testdata.beginCounter == testdata.endCounter, "begin/end counter not equal"); + + // Test 2: iterate over sub-document + ZERO(&testdata, sizeof(Test1Data)); + xmlNode *root2 = xmlDocGetRootElement(doc2); + xmlNode *sub = root2->children->children; + ret = wsxml_iterator(sn->pool, sub, test1_begin, test1_end, &testdata); + UCX_TEST_ASSERT(ret == 0, "test2: wsxml_iterator failed"); + UCX_TEST_ASSERT(!testdata.err, "test2: wrong element order (begin)"); + UCX_TEST_ASSERT(!testdata.endErr, "test2: wrong element order (end)"); + UCX_TEST_ASSERT(!testdata.textErr, "test2: text order error"); + UCX_TEST_ASSERT(testdata.beginCounter == testdata.endCounter, "test2: begin/end counter not equal"); + + xmlFreeDoc(doc); + xmlFreeDoc(doc2); + UCX_TEST_END; +}