2014-12-02 22:16:48 +01:00
|
|
|
#include "FuseReadDirTest.h"
|
|
|
|
|
2015-06-18 19:30:52 +02:00
|
|
|
using cpputils::unique_ref;
|
|
|
|
using cpputils::make_unique_ref;
|
2014-12-02 22:16:48 +01:00
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
using ::testing::Action;
|
|
|
|
using ::testing::Return;
|
|
|
|
|
2015-06-18 19:30:52 +02:00
|
|
|
unique_ref<vector<string>> FuseReadDirTest::ReadDir(const char *dirname) {
|
2014-12-02 22:16:48 +01:00
|
|
|
auto fs = TestFS();
|
|
|
|
|
|
|
|
DIR *dir = openDir(fs.get(), dirname);
|
|
|
|
|
2015-06-18 19:30:52 +02:00
|
|
|
auto result = make_unique_ref<vector<string>>();
|
2014-12-02 22:16:48 +01:00
|
|
|
readDirEntries(dir, result.get());
|
|
|
|
closeDir(dir);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FuseReadDirTest::ReadDirReturnError(const char *dirname) {
|
|
|
|
auto fs = TestFS();
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
DIR *dir = openDirAllowError(fs.get(), dirname);
|
|
|
|
EXPECT_EQ(errno!=0, dir==nullptr) << "errno should exactly be != 0 if opendir returned nullptr";
|
|
|
|
if (errno != 0) {
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
2015-06-18 19:30:52 +02:00
|
|
|
auto result = make_unique_ref<vector<string>>();
|
2014-12-02 22:16:48 +01:00
|
|
|
int error = readDirEntriesAllowError(dir, result.get());
|
|
|
|
closeDir(dir);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
DIR *FuseReadDirTest::openDir(TempTestFS *fs, const char *dirname) {
|
|
|
|
DIR *dir = openDirAllowError(fs, dirname);
|
|
|
|
EXPECT_NE(nullptr, dir) << "Opening directory failed";
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
DIR *FuseReadDirTest::openDirAllowError(TempTestFS *fs, const char *dirname) {
|
|
|
|
auto realpath = fs->mountDir() / dirname;
|
2018-05-21 01:20:38 +02:00
|
|
|
return ::opendir(realpath.string().c_str());
|
2014-12-02 22:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FuseReadDirTest::readDirEntries(DIR *dir, vector<string> *result) {
|
|
|
|
int error = readDirEntriesAllowError(dir, result);
|
|
|
|
EXPECT_EQ(0, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
int FuseReadDirTest::readDirEntriesAllowError(DIR *dir, vector<string> *result) {
|
|
|
|
struct dirent *entry = nullptr;
|
|
|
|
int error = readNextDirEntryAllowError(dir, &entry);
|
|
|
|
if (error != 0) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
while(entry != nullptr) {
|
|
|
|
result->push_back(entry->d_name);
|
|
|
|
int error = readNextDirEntryAllowError(dir, &entry);
|
|
|
|
if (error != 0) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FuseReadDirTest::readNextDirEntryAllowError(DIR *dir, struct dirent **result) {
|
|
|
|
errno = 0;
|
|
|
|
*result = ::readdir(dir);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FuseReadDirTest::closeDir(DIR *dir) {
|
|
|
|
int retval = ::closedir(dir);
|
|
|
|
EXPECT_EQ(0, retval) << "Closing dir failed";
|
|
|
|
}
|
|
|
|
|
2015-03-10 21:51:12 +01:00
|
|
|
Action<vector<fspp::Dir::Entry>*(const char*)> FuseReadDirTest::ReturnDirEntries(vector<std::string> entries) {
|
|
|
|
vector<fspp::Dir::Entry> *direntries = new vector<fspp::Dir::Entry>(entries.size(), fspp::Dir::Entry(fspp::Dir::EntryType::FILE, ""));
|
|
|
|
for(unsigned int i = 0; i < entries.size(); ++i) {
|
|
|
|
(*direntries)[i].name = entries[i];
|
|
|
|
}
|
2014-12-02 22:16:48 +01:00
|
|
|
return Return(direntries);
|
|
|
|
}
|