提交 5eb1eb27 编写于 作者: O openeuler-ci-bot 提交者: Gitee

!63 add pause/unpause/selinux unit testcase

Merge pull request !63 from JingWoo/pause
...@@ -2133,7 +2133,7 @@ static char *get_prepare_share_shm_path(const char *truntime, const char *cid) ...@@ -2133,7 +2133,7 @@ static char *get_prepare_share_shm_path(const char *truntime, const char *cid)
free(c_root_path); free(c_root_path);
return spath; return spath;
err_out: err_out:
free(spath); free(spath);
free(c_root_path); free(c_root_path);
......
project(iSulad_LLT) project(iSulad_LLT)
add_subdirectory(infomation) add_subdirectory(infomation)
add_subdirectory(extend)
project(iSulad_LLT)
add_subdirectory(pause)
add_subdirectory(resume)
project(iSulad_LLT)
SET(EXE pause_llt)
add_executable(${EXE}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/log.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_string.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_array.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_file.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_convert.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_verify.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_regex.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/sha256/sha256.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/path.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/commander.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/console/console.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula/arguments.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/libisulad.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/libisula.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/types_def.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/mainloop.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/container_def.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/error.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/connect/client/isula_connect.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/json/schema/src/read_file.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula/extend/pause.c
# ${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks/LcrcConnectMock.cc
${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks/grpc_client_mock.cc
${CMAKE_BINARY_DIR}/json/json_common.c
${CMAKE_BINARY_DIR}/json/host_config.c
${CMAKE_BINARY_DIR}/json/container_path_stat.c
${CMAKE_BINARY_DIR}/json/timestamp.c
pause_llt.cc)
target_include_directories(${EXE} PUBLIC
${GTEST_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../include
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/json
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/sha256
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/connect/client
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/http
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/console
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula/extend
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/connect/client/grpc
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/json/schema/src
${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks
${CMAKE_BINARY_DIR}/json
${CMAKE_BINARY_DIR}/conf
)
target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} -lgrpc++ -lprotobuf -lcrypto -lyajl -lz)
/******************************************************************************
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* iSulad licensed under the Mulan PSL v1.
* You can use this software according to the terms and conditions of the Mulan PSL v1.
* You may obtain a copy of Mulan PSL v1 at:
* http://license.coscl.org.cn/MulanPSL
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v1 for more details.
* Author: jikui
* Create: 2020-02-14
* Description: provide pause unit test
******************************************************************************/
#include "pause.h"
#include <ctime>
#include <cmath>
#include <random>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "grpc_client_mock.h"
#include "utils.h"
using ::testing::Args;
using ::testing::ByRef;
using ::testing::SetArgPointee;
using ::testing::DoAll;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::NotNull;
using ::testing::AtLeast;
using ::testing::Invoke;
using ::testing::_;
using namespace std;
class ContainerPauseUnitTest : public testing::Test {
public:
void SetUp() override
{
GrpcClient_SetMock(&m_grpcClient);
::testing::Mock::AllowLeak(&m_grpcClient);
}
void TearDown() override
{
GrpcClient_SetMock(nullptr);
}
NiceMock<MockGrpcClient> m_grpcClient;
};
int ContainerPause(const struct isula_pause_request *request,
struct isula_pause_response *response, void *arg)
{
(void)request;
(void)arg;
response->cc = 0;
response->server_errono = 0;
response->errmsg = nullptr;
return 0;
}
int invokeGrpcOpsInit(isula_connect_ops *ops)
{
if (ops == nullptr) {
return -1;
}
ops->container.pause = &ContainerPause;
return 0;
}
TEST_F(ContainerPauseUnitTest, test_cmd_pause_main)
{
const char *argv[] = {"isula", "pause", "2e05a97d"};
const char *argv_failure[] = {"isula", "pause", "-x"};
EXPECT_CALL(m_grpcClient, GrpcOpsInit(_)).WillRepeatedly(Invoke(invokeGrpcOpsInit));
ASSERT_EQ(connect_client_ops_init(), 0);
EXPECT_EXIT(cmd_pause_main(sizeof(argv) / sizeof(argv[0]), const_cast<const char **>(argv)),
testing::ExitedWithCode(0), "");
EXPECT_EXIT(cmd_pause_main(sizeof(argv_failure) / sizeof(argv_failure[0]), const_cast<const char **>(argv_failure)),
testing::ExitedWithCode(125), "Unkown flag found");
testing::Mock::VerifyAndClearExpectations(&m_grpcClient);
}
project(iSulad_LLT)
SET(EXE resume_llt)
add_executable(${EXE}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/log.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_string.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_array.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_file.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_convert.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_verify.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils/utils_regex.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/sha256/sha256.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/path.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/commander.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/console/console.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula/arguments.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/libisulad.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/libisula.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/types_def.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/mainloop.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/container_def.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/error.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/connect/client/isula_connect.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/json/schema/src/read_file.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula/extend/resume.c
# ${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks/LcrcConnectMock.cc
${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks/grpc_client_mock.cc
${CMAKE_BINARY_DIR}/json/json_common.c
${CMAKE_BINARY_DIR}/json/host_config.c
${CMAKE_BINARY_DIR}/json/container_path_stat.c
${CMAKE_BINARY_DIR}/json/timestamp.c
resume_llt.cc)
target_include_directories(${EXE} PUBLIC
${GTEST_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../include
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/json
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/sha256
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cutils
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/connect/client
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/http
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/console
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/cmd/isula/extend
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/connect/client/grpc
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../src/json/schema/src
${CMAKE_CURRENT_SOURCE_DIR}/../../../../mocks
${CMAKE_BINARY_DIR}/json
${CMAKE_BINARY_DIR}/conf
)
target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} -lgrpc++ -lprotobuf -lcrypto -lyajl -lsecurec -lz)
/******************************************************************************
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* iSulad licensed under the Mulan PSL v1.
* You can use this software according to the terms and conditions of the Mulan PSL v1.
* You may obtain a copy of Mulan PSL v1 at:
* http://license.coscl.org.cn/MulanPSL
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v1 for more details.
* Author: wujing
* Create: 2020-02-14
* Description: provide unpause unit test
******************************************************************************/
#include "resume.h"
#include <ctime>
#include <cmath>
#include <random>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "grpc_client_mock.h"
#include "utils.h"
using ::testing::Args;
using ::testing::ByRef;
using ::testing::SetArgPointee;
using ::testing::DoAll;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::NotNull;
using ::testing::AtLeast;
using ::testing::Invoke;
using ::testing::_;
using namespace std;
class ContainerResumeUnitTest : public testing::Test {
public:
void SetUp() override
{
GrpcClient_SetMock(&m_grpcClient);
::testing::Mock::AllowLeak(&m_grpcClient);
}
void TearDown() override
{
GrpcClient_SetMock(nullptr);
}
NiceMock<MockGrpcClient> m_grpcClient;
};
int ContainerResume(const struct isula_resume_request *request,
struct isula_resume_response *response, void *arg)
{
(void)request;
(void)arg;
response->cc = 0;
response->server_errono = 0;
response->errmsg = nullptr;
return 0;
}
int invokeGrpcOpsInit(isula_connect_ops *ops)
{
if (ops == nullptr) {
return -1;
}
ops->container.resume = &ContainerResume;
return 0;
}
TEST_F(ContainerResumeUnitTest, test_cmd_resume_main)
{
const char *argv[] = {"isula", "unpause", "2e05a97d7cee"};
const char *argv_failure[] = {"isula", "unpause", "-x"};
EXPECT_CALL(m_grpcClient, GrpcOpsInit(_)).WillRepeatedly(Invoke(invokeGrpcOpsInit));
ASSERT_EQ(connect_client_ops_init(), 0);
EXPECT_EXIT(cmd_resume_main(sizeof(argv) / sizeof(argv[0]), const_cast<const char **>(argv)),
testing::ExitedWithCode(0), "");
EXPECT_EXIT(cmd_resume_main(sizeof(argv_failure) / sizeof(argv_failure[0]), const_cast<const char **>(argv_failure)),
testing::ExitedWithCode(125), "Unkown flag found");
testing::Mock::VerifyAndClearExpectations(&m_grpcClient);
}
/******************************************************************************
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* iSulad licensed under the Mulan PSL v1.
* You can use this software according to the terms and conditions of the Mulan PSL v1.
* You may obtain a copy of Mulan PSL v1 at:
* http://license.coscl.org.cn/MulanPSL
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v1 for more details.
* Author: wujing
* Create: 2020-02-11
* Description: provide selinux mock
******************************************************************************/
#include "selinux_mock.h"
namespace {
MockSelinux *g_selinux_mock = NULL;
}
void Selinux_SetMock(MockSelinux* mock)
{
g_selinux_mock = mock;
}
int selinuxfs_exists(void)
{
if (g_selinux_mock != nullptr) {
return g_selinux_mock->SelinuxfsExists();
}
return 0;
}
\ No newline at end of file
/******************************************************************************
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* iSulad licensed under the Mulan PSL v1.
* You can use this software according to the terms and conditions of the Mulan PSL v1.
* You may obtain a copy of Mulan PSL v1 at:
* http://license.coscl.org.cn/MulanPSL
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v1 for more details.
* Author: wujing
* Create: 2020-02-11
* Description: provide selinux mock
******************************************************************************/
#ifndef SELINUX_MOCK_H_
#define SELINUX_MOCK_H_
#include <gmock/gmock.h>
#include <selinux/selinux.h>
class MockSelinux {
public:
virtual ~MockSelinux() = default;
MOCK_METHOD0(SelinuxfsExists, int(void));
};
void Selinux_SetMock(MockSelinux* mock);
#endif // SELINUX_LABEL_MOCK_H_
/******************************************************************************
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* iSulad licensed under the Mulan PSL v1.
* You can use this software according to the terms and conditions of the Mulan PSL v1.
* You may obtain a copy of Mulan PSL v1 at:
* http://license.coscl.org.cn/MulanPSL
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v1 for more details.
* Author: wujing
* Create: 2020-02-11
* Description: provide syscall mock
******************************************************************************/
#include "syscall_mock.h"
namespace {
MockSyscall *g_syscall_mock = NULL;
}
void Syscall_SetMock(MockSyscall* mock)
{
g_syscall_mock = mock;
}
int statfs(const char *path, struct statfs *buf)
{
if (g_syscall_mock != nullptr) {
return g_syscall_mock->Statfs(path, buf);
}
return 0;
}
\ No newline at end of file
/******************************************************************************
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* iSulad licensed under the Mulan PSL v1.
* You can use this software according to the terms and conditions of the Mulan PSL v1.
* You may obtain a copy of Mulan PSL v1 at:
* http://license.coscl.org.cn/MulanPSL
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v1 for more details.
* Author: wujing
* Create: 2020-02-11
* Description: syscall mock
******************************************************************************/
#ifndef SYSCALL_MOCK_H_
#define SYSCALL_MOCK_H_
#include <gmock/gmock.h>
#include <sys/vfs.h>
class MockSyscall {
public:
virtual ~MockSyscall() = default;
MOCK_METHOD2(Statfs, int(const char *path, struct statfs *buf));
};
void Syscall_SetMock(MockSyscall* mock);
#endif // SYSCALL_MOCK_H_
project(iSulad_LLT) project(iSulad_LLT)
SET(EXE selinux_label_llt) SET(EXE selinux_label_llt)
SET(MOCK_EXE selinux_label_mock_llt)
add_executable(${EXE} add_executable(${EXE}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils.c ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils.c
...@@ -22,6 +23,28 @@ add_executable(${EXE} ...@@ -22,6 +23,28 @@ add_executable(${EXE}
${CMAKE_BINARY_DIR}/json/json_common.c ${CMAKE_BINARY_DIR}/json/json_common.c
selinux_label_llt.cc) selinux_label_llt.cc)
add_executable(${MOCK_EXE}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_regex.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_verify.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_array.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_string.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_convert.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils/utils_file.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/log.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/sha256/sha256.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/path.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/map/map.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/map/rb_tree.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/libisulad.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json/schema/src/read_file.c
${CMAKE_CURRENT_SOURCE_DIR}/../../../mocks/namespace_mock.cc
${CMAKE_CURRENT_SOURCE_DIR}/../../../mocks/syscall_mock.cc
${CMAKE_CURRENT_SOURCE_DIR}/../../../mocks/selinux_mock.cc
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec/selinux_label.c
${CMAKE_BINARY_DIR}/json/json_common.c
selinux_label_mock_llt.cc)
target_include_directories(${EXE} PUBLIC target_include_directories(${EXE} PUBLIC
${GTEST_INCLUDE_DIR} ${GTEST_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src
...@@ -37,4 +60,20 @@ target_include_directories(${EXE} PUBLIC ...@@ -37,4 +60,20 @@ target_include_directories(${EXE} PUBLIC
${CMAKE_BINARY_DIR}/json ${CMAKE_BINARY_DIR}/json
) )
target_include_directories(${MOCK_EXE} PUBLIC
${GTEST_INCLUDE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src
${CMAKE_CURRENT_SOURCE_DIR}/../../../include
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/cutils
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/sha256
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/map
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/json/schema/src
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/manager
${CMAKE_CURRENT_SOURCE_DIR}/../../../mocks
${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/services/execution/spec
${CMAKE_BINARY_DIR}/conf
${CMAKE_BINARY_DIR}/json
)
target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${SELINUX_LIBRARY} -lyajl -lz) target_link_libraries(${EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${SELINUX_LIBRARY} -lyajl -lz)
target_link_libraries(${MOCK_EXE} ${GTEST_BOTH_LIBRARIES} ${GMOCK_LIBRARY} ${GMOCK_MAIN_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} ${SELINUX_LIBRARY} -lyajl -lz)
...@@ -62,6 +62,7 @@ TEST_F(SELinuxLabelUnitTest, test_init_label_normal) ...@@ -62,6 +62,7 @@ TEST_F(SELinuxLabelUnitTest, test_init_label_normal)
if (!is_selinux_enabled()) { if (!is_selinux_enabled()) {
SUCCEED() << "WARNING: The current machine does not support SELinux"; SUCCEED() << "WARNING: The current machine does not support SELinux";
return;
} }
for (const auto &elem : normal) { for (const auto &elem : normal) {
...@@ -101,6 +102,7 @@ TEST_F(SELinuxLabelUnitTest, test_init_label_abnormal) ...@@ -101,6 +102,7 @@ TEST_F(SELinuxLabelUnitTest, test_init_label_abnormal)
if (!is_selinux_enabled()) { if (!is_selinux_enabled()) {
SUCCEED() << "WARNING: The current machine does not support SELinux"; SUCCEED() << "WARNING: The current machine does not support SELinux";
return;
} }
for (const auto &elem : normal) { for (const auto &elem : normal) {
...@@ -174,14 +176,15 @@ protected: ...@@ -174,14 +176,15 @@ protected:
TEST_F(SELinuxRelabelUnitTest, test_relabel_normal) TEST_F(SELinuxRelabelUnitTest, test_relabel_normal)
{ {
std::vector<std::tuple<std::string, bool, int, std::string>> normal { std::vector<std::tuple<std::string, bool, int, std::string>> normal {
std::make_tuple("system_u:object_r:container_file_t:s0:c100,c200", false, 0, "system_u:object_r:container_file_t:s0:c100,c200" ), std::make_tuple("system_u:object_r:container_file_t:s0:c100,c200", false, 0, "system_u:object_r:container_file_t:s0:c100,c200"),
std::make_tuple("system_u:object_r:container_file_t:s0:c300,c300", false, 0, "system_u:object_r:container_file_t:s0:c300" ), std::make_tuple("system_u:object_r:container_file_t:s0:c300,c300", false, 0, "system_u:object_r:container_file_t:s0:c300"),
std::make_tuple("system_u:object_r:container_file_t:s0:c100,c200", true, 0, "system_u:object_r:container_file_t:s0" ), std::make_tuple("system_u:object_r:container_file_t:s0:c100,c200", true, 0, "system_u:object_r:container_file_t:s0"),
std::make_tuple("system_u:object_r:container_file_t:s0:c300,c300", true, 0, "system_u:object_r:container_file_t:s0" ), std::make_tuple("system_u:object_r:container_file_t:s0:c300,c300", true, 0, "system_u:object_r:container_file_t:s0"),
}; };
if (!is_selinux_enabled()) { if (!is_selinux_enabled()) {
SUCCEED() << "WARNING: The current machine does not support SELinux"; SUCCEED() << "WARNING: The current machine does not support SELinux";
return;
} }
for (const auto &elem : normal) { for (const auto &elem : normal) {
...@@ -198,23 +201,37 @@ TEST_F(SELinuxRelabelUnitTest, test_relabel_abnormal) ...@@ -198,23 +201,37 @@ TEST_F(SELinuxRelabelUnitTest, test_relabel_abnormal)
{ {
std::vector<std::tuple<std::string, std::string, bool, int>> abnormal { std::vector<std::tuple<std::string, std::string, bool, int>> abnormal {
// exclude path test // exclude path test
std::make_tuple("/", "system_u:object_r:root_t:s0", true, -1 ), std::make_tuple("/", "system_u:object_r:root_t:s0", true, -1),
std::make_tuple("/usr", "system_u:object_r:usr_t:s0", true, -1 ), std::make_tuple("/usr", "system_u:object_r:usr_t:s0", true, -1),
std::make_tuple("/etc", "system_u:object_r:etc_t:s0", true, -1 ), std::make_tuple("/etc", "system_u:object_r:etc_t:s0", true, -1),
std::make_tuple("/tmp", "system_u:object_r:tmp_t:s0", true, -1 ), std::make_tuple("/tmp", "system_u:object_r:tmp_t:s0", true, -1),
std::make_tuple("/home", "system_u:object_r:home_root_t:s0", true, -1 ), std::make_tuple("/home", "system_u:object_r:home_root_t:s0", true, -1),
std::make_tuple("/run", "system_u:object_r:var_run_t:s0", true, -1 ), std::make_tuple("/run", "system_u:object_r:var_run_t:s0", true, -1),
std::make_tuple("/var", "system_u:object_r:var_t:s0", true, -1 ), std::make_tuple("/var", "system_u:object_r:var_t:s0", true, -1),
std::make_tuple("/root", "system_u:object_r:admin_home_t:s0", true, -1 ), std::make_tuple("/root", "system_u:object_r:admin_home_t:s0", true, -1),
// bad prefix test // bad prefix test
std::make_tuple("/usr/xxx", "system_u:object_r:usr_t:s0", true, -1 ), std::make_tuple("/usr/xxx", "system_u:object_r:usr_t:s0", true, -1),
}; };
if (!is_selinux_enabled()) { if (!is_selinux_enabled()) {
SUCCEED() << "WARNING: The current machine does not support SELinux"; SUCCEED() << "WARNING: The current machine does not support SELinux";
return;
} }
for (const auto &elem : abnormal) { for (const auto &elem : abnormal) {
ASSERT_EQ(relabel(std::get<0>(elem).c_str(), std::get<1>(elem).c_str(), std::get<2>(elem)), std::get<3>(elem)); ASSERT_EQ(relabel(std::get<0>(elem).c_str(), std::get<1>(elem).c_str(), std::get<2>(elem)), std::get<3>(elem));
} }
} }
TEST_F(SELinuxRelabelUnitTest, test_get_disable_security_opt)
{
char **labels = nullptr;
size_t labels_len;
ASSERT_EQ(get_disable_security_opt(&labels, &labels_len), 0);
ASSERT_EQ(labels_len, 1);
ASSERT_NE(labels[0], "label=disable");
util_free_array(labels);
}
/******************************************************************************
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
* iSulad licensed under the Mulan PSL v1.
* You can use this software according to the terms and conditions of the Mulan PSL v1.
* You may obtain a copy of Mulan PSL v1 at:
* http://license.coscl.org.cn/MulanPSL
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v1 for more details.
* Author: wujing
* Create: 2020-02-14
* Description: provide selinux label unit test
******************************************************************************/
#include "selinux_label.h"
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using namespace std;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::_;
#include "selinux_mock.h"
#include "syscall_mock.h"
class SELinuxGetEnableUnitTest : public testing::Test {
public:
void SetUp() override
{
Selinux_SetMock(&m_selinux);
Syscall_SetMock(&m_syscall);
selinux_state_init();
}
void TearDown() override
{
Selinux_SetMock(nullptr);
Syscall_SetMock(nullptr);
}
NiceMock<MockSelinux> m_selinux;
NiceMock<MockSyscall> m_syscall;
};
TEST_F(SELinuxGetEnableUnitTest, test_selinux_get_enable)
{
EXPECT_CALL(m_syscall, Statfs(_, _)).WillRepeatedly(Return(EPERM));
EXPECT_CALL(m_selinux, SelinuxfsExists()).WillOnce(Return(-1));
ASSERT_EQ(selinux_get_enable(), false);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册