forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-isexecutable.cpp
More file actions
84 lines (66 loc) · 2.02 KB
/
Copy pathtest-isexecutable.cpp
File metadata and controls
84 lines (66 loc) · 2.02 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
//! @file test-isexecutable.cpp
//! @author George Fleming <v-geflem@microsoft.com>
//! @brief Implements test for isexecutable()
#include <gtest/gtest.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include "isexecutable.h"
using namespace std;
class IsExecutableTest : public ::testing::Test
{
protected:
static const int bufSize = 64;
const string fileTemplate = "/tmp/isexecutabletest.fXXXXXXX";
const mode_t mode_700 = S_IRUSR | S_IWUSR | S_IXUSR;
const mode_t mode_070 = S_IRGRP | S_IWGRP | S_IXGRP;
const mode_t mode_007 = S_IROTH | S_IWOTH | S_IXOTH;
const mode_t mode_777 = mode_700 | mode_070 | mode_007;
const mode_t mode_444 = S_IRUSR | S_IRGRP | S_IROTH;
char *file;
char fileTemplateBuf[bufSize];
IsExecutableTest()
{
// since mkstemp modifies the template string, let's give it writable buffers
strcpy(fileTemplateBuf, fileTemplate.c_str());
// First create a file
int fd = mkstemp(fileTemplateBuf);
EXPECT_TRUE(fd != -1);
file = fileTemplateBuf;
}
~IsExecutableTest()
{
EXPECT_FALSE(unlink(file));
}
void ChangeFilePermission(const char* file, mode_t mode)
{
EXPECT_FALSE(chmod(file, mode));
}
};
TEST_F(IsExecutableTest, FilePathNameIsNull)
{
EXPECT_FALSE(IsExecutable(NULL));
EXPECT_EQ(ERROR_INVALID_PARAMETER, errno);
}
TEST_F(IsExecutableTest, FilePathNameDoesNotExist)
{
std::string invalidFile = "/tmp/isexecutabletest_invalidFile";
EXPECT_FALSE(IsExecutable(invalidFile.c_str()));
EXPECT_EQ(ERROR_FILE_NOT_FOUND, errno);
}
TEST_F(IsExecutableTest, NormalFileIsNotIsexecutable)
{
EXPECT_FALSE(IsExecutable(file));
ChangeFilePermission(file, mode_444);
EXPECT_FALSE(IsExecutable(file));
}
TEST_F(IsExecutableTest, FilePermission_700)
{
ChangeFilePermission(file, mode_700);
EXPECT_TRUE(IsExecutable(file));
}
TEST_F(IsExecutableTest, FilePermission_777)
{
ChangeFilePermission(file, mode_777);
EXPECT_TRUE(IsExecutable(file));
}