Skip to content

Commit

Permalink
Merge pull request #1 from nbdy/v2
Browse files Browse the repository at this point in the history
88% test coverage
  • Loading branch information
nbdy authored Sep 8, 2021
2 parents 898d631 + 8a3ff89 commit d8da782
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
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);
}

0 comments on commit d8da782

Please sign in to comment.