Mon, 25 Apr 2022 22:38:05 +0200
fix pg propfind for getlastmodified property
/* * 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; } // 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; } 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); }