Add tests for kiwix::fileExists and kiwix::fileReadable

This commit is contained in:
Nikhil Tanwar 2022-01-06 18:54:58 +05:30
parent 854058f842
commit 3dbcbe542b
3 changed files with 35 additions and 5 deletions

View File

@ -188,9 +188,9 @@ std::string getFileContent(const std::string& path);
bool fileExists(const std::string& path); bool fileExists(const std::string& path);
/** checks if file is readable. /** checks if file is readable.
* *
* This function returns boolean stating if file is readable. * This function returns boolean stating if file is readable.
* *
* @param path The absolute path provided in string format. * @param path The absolute path provided in string format.
* @return Boolean representing if file is readale or not. * @return Boolean representing if file is readale or not.
*/ */
@ -206,7 +206,7 @@ bool fileReadable(const std::string& path);
std::string getMimeTypeForFile(const std::string& filename); std::string getMimeTypeForFile(const std::string& filename);
/** Provides all available network interfaces /** Provides all available network interfaces
* *
* This function provides the available IPv4 network interfaces * This function provides the available IPv4 network interfaces
*/ */
std::map<std::string, std::string> getNetworkInterfaces(); std::map<std::string, std::string> getNetworkInterfaces();

View File

@ -306,8 +306,8 @@ bool kiwix::fileExists(const std::string& path)
{ {
#ifdef _WIN32 #ifdef _WIN32
return (_waccess_s(Utf8ToWide(path).c_str(), 0) == 0); return (_waccess_s(Utf8ToWide(path).c_str(), 0) == 0);
#else #else
return (access(path.c_str(), F_OK) == 0); return (access(path.c_str(), F_OK) == 0);
#endif #endif
} }

View File

@ -20,6 +20,8 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <string> #include <string>
#include <vector> #include <vector>
#include <unistd.h>
#include <fcntl.h>
#include "../include/tools.h" #include "../include/tools.h"
#include "../src/tools/pathTools.h" #include "../src/tools/pathTools.h"
@ -190,6 +192,34 @@ TEST(pathTools, appendToDirectory)
P5("a","b","c",".","foo.xml")); P5("a","b","c",".","foo.xml"));
} }
TEST(pathTools, fileExists)
{
ASSERT_TRUE(kiwix::fileExists(P3(".","test","example.zim")));
ASSERT_FALSE(kiwix::fileExists(P3(".","test","noFile.zim")));
}
TEST(pathTools, fileReadable)
{
ASSERT_TRUE(kiwix::fileReadable(P3(".","test","example.zim")));
ASSERT_FALSE(kiwix::fileReadable(P3(".","test","noFile.zim")));
#ifdef _POSIX_SOURCE
std::string path = P3(".","test","example.zim");
auto myprivs = geteuid();
if (myprivs != 0) {
int fd = open(path.c_str(), O_RDONLY);
if (fd != -1) {
int wResp = fchmod(fd, ~(S_IRWXU | S_IRWXG | S_IRWXO)); // remove all permissions
if (wResp == 0) {
EXPECT_FALSE(kiwix::fileReadable(P3(".","test","example.zim")));
}
int resetResp = fchmod(fd, S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP); // reset back permissions to -rw-rw-r--
if (resetResp == 0) {
EXPECT_TRUE(kiwix::fileReadable(P3(".","test","example.zim")));
}
}
}
#endif
}
TEST(pathTools, goUp) TEST(pathTools, goUp)
{ {