src/server/test/xml.c

changeset 385
a1f4cb076d2f
parent 324
44cf877b3d9f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/server/test/xml.c	Sat Sep 24 16:26:10 2022 +0200
@@ -0,0 +1,430 @@
+/*
+ * 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 <stdio.h>
+#include <stdlib.h>
+
+#include <libxml/tree.h>
+
+#include "testutils.h"
+
+#include "../public/webdav.h"
+#include "../util/writer.h"
+
+#include "../webdav/webdav.h"
+#include "../webdav/xml.h"
+
+typedef struct Test1Data {
+    int beginCounter;
+    int endCounter;
+    int elmCounter;
+    int endElmCounter;
+    int textErr;
+    int err;
+    int endErr;
+    int nodesWithAttributesCounter;
+    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;
+}
+
+static int test2_begin(xmlNode *node, void *userdata) {
+    Test1Data *data = userdata;
+    data->beginCounter++;
+    if(node->type == XML_ELEMENT_NODE) {
+        data->elmCounter++;
+        if(node->properties) {
+            data->nodesWithAttributesCounter++;
+        }
+    }
+    return 0;
+}
+
+static int test2_end(xmlNode *node, void *userdata) {
+    Test1Data *data = userdata;
+    data->endCounter++;
+    if(node->type == XML_ELEMENT_NODE) {
+        data->endElmCounter++;
+    }
+    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);
+    xmlDoc *doc6 = xmlReadMemory(
+            XML_TESTDATA6, strlen(XML_TESTDATA6), NULL, NULL, 0);
+    UCX_TEST_ASSERT(doc, "doc is NULL");
+    UCX_TEST_ASSERT(doc2, "doc2 is NULL");
+    UCX_TEST_ASSERT(doc6, "doc6 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");
+    
+    // Test 3: iterate over document with all kinds of node types
+    xmlNode *root6 = xmlDocGetRootElement(doc6);
+    ZERO(&testdata, sizeof(Test1Data));
+    ret = wsxml_iterator(sn->pool, root6, test2_begin, test2_end, &testdata);
+    UCX_TEST_ASSERT(ret == 0, "test3: wsxml_iterator failed");
+    UCX_TEST_ASSERT(testdata.elmCounter == testdata.endElmCounter, "test3: begin/end counter not equal");
+    UCX_TEST_ASSERT(testdata.elmCounter == 12, "test3: wrong elm counter");
+    UCX_TEST_ASSERT(testdata.nodesWithAttributesCounter == 5, "test3: wrong entity ref counter");
+    
+    xmlFreeDoc(doc);
+    xmlFreeDoc(doc2);
+    xmlFreeDoc(doc6);
+    UCX_TEST_END;
+    
+    testutil_destroy_session(sn);
+}
+
+// checks if the namespace list contains the test namespaces x1, x2, x3 and x4
+static void check_ns_list(WebdavNSList *list, int *x1, int *x2, int *x3, int *x4) {
+    *x1 = 0;
+    *x2 = 0;
+    *x3 = 0;
+    *x4 = 0;
+    
+    WebdavNSList *elm = list;
+    while(elm) {
+        if(!strcmp((const char*)elm->namespace->prefix, "x1") &&
+           !strcmp((const char*)elm->namespace->href, "http://example.com/ns1/"))
+        {
+            *x1 = 1;
+        } else if(!strcmp((const char*)elm->namespace->prefix, "x2") &&
+                  !strcmp((const char*)elm->namespace->href, "http://example.com/ns2/"))
+        {
+            *x2 = 1;
+        } else if(!strcmp((const char*)elm->namespace->prefix, "x3") &&
+                  !strcmp((const char*)elm->namespace->href, "http://example.com/ns_0/"))
+        {
+            *x3 = 1;
+        } else if(!strcmp((const char*)elm->namespace->prefix, "x4") &&
+                  !strcmp((const char*)elm->namespace->href, "http://example.com/ns_0/"))
+        {
+            *x4 = 1;
+        }
+            
+        elm = elm->next;
+    }
+}
+
+UCX_TEST(test_wsxml_get_required_namespaces) {
+    Session *sn = testutil_session();
+    
+    UCX_TEST_BEGIN;
+    
+    xmlDoc *doc3 = xmlReadMemory(
+            XML_TESTDATA3, strlen(XML_TESTDATA3), NULL, NULL, 0);
+    xmlDoc *doc4 = xmlReadMemory(
+            XML_TESTDATA4, strlen(XML_TESTDATA4), NULL, NULL, 0);
+    xmlDoc *doc5 = xmlReadMemory(
+            XML_TESTDATA5, strlen(XML_TESTDATA5), NULL, NULL, 0);
+    
+    xmlNode *node0 = xmlDocGetRootElement(doc3);
+    xmlNode *node1 = xmlDocGetRootElement(doc3)->children;
+    xmlNode *node2 = xmlDocGetRootElement(doc4)->children;
+    xmlNode *node3 = xmlDocGetRootElement(doc5)->children;
+    
+    UCX_TEST_ASSERT(doc3, "doc3 is NULL");
+    UCX_TEST_ASSERT(doc4, "doc4 is NULL");
+    UCX_TEST_ASSERT(doc5, "doc5 is NULL");
+    
+    int err0, err1, err2, err3;
+    int x1 = 0;
+    int x2 = 0;
+    int x3 = 0;
+    int x4 = 0;
+    WebdavNSList *elm = NULL;
+    
+    // Test 0: 
+    WebdavNSList *ns0 = wsxml_get_required_namespaces(sn->pool, node0, &err0);
+    UCX_TEST_ASSERT(!err0, "ns0 failed");
+    UCX_TEST_ASSERT(!ns0, "ns0: nsdefs should be ignored");
+    
+    WebdavNSList *ns1 = wsxml_get_required_namespaces(sn->pool, node1, &err1);
+    check_ns_list(ns1, &x1, &x2, &x3, &x4);
+    UCX_TEST_ASSERT(!err1, "ns1 failed");
+    UCX_TEST_ASSERT(ns1, "ns1: no list");
+    UCX_TEST_ASSERT(x1, "ns1: x1 missing");
+    UCX_TEST_ASSERT(x2, "ns1: x2 missing");
+    UCX_TEST_ASSERT(x3, "ns1: x3 missing");
+    UCX_TEST_ASSERT(x4, "ns1: x4 missing");
+    
+    WebdavNSList *ns2 = wsxml_get_required_namespaces(sn->pool, node2, &err2);
+    check_ns_list(ns2, &x1, &x2, &x3, &x4);
+    UCX_TEST_ASSERT(!err2, "ns2 failed");
+    UCX_TEST_ASSERT(ns2, "ns2: no list");
+    UCX_TEST_ASSERT(x1, "ns2: x1 missing");
+    UCX_TEST_ASSERT(x2, "ns2: x2 missing");
+    UCX_TEST_ASSERT(!x3, "ns2: x3");
+    UCX_TEST_ASSERT(!x4, "ns2: x4");
+    
+    WebdavNSList *ns3 = wsxml_get_required_namespaces(sn->pool, node3, &err3);
+    check_ns_list(ns3, &x1, &x2, &x3, &x4);
+    UCX_TEST_ASSERT(!err3, "ns3 failed");
+    UCX_TEST_ASSERT(ns3, "ns3: no list");
+    UCX_TEST_ASSERT(x1, "ns3: x1 missing");
+    UCX_TEST_ASSERT(x2, "ns3: x2 missing");
+    UCX_TEST_ASSERT(!x3, "ns3: x3");
+    UCX_TEST_ASSERT(!x4, "ns3: x4");
+    
+    xmlFreeDoc(doc3);
+    xmlFreeDoc(doc4);
+    xmlFreeDoc(doc5);
+    UCX_TEST_END;
+    
+    testutil_destroy_session(sn);
+}
+
+UCX_TEST(test_wsxml_write_nodes) {
+    Session *sn = testutil_session();
+    TestIOStream *st = testutil_iostream(2048, TRUE);
+    
+    UCX_TEST_BEGIN;
+    xmlDoc *doc = xmlReadMemory(
+            XML_TESTDATA6, strlen(XML_TESTDATA6), NULL, NULL, 0);
+    UCX_TEST_ASSERT(doc, "xml parser error");
+    xmlNode *root = xmlDocGetRootElement(doc);
+    
+    Writer writer;
+    char buffer[1024];
+    writer_init(&writer, st, buffer, 1024);
+    
+    int err = wsxml_write_nodes(sn->pool, &writer, NULL, root);
+    writer_flush(&writer);
+    UCX_TEST_ASSERT(err == 0, "wsxml_write_nodes error");
+    UCX_TEST_ASSERT(st->buf->pos > 0, "buffer is empty");
+    
+    //printf("\n\n");
+    //printf("%.*s\n", (int)st->buf->size, st->buf->space);
+    //printf("\n\n");
+    
+    xmlDoc *genDoc = xmlReadMemory(
+            st->buf->space, st->buf->size, NULL, NULL, 0);
+    UCX_TEST_ASSERT(genDoc, "generated doc is not valid xml");
+    
+    xmlFreeDoc(doc);
+    xmlFreeDoc(genDoc);
+    
+    UCX_TEST_END;
+    testutil_iostream_destroy(st);
+    testutil_destroy_session(sn);
+}
+
+UCX_TEST(test_wsxml_nslist2string) {
+    Session *sn = testutil_session();
+    
+    UCX_TEST_BEGIN;
+    
+    WSNamespace ns1;
+    WSNamespace ns2;
+    WSNamespace ns3;  
+    memset(&ns1, 0, sizeof(WSNamespace));
+    memset(&ns2, 0, sizeof(WSNamespace));
+    memset(&ns3, 0, sizeof(WSNamespace));
+    ns1.prefix = (const xmlChar*)"x";
+    ns1.href = (const xmlChar*)"ns1";
+    ns2.prefix = (const xmlChar*)"y";
+    ns2.href = (const xmlChar*)"ns2";
+    ns3.prefix = (const xmlChar*)"z";
+    ns3.href = (const xmlChar*)"ns3";
+    
+    WebdavNSList elm1 = { &ns1, NULL, NULL };
+    WebdavNSList elm2 = { &ns2, NULL, NULL };
+    WebdavNSList elm3 = { &ns3, NULL, NULL };
+    
+    // single elm test
+    char *str1 = wsxml_nslist2string(sn->pool, &elm1);
+    UCX_TEST_ASSERT(str1, "str1 is null");
+    UCX_TEST_ASSERT(!strcmp(str1, "x:ns1"), "str1: wrong content");
+    
+    // 2 elm test
+    elm1.next = &elm2;
+    char *str2 = wsxml_nslist2string(sn->pool, &elm1);
+    UCX_TEST_ASSERT(str2, "str2 is null");
+    UCX_TEST_ASSERT(!strcmp(str2, "x:ns1\ny:ns2"), "str2: wrong content");
+    
+    // 3 elm test
+    elm2.next = &elm3;
+    char *str3 = wsxml_nslist2string(sn->pool, &elm1);
+    UCX_TEST_ASSERT(str3, "str3 is null");
+    UCX_TEST_ASSERT(!strcmp(str3, "x:ns1\ny:ns2\nz:ns3"), "str3: wrong content");
+    
+    // empty prefix test
+    ns1.prefix = NULL;
+    char *str4 = wsxml_nslist2string(sn->pool, &elm1);
+    UCX_TEST_ASSERT(str4, "str3 is null");
+    UCX_TEST_ASSERT(!strcmp(str4, ":ns1\ny:ns2\nz:ns3"), "str4: wrong content");
+    
+    UCX_TEST_END;
+    testutil_destroy_session(sn);
+}
+
+UCX_TEST(test_wsxml_string2nslist) {
+    Session *sn = testutil_session();
+    
+    UCX_TEST_BEGIN;
+    
+    // empty list
+    WebdavNSList *list1 = wsxml_string2nslist(sn->pool, "");
+    UCX_TEST_ASSERT(!list1, "list1 should be NULL");
+    
+    // 1 elm list
+    WebdavNSList *list2 = wsxml_string2nslist(sn->pool, "x:ns1");
+    UCX_TEST_ASSERT(list2, "list2 is NULL");
+    UCX_TEST_ASSERT(list2->namespace, "list2 namespace is NULL");
+    UCX_TEST_ASSERT(!strcmp((const char*)list2->namespace->prefix, "x"), "list2: wrong prefix");
+    UCX_TEST_ASSERT(!strcmp((const char*)list2->namespace->href, "ns1"), "list2: wrong href");
+    
+    // 2 elm list
+    WebdavNSList *list3 = wsxml_string2nslist(sn->pool, "x:ns1\ny:ns2");
+    UCX_TEST_ASSERT(list3, "list3 is NULL");
+    UCX_TEST_ASSERT(list3->namespace, "list3 namespace is NULL");
+    UCX_TEST_ASSERT(!strcmp((const char*)list3->namespace->prefix, "x"), "list3: wrong prefix");
+    UCX_TEST_ASSERT(!strcmp((const char*)list3->namespace->href, "ns1"), "list3: wrong href");
+    UCX_TEST_ASSERT(list3->next, "list3 elm2 is NULL");
+    UCX_TEST_ASSERT(list3->next->namespace, "list3 namespace 2 is NULL");
+    UCX_TEST_ASSERT(!strcmp((const char*)list3->namespace->prefix, "x"), "list3: elm2 wrong prefix");
+    UCX_TEST_ASSERT(!strcmp((const char*)list3->namespace->href, "ns1"), "list3: elm2 wrong href");
+    
+    // empty prefix
+    WebdavNSList *list4 = wsxml_string2nslist(sn->pool, ":x\ny:ns2");
+    UCX_TEST_ASSERT(list4, "list4 is NULL");
+    UCX_TEST_ASSERT(list4->namespace, "list4 namespace is NULL");
+    UCX_TEST_ASSERT(!list4->namespace->prefix, "list4 elm1 prefix should be NULL");
+    UCX_TEST_ASSERT(!strcmp((const char*)list3->namespace->href, "ns1"), "list3: wrong href");
+    
+    
+    UCX_TEST_END;
+    testutil_destroy_session(sn);
+}

mercurial