add webdav_delete tests webdav

Sun, 31 May 2020 19:35:29 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 31 May 2020 19:35:29 +0200
branch
webdav
changeset 249
3b302093945c
parent 248
bc8f8ddbad2e
child 250
f4d93355b054

add webdav_delete tests

src/server/test/main.c file | annotate | diff | comparison | revisions
src/server/test/vfs.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/operation.c file | annotate | diff | comparison | revisions
src/server/webdav/webdav.c file | annotate | diff | comparison | revisions
--- a/src/server/test/main.c	Sun May 31 17:31:43 2020 +0200
+++ b/src/server/test/main.c	Sun May 31 19:35:29 2020 +0200
@@ -99,6 +99,7 @@
     ucx_test_register(suite, test_msresponse_addproperty_with_errors);
     ucx_test_register(suite, test_webdav_op_proppatch);
     ucx_test_register(suite, test_webdav_vfs_op_do);
+    ucx_test_register(suite, test_webdav_delete);
        
     // webdav methods
     ucx_test_register(suite, test_webdav_propfind);
--- a/src/server/test/vfs.c	Sun May 31 17:31:43 2020 +0200
+++ b/src/server/test/vfs.c	Sun May 31 19:35:29 2020 +0200
@@ -81,7 +81,14 @@
     
     TestVFSFile *file = NULL;
     UCX_MAP_FOREACH(key, file, vfsdir->i) {
-        if(sstrprefix(file->path, prefix)) {
+        sstr_t file_path = sstrcat(
+                2,
+                prefix,
+                sstr(test_resource_name(file->path.ptr)));
+        void *m = ucx_map_get(vfs->files, ucx_key(file_path.ptr, file_path.length));
+        // don't ask why alfree and not free()
+        alfree(ucx_default_allocator(), file_path.ptr);
+        if(m) {
             break;
         } else {
             file = NULL;
@@ -95,6 +102,14 @@
                 sstr(test_resource_name(file->path.ptr)));
         ZERO(entry, sizeof(VFS_ENTRY));
         entry->name = vfsdir->name.ptr;
+        
+        if(getstat) {
+            ZERO(&entry->stat, sizeof(struct stat));
+            if(file->isdir) {
+                entry->stat.st_mode = S_IFDIR;
+            }
+        }
+        
         return 1;
     } else {
         return 0;
@@ -151,6 +166,12 @@
     if(!file) {
         return 1;
     }
+    
+    ZERO(buf, sizeof(struct stat));
+    if(file->isdir) {
+        buf->st_mode = S_IFDIR;
+    }
+    
     return 0;
 }
 
--- a/src/server/test/webdav.c	Sun May 31 17:31:43 2020 +0200
+++ b/src/server/test/webdav.c	Sun May 31 19:35:29 2020 +0200
@@ -1558,6 +1558,18 @@
     return 1;
 }
 
+static int delete_count = 0;
+static int delete_finish_count = 0;
+
+static int test_backend_webdav_delete(WebdavVFSRequest *req, WSBool *created) {
+    delete_count++;    
+    return 0;
+}
+
+static int test_backend_webdav_delete_finish(WebdavVFSRequest *req, WSBool success) {
+    delete_finish_count++;
+    return 0;
+}
 
 
 UCX_TEST(test_webdav_vfs_op_do) {
@@ -1566,13 +1578,20 @@
     TestIOStream *st;
     pblock *pb;
     
+    // Tests performed primarily with MKCOL, because webdav_vfs_op_do
+    // behaves the same for both operations
+    // the only difference are the callbacks
+    
     init_test_webdav_method(&sn, &rq, &st, &pb, "MKCOL", NULL);
-    rq->vfs = testvfs_create(sn);
+    VFS *testvfs = testvfs_create(sn);
+    rq->vfs = testvfs;
     
     WebdavBackend dav1;
     ZERO(&dav1, sizeof(WebdavBackend));
     dav1.opt_mkcol = test_webdav_mkcol;
     dav1.opt_mkcol_finish = test_webdav_mkcol_finish;
+    dav1.opt_delete = test_backend_webdav_delete;
+    dav1.opt_delete_finish = test_backend_webdav_delete_finish;
     
     WebdavBackend dav2;
     ZERO(&dav2, sizeof(WebdavBackend));
@@ -1625,6 +1644,83 @@
     UCX_TEST_ASSERT(mkcol_count == 1, "op3: wrong mkcol_count");
     UCX_TEST_ASSERT(mkcol_finish_count == 1, "op3: wrong mkcol_finish_count");
     
+    // test DELETE to make sure, delete callbacks will be used
+    pblock_replace("path", "/deltest", rq->vars);
+    rq->vfs = testvfs;
+    WebdavVFSOperation *op_del = webdav_vfs_op(sn, rq, &dav1, FALSE);
+    vfs_open(op_del->vfs, "/deltest", O_CREAT);
+    ret = webdav_vfs_op_do(op_del, WEBDAV_VFS_DELETE);
+    
+    UCX_TEST_ASSERT(!ret, "op_del failed");
+    UCX_TEST_ASSERT(delete_count == 1, "op_del: wrong delete_count");
+    UCX_TEST_ASSERT(delete_finish_count == 1, "op_del: wrong delete_finish_count");
+    
     
     UCX_TEST_END;
 }
