Sun, 31 May 2020 17:31:43 +0200
add vfs unlink and rmdir tests
--- a/src/server/test/main.c Sun May 31 16:58:23 2020 +0200 +++ b/src/server/test/main.c Sun May 31 17:31:43 2020 +0200 @@ -69,6 +69,8 @@ ucx_test_register(suite, test_vfs_mkdir); ucx_test_register(suite, test_vfs_opendir); ucx_test_register(suite, test_vfs_readdir); + ucx_test_register(suite, test_vfs_unlink); + ucx_test_register(suite, test_vfs_rmdir); // writer tests ucx_test_register(suite, test_writer_putc);
--- a/src/server/test/vfs.c Sun May 31 16:58:23 2020 +0200 +++ b/src/server/test/vfs.c Sun May 31 17:31:43 2020 +0200 @@ -41,6 +41,8 @@ typedef struct TestVFS { UcxMap *files; + int count_unlink; + int count_rmdir; } TestVFS; typedef struct TestVFSFile { @@ -216,6 +218,42 @@ } int testvfs_unlink(VFSContext *ctx, const char *path) { + TestVFS *vfs = ctx->vfs->instance; + TestVFSFile *file = ucx_map_cstr_get(vfs->files, path); + if(!file) { + return 1; + } + + if(file->isdir) { + return 1; + } + + ucx_map_cstr_remove(vfs->files, path); + vfs->count_unlink++; + return 0; +} + +int testvfs_rmdir(VFSContext *ctx, const char *path) { + TestVFS *vfs = ctx->vfs->instance; + TestVFSFile *dir = ucx_map_cstr_get(vfs->files, path); + if(!dir) { + return 1; + } + + if(!dir->isdir) { + return 1; + } + + UcxMapIterator i = ucx_map_iterator(vfs->files); + TestVFSFile *f; + UCX_MAP_FOREACH(key, f, i) { + if(f->path.length > dir->path.length && sstrprefix(f->path, dir->path)){ + return 1; // dir not empty + } + } + + ucx_map_cstr_remove(vfs->files, path); + vfs->count_rmdir++; return 0; } @@ -227,6 +265,7 @@ testvfs_fdopendir, testvfs_mkdir, testvfs_unlink, + testvfs_rmdir, 0, NULL }; @@ -234,6 +273,8 @@ VFS* testvfs_create(Session *sn) { TestVFS *vfs = pool_malloc(sn->pool, sizeof(TestVFS)); + vfs->count_unlink = 0; + vfs->count_rmdir = 0; vfs->files = ucx_map_new_a(session_get_allocator(sn), 64); testVFSClass.instance = vfs; @@ -349,3 +390,88 @@ testutil_destroy_session(sn); } + +UCX_TEST(test_vfs_unlink) { + Session *sn = testutil_session(); + Request *rq = testutil_request(sn->pool, "PUT", "/"); + rq->vfs = testvfs_create(sn); + + VFSContext *vfs = vfs_request_context(sn, rq); + + UCX_TEST_BEGIN; + // prepare test + int err; + err = vfs_mkdir(vfs, "/dir1"); + UCX_TEST_ASSERT(err == 0, "mkdir 1: error not 0"); + err = vfs_mkdir(vfs, "/dir2"); + UCX_TEST_ASSERT(err == 0, "mkdir 1: error not 0"); + + SYS_FILE f1 = vfs_open(vfs, "/file1", O_CREAT); + UCX_TEST_ASSERT(f1, "f1 not opened"); + + SYS_FILE f2 = vfs_open(vfs, "/file2", O_CREAT); + UCX_TEST_ASSERT(f1, "f2 not opened"); + + SYS_FILE f3 = vfs_open(vfs, "/dir1/file3", O_CREAT); + UCX_TEST_ASSERT(f1, "f3 not opened"); + + // test unlink + err = vfs_unlink(vfs, "/file1"); + UCX_TEST_ASSERT(err == 0, "unlink /file1 failed"); + err = vfs_unlink(vfs, "/dir1/file3"); + UCX_TEST_ASSERT(err == 0, "unlink /dir1/file3 failed"); + + err = vfs_unlink(vfs, "/filex"); + UCX_TEST_ASSERT(err != 0, "unlink /filex should fail"); + + // check if files were removed + SYS_FILE o1 = vfs_open(vfs, "/file1", O_RDONLY); + UCX_TEST_ASSERT(o1 == NULL, "/file1 not deleted"); + SYS_FILE o3 = vfs_open(vfs, "/dir1/file3", O_RDONLY); + UCX_TEST_ASSERT(o1 == NULL, "/dir1/file3 not deleted"); + + // file2 should still be there + SYS_FILE o2 = vfs_open(vfs, "/file2", O_RDONLY); + UCX_TEST_ASSERT(o2, "/file2 deleted"); + + // check if dir unlink fails + err = vfs_unlink(vfs, "/dir1"); + UCX_TEST_ASSERT(err != 0, "unlink dir1 should fail"); + + UCX_TEST_END; + + testutil_destroy_session(sn); +} + +UCX_TEST(test_vfs_rmdir) { + Session *sn = testutil_session(); + Request *rq = testutil_request(sn->pool, "PUT", "/"); + rq->vfs = testvfs_create(sn); + + VFSContext *vfs = vfs_request_context(sn, rq); + + UCX_TEST_BEGIN; + // prepare test + int err; + err = vfs_mkdir(vfs, "/dir1"); + UCX_TEST_ASSERT(err == 0, "mkdir 1: error not 0"); + err = vfs_mkdir(vfs, "/dir2"); + UCX_TEST_ASSERT(err == 0, "mkdir 1: error not 0"); + + SYS_FILE f1 = vfs_open(vfs, "/dir1/file1", O_CREAT); + UCX_TEST_ASSERT(f1, "f1 not opened"); + + err = vfs_rmdir(vfs, "/dir1"); + UCX_TEST_ASSERT(err != 0, "rmdir /dir1 should fail"); + err = vfs_rmdir(vfs, "/dir2"); + UCX_TEST_ASSERT(err == 0, "rmdir /dir2 failed"); + + err = vfs_unlink(vfs, "/dir1/file1"); + UCX_TEST_ASSERT(err == 0, "unlink failed"); + err = vfs_rmdir(vfs, "/dir1"); + UCX_TEST_ASSERT(err == 0, "rmdir /dir1 (2) failed"); + + UCX_TEST_END; + + testutil_destroy_session(sn); +}