From 3dbcbe542b2f68d2bc6c2a99a6f4bf188df0e870 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Thu, 6 Jan 2022 18:54:58 +0530 Subject: [PATCH] Add tests for kiwix::fileExists and kiwix::fileReadable --- include/tools.h | 6 +++--- src/tools/pathTools.cpp | 4 ++-- test/pathTools.cpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/include/tools.h b/include/tools.h index b7ffb576d..75bac9c25 100644 --- a/include/tools.h +++ b/include/tools.h @@ -188,9 +188,9 @@ std::string getFileContent(const std::string& path); bool fileExists(const std::string& path); /** checks if file is readable. - * + * * This function returns boolean stating if file is readable. - * + * * @param path The absolute path provided in string format. * @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); /** Provides all available network interfaces - * + * * This function provides the available IPv4 network interfaces */ std::map getNetworkInterfaces(); diff --git a/src/tools/pathTools.cpp b/src/tools/pathTools.cpp index 7680da3eb..31862fef4 100644 --- a/src/tools/pathTools.cpp +++ b/src/tools/pathTools.cpp @@ -306,8 +306,8 @@ bool kiwix::fileExists(const std::string& path) { #ifdef _WIN32 return (_waccess_s(Utf8ToWide(path).c_str(), 0) == 0); -#else - return (access(path.c_str(), F_OK) == 0); +#else + return (access(path.c_str(), F_OK) == 0); #endif } diff --git a/test/pathTools.cpp b/test/pathTools.cpp index 9047e1873..1f8e1e2b6 100644 --- a/test/pathTools.cpp +++ b/test/pathTools.cpp @@ -20,6 +20,8 @@ #include "gtest/gtest.h" #include #include +#include +#include #include "../include/tools.h" #include "../src/tools/pathTools.h" @@ -190,6 +192,34 @@ TEST(pathTools, appendToDirectory) 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) {