static void test_all(TestBatch *batch) { CharBuf *foo = (CharBuf*)ZCB_WRAP_STR("foo", 3); CharBuf *boffo = (CharBuf*)ZCB_WRAP_STR("boffo", 5); CharBuf *foo_boffo = (CharBuf*)ZCB_WRAP_STR("foo/boffo", 9); CharBuf *test_dir = (CharBuf*)ZCB_WRAP_STR("_fsdir_test", 11); FSFolder *folder = FSFolder_new(test_dir); bool_t saw_foo = false; bool_t saw_boffo = false; bool_t foo_was_dir = false; bool_t boffo_was_dir = false; int count = 0; // Clean up after previous failed runs. FSFolder_Delete(folder, foo_boffo); FSFolder_Delete(folder, foo); FSFolder_Delete(folder, boffo); rmdir("_fsdir_test"); FSFolder_Initialize(folder); FSFolder_MkDir(folder, foo); OutStream *outstream = FSFolder_Open_Out(folder, boffo); DECREF(outstream); outstream = FSFolder_Open_Out(folder, foo_boffo); DECREF(outstream); FSDirHandle *dh = FSDH_open(test_dir); CharBuf *entry = FSDH_Get_Entry(dh); while (FSDH_Next(dh)) { count++; if (CB_Equals(entry, (Obj*)foo)) { saw_foo = true; foo_was_dir = FSDH_Entry_Is_Dir(dh); } else if (CB_Equals(entry, (Obj*)boffo)) { saw_boffo = true; boffo_was_dir = FSDH_Entry_Is_Dir(dh); } } TEST_INT_EQ(batch, 2, count, "correct number of entries"); TEST_TRUE(batch, saw_foo, "Directory was iterated over"); TEST_TRUE(batch, foo_was_dir, "Dir correctly identified by Entry_Is_Dir"); TEST_TRUE(batch, saw_boffo, "File was iterated over"); TEST_FALSE(batch, boffo_was_dir, "File correctly identified by Entry_Is_Dir"); DECREF(dh); FSFolder_Delete(folder, foo_boffo); FSFolder_Delete(folder, foo); FSFolder_Delete(folder, boffo); DECREF(folder); rmdir("_fsdir_test"); }
static void test_protect_symlinks(TestBatchRunner *runner) { #ifdef ENABLE_SYMLINK_TESTS FSFolder *folder = (FSFolder*)S_set_up(); String *foo = (String*)SSTR_WRAP_UTF8("foo", 3); String *bar = (String*)SSTR_WRAP_UTF8("bar", 3); String *foo_boffo = (String*)SSTR_WRAP_UTF8("foo/boffo", 9); FSFolder_MkDir(folder, foo); FSFolder_MkDir(folder, bar); OutStream *outstream = FSFolder_Open_Out(folder, foo_boffo); DECREF(outstream); if (!S_create_test_symlinks()) { FAIL(runner, "symlink creation failed"); FAIL(runner, "symlink creation failed"); FAIL(runner, "symlink creation failed"); FAIL(runner, "symlink creation failed"); FAIL(runner, "symlink creation failed"); // Try to clean up anyway. FSFolder_Delete_Tree(folder, foo); FSFolder_Delete_Tree(folder, bar); } else { VArray *list = FSFolder_List_R(folder, NULL); bool saw_bazooka_boffo = false; for (uint32_t i = 0, max = VA_Get_Size(list); i < max; i++) { String *entry = (String*)VA_Fetch(list, i); if (Str_Ends_With_Utf8(entry, "bazooka/boffo", 13)) { saw_bazooka_boffo = true; } } TEST_FALSE(runner, saw_bazooka_boffo, "List_R() shouldn't follow symlinks"); DECREF(list); TEST_TRUE(runner, FSFolder_Delete_Tree(folder, bar), "Delete_Tree() returns true"); TEST_FALSE(runner, FSFolder_Exists(folder, bar), "Tree is really gone"); TEST_TRUE(runner, FSFolder_Exists(folder, foo), "Original folder sill there"); TEST_TRUE(runner, FSFolder_Exists(folder, foo_boffo), "Delete_Tree() did not follow directory symlink"); FSFolder_Delete_Tree(folder, foo); } DECREF(folder); S_tear_down(); #else SKIP(runner, "Tests requiring symlink() disabled"); SKIP(runner, "Tests requiring symlink() disabled"); SKIP(runner, "Tests requiring symlink() disabled"); SKIP(runner, "Tests requiring symlink() disabled"); SKIP(runner, "Tests requiring symlink() disabled"); #endif // ENABLE_SYMLINK_TESTS }