add webdav_plist_iterator_remove_current tests and fix some plist related bugs webdav

Mon, 30 Dec 2019 16:33:20 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 30 Dec 2019 16:33:20 +0100
branch
webdav
changeset 216
ce2866ec97f6
parent 215
68e824ba4a4f
child 217
8ed14d76db42

add webdav_plist_iterator_remove_current tests and fix some plist related bugs

src/server/public/webdav.h file | annotate | diff | comparison | revisions
src/server/test/main.c file | annotate | diff | comparison | revisions
src/server/test/webdav.c file | annotate | diff | comparison | revisions
src/server/test/webdav.h file | annotate | diff | comparison | revisions
src/server/webdav/requestparser.c file | annotate | diff | comparison | revisions
src/server/webdav/webdav.c file | annotate | diff | comparison | revisions
--- a/src/server/public/webdav.h	Sun Dec 29 22:39:35 2019 +0100
+++ b/src/server/public/webdav.h	Mon Dec 30 16:33:20 2019 +0100
@@ -280,7 +280,7 @@
 
 WebdavPList* webdav_plist_clone(pool_handle_t *pool, WebdavPList *list);
 
-size_t webdav_plist_count(WebdavPList *list);
+size_t webdav_plist_size(WebdavPList *list);
 
 WebdavPListIterator webdav_plist_iterator(WebdavPList **list);
 int webdav_plist_iterator_next(WebdavPListIterator *i, WebdavPList **cur);
--- a/src/server/test/main.c	Sun Dec 29 22:39:35 2019 +0100
+++ b/src/server/test/main.c	Mon Dec 30 16:33:20 2019 +0100
@@ -67,6 +67,7 @@
     ucx_test_register(suite, test_lock_parse);
     ucx_test_register(suite, test_rqbody2buffer);
     ucx_test_register(suite, test_webdav_plist_iterator);
+    ucx_test_register(suite, test_webdav_plist_iterator_remove_current);
     ucx_test_register(suite, test_msresponse_addproperty);
     ucx_test_register(suite, test_webdav_propfind_init);
     
--- a/src/server/test/webdav.c	Sun Dec 29 22:39:35 2019 +0100
+++ b/src/server/test/webdav.c	Mon Dec 30 16:33:20 2019 +0100
@@ -453,6 +453,100 @@
     testutil_destroy_session(sn);
 }
 
