forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-createsymlink.cpp
More file actions
121 lines (96 loc) · 3.24 KB
/
Copy pathtest-createsymlink.cpp
File metadata and controls
121 lines (96 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//! @file test-createsymlink.cpp
//! @author George Fleming <v-geflem@microsoft.com>
//! @brief Implements test for CreateSymLink() and FollowSymLink()
#include <gtest/gtest.h>
#include <errno.h>
#include <unistd.h>
#include "issymlink.h"
#include "createsymlink.h"
#include "followsymlink.h"
using namespace std;
class CreateSymLinkTest : public ::testing::Test
{
protected:
static const int bufSize = 64;
const string fileTemplate = "/tmp/symlinktest.fXXXXXX";
const string dirTemplate = "/tmp/symlinktest.dXXXXXX";
const string fileSymLink = "/tmp/symlinktest.flink";
const string dirSymLink = "/tmp/symlinktest.dlink";
char *file, *dir;
char fileTemplateBuf[bufSize], dirTemplateBuf[bufSize];
CreateSymLinkTest()
{
// 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 temp file
int fd = mkstemp(fileTemplateBuf);
EXPECT_TRUE(fd != -1);
file = fileTemplateBuf;
// Create a temp directory
dir = mkdtemp(dirTemplateBuf);
EXPECT_TRUE(dir != NULL);
// Create symbolic link to file
bool ret1 = CreateSymLink(fileSymLink.c_str(), file);
EXPECT_TRUE(ret1);
// Create symbolic link to directory
bool ret2 = CreateSymLink(dirSymLink.c_str(), dir);
EXPECT_TRUE(ret2);
}
~CreateSymLinkTest()
{
bool ret;
ret = unlink(fileSymLink.c_str());
EXPECT_FALSE(ret);
ret = unlink(dirSymLink.c_str());
EXPECT_FALSE(ret);
ret = unlink(file);
EXPECT_FALSE(ret);
ret = rmdir(dir);
EXPECT_FALSE(ret);
}
};
TEST_F(CreateSymLinkTest, FilePathNameIsNull)
{
bool retVal = CreateSymLink(NULL, NULL);
EXPECT_FALSE(retVal);
EXPECT_EQ(ERROR_INVALID_PARAMETER, errno);
}
TEST_F(CreateSymLinkTest, FilePathNameDoesNotExist)
{
std::string invalidFile = "/tmp/symlinktest_invalidFile";
std::string invalidLink = "/tmp/symlinktest_invalidLink";
// make sure neither exists
unlink(invalidFile.c_str());
unlink(invalidLink.c_str());
// Linux allows creation of symbolic link that points to an invalid file
bool retVal = CreateSymLink(invalidLink.c_str(), invalidFile.c_str());
EXPECT_TRUE(retVal);
std::string target = FollowSymLink(invalidLink.c_str());
EXPECT_EQ(target, invalidFile);
unlink(invalidLink.c_str());
}
TEST_F(CreateSymLinkTest, SymLinkToFile)
{
bool retVal = IsSymLink(fileSymLink.c_str());
EXPECT_TRUE(retVal);
std::string target = FollowSymLink(fileSymLink.c_str());
char buffer[PATH_MAX];
std::string expected = realpath(file, buffer);
EXPECT_EQ(target, expected);
}
TEST_F(CreateSymLinkTest, SymLinkToDirectory)
{
bool retVal = IsSymLink(dirSymLink.c_str());
EXPECT_TRUE(retVal);
std::string target = FollowSymLink(dirSymLink.c_str());
char buffer[PATH_MAX];
std::string expected = realpath(dir, buffer);
EXPECT_EQ(target, expected);
}
TEST_F(CreateSymLinkTest, SymLinkAgain)
{
bool retVal = CreateSymLink(fileSymLink.c_str(), file);
EXPECT_FALSE(retVal);
EXPECT_EQ(ERROR_FILE_EXISTS, errno);
}