src/server/test/vfs.c

branch
webdav
changeset 248
bc8f8ddbad2e
parent 241
4adad7faf452
child 249
3b302093945c
equal deleted inserted replaced
247:1df803e06076 248:bc8f8ddbad2e
39 39
40 #include "vfs.h" 40 #include "vfs.h"
41 41
42 typedef struct TestVFS { 42 typedef struct TestVFS {
43 UcxMap *files; 43 UcxMap *files;
44 int count_unlink;
45 int count_rmdir;
44 } TestVFS; 46 } TestVFS;
45 47
46 typedef struct TestVFSFile { 48 typedef struct TestVFSFile {
47 VFSFile file; 49 VFSFile file;
48 sstr_t path; 50 sstr_t path;
214 216
215 return 0; 217 return 0;
216 } 218 }
217 219
218 int testvfs_unlink(VFSContext *ctx, const char *path) { 220 int testvfs_unlink(VFSContext *ctx, const char *path) {
221 TestVFS *vfs = ctx->vfs->instance;
222 TestVFSFile *file = ucx_map_cstr_get(vfs->files, path);
223 if(!file) {
224 return 1;
225 }
226
227 if(file->isdir) {
228 return 1;
229 }
230
231 ucx_map_cstr_remove(vfs->files, path);
232 vfs->count_unlink++;
233 return 0;
234 }
235
236 int testvfs_rmdir(VFSContext *ctx, const char *path) {
237 TestVFS *vfs = ctx->vfs->instance;
238 TestVFSFile *dir = ucx_map_cstr_get(vfs->files, path);
239 if(!dir) {
240 return 1;
241 }
242
243 if(!dir->isdir) {
244 return 1;
245 }
246
247 UcxMapIterator i = ucx_map_iterator(vfs->files);
248 TestVFSFile *f;
249 UCX_MAP_FOREACH(key, f, i) {
250 if(f->path.length > dir->path.length && sstrprefix(f->path, dir->path)){
251 return 1; // dir not empty
252 }
253 }
254
255 ucx_map_cstr_remove(vfs->files, path);
256 vfs->count_rmdir++;
219 return 0; 257 return 0;
220 } 258 }
221 259
222 static VFS testVFSClass = { 260 static VFS testVFSClass = {
223 testvfs_open, 261 testvfs_open,
225 testvfs_fstat, 263 testvfs_fstat,
226 testvfs_opendir, 264 testvfs_opendir,
227 testvfs_fdopendir, 265 testvfs_fdopendir,
228 testvfs_mkdir, 266 testvfs_mkdir,
229 testvfs_unlink, 267 testvfs_unlink,
268 testvfs_rmdir,
230 0, 269 0,
231 NULL 270 NULL
232 }; 271 };
233 272
234 273
235 VFS* testvfs_create(Session *sn) { 274 VFS* testvfs_create(Session *sn) {
236 TestVFS *vfs = pool_malloc(sn->pool, sizeof(TestVFS)); 275 TestVFS *vfs = pool_malloc(sn->pool, sizeof(TestVFS));
276 vfs->count_unlink = 0;
277 vfs->count_rmdir = 0;
237 vfs->files = ucx_map_new_a(session_get_allocator(sn), 64); 278 vfs->files = ucx_map_new_a(session_get_allocator(sn), 64);
238 279
239 testVFSClass.instance = vfs; 280 testVFSClass.instance = vfs;
240 return &testVFSClass; 281 return &testVFSClass;
241 } 282 }
347 388
348 UCX_TEST_END; 389 UCX_TEST_END;
349 390
350 testutil_destroy_session(sn); 391 testutil_destroy_session(sn);
351 } 392 }
393
394 UCX_TEST(test_vfs_unlink) {
395 Session *sn = testutil_session();
396 Request *rq = testutil_request(sn->pool, "PUT", "/");
397 rq->vfs = testvfs_create(sn);
398
399 VFSContext *vfs = vfs_request_context(sn, rq);
400
401 UCX_TEST_BEGIN;
402 // prepare test
403 int err;
404 err = vfs_mkdir(vfs, "/dir1");
405 UCX_TEST_ASSERT(err == 0, "mkdir 1: error not 0");
406 err = vfs_mkdir(vfs, "/dir2");
407 UCX_TEST_ASSERT(err == 0, "mkdir 1: error not 0");
408
409 SYS_FILE f1 = vfs_open(vfs, "/file1", O_CREAT);
410 UCX_TEST_ASSERT(f1, "f1 not opened");
411
412 SYS_FILE f2 = vfs_open(vfs, "/file2", O_CREAT);
413 UCX_TEST_ASSERT(f1, "f2 not opened");
414
415 SYS_FILE f3 = vfs_open(vfs, "/dir1/file3", O_CREAT);
416 UCX_TEST_ASSERT(f1, "f3 not opened");
417
418 // test unlink
419 err = vfs_unlink(vfs, "/file1");
420 UCX_TEST_ASSERT(err == 0, "unlink /file1 failed");
421 err = vfs_unlink(vfs, "/dir1/file3");
422 UCX_TEST_ASSERT(err == 0, "unlink /dir1/file3 failed");
423
424 err = vfs_unlink(vfs, "/filex");
425 UCX_TEST_ASSERT(err != 0, "unlink /filex should fail");
426
427 // check if files were removed
428 SYS_FILE o1 = vfs_open(vfs, "/file1", O_RDONLY);
429 UCX_TEST_ASSERT(o1 == NULL, "/file1 not deleted");
430 SYS_FILE o3 = vfs_open(vfs, "/dir1/file3", O_RDONLY);
431 UCX_TEST_ASSERT(o1 == NULL, "/dir1/file3 not deleted");
432
433 // file2 should still be there
434 SYS_FILE o2 = vfs_open(vfs, "/file2", O_RDONLY);
435 UCX_TEST_ASSERT(o2, "/file2 deleted");
436
437 // check if dir unlink fails
438 err = vfs_unlink(vfs, "/dir1");
439 UCX_TEST_ASSERT(err != 0, "unlink dir1 should fail");
440
441 UCX_TEST_END;
442
443 testutil_destroy_session(sn);
444 }
445
446 UCX_TEST(test_vfs_rmdir) {
447 Session *sn = testutil_session();
448 Request *rq = testutil_request(sn->pool, "PUT", "/");
449 rq->vfs = testvfs_create(sn);
450
451 VFSContext *vfs = vfs_request_context(sn, rq);
452
453 UCX_TEST_BEGIN;
454 // prepare test
455 int err;
456 err = vfs_mkdir(vfs, "/dir1");
457 UCX_TEST_ASSERT(err == 0, "mkdir 1: error not 0");
458 err = vfs_mkdir(vfs, "/dir2");
459 UCX_TEST_ASSERT(err == 0, "mkdir 1: error not 0");
460
461 SYS_FILE f1 = vfs_open(vfs, "/dir1/file1", O_CREAT);
462 UCX_TEST_ASSERT(f1, "f1 not opened");
463
464 err = vfs_rmdir(vfs, "/dir1");
465 UCX_TEST_ASSERT(err != 0, "rmdir /dir1 should fail");
466 err = vfs_rmdir(vfs, "/dir2");
467 UCX_TEST_ASSERT(err == 0, "rmdir /dir2 failed");
468
469 err = vfs_unlink(vfs, "/dir1/file1");
470 UCX_TEST_ASSERT(err == 0, "unlink failed");
471 err = vfs_rmdir(vfs, "/dir1");
472 UCX_TEST_ASSERT(err == 0, "rmdir /dir1 (2) failed");
473
474 UCX_TEST_END;
475
476 testutil_destroy_session(sn);
477 }

mercurial