+UCX_TEST(test_webdav_plist_iterator_remove_current) {
+    Session *sn;
+    Request *rq;
+    WebdavPropfindRequest *propfind;
+    
+    UCX_TEST_BEGIN;
+    UCX_TEST_ASSERT(!test_init(&sn, &rq, &propfind, TEST_PROPFIND1), "init failed");
+    
+    WebdavPList *properties1 = webdav_plist_clone(sn->pool, propfind->properties);
+    WebdavPList *properties2 = webdav_plist_clone(sn->pool, propfind->properties);
+    WebdavPList *properties3 = webdav_plist_clone(sn->pool, propfind->properties);
+    WebdavPList *properties4 = webdav_plist_clone(sn->pool, propfind->properties);
+    
+    WebdavPListIterator i;
+    WebdavPList *cur;
+    
+    // test removal of first element
+    i = webdav_plist_iterator(&properties1);
+    while(webdav_plist_iterator_next(&i, &cur)) {
+        if(i.index == 0) {
+            webdav_plist_iterator_remove_current(&i);
+        }
+    }
+    
+    UCX_TEST_ASSERT(!properties1->prev, "test1: prev not cleared");
+    UCX_TEST_ASSERT(!strcmp(properties1->property->name, "getcontentlength"), "test1: wrong property");
+    UCX_TEST_ASSERT(!strcmp(properties1->next->property->name, "getcontenttype"), "test1: wrong property 2");
+    UCX_TEST_ASSERT(properties1->next->prev == properties1, "test1: wrong link");
+    
+    // test removal of second element
+    i = webdav_plist_iterator(&properties2);
+    while(webdav_plist_iterator_next(&i, &cur)) {
+        if(i.index == 1) {
+            webdav_plist_iterator_remove_current(&i);
+        }
+    }
+    
+    UCX_TEST_ASSERT(!strcmp(properties2->next->property->name, "getcontenttype"), "test2: wrong property");
+    UCX_TEST_ASSERT(properties2->next->prev == properties2, "test2: wrong link");
+    UCX_TEST_ASSERT(webdav_plist_size(properties2) == 5, "test2: wrong size");
+    
+    // remove last element
+    i = webdav_plist_iterator(&properties3);
+    while(webdav_plist_iterator_next(&i, &cur)) {
+        if(i.index == 5) {
+            webdav_plist_iterator_remove_current(&i);
+        }
+    }
+    
+    UCX_TEST_ASSERT(webdav_plist_size(properties3) == 5, "test3: wrong size");
+    UCX_TEST_ASSERT(!strcmp(properties3->next->next->next->next->property->name, "resourcetype"), "test2: wrong property");
+    
+    // remove all elements
+    i = webdav_plist_iterator(&properties4);
+    while(webdav_plist_iterator_next(&i, &cur)) {
+        webdav_plist_iterator_remove_current(&i);
+        switch(i.index) {
+            case 0: {
+                UCX_TEST_ASSERT(!strcmp(properties4->property->name, "getcontentlength"), "test4: wrong property 2");
+                UCX_TEST_ASSERT(properties4->prev == NULL, "test4: prev not NULL (0)");
+                break;
+            }
+            case 1: {
+                UCX_TEST_ASSERT(!strcmp(properties4->property->name, "getcontenttype"), "test4: wrong property 3");
+                UCX_TEST_ASSERT(properties4->prev == NULL, "test4: prev not NULL (1)");
+                break;
+            }
+            case 2: {
+                UCX_TEST_ASSERT(!strcmp(properties4->property->name, "getlastmodified"), "test4: wrong property 4");
+                UCX_TEST_ASSERT(properties4->prev == NULL, "test4: prev not NULL (2)");
+                break;
+            }
+            case 3: {
+                UCX_TEST_ASSERT(!strcmp(properties4->property->name, "resourcetype"), "test4: wrong property 5");
+                UCX_TEST_ASSERT(properties4->prev == NULL, "test4: prev not NULL (3)");
+                break;
+            }
+            case 4: {
+                UCX_TEST_ASSERT(!strcmp(properties4->property->name, "getetag"), "test4: wrong property 6");
+                UCX_TEST_ASSERT(properties4->prev == NULL, "test4: prev not NULL (4)");
+                break;
+            }
+            default: {
+                UCX_TEST_ASSERT(i.index <= 5, "fail");
+            }
+        }
+    }
+    
+    UCX_TEST_ASSERT(properties4 == NULL, "test4: list not NULL");
+    
+    UCX_TEST_END;
+    testutil_destroy_session(sn);
+}
+
 UCX_TEST(test_msresponse_addproperty) {
     Session *sn = testutil_session();
     Request *rq = testutil_request(sn->pool, "PROPFIND", "/");
--- a/src/server/test/webdav.h	Sun Dec 29 22:39:35 2019 +0100
+++ b/src/server/test/webdav.h	Mon Dec 30 16:33:20 2019 +0100
@@ -49,6 +49,7 @@
 UCX_TEST(test_msresponse_addproperty);
 
 UCX_TEST(test_webdav_propfind_init);
+UCX_TEST(test_webdav_plist_iterator_remove_current);
 
 /* --------------------------- PROPFIND --------------------------- */
 
--- a/src/server/webdav/requestparser.c	Sun Dec 29 22:39:35 2019 +0100
+++ b/src/server/webdav/requestparser.c	Mon Dec 30 16:33:20 2019 +0100
@@ -38,6 +38,7 @@
 
 #define xstreq(a, b) !strcmp((const char*)a, (const char*)b)
 
+// TODO: make function public
 int proplist_add(
         pool_handle_t *pool,
         WebdavPList **begin,
@@ -48,6 +49,7 @@
     if(!elm) {
         return 1;
     }
+    elm->prev = *end;
     elm->next = NULL;
     elm->property = prop;
     
--- a/src/server/webdav/webdav.c	Sun Dec 29 22:39:35 2019 +0100
+++ b/src/server/webdav/webdav.c	Mon Dec 30 16:33:20 2019 +0100
@@ -310,7 +310,7 @@
             return REQ_ABORTED;
         }
         
-        newPropCount = webdav_plist_count(newProp);
+        newPropCount = webdav_plist_size(newProp);
         
         davList = davList->next;
     }
@@ -625,18 +625,15 @@
             return NULL;
         }
         new_elm->property = elm->property; // new list contains original ptr
-        new_elm->prev = NULL;
+        new_elm->prev = new_list_end;
         new_elm->next = NULL;
         
         if(new_list_end) {
-            new_list_end->next = elm;
-            elm->prev = new_list_end;
-            
-            new_list_end = elm;
+            new_list_end->next = new_elm;
         } else {
             new_list = new_elm;
-            new_list_end = new_elm;
         }
+        new_list_end = new_elm;
         
         elm = elm->next;
     }
@@ -644,7 +641,7 @@
     return new_list;
 }
 
-size_t webdav_plist_count(WebdavPList *list) {
+size_t webdav_plist_size(WebdavPList *list) {
     size_t count = 0;
     WebdavPList *elm = list;
     while(elm) {
@@ -659,7 +656,7 @@
     i.list = list;
     i.cur = NULL;
     i.next = *list;
-    i.index;
+    i.index = 0;
     return i;
 }
 
@@ -678,7 +675,7 @@
 void webdav_plist_iterator_remove_current(WebdavPListIterator *i) {
     WebdavPList *cur = i->cur;
     if(cur->prev) {
-        cur->prev = cur->next;
+        cur->prev->next = cur->next;
         if(cur->next) {
             cur->next->prev = cur->prev;
         }

mercurial