+
+UCX_TEST(test_webdav_delete){
+    Session *sn;
+    Request *rq; 
+    TestIOStream *st;
+    pblock *pb;
+    
+    init_test_webdav_method(&sn, &rq, &st, &pb, "DELETE", NULL);
+    rq->vfs = testvfs_create(sn);
+    
+    WebdavBackend dav1;
+    ZERO(&dav1, sizeof(WebdavBackend));
+    dav1.opt_delete = test_backend_webdav_delete;
+    dav1.opt_delete_finish = test_backend_webdav_delete_finish;
+    delete_count = 0;
+    delete_finish_count = 0;
+    rq->davCollection = &dav1;
+    
+    UCX_TEST_BEGIN;
+    
+    // prepare
+    VFSContext *vfs = vfs_request_context(sn, rq);
+    int err;
+    err = vfs_mkdir(vfs, "/dir1");
+    UCX_TEST_ASSERT(err == 0, "mkdir dir1 failed");
+    err = vfs_mkdir(vfs, "/dir2");
+    UCX_TEST_ASSERT(err == 0, "mkdir dir2 failed");
+    err = vfs_mkdir(vfs, "/dir2/dir3");
+    UCX_TEST_ASSERT(err == 0, "mkdir dir3 failed");
+    err = vfs_mkdir(vfs, "/dir2/dir4");
+    UCX_TEST_ASSERT(err == 0, "mkdir dir4 failed");
+    err = vfs_mkdir(vfs, "/dir2/dir4/dir5");
+    UCX_TEST_ASSERT(err == 0, "mkdir dir5 failed");
+    
+    SYS_FILE f0 = vfs_open(vfs, "/file0", O_CREAT);
+    UCX_TEST_ASSERT(f0, "f0 create failed");
+    // no f1
+    SYS_FILE f2 = vfs_open(vfs, "/dir2/file2", O_CREAT);
+    UCX_TEST_ASSERT(f2, "f2 create failed");
+    SYS_FILE f3 = vfs_open(vfs, "/dir2/dir3/file3", O_CREAT);
+    UCX_TEST_ASSERT(f3, "f3 create failed");
+    SYS_FILE f4 = vfs_open(vfs, "/dir2/dir4/file4", O_CREAT);
+    UCX_TEST_ASSERT(f4, "f4 create failed");
+    SYS_FILE f5 = vfs_open(vfs, "/dir2/dir4/dir5/file5", O_CREAT);
+    UCX_TEST_ASSERT(f5, "f5 create failed");
+    
+    // delete single file
+    pblock_replace("path", "/file0", rq->vars);
+    err = webdav_delete(NULL, sn, rq);
+    UCX_TEST_ASSERT(err == 0, "DELETE /file0 failed");
+    UCX_TEST_ASSERT(delete_count == 1, "del1: wrong delete count");
+    
+    delete_count = 0;
+    pblock_replace("path", "/dir1", rq->vars);
+    err = webdav_delete(NULL, sn, rq);
+    UCX_TEST_ASSERT(err == 0, "DELETE /dir1 failed");
+    UCX_TEST_ASSERT(delete_count == 1, "del1: wrong delete count");
+    
+    delete_count = 0;
+    pblock_replace("path", "/dir2", rq->vars);
+    err = webdav_delete(NULL, sn, rq);
+    UCX_TEST_ASSERT(err == 0, "DELETE /dir2 failed");
+    UCX_TEST_ASSERT(delete_count == 8, "del2: wrong delete count");
+    
+    UCX_TEST_END;
+}
--- a/src/server/test/webdav.h	Sun May 31 17:31:43 2020 +0200
+++ b/src/server/test/webdav.h	Sun May 31 19:35:29 2020 +0200
@@ -66,6 +66,8 @@
 
 UCX_TEST(test_webdav_vfs_op_do);
 
+UCX_TEST(test_webdav_delete);
+
 /* --------------------------- PROPFIND --------------------------- */
 
 #define TEST_PROPFIND1 "<?xml version=\"1.0\" encoding=\"utf-8\" ?> \
--- a/src/server/webdav/operation.c	Sun May 31 17:31:43 2020 +0200
+++ b/src/server/webdav/operation.c	Sun May 31 19:35:29 2020 +0200
@@ -454,7 +454,7 @@
     sub.path = path;
     sub.sn = op->sn;
     sub.vfs = op->vfs;
-    sub.path = op->path;
+    sub.path = path;
     sub.stat = s;
     sub.stat_errno = 0;
     return sub;
@@ -749,6 +749,4 @@
             return vfs_rmdir(op->vfs, op->path);
         }
     }
-    
-    return 0;
 }
--- a/src/server/webdav/webdav.c	Sun May 31 17:31:43 2020 +0200
+++ b/src/server/webdav/webdav.c	Sun May 31 19:35:29 2020 +0200
@@ -537,6 +537,23 @@
         return 1;
     }
     
+    // add root to list of dir list
+    DeleteFile root;
+    root.path = op->path;
+    root.s = *op->stat;
+    UcxList root_elm;
+    root_elm.data = &root;
+    root_elm.prev = NULL;
+    root_elm.next = del.dirs_begin;
+    
+    if(del.dirs_begin) {
+        del.dirs_begin->prev = &root_elm;
+        del.dirs_begin = &root_elm;
+    } else {
+        del.dirs_begin = &root_elm;
+        del.dirs_end = &root_elm;
+    }
+    
     // delete files first
     UCX_FOREACH(elm, del.files_begin) {
         DeleteFile *file = elm->data;
@@ -570,6 +587,7 @@
         sys_set_error_status(op->vfs);
         return REQ_ABORTED;
     }
+    op->stat = &s;
     
     int ret;
     if(S_ISDIR(s.st_mode)) {

mercurial