Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

88% test coverage #1

Merged
merged 1 commit into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion binfmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ struct FileUtils {
static FILE* OpenBinaryFile(const std::string& FilePath, bool Create = false, uint32_t offset = 0) {
FILE* r = nullptr;
auto filePath = Fs::path(FilePath);
if(!Fs::exists(filePath.parent_path())) {
if(!Fs::exists(filePath.parent_path()) && Create) {
Fs::create_directories(filePath.parent_path());
}
if (Fs::exists(FilePath) || Create) {
Expand Down
49 changes: 49 additions & 0 deletions tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#define TEST_DIRECTORY "/tmp/xxxxxxxxxx__xxxxxxxxxxx__xx"
#define TEST_BINARY_FILE "/tmp/xxxxxxx__xxxxxxxxx.bin"
#define TEST_BINARY_FILE_IN_NON_EXISTENT_DIRECTORY "/tmp/xxxxxxxxxxxxxxxxxxx/xxxxxxx.bin"
#define TEST_MAX_ENTRIES 1000


Expand Down Expand Up @@ -143,6 +144,43 @@ TEST(FileUtils, testWriteHeader) {
cleanup(TEST_BINARY_FILE);
}

TEST(FileUtils, testWriteNotExistentFile) {
EXPECT_FALSE(Fs::exists(TEST_BINARY_FILE));
TestBinaryEntryContainer container;
auto r = FileUtils::WriteData<TestBinaryFileHeader, TestBinaryEntryContainer>(TEST_BINARY_FILE, container);
EXPECT_FALSE(r);
}

TEST(FileUtils, testOpenInNonExistentDirectoryNoCreate) {
EXPECT_FALSE(Fs::exists(TEST_BINARY_FILE));
auto r = FileUtils::OpenBinaryFile(TEST_BINARY_FILE_IN_NON_EXISTENT_DIRECTORY, false);
EXPECT_EQ(r, nullptr);
}

TEST(FileUtils, testOpenInNonExistentDirectoryCreate) {
EXPECT_FALSE(Fs::exists(TEST_BINARY_FILE));
auto r = FileUtils::OpenBinaryFile(TEST_BINARY_FILE_IN_NON_EXISTENT_DIRECTORY, true);
EXPECT_NE(r, nullptr);
auto c = FileUtils::CloseBinaryFile(r);
EXPECT_TRUE(c);
c = FileUtils::DeleteDirectory(TEST_BINARY_FILE_IN_NON_EXISTENT_DIRECTORY);
EXPECT_TRUE(c);
}

TEST(FileUtils, testDeleteDirectory) {
auto r = FileUtils::DeleteDirectory("/tmp/ThisDirectoryProbablyDoesNotExistAndIHopeItDoesNot");
EXPECT_TRUE(r);
}

TEST(FileUtils, testExistentDirectory) {
auto r = FileUtils::CreateDirectory("/tmp/");
EXPECT_TRUE(r);
}

TEST(FileUtils, testDeleteNotExistentFile) {
auto r = FileUtils::DeleteFile("/tmp/ThisFileProbablyDoesNotExistAndIHopeItDoesNot");
EXPECT_FALSE(r);
}

// Tests
// - WriteHeader
Expand Down Expand Up @@ -290,3 +328,14 @@ TEST(BinaryFile, testRemoveEntryAt) {
cleanupTestFile(t);
}

TEST(TimeUtils, GetSecondsSinceEpoch) {
EXPECT_GT(TimeUtils::GetSecondsSinceEpoch(), 0);
}

TEST(BinaryFile, testNoHeader) {
static std::string filePath("/tmp/binfmt_no_hdr_test.bin");
static std::string touchCmd("touch ");
static std::string cmd(touchCmd + filePath);
system(cmd.c_str());
// BinaryFile<TestBinaryFileHeader, TestBinaryEntry, 50> f(filePath);
}