Verified Commit 1ac03c37 authored by Tommaso Tocci's avatar Tommaso Tocci
Browse files

tests: added directory functionalities test

parent 33865a14
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ add_executable(ifs_test_temp ${SOURCE_FILES_TEMP})

add_executable(ifs_test_wr wr_test.cpp)

add_executable(ifs_test_dir dir_test.cpp)

find_package(MPI)
if(${MPI_FOUND})
    set(SOURCE_FILES_MPI main_MPI.cpp)

ifs_test/dir_test.cpp

0 → 100644
+150 −0
Original line number Diff line number Diff line
/* Test directories functionalities
 *
 */

#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <cstring>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <cerrno>
#include <cassert>
#include <unordered_map>


int main(int argc, char* argv[]) {

    /**
        /tmp/mountdir
        ├── top_plus
        └── top
            ├── dir_a
            |   └── subdir_a
            ├── dir_b
            └── file_a
    */
    const std::string mntdir = "/tmp/mountdir";
    const std::string topdir = mntdir + "/top";
    const std::string longer = topdir + "_plus";
    const std::string dir_a  = topdir + "/dir_a";
    const std::string dir_b  = topdir + "/dir_b";
    const std::string file_a = topdir + "/file_a";
    const std::string subdir_a  = dir_a + "/subdir_a";


    int ret;
    DIR * dirstream = NULL;
    struct stat dirstat;

    // Open nonexistsing directory
    dirstream = opendir(topdir.c_str());
    if(dirstream != NULL || errno != ENOENT){
        std::cerr << "ERROR: succeeded on opening nonexisting dir: " << std::strerror(errno) << std::endl;
        return -1;
    }

    // Stat nonexisting directory
    ret = stat(topdir.c_str(), &dirstat);
    if(ret == 0 || errno != ENOENT){
        std::cerr << "Error stating nonexisitng directory: " << std::strerror(errno) << std::endl;
        return -1;
    }

    // Close nonexisting directory
    ret = closedir(NULL);
    if(ret != -1 || errno != EBADF){
        std::cerr << "Error closing nonexisting directory: " << std::strerror(errno) << std::endl;
        return -1;
    }
    
    //Create topdir
    ret = mkdir(topdir.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
    if(ret != 0){
        std::cerr << "Error creating topdir: " << std::strerror(errno) << std::endl;
        return -1;
    }

    //Test stat on existing dir
    ret = stat(topdir.c_str(), &dirstat);
    if(ret != 0){
        std::cerr << "Error stating topdir: " << std::strerror(errno) << std::endl;
        return -1;
    }
    assert(S_ISDIR(dirstat.st_mode));


    /* Read top directory that is empty */
    //opening top directory
    dirstream = opendir(topdir.c_str());
    if(dirstream == NULL){
        std::cerr << "Error opening topdir: " << std::strerror(errno) << std::endl;
        return -1;
    }

    // Read empty directory
    errno = 0;
    struct dirent * d = readdir(dirstream);
    if(d == NULL && errno != 0){
        std::cerr << "Error reading topdir: " << std::strerror(errno) << std::endl;
        return -1;
    }
    if(closedir(dirstream) != 0){
        std::cerr << "Error closing topdir" << std::strerror(errno) << std::endl;
        return -1;
    }


    /* Populate top directory */

    std::unordered_map<std::string, bool> expected_dirents = {
        {"dir_a", true},
        {"dir_b", true},
        {"file_a", false}
    };

    for(auto f: expected_dirents){
        auto complete_name = topdir + "/" + f.first;
        if(f.second){
            // directory
            ret = mkdir(complete_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
            assert(ret == 0);
        }else{
            // regular file
            ret = creat(complete_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
            assert(ret != -1);
            ret = close(ret);
            assert(ret == 0);
        }
    }

    //create directory with the same prefix of topdir but with longer name
    ret = mkdir(longer.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
    assert(ret == 0);
    //create sub directory at level 2 that must not be included in readdir
    ret = mkdir(subdir_a.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
    assert(ret == 0);


    /* Read top directory that has been populated */

    //opening top directory
    dirstream = opendir(topdir.c_str());
    if(dirstream == NULL){
        std::cerr << "Error opening topdir: " << std::strerror(errno) << std::endl;
        return -1;
    }

    std::unordered_map<std::string, bool> found_dirents;

    while((d = readdir(dirstream)) != NULL){
        found_dirents.insert(std::make_pair(d->d_name, (d->d_type == DT_DIR)));
    }
    assert(found_dirents  == expected_dirents);

    if(closedir(dirstream) != 0){
        std::cerr << "Error closing topdir" << std::strerror(errno) << std::endl;
        return -1;
    }
}