mirror of
https://gitee.com/openharmony/security_selinux
synced 2024-11-23 05:19:57 +00:00
fix long func
Signed-off-by: steven_q <qichanggui@huawei.com> Change-Id: Ia63cd0dd32fe7d4228fa050517a129c4b4b2d326
This commit is contained in:
parent
1ffb17f86e
commit
d3bb82769f
@ -202,6 +202,34 @@ static void SetOptions(int argc, char *argv[], const option *options, testInput
|
||||
}
|
||||
}
|
||||
|
||||
static void TestWriteParameters(testInput &testCmd)
|
||||
{
|
||||
int fd[2];
|
||||
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fd) < 0) {
|
||||
perror("socketpair");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
SrcInfo info;
|
||||
info.uc.pid = getpid();
|
||||
info.uc.uid = getuid();
|
||||
info.uc.gid = getgid();
|
||||
info.sockFd = fd[0];
|
||||
if (!testCmd.paraName.empty()) {
|
||||
TestSetPara(testCmd.paraName, &info);
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
exit(0);
|
||||
}
|
||||
std::string paraName;
|
||||
while (std::cin >> paraName) {
|
||||
TestSetPara(paraName, &info);
|
||||
}
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
static void Test(testInput &testCmd)
|
||||
{
|
||||
std::string paraName;
|
||||
@ -227,29 +255,7 @@ static void Test(testInput &testCmd)
|
||||
exit(0);
|
||||
}
|
||||
case 'w': {
|
||||
int fd[2];
|
||||
if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fd) < 0) {
|
||||
perror("socketpair");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
SrcInfo info;
|
||||
info.uc.pid = getpid();
|
||||
info.uc.uid = getuid();
|
||||
info.uc.gid = getgid();
|
||||
info.sockFd = fd[0];
|
||||
if (!testCmd.paraName.empty()) {
|
||||
TestSetPara(testCmd.paraName, &info);
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
exit(0);
|
||||
}
|
||||
while (std::cin >> paraName) {
|
||||
TestSetPara(paraName, &info);
|
||||
}
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
exit(0);
|
||||
TestWriteParameters(testCmd);
|
||||
}
|
||||
default:
|
||||
PrintUsage();
|
||||
|
@ -168,6 +168,17 @@ def check_common_contexts(args, contexts_file):
|
||||
os.unlink(contexts_file + ".bin")
|
||||
|
||||
|
||||
def echo_error():
|
||||
print("***********************************************************")
|
||||
print("please check whether the format meets the following rules:")
|
||||
print("[required format]: apl=* name=* domain=* type=*")
|
||||
print("apl=*, apl should be one of system_core|system_basic|normal")
|
||||
print("name=*, name is 'optional'")
|
||||
print("domain=*, hapdomain selinux type")
|
||||
print("type=*, hapdatafile selinux type")
|
||||
print("***********************************************************")
|
||||
|
||||
|
||||
def check_sehap_contexts(args, contexts_file, domain):
|
||||
"""
|
||||
check domain or type defined in sehap_contexts.
|
||||
@ -200,18 +211,9 @@ def check_sehap_contexts(args, contexts_file, domain):
|
||||
print(contexts_file + ":" +
|
||||
str(line_index) + " format check fail")
|
||||
err = 1
|
||||
contexts_read.close()
|
||||
contexts_write.close()
|
||||
if err:
|
||||
shutil.move(contexts_file + "_bk", contexts_file)
|
||||
print("***********************************************************")
|
||||
print("please check whether the format meets the following rules:")
|
||||
print("[required format]: apl=* name=* domain=* type=*")
|
||||
print("apl=*, apl should be one of system_core|system_basic|normal")
|
||||
print("name=*, name is 'optional'")
|
||||
print("domain=*, hapdomain selinux type")
|
||||
print("type=*, hapdatafile selinux type")
|
||||
print("***********************************************************")
|
||||
echo_error()
|
||||
raise Exception(err)
|
||||
check_cmd = [os.path.join(args.tool_path, "sefcontext_compile"),
|
||||
"-o", contexts_file + ".bin",
|
||||
|
@ -384,6 +384,16 @@ HWTEST_F(SelinuxUnitTest, HapFileRestorecon008, TestSize.Level1)
|
||||
ASSERT_EQ(true, RemoveDirectory(TEST_HAP_PATH));
|
||||
}
|
||||
|
||||
static bool CompareContexts(const std::string &path, const std::string &label)
|
||||
{
|
||||
char *secontext = nullptr;
|
||||
getfilecon(path.c_str(), &secontext);
|
||||
bool res = (strcmp(label.c_str(), secontext) == 0);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: HapFileRestorecon009
|
||||
* @tc.desc: test HapFileRestorecon input multi path/file no recurse.
|
||||
@ -401,45 +411,24 @@ HWTEST_F(SelinuxUnitTest, HapFileRestorecon009, TestSize.Level1)
|
||||
char *secontextOld = nullptr;
|
||||
getfilecon(TEST_SUB_PATH_2_FILE_1.c_str(), &secontextOld);
|
||||
|
||||
std::vector<std::string> tmp;
|
||||
tmp.emplace_back(TEST_SUB_PATH_1);
|
||||
tmp.emplace_back(TEST_SUB_PATH_2);
|
||||
tmp.emplace_back(TEST_SUB_PATH_1_FILE_1);
|
||||
tmp.emplace_back(TEST_SUB_PATH_1_FILE_2);
|
||||
tmp.emplace_back(TEST_UNSIMPLIFY_FILE);
|
||||
tmp.emplace_back(TEST_UNSIMPLIFY_PATH);
|
||||
|
||||
HapFileInfo hapFileInfo = {
|
||||
.pathNameOrig = tmp,
|
||||
.pathNameOrig = {TEST_SUB_PATH_1, TEST_SUB_PATH_2, TEST_SUB_PATH_1_FILE_1, TEST_SUB_PATH_1_FILE_2,
|
||||
TEST_UNSIMPLIFY_FILE, TEST_UNSIMPLIFY_PATH},
|
||||
.apl = SYSTEM_CORE_APL,
|
||||
.packageName = TEST_HAP_BUNDLE_NAME,
|
||||
.flags = 0,
|
||||
.hapFlags = 1,
|
||||
};
|
||||
int ret = test.HapFileRestorecon(hapFileInfo);
|
||||
ASSERT_EQ(SELINUX_SUCC, ret);
|
||||
|
||||
ASSERT_EQ(SELINUX_SUCC, test.HapFileRestorecon(hapFileInfo));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_1, TEST_HAP_DATA_FILE_LABEL));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_2, TEST_HAP_DATA_FILE_LABEL));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_1_FILE_1, TEST_HAP_DATA_FILE_LABEL));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_1_FILE_2, TEST_HAP_DATA_FILE_LABEL));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_3_FILE_1, TEST_HAP_DATA_FILE_LABEL));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_4, TEST_HAP_DATA_FILE_LABEL));
|
||||
|
||||
char *secontext = nullptr;
|
||||
getfilecon(TEST_SUB_PATH_1.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_2.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_1_FILE_1.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_1_FILE_2.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_2_FILE_1.c_str(), &secontext); // this file should not be restorecon
|
||||
EXPECT_STREQ(secontextOld, secontext);
|
||||
freecon(secontext);
|
||||
@ -447,16 +436,6 @@ HWTEST_F(SelinuxUnitTest, HapFileRestorecon009, TestSize.Level1)
|
||||
secontext = nullptr;
|
||||
secontextOld = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_3_FILE_1.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_4.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
ASSERT_EQ(true, RemoveDirectory(TEST_HAP_PATH));
|
||||
}
|
||||
|
||||
@ -477,76 +456,29 @@ HWTEST_F(SelinuxUnitTest, HapFileRestorecon010, TestSize.Level1)
|
||||
ASSERT_EQ(true, CreateFile(TEST_SUB_PATH_4_FILE_1));
|
||||
ASSERT_EQ(true, CreateFile(TEST_SUB_PATH_4_FILE_2));
|
||||
|
||||
std::vector<std::string> tmp;
|
||||
tmp.emplace_back(TEST_SUB_PATH_1);
|
||||
tmp.emplace_back(TEST_SUB_PATH_2);
|
||||
tmp.emplace_back(TEST_UNSIMPLIFY_FILE); // TEST_SUB_PATH_3_FILE_1
|
||||
tmp.emplace_back(TEST_UNSIMPLIFY_PATH); // TEST_SUB_PATH_4
|
||||
|
||||
char *secontextOld = nullptr;
|
||||
getfilecon(TEST_SUB_PATH_3_FILE_2.c_str(), &secontextOld);
|
||||
|
||||
HapFileInfo hapFileInfo = {
|
||||
.pathNameOrig = tmp,
|
||||
.pathNameOrig = { TEST_SUB_PATH_1, TEST_SUB_PATH_2, TEST_UNSIMPLIFY_FILE, TEST_UNSIMPLIFY_PATH },
|
||||
.apl = SYSTEM_CORE_APL,
|
||||
.packageName = TEST_HAP_BUNDLE_NAME,
|
||||
.flags = 1,
|
||||
.hapFlags = 1,
|
||||
};
|
||||
int ret = test.HapFileRestorecon(hapFileInfo);
|
||||
ASSERT_EQ(SELINUX_SUCC, ret);
|
||||
ASSERT_EQ(SELINUX_SUCC, test.HapFileRestorecon(hapFileInfo));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_1, TEST_HAP_DATA_FILE_LABEL));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_2, TEST_HAP_DATA_FILE_LABEL));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_4, TEST_HAP_DATA_FILE_LABEL));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_1_FILE_1, TEST_HAP_DATA_FILE_LABEL));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_1_FILE_2, TEST_HAP_DATA_FILE_LABEL));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_2_FILE_1, TEST_HAP_DATA_FILE_LABEL));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_2_FILE_2, TEST_HAP_DATA_FILE_LABEL));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_4_FILE_1, TEST_HAP_DATA_FILE_LABEL));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_4_FILE_2, TEST_HAP_DATA_FILE_LABEL));
|
||||
EXPECT_TRUE(CompareContexts(TEST_SUB_PATH_3_FILE_1, TEST_HAP_DATA_FILE_LABEL));
|
||||
|
||||
char *secontext = nullptr;
|
||||
getfilecon(TEST_SUB_PATH_1.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_2.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_4.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_1_FILE_1.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_1_FILE_2.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_2_FILE_1.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_2_FILE_2.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_4_FILE_1.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_4_FILE_2.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_3_FILE_1.c_str(), &secontext);
|
||||
EXPECT_STREQ(TEST_HAP_DATA_FILE_LABEL.c_str(), secontext);
|
||||
freecon(secontext);
|
||||
secontext = nullptr;
|
||||
|
||||
getfilecon(TEST_SUB_PATH_3_FILE_2.c_str(), &secontext);
|
||||
EXPECT_STREQ(secontextOld, secontext);
|
||||
freecon(secontext);
|
||||
|
Loading…
Reference in New Issue
Block a user