feat: 支持IPC容器
BREAKING CHANGE: 支持ipc容器及增强对外变更: 1.clone 支持CLONE_NEWIPC 2.增加”/proc/[pid]/container/ipc" 用于查询容器信息 Close #I6AVMY Signed-off-by: zhushengle <zhushengle@huawei.com> Change-Id: I6a3c248d2d66a5342994c6e0b0aecddea8e32c72
This commit is contained in:
@@ -136,6 +136,7 @@ LOSCFG_USER_TEST_CONTAINER = false
|
||||
LOSCFG_USER_TEST_PID_CONTAINER = false
|
||||
LOSCFG_USER_TEST_UTS_CONTAINER = false
|
||||
LOSCFG_USER_TEST_MNT_CONTAINER = false
|
||||
LOSCFG_USER_TEST_IPC_CONTAINER = false
|
||||
if (defined(LOSCFG_KERNEL_CONTAINER)) {
|
||||
LOSCFG_USER_TEST_CONTAINER = true
|
||||
if (defined(LOSCFG_PID_CONTAINER)) {
|
||||
@@ -147,4 +148,7 @@ if (defined(LOSCFG_KERNEL_CONTAINER)) {
|
||||
if (defined(LOSCFG_MNT_CONTAINER)) {
|
||||
LOSCFG_USER_TEST_MNT_CONTAINER = true
|
||||
}
|
||||
if (defined(LOSCFG_IPC_CONTAINER)) {
|
||||
LOSCFG_USER_TEST_IPC_CONTAINER = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,9 @@ config("container_config") {
|
||||
if (defined(LOSCFG_USER_TEST_MNT_CONTAINER)) {
|
||||
cflags += [ "-DLOSCFG_USER_TEST_MNT_CONTAINER" ]
|
||||
}
|
||||
if (defined(LOSCFG_USER_TEST_IPC_CONTAINER)) {
|
||||
cflags += [ "-DLOSCFG_USER_TEST_IPC_CONTAINER" ]
|
||||
}
|
||||
cflags_cc = cflags
|
||||
}
|
||||
|
||||
|
||||
@@ -215,7 +215,33 @@ HWTEST_F(ContainerTest, ItContainerChroot001, TestSize.Level0)
|
||||
ItContainerChroot001();
|
||||
}
|
||||
|
||||
#endif /* LOSCFG_MNT_CONTAINER */
|
||||
#endif /* LOSCFG_USER_TEST_MNT_CONTAINER */
|
||||
|
||||
#if defined(LOSCFG_USER_TEST_IPC_CONTAINER)
|
||||
/**
|
||||
* @tc.name: Container_IPC_Test_001
|
||||
* @tc.desc: ipc container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6AVMY
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItIpcContainer001, TestSize.Level0)
|
||||
{
|
||||
ItIpcContainer001();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Container_IPC_Test_004
|
||||
* @tc.desc: ipc container function test case
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6AVMY
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ContainerTest, ItIpcContainer004, TestSize.Level0)
|
||||
{
|
||||
ItIpcContainer004();
|
||||
}
|
||||
#endif
|
||||
#endif /* LOSCFG_USER_TEST_SMOKE */
|
||||
|
||||
#if defined(LOSCFG_USER_TEST_FULL)
|
||||
|
||||
@@ -35,9 +35,14 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <regex>
|
||||
#include <csignal>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/capability.h>
|
||||
#include "osTest.h"
|
||||
#include "mqueue.h"
|
||||
#include "sys/time.h"
|
||||
#include "sys/shm.h"
|
||||
#include "sys/types.h"
|
||||
|
||||
const int EXIT_CODE_ERRNO_1 = 1;
|
||||
const int EXIT_CODE_ERRNO_2 = 2;
|
||||
@@ -55,11 +60,19 @@ const int EXIT_CODE_ERRNO_13 = 13;
|
||||
const int EXIT_CODE_ERRNO_14 = 14;
|
||||
const int EXIT_CODE_ERRNO_15 = 15;
|
||||
const int EXIT_CODE_ERRNO_16 = 16;
|
||||
const int EXIT_CODE_ERRNO_17 = 17;
|
||||
const int EXIT_CODE_ERRNO_255 = 255;
|
||||
const int CONTAINER_FIRST_PID = 1;
|
||||
const int CONTAINER_SECOND_PID = 2;
|
||||
const int CONTAINER_THIRD_PID = 3;
|
||||
|
||||
const int MQUEUE_TEST_SIZE = 50;
|
||||
const int MQUEUE_TEST_MAX_MSG = 255;
|
||||
|
||||
const int SHM_TEST_DATA_SIZE = 1024;
|
||||
const int SHM_TEST_KEY1 = 1234;
|
||||
const int SHM_TEST_OPEN_PERM = 0666;
|
||||
const int CLONE_STACK_MMAP_FLAG = MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK;
|
||||
|
||||
extern const char *USERDATA_DIR_NAME;
|
||||
extern const char *ACCESS_FILE_NAME;
|
||||
@@ -71,6 +84,8 @@ extern const int BIT_ON_RETURN_VALUE;
|
||||
extern const int STACK_SIZE;
|
||||
extern const int CHILD_FUNC_ARG;
|
||||
|
||||
const int MQUEUE_STANDARD_NAME_LENGTH = 255;
|
||||
|
||||
int ChildFunction(void *args);
|
||||
|
||||
pid_t CloneWrapper(int (*func)(void *), int flag, void *args);
|
||||
@@ -79,6 +94,42 @@ std::string GenContainerLinkPath(int pid, const std::string& containerType);
|
||||
|
||||
extern std::string ReadlinkContainer(int pid, const std::string& containerType);
|
||||
|
||||
class MQueueFinalizer {
|
||||
public:
|
||||
explicit MQueueFinalizer(mqd_t mqueueParent, const std::string& mqname)
|
||||
{
|
||||
m_mqueueParent = mqueueParent;
|
||||
m_mqname = mqname;
|
||||
}
|
||||
~MQueueFinalizer()
|
||||
{
|
||||
if (m_mqueueParent >= 0) {
|
||||
mq_close(m_mqueueParent);
|
||||
mq_unlink(m_mqname.c_str());
|
||||
}
|
||||
}
|
||||
private:
|
||||
mqd_t m_mqueueParent;
|
||||
std::string m_mqname;
|
||||
};
|
||||
|
||||
class ShmFinalizer {
|
||||
public:
|
||||
explicit ShmFinalizer(void* shm, int shmid)
|
||||
{
|
||||
m_shm = shm;
|
||||
m_shmid = shmid;
|
||||
}
|
||||
~ShmFinalizer()
|
||||
{
|
||||
shmdt(m_shm);
|
||||
shmctl(m_shmid, IPC_RMID, nullptr);
|
||||
}
|
||||
private:
|
||||
void* m_shm;
|
||||
int m_shmid;
|
||||
};
|
||||
|
||||
#if defined(LOSCFG_USER_TEST_SMOKE)
|
||||
void ItContainer001(void);
|
||||
void ItContainerChroot001(void);
|
||||
@@ -99,6 +150,14 @@ void ItMntContainer006(void);
|
||||
void ItMntContainer007(void);
|
||||
void ItMntContainer008(void);
|
||||
#endif
|
||||
#if defined(LOSCFG_USER_TEST_IPC_CONTAINER)
|
||||
void ItIpcContainer001(void);
|
||||
void ItIpcContainer002(void);
|
||||
void ItIpcContainer003(void);
|
||||
void ItIpcContainer004(void);
|
||||
void ItIpcContainer005(void);
|
||||
void ItIpcContainer006(void);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(LOSCFG_USER_TEST_FULL)
|
||||
|
||||
@@ -91,3 +91,14 @@ if (defined(LOSCFG_USER_TEST_MNT_CONTAINER)) {
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_mnt_container_008.cpp",
|
||||
]
|
||||
}
|
||||
|
||||
if (defined(LOSCFG_USER_TEST_IPC_CONTAINER)) {
|
||||
sources_smoke += [
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_001.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_002.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_003.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_004.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_005.cpp",
|
||||
"$TEST_UNITTEST_DIR/container/smoke/It_ipc_container_006.cpp",
|
||||
]
|
||||
}
|
||||
|
||||
129
testsuites/unittest/container/smoke/It_ipc_container_001.cpp
Normal file
129
testsuites/unittest/container/smoke/It_ipc_container_001.cpp
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include "It_container_test.h"
|
||||
using namespace std;
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
int ret;
|
||||
(void)arg;
|
||||
mqd_t mqueueChild;
|
||||
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
|
||||
char mqname[MQUEUE_STANDARD_NAME_LENGTH] = "/testMQueue001";
|
||||
const char msgptr[] = "childMsgs";
|
||||
struct mq_attr attr = { 0 };
|
||||
attr.mq_msgsize = MQUEUE_TEST_SIZE;
|
||||
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
|
||||
mqueueChild = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, &attr);
|
||||
if (mqueueChild == (mqd_t)-1) {
|
||||
goto EXIT1;
|
||||
}
|
||||
ret = mq_send(mqueueChild, msgptr, strlen(msgptr), 0);
|
||||
if (ret != 0) {
|
||||
goto EXIT1;
|
||||
}
|
||||
ret = mq_receive(mqueueChild, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
|
||||
if (ret == -1) {
|
||||
goto EXIT1;
|
||||
}
|
||||
if (strncmp(msgrcd, msgptr, strlen(msgptr)) != 0) {
|
||||
goto EXIT1;
|
||||
}
|
||||
|
||||
EXIT1:
|
||||
if (mqueueChild >= 0) {
|
||||
ret = mq_close(mqueueChild);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
ret = mq_unlink(mqname);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
}
|
||||
return EXIT_CODE_ERRNO_7;
|
||||
}
|
||||
|
||||
void ItIpcContainer001(void)
|
||||
{
|
||||
uint32_t ret;
|
||||
int status;
|
||||
int exitCode;
|
||||
pid_t pid;
|
||||
mqd_t mqueueParent;
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
|
||||
char mqname[MQUEUE_STANDARD_NAME_LENGTH] = "/testMQueue001";
|
||||
const char msgptr[] = "parentMsg";
|
||||
struct mq_attr attr = { 0 };
|
||||
attr.mq_msgsize = MQUEUE_TEST_SIZE;
|
||||
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
|
||||
|
||||
mqueueParent = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, NULL);
|
||||
MQueueFinalizer mQueueFinalizer(mqueueParent, mqname);
|
||||
|
||||
ASSERT_NE(mqueueParent, (mqd_t)-1);
|
||||
|
||||
(void)memset_s(&attr, sizeof(attr), 0, sizeof(attr));
|
||||
ret = mq_getattr(mqueueParent, &attr);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
attr.mq_flags |= O_NONBLOCK;
|
||||
ret = mq_setattr(mqueueParent, &attr, NULL);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = mq_getattr(mqueueParent, &attr);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = mq_send(mqueueParent, msgptr, strlen(msgptr), 0);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
pid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
|
||||
ASSERT_NE(pid, -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, EXIT_CODE_ERRNO_7);
|
||||
|
||||
ret = mq_receive(mqueueParent, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
|
||||
ASSERT_NE(ret, -1);
|
||||
ASSERT_STREQ(msgrcd, msgptr);
|
||||
}
|
||||
145
testsuites/unittest/container/smoke/It_ipc_container_002.cpp
Normal file
145
testsuites/unittest/container/smoke/It_ipc_container_002.cpp
Normal file
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include "It_container_test.h"
|
||||
using namespace std;
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
int ret;
|
||||
(void)arg;
|
||||
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
|
||||
char mqname[] = "/testMQueue003";
|
||||
const char msgptr[] = "childMsgs";
|
||||
struct mq_attr attr = { 0 };
|
||||
mqd_t mqueue;
|
||||
attr.mq_msgsize = MQUEUE_TEST_SIZE;
|
||||
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
|
||||
|
||||
mqueue = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, &attr);
|
||||
if (mqueue == (mqd_t)-1) {
|
||||
goto EXIT1;
|
||||
}
|
||||
ret = mq_send(mqueue, msgptr, strlen(msgptr), 0);
|
||||
if (ret != 0) {
|
||||
goto EXIT1;
|
||||
}
|
||||
ret = mq_receive(mqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
|
||||
if (ret != strlen(msgptr)) {
|
||||
goto EXIT1;
|
||||
}
|
||||
if (strncmp(msgrcd, msgptr, strlen(msgptr)) != 0) {
|
||||
goto EXIT1;
|
||||
}
|
||||
|
||||
EXIT1:
|
||||
if (mqueue >= 0) {
|
||||
ret = mq_close(mqueue);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
ret = mq_unlink(mqname);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
}
|
||||
return EXIT_CODE_ERRNO_7;
|
||||
}
|
||||
|
||||
void ItIpcContainer002(void)
|
||||
{
|
||||
uint32_t ret;
|
||||
int status;
|
||||
int exitCode;
|
||||
mqd_t mqueue;
|
||||
pid_t childPid;
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
struct mq_attr attr = { 0 };
|
||||
attr.mq_msgsize = MQUEUE_TEST_SIZE;
|
||||
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
|
||||
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
|
||||
char mqname[] = "/testMQueue004";
|
||||
const char msgptr[] = "parentMsg";
|
||||
std::string containerType = "ipc";
|
||||
std::string filePath;
|
||||
int fd;
|
||||
int parentPid;
|
||||
std::string parentlink;
|
||||
std::string childlink;
|
||||
|
||||
mqueue = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, NULL);
|
||||
MQueueFinalizer mQueueFinalizer(mqueue, mqname);
|
||||
|
||||
ASSERT_NE(mqueue, (mqd_t)-1);
|
||||
|
||||
(void)memset_s(&attr, sizeof(attr), 0, sizeof(attr));
|
||||
ret = mq_getattr(mqueue, &attr);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
attr.mq_flags |= O_NONBLOCK;
|
||||
ret = mq_setattr(mqueue, &attr, NULL);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = mq_getattr(mqueue, &attr);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = mq_send(mqueue, msgptr, strlen(msgptr), 0);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
childPid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
|
||||
ASSERT_NE(childPid, -1);
|
||||
|
||||
parentPid = getpid();
|
||||
parentlink = ReadlinkContainer(parentPid, containerType);
|
||||
childlink = ReadlinkContainer(childPid, containerType);
|
||||
filePath = GenContainerLinkPath(childPid, containerType);
|
||||
fd = open(filePath.c_str(), O_RDONLY);
|
||||
ASSERT_NE(fd, -1);
|
||||
|
||||
ret = setns(fd, CLONE_NEWIPC);
|
||||
ASSERT_NE(ret, -1);
|
||||
|
||||
ret = close(fd);
|
||||
ASSERT_NE(ret, -1);
|
||||
|
||||
ret = waitpid(childPid, &status, 0);
|
||||
ASSERT_EQ(ret, childPid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, EXIT_CODE_ERRNO_7);
|
||||
}
|
||||
151
testsuites/unittest/container/smoke/It_ipc_container_003.cpp
Normal file
151
testsuites/unittest/container/smoke/It_ipc_container_003.cpp
Normal file
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include "It_container_test.h"
|
||||
using namespace std;
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
int ret;
|
||||
(void)arg;
|
||||
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
|
||||
char mqname[] = "/testMQueue005";
|
||||
const char msgptr[] = "childMsg";
|
||||
struct mq_attr attr = { 0 };
|
||||
mqd_t mqueue;
|
||||
attr.mq_msgsize = MQUEUE_TEST_SIZE;
|
||||
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
|
||||
|
||||
mqueue = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, &attr);
|
||||
if (mqueue == (mqd_t)-1) {
|
||||
goto EXIT1;
|
||||
}
|
||||
ret = mq_send(mqueue, msgptr, strlen(msgptr), 0);
|
||||
if (ret != 0) {
|
||||
goto EXIT1;
|
||||
}
|
||||
ret = mq_receive(mqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
|
||||
if (ret != strlen(msgptr)) {
|
||||
goto EXIT1;
|
||||
}
|
||||
if (strncmp(msgrcd, msgptr, strlen(msgptr)) != 0) {
|
||||
goto EXIT1;
|
||||
}
|
||||
|
||||
EXIT1:
|
||||
if (mqueue >= 0) {
|
||||
ret = mq_close(mqueue);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
ret = mq_unlink(mqname);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
}
|
||||
return EXIT_CODE_ERRNO_7;
|
||||
}
|
||||
|
||||
void ItIpcContainer003(void)
|
||||
{
|
||||
int status, exitCode, ret;
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
struct mq_attr attr = { 0 };
|
||||
attr.mq_msgsize = MQUEUE_TEST_SIZE;
|
||||
attr.mq_maxmsg = MQUEUE_TEST_MAX_MSG;
|
||||
struct sigevent notification;
|
||||
notification.sigev_notify = 5; /* 5: test data */
|
||||
char msgrcd[MQUEUE_STANDARD_NAME_LENGTH] = {0};
|
||||
char mqname[] = "/testMQueue006";
|
||||
const char msgptr[] = "parentMsg";
|
||||
|
||||
ret = unshare(CLONE_NEWIPC);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
mqd_t mqueue = mq_open(mqname, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR, &attr);
|
||||
MQueueFinalizer mQueueFinalizer(mqueue, mqname);
|
||||
|
||||
ASSERT_NE(mqueue, (mqd_t)-1);
|
||||
|
||||
ret = mq_notify(mqueue, ¬ification);
|
||||
ASSERT_EQ(ret, -1);
|
||||
ASSERT_EQ(errno, EINVAL);
|
||||
|
||||
notification.sigev_notify = SIGEV_THREAD;
|
||||
ret = mq_notify(mqueue, ¬ification);
|
||||
ASSERT_EQ(ret, -1);
|
||||
ASSERT_EQ(errno, ENOTSUP);
|
||||
|
||||
notification.sigev_notify = SIGEV_NONE;
|
||||
|
||||
ret = mq_notify(-1, ¬ification);
|
||||
ASSERT_EQ(ret, -1);
|
||||
ASSERT_EQ(errno, EBADF);
|
||||
|
||||
ret = mq_notify(mqueue, ¬ification);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
(void)memset_s(&attr, sizeof(attr), 0, sizeof(attr));
|
||||
ret = mq_getattr(mqueue, &attr);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
attr.mq_flags |= O_NONBLOCK;
|
||||
ret = mq_setattr(mqueue, &attr, NULL);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = mq_getattr(mqueue, &attr);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = mq_send(mqueue, msgptr, strlen(msgptr), 0);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
pid_t pid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
|
||||
ASSERT_NE(pid, -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(pid, 0);
|
||||
|
||||
exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, EXIT_CODE_ERRNO_7);
|
||||
|
||||
ret = mq_notify(mqueue, nullptr);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = mq_receive(mqueue, msgrcd, MQUEUE_STANDARD_NAME_LENGTH, NULL);
|
||||
ASSERT_EQ(ret, strlen(msgptr));
|
||||
ASSERT_STREQ(msgrcd, msgptr);
|
||||
}
|
||||
217
testsuites/unittest/container/smoke/It_ipc_container_004.cpp
Normal file
217
testsuites/unittest/container/smoke/It_ipc_container_004.cpp
Normal file
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
using namespace std;
|
||||
|
||||
static const char *containerType = "ipc";
|
||||
|
||||
struct shared_use_st {
|
||||
char test[SHM_TEST_DATA_SIZE];
|
||||
};
|
||||
|
||||
static int childFunc1(void *arg)
|
||||
{
|
||||
struct shared_use_st *shared = NULL;
|
||||
const char testBuf[] = "child test shm";
|
||||
int ret;
|
||||
(void)arg;
|
||||
|
||||
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
|
||||
if (shmid == -1) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
void *shm = shmat(shmid, 0, 0);
|
||||
if (shm == reinterpret_cast<void *>(-1)) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
shared = (struct shared_use_st *)shm;
|
||||
ret = strncmp(shared->test, testBuf, strlen(testBuf));
|
||||
if (ret != 0) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
ret = shmdt(shm);
|
||||
if (ret == -1) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_4;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
const char testBuf[] = "parent test shm";
|
||||
const char testBuf1[] = "child test shm";
|
||||
int ret, status, pid, exitCode;
|
||||
(void)arg;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
if (stack == nullptr) {
|
||||
return EXIT_CODE_ERRNO_17;
|
||||
}
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
|
||||
|
||||
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
|
||||
if (shmid == -1) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
void *shm = shmat(shmid, 0, 0);
|
||||
if (shm == reinterpret_cast<void *>(-1)) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
|
||||
struct shared_use_st *shared = (struct shared_use_st *)shm;
|
||||
ret = strncmp(shared->test, testBuf, strlen(testBuf));
|
||||
if (ret == 0) {
|
||||
ret = EXIT_CODE_ERRNO_3;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
ret = memcpy_s(shared->test, sizeof(struct shared_use_st), testBuf1, sizeof(testBuf1));
|
||||
if (ret != 0) {
|
||||
ret = EXIT_CODE_ERRNO_4;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
pid = clone(childFunc1, stackTop, SIGCHLD, &arg);
|
||||
if (pid == -1) {
|
||||
ret = EXIT_CODE_ERRNO_5;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
if (ret != pid) {
|
||||
ret = EXIT_CODE_ERRNO_6;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
if (ret == 0) {
|
||||
ret = EXIT_CODE_ERRNO_7;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
exitCode = WEXITSTATUS(status);
|
||||
if (exitCode != 0) {
|
||||
ret = EXIT_CODE_ERRNO_8;
|
||||
goto EXIT;
|
||||
}
|
||||
|
||||
ret = shmdt(shm);
|
||||
if (ret == -1) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_9;
|
||||
}
|
||||
|
||||
ret = shmctl(shmid, IPC_RMID, 0);
|
||||
if (ret == -1) {
|
||||
return EXIT_CODE_ERRNO_10;
|
||||
}
|
||||
|
||||
return 0;
|
||||
EXIT:
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void ItIpcContainer004(void)
|
||||
{
|
||||
const char testBuf[] = "parent test shm";
|
||||
int pid, exitCode, status, ret;
|
||||
void *shm = NULL;
|
||||
struct shmid_ds ds = {};
|
||||
struct shminfo info = {};
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
|
||||
ShmFinalizer ShmFinalizer(shm, shmid);
|
||||
ASSERT_NE(shmid, -1);
|
||||
|
||||
shm = shmat(shmid, 0, 0);
|
||||
ASSERT_NE((int)shm, -1);
|
||||
|
||||
struct shared_use_st *shared = (struct shared_use_st *)shm;
|
||||
ret = memcpy_s(shared->test, sizeof(struct shared_use_st), testBuf, sizeof(testBuf));
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
pid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
|
||||
ASSERT_NE(pid, -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
|
||||
ret = shmctl(shmid, IPC_STAT, &ds);
|
||||
ASSERT_EQ(ret, 0);
|
||||
ASSERT_EQ(ds.shm_segsz, PAGE_SIZE);
|
||||
ASSERT_EQ(ds.shm_nattch, 1);
|
||||
ASSERT_EQ(ds.shm_cpid, getpid());
|
||||
ASSERT_EQ(ds.shm_lpid, getpid());
|
||||
ASSERT_EQ(ds.shm_perm.uid, getuid());
|
||||
|
||||
ret = shmctl(shmid, SHM_STAT, &ds);
|
||||
ASSERT_NE(ret, -1);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
ds.shm_perm.uid = getuid();
|
||||
ds.shm_perm.gid = getgid();
|
||||
ds.shm_perm.mode = 0;
|
||||
ret = shmctl(shmid, IPC_SET, &ds);
|
||||
ASSERT_EQ(ret, 0);
|
||||
|
||||
ret = shmctl(shmid, IPC_INFO, (struct shmid_ds *)&info);
|
||||
ASSERT_EQ(ret, 192); /* 192: test value */
|
||||
ASSERT_EQ(info.shmmax, 0x1000000); /* 0x1000000: Shared memory information */
|
||||
ASSERT_EQ(info.shmmin, 1); /* 1: Shared memory information */
|
||||
ASSERT_EQ(info.shmmni, 192); /* 192: Shared memory information */
|
||||
ASSERT_EQ(info.shmseg, 128); /* 128: Shared memory information */
|
||||
ASSERT_EQ(info.shmall, 0x1000); /* 0x1000: Shared memory information */
|
||||
|
||||
ret = shmdt(shm);
|
||||
ASSERT_NE(ret, -1);
|
||||
|
||||
ret = shmctl(shmid, IPC_RMID, NULL);
|
||||
ASSERT_EQ(ret, 0);
|
||||
}
|
||||
187
testsuites/unittest/container/smoke/It_ipc_container_005.cpp
Normal file
187
testsuites/unittest/container/smoke/It_ipc_container_005.cpp
Normal file
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
using namespace std;
|
||||
|
||||
static const char testBuf[] = "test shm";
|
||||
struct shared_use_st {
|
||||
char test[SHM_TEST_DATA_SIZE];
|
||||
};
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
struct shared_use_st *shared = NULL;
|
||||
int ret;
|
||||
(void)arg;
|
||||
char *containerType = "ipc";
|
||||
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
|
||||
|
||||
ret = unshare(CLONE_NEWIPC);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType);
|
||||
ret = linkBuffer.compare(linkBuffer1);
|
||||
if (ret == 0) {
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
|
||||
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
|
||||
if (shmid == -1) {
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
|
||||
void *shm = shmat(shmid, 0, 0);
|
||||
if (shm == reinterpret_cast<void *>(-1)) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_4;
|
||||
}
|
||||
|
||||
shared = (struct shared_use_st *)shm;
|
||||
ret = strncmp(shared->test, testBuf, strlen(testBuf));
|
||||
if (ret == 0) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_5;
|
||||
}
|
||||
|
||||
ret = shmdt(shm);
|
||||
if (ret == -1) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_6;
|
||||
}
|
||||
|
||||
ret = shmctl(shmid, IPC_RMID, 0);
|
||||
if (ret == -1) {
|
||||
return EXIT_CODE_ERRNO_7;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int testChild(void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
int ret;
|
||||
struct shared_use_st *shared = NULL;
|
||||
int status;
|
||||
int exitCode;
|
||||
int pid;
|
||||
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
if (stack == nullptr) {
|
||||
return EXIT_CODE_ERRNO_17;
|
||||
}
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
|
||||
if (shmid == -1) {
|
||||
return EXIT_CODE_ERRNO_8;
|
||||
}
|
||||
|
||||
void *shm = shmat(shmid, 0, 0);
|
||||
if (shm == reinterpret_cast<void *>(-1)) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_9;
|
||||
}
|
||||
|
||||
shared = (struct shared_use_st *)shm;
|
||||
ret = memcpy_s(shared->test, sizeof(struct shared_use_st), testBuf, sizeof(testBuf));
|
||||
if (ret != 0) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_10;
|
||||
}
|
||||
|
||||
pid = clone(childFunc, stackTop, SIGCHLD, &arg);
|
||||
if (pid == -1) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_11;
|
||||
}
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
if (ret != pid) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_12;
|
||||
}
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
if (ret == 0) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_13;
|
||||
}
|
||||
|
||||
exitCode = WEXITSTATUS(status);
|
||||
if (exitCode != 0) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_14;
|
||||
}
|
||||
|
||||
ret = shmdt(shm);
|
||||
if (ret == -1) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_15;
|
||||
}
|
||||
|
||||
ret = shmctl(shmid, IPC_RMID, 0);
|
||||
if (ret == -1) {
|
||||
return EXIT_CODE_ERRNO_16;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ItIpcContainer005(void)
|
||||
{
|
||||
int ret;
|
||||
int status;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
auto pid = clone(testChild, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
|
||||
ASSERT_NE(pid, -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
int exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
}
|
||||
184
testsuites/unittest/container/smoke/It_ipc_container_006.cpp
Normal file
184
testsuites/unittest/container/smoke/It_ipc_container_006.cpp
Normal file
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include "It_container_test.h"
|
||||
using namespace std;
|
||||
|
||||
static const char testBuf[] = "test shm";
|
||||
static const char *containerType = "ipc";
|
||||
struct shared_use_st {
|
||||
char test[SHM_TEST_DATA_SIZE];
|
||||
};
|
||||
|
||||
static int childFunc1(void *arg)
|
||||
{
|
||||
struct shared_use_st *shared = NULL;
|
||||
int ret;
|
||||
(void)arg;
|
||||
const int sleep_num = 3; /* 3: delay */
|
||||
|
||||
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
|
||||
if (shmid == -1) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
void *shm = shmat(shmid, 0, 0);
|
||||
if (shm == reinterpret_cast<void *>(-1)) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_2;
|
||||
}
|
||||
shared = (struct shared_use_st *)shm;
|
||||
sleep(sleep_num);
|
||||
ret = strncmp(shared->test, testBuf, strlen(testBuf));
|
||||
if (ret != 0) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_3;
|
||||
}
|
||||
|
||||
ret = shmdt(shm);
|
||||
if (ret == -1) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_4;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int childFunc(void *arg)
|
||||
{
|
||||
struct shared_use_st *shared = NULL;
|
||||
int ret;
|
||||
(void)arg;
|
||||
int status;
|
||||
int exitCode;
|
||||
const int sleep_num = 1;
|
||||
auto linkBuffer = ReadlinkContainer(getpid(), containerType);
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
if (stack == nullptr) {
|
||||
return EXIT_CODE_ERRNO_17;
|
||||
}
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
auto pid = clone(childFunc1, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
|
||||
if (pid == -1) {
|
||||
return EXIT_CODE_ERRNO_6;
|
||||
}
|
||||
|
||||
std::string filePath = GenContainerLinkPath(pid, containerType);
|
||||
int fd = open(filePath.c_str(), O_RDONLY);
|
||||
if (fd == -1) {
|
||||
return EXIT_CODE_ERRNO_7;
|
||||
}
|
||||
sleep(sleep_num);
|
||||
ret = setns(fd, CLONE_NEWIPC);
|
||||
if (ret == -1) {
|
||||
close(fd);
|
||||
return EXIT_CODE_ERRNO_8;
|
||||
}
|
||||
close(fd);
|
||||
auto linkBuffer1 = ReadlinkContainer(getpid(), containerType);
|
||||
auto linkBuffer2 = ReadlinkContainer(pid, containerType);
|
||||
ret = linkBuffer.compare(linkBuffer1);
|
||||
if (ret == 0) {
|
||||
return EXIT_CODE_ERRNO_9;
|
||||
}
|
||||
ret = linkBuffer1.compare(linkBuffer2);
|
||||
if (ret != 0) {
|
||||
return EXIT_CODE_ERRNO_10;
|
||||
}
|
||||
|
||||
int shmid = shmget((key_t)SHM_TEST_KEY1, sizeof(struct shared_use_st), SHM_TEST_OPEN_PERM | IPC_CREAT);
|
||||
if (shmid == -1) {
|
||||
return EXIT_CODE_ERRNO_11;
|
||||
}
|
||||
|
||||
void *shm = shmat(shmid, 0, 0);
|
||||
if (shm == reinterpret_cast<void *>(-1)) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_12;
|
||||
}
|
||||
|
||||
shared = (struct shared_use_st *)shm;
|
||||
|
||||
ret = memcpy_s(shared->test, sizeof(struct shared_use_st), testBuf, sizeof(testBuf));
|
||||
if (ret != 0) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_13;
|
||||
}
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
if (ret != pid) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_14;
|
||||
}
|
||||
|
||||
exitCode = WEXITSTATUS(status);
|
||||
if (exitCode != 0) {
|
||||
shmdt(shm);
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_15;
|
||||
}
|
||||
|
||||
ret = shmdt(shm);
|
||||
if (ret == -1) {
|
||||
shmctl(shmid, IPC_RMID, 0);
|
||||
return EXIT_CODE_ERRNO_16;
|
||||
}
|
||||
|
||||
ret = shmctl(shmid, IPC_RMID, 0);
|
||||
if (ret == -1) {
|
||||
return EXIT_CODE_ERRNO_255;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ItIpcContainer006(void)
|
||||
{
|
||||
int ret;
|
||||
int status;
|
||||
char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK,
|
||||
-1, 0);
|
||||
ASSERT_NE(stack, nullptr);
|
||||
char *stackTop = stack + STACK_SIZE;
|
||||
|
||||
int arg = CHILD_FUNC_ARG;
|
||||
auto pid = clone(childFunc, stackTop, CLONE_NEWIPC | SIGCHLD, &arg);
|
||||
ASSERT_NE(pid, -1);
|
||||
|
||||
ret = waitpid(pid, &status, 0);
|
||||
ASSERT_EQ(ret, pid);
|
||||
|
||||
ret = WIFEXITED(status);
|
||||
ASSERT_NE(ret, 0);
|
||||
|
||||
int exitCode = WEXITSTATUS(status);
|
||||
ASSERT_EQ(exitCode, 0);
|
||||
}
|
||||
@@ -34,7 +34,6 @@ static int ChildFunc(void *arg)
|
||||
{
|
||||
int ret;
|
||||
int value = *((int *)arg);
|
||||
|
||||
if (value != CHILD_FUNC_ARG) {
|
||||
return EXIT_CODE_ERRNO_1;
|
||||
}
|
||||
|
||||
@@ -54,4 +54,5 @@ extern void ItProcessFs012(void);
|
||||
extern void ItProcessFs013(void);
|
||||
extern void ItProcessFs014(void);
|
||||
extern void ItProcessFs015(void);
|
||||
extern void ItProcessFs021(void);
|
||||
#endif
|
||||
|
||||
@@ -61,6 +61,7 @@ process_fs_sources_smoke = [
|
||||
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_013.cpp",
|
||||
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_014.cpp",
|
||||
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_015.cpp",
|
||||
"$TEST_UNITTEST_DIR/process/fs/smoke/It_process_fs_021.cpp",
|
||||
]
|
||||
|
||||
process_fs_sources_full = []
|
||||
|
||||
@@ -141,6 +141,18 @@ HWTEST_F(ProcessFsTest, ItProcessFs007, TestSize.Level0)
|
||||
ItProcessFs007();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Process_fs_Test_008
|
||||
* @tc.desc: Process mount directory test
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6APW2
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ProcessFsTest, ItProcessFs008, TestSize.Level0)
|
||||
{
|
||||
ItProcessFs008();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Process_fs_Test_010
|
||||
* @tc.desc: Process mount directory test
|
||||
@@ -200,5 +212,17 @@ HWTEST_F(ProcessFsTest, ItProcessFs015, TestSize.Level0)
|
||||
{
|
||||
ItProcessFs015();
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: Process_fs_Test_021
|
||||
* @tc.desc: Process mount directory test
|
||||
* @tc.type: FUNC
|
||||
* @tc.require: issueI6AVMY
|
||||
* @tc.author:
|
||||
*/
|
||||
HWTEST_F(ProcessFsTest, ItProcessFs021, TestSize.Level0)
|
||||
{
|
||||
ItProcessFs021();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
49
testsuites/unittest/process/fs/smoke/It_process_fs_021.cpp
Normal file
49
testsuites/unittest/process/fs/smoke/It_process_fs_021.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#include <sched.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <climits>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
#include "It_process_fs_test.h"
|
||||
|
||||
void ItProcessFs021(void)
|
||||
{
|
||||
auto path = GenProcPidContainerPath(getpid(), "ipc");
|
||||
std::vector<char> buf(PATH_MAX);
|
||||
auto nbytes = readlink(path.c_str(), buf.data(), PATH_MAX);
|
||||
ASSERT_NE(nbytes, -1);
|
||||
|
||||
std::regex reg("'ipc:\\[[0-9]+\\]'");
|
||||
bool ret = std::regex_match(buf.data(), reg);
|
||||
ASSERT_EQ(ret, true);
|
||||
}
|
||||
Reference in New Issue
Block a user