Files
PowerShell/src/libpsl-native/test/test-issymlink.cpp
T

89 lines
2.2 KiB
C++
Raw Normal View History

2015-10-30 11:47:44 -07:00
//! @file test-issymlink.cpp
//! @author George Fleming <v-geflem@microsoft.com>
//! @brief Implements test for isSymLink()
#include <gtest/gtest.h>
#include <errno.h>
#include <unistd.h>
#include "issymlink.h"
using namespace std;
class isSymLinkTest : public ::testing::Test
{
protected:
static const int bufSize = 64;
2015-11-02 16:40:28 -08:00
const string fileTemplate = "/tmp/symlinktest.fXXXXXX";
const string dirTemplate = "/tmp/symlinktest.dXXXXXX";
2015-10-30 11:47:44 -07:00
const string fileSymLink = "/tmp/symlinktest.flink";
const string dirSymLink = "/tmp/symlinktest.dlink";
char *file, *dir;
char fileTemplateBuf[bufSize], dirTemplateBuf[bufSize];
isSymLinkTest()
{
// since mkstemp and mkdtemp modifies the template string, let's give them writable buffers
strcpy(fileTemplateBuf, fileTemplate.c_str());
strcpy(dirTemplateBuf, dirTemplate.c_str());
// First create a file
int fd = mkstemp(fileTemplateBuf);
EXPECT_TRUE(fd != -1);
2016-07-13 21:39:34 -07:00
file = fileTemplateBuf;
2015-10-30 11:47:44 -07:00
2016-07-13 21:39:34 -07:00
// Create a temp directory
dir = mkdtemp(dirTemplateBuf);
2015-10-30 11:47:44 -07:00
EXPECT_TRUE(dir != NULL);
// Create symbolic link to file
2016-07-13 21:39:34 -07:00
EXPECT_FALSE(symlink(file, fileSymLink.c_str()));
2015-10-30 11:47:44 -07:00
// Create symbolic link to directory
2016-07-13 21:39:34 -07:00
EXPECT_FALSE(symlink(dir, dirSymLink.c_str()));
2015-10-30 11:47:44 -07:00
}
~isSymLinkTest()
{
2016-07-13 21:39:34 -07:00
EXPECT_FALSE(unlink(fileSymLink.c_str()));
2015-10-30 11:47:44 -07:00
2016-07-13 21:39:34 -07:00
EXPECT_FALSE(unlink(dirSymLink.c_str()));
2015-10-30 11:47:44 -07:00
2016-07-13 21:39:34 -07:00
EXPECT_FALSE(unlink(file));
2015-10-30 11:47:44 -07:00
2016-07-13 21:39:34 -07:00
EXPECT_FALSE(rmdir(dir));
2015-10-30 11:47:44 -07:00
}
};
TEST_F(isSymLinkTest, FilePathNameIsNull)
{
2016-07-13 21:39:34 -07:00
EXPECT_FALSE(IsSymLink(NULL));
2015-10-30 11:47:44 -07:00
EXPECT_EQ(ERROR_INVALID_PARAMETER, errno);
}
TEST_F(isSymLinkTest, FilePathNameDoesNotExist)
{
std::string invalidFile = "/tmp/symlinktest_invalidFile";
2016-07-13 21:39:34 -07:00
EXPECT_FALSE(IsSymLink(invalidFile.c_str()));
2015-10-30 11:47:44 -07:00
EXPECT_EQ(ERROR_FILE_NOT_FOUND, errno);
}
TEST_F(isSymLinkTest, NormalFileIsNotSymLink)
{
2016-07-13 21:39:34 -07:00
EXPECT_FALSE(IsSymLink(file));
2015-10-30 11:47:44 -07:00
}
TEST_F(isSymLinkTest, SymLinkToFile)
{
2016-07-13 21:39:34 -07:00
EXPECT_TRUE(IsSymLink(fileSymLink.c_str()));
2015-10-30 11:47:44 -07:00
}
TEST_F(isSymLinkTest, NormalDirectoryIsNotSymbLink)
{
2016-07-13 21:39:34 -07:00
EXPECT_FALSE(IsSymLink(dir));
2015-10-30 11:47:44 -07:00
}
TEST_F(isSymLinkTest, SymLinkToDirectory)
{
2016-07-13 21:39:34 -07:00
EXPECT_TRUE(IsSymLink(dirSymLink.c_str()));
2015-10-30 11:47:44 -07:00
}