Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
vvromanov committed Dec 5, 2024
1 parent d335178 commit d11ca3a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
32 changes: 9 additions & 23 deletions src/file_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,15 @@ bool get_file_size(const char *name, size_t &size) {


bool remove_test_file(const char *path) {
if (!is_file_exists(path)) {
return true;
struct stat structstat;
if (stat(path, &structstat) == -1) {
if (errno == ENOENT) {
return true;
}
return false;
}
if (!S_ISREG(structstat.st_mode)) {
return false;
}
if (0 == remove(path)) {
return true;
Expand All @@ -56,27 +63,6 @@ bool remove_test_file(const char *path) {
return false;
}

bool touch(const std::string &pathname) {
int fd = open(pathname.c_str(),
O_WRONLY | O_CREAT | O_NOCTTY | O_NONBLOCK,
0666);
if (fd < 0) // Couldn't open that path.
{
log_write(LOG_LEVEL_ERR_ERRNO, "%s: Couldn't open() path %s", __PRETTY_FUNCTION__, pathname.c_str());
return false;
}
close(fd);
int rc = utimensat(AT_FDCWD,
pathname.c_str(),
nullptr,
0);
if (rc) {
log_write(LOG_LEVEL_ERR_ERRNO, "%s: Couldn't utimensat() path %s", __PRETTY_FUNCTION__, pathname.c_str());
return false;
}
return true;
}

bool mkdir_for_file(const char *filename, __mode_t mode) {
char tmp[PATH_MAX];
char *p = NULL;
Expand Down
1 change: 0 additions & 1 deletion src/file_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ bool is_file_exists(const char *name);
bool is_dir_exists(const char* name);
bool remove_test_file(const char* path);
bool get_file_size(const char *name, size_t &size);
bool touch(const std::string& pathname);
bool mkdir_for_file(const char* filename, __mode_t mode);
const char* get_ext(const char* path);
43 changes: 39 additions & 4 deletions tests/FileUtils_Test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
#include "file_utils.h"
#include "gtest/gtest.h"
#include <ftw.h>

#define TEST_FILE "/tmp/file_utils.tst"
#define TEST_FILE_INVALID "/root/*"
#define TEST_DIR "/tmp/level1/level2/level3"


TEST(FileUtils, RemoveTestFile) {
FILE* fp = fopen(TEST_FILE, "ab+");
fclose(fp);
EXPECT_TRUE(remove_test_file(TEST_FILE));
EXPECT_TRUE(remove_test_file(TEST_FILE));
EXPECT_FALSE(remove_test_file(TEST_FILE_INVALID));
}

TEST(FileUtils, FileExists) {
FILE *fp = fopen(TEST_FILE, "ab+");
Expand Down Expand Up @@ -36,7 +47,7 @@ TEST(FileUtils, FileSize) {
}

TEST(FileUtils, FileSizeNotExists) {
remove(TEST_FILE);
EXPECT_TRUE(remove_test_file(TEST_FILE));
size_t size = 123;
EXPECT_FALSE(get_file_size(TEST_FILE, size));
EXPECT_EQ(0, size);
Expand All @@ -62,20 +73,44 @@ TEST(FileUtils, DirExists) {
}

TEST(FileUtils, DirNotExists) {
remove(TEST_FILE);
EXPECT_TRUE(remove_test_file(TEST_FILE));
EXPECT_FALSE(is_dir_exists(TEST_FILE));
}


TEST(FileUtils, DirNotExistsInvalid) {
remove(TEST_FILE);
EXPECT_TRUE(remove_test_file(TEST_FILE));
EXPECT_FALSE(is_dir_exists(""));
EXPECT_FALSE(is_dir_exists(TEST_FILE_INVALID));
EXPECT_FALSE(is_dir_exists("../../../../../../../../../some_file.txt"));
}


static int unlink_cb(const char* fpath, const struct stat* sb, int typeflag, struct FTW* ftwbuf)
{
int rv = remove(fpath);

if (rv)
perror(fpath);

return rv;
}

static bool rmrf(const char* path)
{
return 0 == nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
}

TEST(FileUtils, mkdir_for_file) {
rmrf(TEST_DIR);
EXPECT_FALSE(is_dir_exists(TEST_DIR));
EXPECT_TRUE(mkdir_for_file(TEST_DIR "/file.txt", 0755));
EXPECT_TRUE(is_dir_exists(TEST_DIR));
EXPECT_TRUE(rmrf(TEST_DIR));
}

TEST(FileUtils, GetExt) {
remove(TEST_FILE);
EXPECT_TRUE(remove_test_file(TEST_FILE));
EXPECT_STREQ(nullptr, get_ext(NULL));
EXPECT_STREQ("", get_ext(""));
EXPECT_STREQ("", get_ext("filename"));
Expand Down

0 comments on commit d11ca3a

Please sign in to comment.