//! @file test-getlinkcount.cpp //! @author George Fleming //! @brief Implements test for getLinkCount() #include #include #include #include #include #include #include #include "getlinkcount.h" class getLinkCountTest : public ::testing::Test { protected: std::string file; const std::string fileTemplate = "/tmp/createFile.XXXXXXX"; ulong count; getLinkCountTest() { file = fileTemplate; file = mktemp(const_cast(file.c_str())); std::ifstream fileCondition(file); EXPECT_EQ(0, fileCondition.good()); } void createFileForTesting(std::string theFile) { std::ofstream ofs; ofs.open(theFile, std::ofstream::out); ofs << "hi there, ms ostc!"; ofs.close(); } std::string createHardLink(std::string origFile) { std::string newFile = origFile + "_link"; int ret = link(const_cast(origFile.c_str()), const_cast(newFile.c_str())); EXPECT_EQ(0, ret); return newFile; } void removeFile(std::string fileName) { int ret = unlink(const_cast(fileName.c_str())); EXPECT_EQ(0, ret); } }; TEST_F(getLinkCountTest, FilePathNameIsNull) { bool retVal = GetLinkCount(NULL, &count ); ASSERT_FALSE(retVal); EXPECT_EQ(ERROR_INVALID_PARAMETER, errno); } TEST_F(getLinkCountTest, FilePathNameDoesNotExist) { std::string invalidFile = "/tmp/createFile"; bool retVal = GetLinkCount(const_cast(invalidFile.c_str()), &count); ASSERT_FALSE(retVal); EXPECT_EQ(ERROR_FILE_NOT_FOUND, errno); } TEST_F(getLinkCountTest, LinkCountOfSinglyLinkedFile) { createFileForTesting(file); bool retVal = GetLinkCount(const_cast(file.c_str()), &count); ASSERT_TRUE(retVal); EXPECT_EQ(1, count); removeFile(file); } TEST_F(getLinkCountTest, LinkCountOfMultipliLinkedFile) { createFileForTesting(file); std::string newFile = createHardLink(file); bool retVal = GetLinkCount(const_cast(file.c_str()), &count); ASSERT_TRUE(retVal); EXPECT_EQ(2, count); removeFile(file); removeFile(newFile); }