Пример #1
0
static void
test_Local_Delete(TestBatchRunner *runner) {
    RAMFolder *folder = RAMFolder_new(NULL);
    FileHandle *fh;

    fh = RAMFolder_Open_FileHandle(folder, boffo, FH_CREATE | FH_WRITE_ONLY);
    DECREF(fh);
    TEST_TRUE(runner, RAMFolder_Local_Delete(folder, boffo),
              "Local_Delete on file succeeds");

    RAMFolder_Local_MkDir(folder, foo);
    fh = RAMFolder_Open_FileHandle(folder, foo_boffo,
                                   FH_CREATE | FH_WRITE_ONLY);
    DECREF(fh);

    Err_set_error(NULL);
    TEST_FALSE(runner, RAMFolder_Local_Delete(folder, foo),
               "Local_Delete on non-empty dir fails");

    RAMFolder_Delete(folder, foo_boffo);
    TEST_TRUE(runner, RAMFolder_Local_Delete(folder, foo),
              "Local_Delete on empty dir succeeds");

    DECREF(folder);
}
Пример #2
0
static void
test_all(TestBatchRunner *runner) {
    RAMFolder *folder        = RAMFolder_new(NULL);
    String    *foo           = SSTR_WRAP_C("foo");
    String    *boffo         = SSTR_WRAP_C("boffo");
    String    *foo_boffo     = SSTR_WRAP_C("foo/boffo");
    bool       saw_foo       = false;
    bool       saw_boffo     = false;
    bool       foo_was_dir   = false;
    bool       boffo_was_dir = false;
    int        count         = 0;

    // Set up folder contents.
    RAMFolder_MkDir(folder, foo);
    FileHandle *fh = RAMFolder_Open_FileHandle(folder, boffo,
                                               FH_CREATE | FH_WRITE_ONLY);
    DECREF(fh);
    fh = RAMFolder_Open_FileHandle(folder, foo_boffo,
                                   FH_CREATE | FH_WRITE_ONLY);
    DECREF(fh);

    RAMDirHandle *dh = RAMDH_new(folder);
    while (RAMDH_Next(dh)) {
        count++;
        String *entry = RAMDH_Get_Entry(dh);
        if (Str_Equals(entry, (Obj*)foo)) {
            saw_foo = true;
            foo_was_dir = RAMDH_Entry_Is_Dir(dh);
        }
        else if (Str_Equals(entry, (Obj*)boffo)) {
            saw_boffo = true;
            boffo_was_dir = RAMDH_Entry_Is_Dir(dh);
        }
        DECREF(entry);
    }
    TEST_INT_EQ(runner, 2, count, "correct number of entries");
    TEST_TRUE(runner, saw_foo, "Directory was iterated over");
    TEST_TRUE(runner, foo_was_dir,
              "Dir correctly identified by Entry_Is_Dir");
    TEST_TRUE(runner, saw_boffo, "File was iterated over");
    TEST_FALSE(runner, boffo_was_dir,
               "File correctly identified by Entry_Is_Dir");

    uint32_t refcount = REFCOUNT_NN(folder);
    RAMDH_Close(dh);
    TEST_INT_EQ(runner, REFCOUNT_NN(folder), refcount - 1,
                "Folder reference released by Close()");

    DECREF(dh);
    DECREF(folder);
}
Пример #3
0
static void
test_Local_MkDir(TestBatchRunner *runner) {
    RAMFolder *folder = RAMFolder_new(NULL);
    bool result;

    result = RAMFolder_Local_MkDir(folder, foo);
    TEST_TRUE(runner, result, "Local_MkDir succeeds and returns true");

    Err_set_error(NULL);
    result = RAMFolder_Local_MkDir(folder, foo);
    TEST_FALSE(runner, result,
               "Local_MkDir returns false when a dir already exists");
    TEST_TRUE(runner, Err_get_error() != NULL,
              "Local_MkDir sets Err_error when a dir already exists");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo),
              "Existing dir untouched after failed Local_MkDir");

    FileHandle *fh = RAMFolder_Open_FileHandle(folder, boffo,
                                               FH_CREATE | FH_WRITE_ONLY);
    DECREF(fh);
    Err_set_error(NULL);
    result = RAMFolder_Local_MkDir(folder, foo);
    TEST_FALSE(runner, result,
               "Local_MkDir returns false when a file already exists");
    TEST_TRUE(runner, Err_get_error() != NULL,
              "Local_MkDir sets Err_error when a file already exists");
    TEST_TRUE(runner, RAMFolder_Exists(folder, boffo) &&
              !RAMFolder_Local_Is_Directory(folder, boffo),
              "Existing file untouched after failed Local_MkDir");

    DECREF(folder);
}
Пример #4
0
static void
test_Local_Find_Folder(TestBatchRunner *runner) {
    RAMFolder *folder = RAMFolder_new(NULL);
    RAMFolder *local;
    FileHandle *fh;

    RAMFolder_MkDir(folder, foo);
    RAMFolder_MkDir(folder, foo_bar);
    fh = RAMFolder_Open_FileHandle(folder, boffo,
                                   FH_CREATE | FH_WRITE_ONLY);
    DECREF(fh);
    fh = RAMFolder_Open_FileHandle(folder, foo_boffo,
                                   FH_CREATE | FH_WRITE_ONLY);
    DECREF(fh);

    local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, nope);
    TEST_TRUE(runner, local == NULL, "Non-existent entry yields NULL");

    ZombieCharBuf *empty = ZCB_BLANK();
    local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, (CharBuf*)empty);
    TEST_TRUE(runner, local == NULL, "Empty string yields NULL");

    local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, foo_bar);
    TEST_TRUE(runner, local == NULL, "nested folder yields NULL");

    local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, foo_boffo);
    TEST_TRUE(runner, local == NULL, "nested file yields NULL");

    local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, boffo);
    TEST_TRUE(runner, local == NULL, "local file yields NULL");

    local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, bar);
    TEST_TRUE(runner, local == NULL, "name of nested folder yields NULL");

    local = (RAMFolder*)RAMFolder_Local_Find_Folder(folder, foo);
    TEST_TRUE(runner,
              local
              && RAMFolder_Is_A(local, RAMFOLDER)
              && CB_Equals_Str(RAMFolder_Get_Path(local), "foo", 3),
              "Find local directory");

    DECREF(folder);
}
Пример #5
0
static void
test_Local_Exists(TestBatchRunner *runner) {
    RAMFolder *folder = RAMFolder_new(NULL);
    FileHandle *fh = RAMFolder_Open_FileHandle(folder, boffo,
                                               FH_CREATE | FH_WRITE_ONLY);
    DECREF(fh);
    RAMFolder_Local_MkDir(folder, foo);

    TEST_TRUE(runner, RAMFolder_Local_Exists(folder, boffo),
              "Local_Exists() returns true for file");
    TEST_TRUE(runner, RAMFolder_Local_Exists(folder, foo),
              "Local_Exists() returns true for dir");
    TEST_FALSE(runner, RAMFolder_Local_Exists(folder, bar),
               "Local_Exists() returns false for non-existent entry");

    DECREF(folder);
}
Пример #6
0
static void
test_Hard_Link(TestBatchRunner *runner) {
    RAMFolder *folder = RAMFolder_new(NULL);
    FileHandle *fh;
    bool result;

    RAMFolder_MkDir(folder, foo);
    RAMFolder_MkDir(folder, foo_bar);
    fh = RAMFolder_Open_FileHandle(folder, boffo, FH_CREATE | FH_WRITE_ONLY);
    DECREF(fh);

    // Link files.

    result = RAMFolder_Hard_Link(folder, boffo, banana);
    TEST_TRUE(runner, result, "Hard_Link succeeds and returns true");
    TEST_TRUE(runner, RAMFolder_Exists(folder, banana),
              "File exists at new path");
    TEST_TRUE(runner, RAMFolder_Exists(folder, boffo),
              "File still exists at old path");
    RAMFolder_Delete(folder, boffo);

    result = RAMFolder_Hard_Link(folder, banana, foo_bar_boffo);
    TEST_TRUE(runner, result, "Hard_Link to target within nested dir");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_bar_boffo),
              "File exists at new path");
    TEST_TRUE(runner, RAMFolder_Exists(folder, banana),
              "File still exists at old path");
    RAMFolder_Delete(folder, banana);

    result = RAMFolder_Hard_Link(folder, foo_bar_boffo, foo_boffo);
    TEST_TRUE(runner, result, "Hard_Link from file in nested dir");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_boffo),
              "File exists at new path");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_bar_boffo),
              "File still exists at old path");
    RAMFolder_Delete(folder, foo_bar_boffo);

    // Invalid clobbers.

    fh = RAMFolder_Open_FileHandle(folder, boffo, FH_CREATE | FH_WRITE_ONLY);
    DECREF(fh);
    result = RAMFolder_Hard_Link(folder, foo_boffo, boffo);
    TEST_FALSE(runner, result, "Clobber of file fails");
    TEST_TRUE(runner, RAMFolder_Exists(folder, boffo),
              "File still exists at new path");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_boffo),
              "File still exists at old path");
    RAMFolder_Delete(folder, boffo);

    RAMFolder_MkDir(folder, baz);
    result = RAMFolder_Hard_Link(folder, foo_boffo, baz);
    TEST_FALSE(runner, result, "Clobber of dir fails");
    TEST_TRUE(runner, RAMFolder_Exists(folder, baz),
              "Dir still exists at new path");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_boffo),
              "File still exists at old path");
    RAMFolder_Delete(folder, baz);

    // Invalid Hard_Link of dir.

    RAMFolder_MkDir(folder, baz);
    result = RAMFolder_Hard_Link(folder, baz, banana);
    TEST_FALSE(runner, result, "Hard_Link dir fails");
    TEST_FALSE(runner, RAMFolder_Exists(folder, banana),
               "Nothing at new path");
    TEST_TRUE(runner, RAMFolder_Exists(folder, baz),
              "Folder still exists at old path");
    RAMFolder_Delete(folder, baz);

    // Test that linking to yourself fails.

    result = RAMFolder_Hard_Link(folder, foo_boffo, foo_boffo);
    TEST_FALSE(runner, result, "Hard_Link file to itself fails");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_boffo),
              "File still exists");

    // Invalid filepaths.

    Err_set_error(NULL);
    result = RAMFolder_Rename(folder, foo_boffo, nope_nyet);
    TEST_FALSE(runner, result, "Hard_Link into non-existent subdir fails");
    TEST_TRUE(runner, Err_get_error() != NULL,
              "Hard_Link into non-existent subdir sets Err_error");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_boffo),
              "Entry still exists at old path");

    Err_set_error(NULL);
    result = RAMFolder_Rename(folder, nope_nyet, boffo);
    TEST_FALSE(runner, result, "Hard_Link non-existent source file fails");
    TEST_TRUE(runner, Err_get_error() != NULL,
              "Hard_Link non-existent source file sets Err_error");

    DECREF(folder);
}
Пример #7
0
static void
test_Rename(TestBatchRunner *runner) {
    RAMFolder *folder = RAMFolder_new(NULL);
    FileHandle *fh;
    bool result;

    RAMFolder_MkDir(folder, foo);
    RAMFolder_MkDir(folder, foo_bar);
    fh = RAMFolder_Open_FileHandle(folder, boffo, FH_CREATE | FH_WRITE_ONLY);
    DECREF(fh);

    // Move files.

    result = RAMFolder_Rename(folder, boffo, banana);
    TEST_TRUE(runner, result, "Rename succeeds and returns true");
    TEST_TRUE(runner, RAMFolder_Exists(folder, banana),
              "File exists at new path");
    TEST_FALSE(runner, RAMFolder_Exists(folder, boffo),
               "File no longer exists at old path");

    result = RAMFolder_Rename(folder, banana, foo_bar_boffo);
    TEST_TRUE(runner, result, "Rename to file in nested dir");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_bar_boffo),
              "File exists at new path");
    TEST_FALSE(runner, RAMFolder_Exists(folder, banana),
               "File no longer exists at old path");

    result = RAMFolder_Rename(folder, foo_bar_boffo, boffo);
    TEST_TRUE(runner, result, "Rename from file in nested dir");
    TEST_TRUE(runner, RAMFolder_Exists(folder, boffo),
              "File exists at new path");
    TEST_FALSE(runner, RAMFolder_Exists(folder, foo_bar_boffo),
               "File no longer exists at old path");

    fh = RAMFolder_Open_FileHandle(folder, foo_boffo,
                                   FH_CREATE | FH_WRITE_ONLY);
    DECREF(fh);
    result = RAMFolder_Rename(folder, boffo, foo_boffo);
    TEST_TRUE(runner, result, "Clobber");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_boffo),
              "File exists at new path");
    TEST_FALSE(runner, RAMFolder_Exists(folder, boffo),
               "File no longer exists at old path");

    // Move Dirs.

    RAMFolder_MkDir(folder, baz);
    result = RAMFolder_Rename(folder, baz, boffo);
    TEST_TRUE(runner, result, "Rename dir");
    TEST_TRUE(runner, RAMFolder_Exists(folder, boffo),
              "Folder exists at new path");
    TEST_FALSE(runner, RAMFolder_Exists(folder, baz),
               "Folder no longer exists at old path");

    result = RAMFolder_Rename(folder, boffo, foo_foo);
    TEST_TRUE(runner, result, "Rename dir into nested subdir");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_foo),
              "Folder exists at new path");
    TEST_FALSE(runner, RAMFolder_Exists(folder, boffo),
               "Folder no longer exists at old path");

    result = RAMFolder_Rename(folder, foo_foo, foo_bar_baz);
    TEST_TRUE(runner, result, "Rename dir from nested subdir");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_bar_baz),
              "Folder exists at new path");
    TEST_FALSE(runner, RAMFolder_Exists(folder, foo_foo),
               "Folder no longer exists at old path");

    // Test failed clobbers.

    Err_set_error(NULL);
    result = RAMFolder_Rename(folder, foo_boffo, foo_bar);
    TEST_FALSE(runner, result, "Rename file clobbering dir fails");
    TEST_TRUE(runner, Err_get_error() != NULL,
              "Failed rename sets Err_error");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_boffo),
              "File still exists at old path");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_bar),
              "Dir still exists after failed clobber");

    Err_set_error(NULL);
    result = RAMFolder_Rename(folder, foo_bar, foo_boffo);
    TEST_FALSE(runner, result, "Rename dir clobbering file fails");
    TEST_TRUE(runner, Err_get_error() != NULL,
              "Failed rename sets Err_error");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_bar),
              "Dir still exists at old path");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_boffo),
              "File still exists after failed clobber");

    // Test that "renaming" succeeds where to and from are the same.

    result = RAMFolder_Rename(folder, foo_boffo, foo_boffo);
    TEST_TRUE(runner, result, "Renaming file to itself succeeds");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_boffo),
              "File still exists");

    result = RAMFolder_Rename(folder, foo_bar, foo_bar);
    TEST_TRUE(runner, result, "Renaming dir to itself succeeds");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_bar),
              "Dir still exists");

    // Invalid filepaths.

    Err_set_error(NULL);
    result = RAMFolder_Rename(folder, foo_boffo, nope_nyet);
    TEST_FALSE(runner, result, "Rename into non-existent subdir fails");
    TEST_TRUE(runner, Err_get_error() != NULL,
              "Renaming into non-existent subdir sets Err_error");
    TEST_TRUE(runner, RAMFolder_Exists(folder, foo_boffo),
              "Entry still exists at old path");

    Err_set_error(NULL);
    result = RAMFolder_Rename(folder, nope_nyet, boffo);
    TEST_FALSE(runner, result, "Rename non-existent file fails");
    TEST_TRUE(runner, Err_get_error() != NULL,
              "Renaming non-existent source file sets Err_error");

    DECREF(folder);
}