未验证 提交 4ff66c7f 编写于 作者: O openharmony_ci 提交者: Gitee

!1133 fix: 修复网络容器测试失败问题

Merge pull request !1133 from zhushengle/container_net
...@@ -30,6 +30,10 @@ ...@@ -30,6 +30,10 @@
#include <climits> #include <climits>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <cstdio> #include <cstdio>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/route.h>
#include "It_container_test.h" #include "It_container_test.h"
const char *USERDATA_DIR_NAME = "/userdata"; const char *USERDATA_DIR_NAME = "/userdata";
...@@ -41,6 +45,8 @@ const char *FS_TYPE = "vfat"; ...@@ -41,6 +45,8 @@ const char *FS_TYPE = "vfat";
const int BIT_ON_RETURN_VALUE = 8; const int BIT_ON_RETURN_VALUE = 8;
const int STACK_SIZE = 1024 * 1024; const int STACK_SIZE = 1024 * 1024;
const int CHILD_FUNC_ARG = 0x2088; const int CHILD_FUNC_ARG = 0x2088;
static const int TRY_COUNT = 5;
static const int OFFSET = 2;
int ChildFunction(void *args) int ChildFunction(void *args)
{ {
...@@ -145,6 +151,98 @@ int GetLine(char *buf, int count, int maxLen, char **array) ...@@ -145,6 +151,98 @@ int GetLine(char *buf, int count, int maxLen, char **array)
return (index + 1); return (index + 1);
} }
static int TryResetNetAddr(const char *ifname, const char *ip, const char *netmask, const char *gw)
{
int ret;
struct ifreq ifr;
struct rtentry rt;
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd < 0) {
return -1;
}
ret = strncpy_s(ifr.ifr_name, sizeof(ifr.ifr_name), ifname, IFNAMSIZ);
if (ret != EOK) {
(void)close(fd);
return -1;
}
ifr.ifr_addr.sa_family = AF_INET;
inet_pton(AF_INET, netmask, ifr.ifr_addr.sa_data + OFFSET);
ret = ioctl(fd, SIOCSIFNETMASK, &ifr);
if (ret != 0) {
printf("[ERR][%s:%d] ioctl SIOCSIFNETMASK failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
(void)close(fd);
return -1;
}
inet_pton(AF_INET, ip, ifr.ifr_addr.sa_data + OFFSET);
ret = ioctl(fd, SIOCSIFADDR, &ifr);
if (ret != 0) {
(void)close(fd);
printf("[ERR][%s:%d] ioctl SIOCGIFADDR failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return -1;
}
struct sockaddr_in *addr = reinterpret_cast<struct sockaddr_in *>(&rt.rt_gateway);
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(gw);
rt.rt_flags = RTF_GATEWAY;
ret = ioctl(fd, SIOCADDRT, &rt);
if (ret != 0) {
(void)close(fd);
printf("[ERR][%s:%d] ioctl SIOCADDRT failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return ret;
}
ret = close(fd);
if (ret != 0) {
printf("[ERR][%s:%d] close failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return ret;
}
return ret;
}
int NetContainerResetNetAddr(const char *ifname, const char *ip, const char *netmask, const char *gw)
{
int ret;
int try_count = TRY_COUNT;
while (try_count--) {
ret = TryResetNetAddr(ifname, ip, netmask, gw);
if (ret == 0) {
break;
}
sleep(1);
}
return ret;
}
int NetContainerGetLocalIP(const char *ifname, char *ip, int ipLen)
{
struct ifreq ifr = {0};
int ret = strcpy_s(ifr.ifr_name, sizeof(ifr.ifr_name), ifname);
if (ret != EOK) {
return -1; /* -1: errno */
}
int inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
if (inet_sock < 0) {
return -2; /* -2: errno */
}
ret = ioctl(inet_sock, SIOCGIFADDR, &ifr);
if (ret != 0) {
(void)close(inet_sock);
printf("[ERR][%s:%d] ioctl SIOCGIFADDR failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return -3; /* -3: errno */
}
ret = close(inet_sock);
if (ret != 0) {
return -4; /* -4: errno */
}
ret = strcpy_s(ip, ipLen, inet_ntoa((reinterpret_cast<struct sockaddr_in *>(&ifr.ifr_addr))->sin_addr));
if (ret != EOK) {
(void)close(inet_sock);
return -5; /* -5: errno */
}
return 0;
}
std::string GenContainerLinkPath(int pid, const std::string& containerType) std::string GenContainerLinkPath(int pid, const std::string& containerType)
{ {
std::ostringstream buf; std::ostringstream buf;
...@@ -290,18 +388,6 @@ HWTEST_F(ContainerTest, ItNetContainer009, TestSize.Level0) ...@@ -290,18 +388,6 @@ HWTEST_F(ContainerTest, ItNetContainer009, TestSize.Level0)
ItNetContainer009(); ItNetContainer009();
} }
/**
* @tc.name: Container_NET_Test_010
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6HPH2
* @tc.author:
*/
HWTEST_F(ContainerTest, ItNetContainer010, TestSize.Level0)
{
ItNetContainer010();
}
/** /**
* @tc.name: Container_NET_Test_011 * @tc.name: Container_NET_Test_011
* @tc.desc: uts container function test case * @tc.desc: uts container function test case
...@@ -400,6 +486,30 @@ HWTEST_F(ContainerTest, ItUserContainer007, TestSize.Level0) ...@@ -400,6 +486,30 @@ HWTEST_F(ContainerTest, ItUserContainer007, TestSize.Level0)
} }
#endif #endif
#if defined(LOSCFG_USER_TEST_PID_CONTAINER) #if defined(LOSCFG_USER_TEST_PID_CONTAINER)
/**
* @tc.name: Container_Pid_Test_032
* @tc.desc: pid container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItPidContainer032, TestSize.Level0)
{
ItPidContainer032();
}
/**
* @tc.name: Container_Pid_Test_033
* @tc.desc: pid container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItPidContainer033, TestSize.Level0)
{
ItPidContainer033();
}
/** /**
* @tc.name: Container_Pid_Test_023 * @tc.name: Container_Pid_Test_023
* @tc.desc: pid container function test case * @tc.desc: pid container function test case
...@@ -495,30 +605,6 @@ HWTEST_F(ContainerTest, ItPidContainer031, TestSize.Level0) ...@@ -495,30 +605,6 @@ HWTEST_F(ContainerTest, ItPidContainer031, TestSize.Level0)
{ {
ItPidContainer031(); ItPidContainer031();
} }
/**
* @tc.name: Container_Pid_Test_032
* @tc.desc: pid container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItPidContainer032, TestSize.Level0)
{
ItPidContainer032();
}
/**
* @tc.name: Container_Pid_Test_033
* @tc.desc: pid container function test case
* @tc.type: FUNC
* @tc.require: issueI6HDQK
* @tc.author:
*/
HWTEST_F(ContainerTest, ItPidContainer033, TestSize.Level0)
{
ItPidContainer033();
}
#endif #endif
#if defined(LOSCFG_USER_TEST_UTS_CONTAINER) #if defined(LOSCFG_USER_TEST_UTS_CONTAINER)
/** /**
...@@ -1279,6 +1365,19 @@ HWTEST_F(ContainerTest, ItUserContainer005, TestSize.Level0) ...@@ -1279,6 +1365,19 @@ HWTEST_F(ContainerTest, ItUserContainer005, TestSize.Level0)
ItUserContainer005(); ItUserContainer005();
} }
#endif #endif
#if defined(LOSCFG_USER_TEST_NET_CONTAINER)
/**
* @tc.name: Container_NET_Test_010
* @tc.desc: uts container function test case
* @tc.type: FUNC
* @tc.require: issueI6HPH2
* @tc.author:
*/
HWTEST_F(ContainerTest, ItNetContainer010, TestSize.Level0)
{
ItNetContainer010();
}
#endif
#endif #endif
} // namespace OHOS } // namespace OHOS
......
...@@ -101,6 +101,10 @@ pid_t CloneWrapper(int (*func)(void *), int flag, void *args); ...@@ -101,6 +101,10 @@ pid_t CloneWrapper(int (*func)(void *), int flag, void *args);
int WaitChild(pid_t pid, int *status, int errNo1, int errNo2); int WaitChild(pid_t pid, int *status, int errNo1, int errNo2);
int NetContainerResetNetAddr(const char *ifname, const char *ip, const char *netmask, const char *gw);
int NetContainerGetLocalIP(const char *ifname, char *ip, int ipLen);
std::string GenContainerLinkPath(int pid, const std::string& containerType); std::string GenContainerLinkPath(int pid, const std::string& containerType);
extern std::string ReadlinkContainer(int pid, const std::string& containerType); extern std::string ReadlinkContainer(int pid, const std::string& containerType);
......
...@@ -160,8 +160,9 @@ if (defined(LOSCFG_USER_TEST_NET_CONTAINER)) { ...@@ -160,8 +160,9 @@ if (defined(LOSCFG_USER_TEST_NET_CONTAINER)) {
"$TEST_UNITTEST_DIR/container/smoke/It_net_container_007.cpp", "$TEST_UNITTEST_DIR/container/smoke/It_net_container_007.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_net_container_008.cpp", "$TEST_UNITTEST_DIR/container/smoke/It_net_container_008.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_net_container_009.cpp", "$TEST_UNITTEST_DIR/container/smoke/It_net_container_009.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_net_container_010.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_net_container_011.cpp", "$TEST_UNITTEST_DIR/container/smoke/It_net_container_011.cpp",
"$TEST_UNITTEST_DIR/container/smoke/It_net_container_012.cpp", "$TEST_UNITTEST_DIR/container/smoke/It_net_container_012.cpp",
] ]
sources_full +=
[ "$TEST_UNITTEST_DIR/container/full/It_net_container_010.cpp" ]
} }
...@@ -77,4 +77,6 @@ void ItNetContainer010(void) ...@@ -77,4 +77,6 @@ void ItNetContainer010(void)
ASSERT_EQ(ret, pid); ASSERT_EQ(ret, pid);
status = WEXITSTATUS(status); status = WEXITSTATUS(status);
ASSERT_EQ(status, EXIT_CODE_ERRNO_5); ASSERT_EQ(status, EXIT_CODE_ERRNO_5);
sleep(5); /* 5: Wait for process resources to be reclaimed */
} }
...@@ -99,4 +99,6 @@ void ItPidContainer003(void) ...@@ -99,4 +99,6 @@ void ItPidContainer003(void)
ASSERT_NE(ret, 0); ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status); ret = WEXITSTATUS(status);
ASSERT_EQ(ret, CONTAINER_FIRST_PID); ASSERT_EQ(ret, CONTAINER_FIRST_PID);
sleep(5); /* 5: Wait for process resources to be reclaimed */
} }
...@@ -46,7 +46,7 @@ static int ChildFun(void *p) ...@@ -46,7 +46,7 @@ static int ChildFun(void *p)
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
pid = fork(); pid = fork();
if (pid == 0) { if (pid == 0) {
sleep(5); /* delay 5s */ sleep(2); /* delay 2s */
exit(0); exit(0);
} else if (pid < 0) { } else if (pid < 0) {
if (errno != EAGAIN) { if (errno != EAGAIN) {
...@@ -91,4 +91,6 @@ void ItPidContainer009(void) ...@@ -91,4 +91,6 @@ void ItPidContainer009(void)
ASSERT_NE(ret, 0); ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status); ret = WEXITSTATUS(status);
ASSERT_EQ(ret, EXIT_CODE_ERRNO_255); ASSERT_EQ(ret, EXIT_CODE_ERRNO_255);
sleep(5); /* 5: Wait for process resources to be reclaimed */
} }
...@@ -112,4 +112,6 @@ void ItPidContainer020(void) ...@@ -112,4 +112,6 @@ void ItPidContainer020(void)
ASSERT_NE(ret, 0); ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status); ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
sleep(5); /* 5: Wait for process resources to be reclaimed */
} }
...@@ -77,4 +77,6 @@ void ItPidContainer024(void) ...@@ -77,4 +77,6 @@ void ItPidContainer024(void)
ASSERT_EQ(ret, pid); ASSERT_EQ(ret, pid);
status = WEXITSTATUS(status); status = WEXITSTATUS(status);
ASSERT_EQ(status, EXIT_CODE_ERRNO_5); ASSERT_EQ(status, EXIT_CODE_ERRNO_5);
sleep(5); /* 5: Wait for process resources to be reclaimed */
} }
...@@ -85,4 +85,6 @@ void ItUserContainer005(void) ...@@ -85,4 +85,6 @@ void ItUserContainer005(void)
status = WEXITSTATUS(status); status = WEXITSTATUS(status);
ASSERT_EQ(status, EXIT_TRUE_CODE); ASSERT_EQ(status, EXIT_TRUE_CODE);
sleep(5); /* 5: Wait for process resources to be reclaimed */
} }
...@@ -78,4 +78,6 @@ void ItUtsContainer003(void) ...@@ -78,4 +78,6 @@ void ItUtsContainer003(void)
ASSERT_EQ(ret, pid); ASSERT_EQ(ret, pid);
status = WEXITSTATUS(status); status = WEXITSTATUS(status);
ASSERT_EQ(status, EXIT_CODE_ERRNO_5); ASSERT_EQ(status, EXIT_CODE_ERRNO_5);
sleep(5); /* 5: Wait for process resources to be reclaimed */
} }
...@@ -32,32 +32,11 @@ ...@@ -32,32 +32,11 @@
#include "It_container_test.h" #include "It_container_test.h"
using namespace std; using namespace std;
const int IpLen = 16; static const int IpLen = 16;
static const char *NETMASK = "255.255.255.0";
static int GetLocalIP(char *ip) static const char *GW = "192.168.100.1";
{ static const char *IFNAME = "veth0";
struct ifreq ifr; static const char *PEER_IP = "192.168.100.5";
int inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
if (inet_sock < 0) {
return -1;
}
int ret = strcpy_s(ifr.ifr_name, sizeof(ifr.ifr_name), "eth0");
if (ret != EOK) {
(void)close(inet_sock);
return -1;
}
ioctl(inet_sock, SIOCGIFADDR, &ifr);
ret = strcpy_s(ip, IpLen, inet_ntoa((reinterpret_cast<struct sockaddr_in *>(&ifr.ifr_addr))->sin_addr));
if (ret != EOK) {
(void)close(inet_sock);
return -1;
}
ret = close(inet_sock);
if (ret != 0) {
return -1;
}
return 0;
}
static int SetIP(char *ip) static int SetIP(char *ip)
{ {
...@@ -73,7 +52,13 @@ static int SetIP(char *ip) ...@@ -73,7 +52,13 @@ static int SetIP(char *ip)
} }
ifr.ifr_addr.sa_family = AF_INET; ifr.ifr_addr.sa_family = AF_INET;
inet_pton(AF_INET, ip, ifr.ifr_addr.sa_data + 2); /* 2: offset */ inet_pton(AF_INET, ip, ifr.ifr_addr.sa_data + 2); /* 2: offset */
ioctl(fd, SIOCSIFADDR, &ifr); ret = ioctl(fd, SIOCSIFADDR, &ifr);
if (ret != 0) {
(void)close(fd);
printf("[ERR][%s:%d] ioctl SIOCSIFADDR failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return -1;
}
ret = close(fd); ret = close(fd);
if (ret != 0) { if (ret != 0) {
return -1; return -1;
...@@ -88,27 +73,31 @@ static int ChildFunc(void *arg) ...@@ -88,27 +73,31 @@ static int ChildFunc(void *arg)
char oldIp[IpLen] = {NULL}; char oldIp[IpLen] = {NULL};
char newIp[IpLen] = {NULL}; char newIp[IpLen] = {NULL};
ret = GetLocalIP(oldIp); ret = NetContainerGetLocalIP("eth0", oldIp, IpLen);
if (ret != 0) { if (ret == 0) {
return EXIT_CODE_ERRNO_1; return EXIT_CODE_ERRNO_1;
} }
ret = SetIP("192.168.1.233"); ret = SetIP("192.168.1.233");
if (ret != 0) { if (ret == 0) {
return EXIT_CODE_ERRNO_2; return EXIT_CODE_ERRNO_2;
} }
ret = GetLocalIP(newIp); ret = NetContainerResetNetAddr(IFNAME, PEER_IP, NETMASK, GW);
if (ret != 0) { if (ret != 0) {
return EXIT_CODE_ERRNO_3; return EXIT_CODE_ERRNO_3;
} }
ret = strcmp(oldIp, newIp); (void)memset_s(newIp, IpLen, 0, IpLen);
if (ret == 0) { ret = NetContainerGetLocalIP(IFNAME, newIp, IpLen);
if (ret != 0) {
return EXIT_CODE_ERRNO_4; return EXIT_CODE_ERRNO_4;
} }
printf("%s %d\n", __FUNCTION__, __LINE__); if (strcmp(PEER_IP, newIp) != 0) {
return EXIT_CODE_ERRNO_5;
}
printf("######## [%s:%d] %s: %s ########\n", __FUNCTION__, __LINE__, IFNAME, newIp);
return 0; return 0;
} }
...@@ -122,11 +111,10 @@ void ItNetContainer001(void) ...@@ -122,11 +111,10 @@ void ItNetContainer001(void)
EXPECT_STRNE(stack, NULL); EXPECT_STRNE(stack, NULL);
char *stackTop = stack + STACK_SIZE; char *stackTop = stack + STACK_SIZE;
ret = GetLocalIP(oldIp); ret = NetContainerGetLocalIP("eth0", oldIp, IpLen);
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
int arg = CHILD_FUNC_ARG; auto pid = clone(ChildFunc, stackTop, SIGCHLD | CLONE_NEWNET, NULL);
auto pid = clone(ChildFunc, stackTop, SIGCHLD | CLONE_NEWNET, &arg);
ASSERT_NE(pid, -1); ASSERT_NE(pid, -1);
int status; int status;
...@@ -136,7 +124,7 @@ void ItNetContainer001(void) ...@@ -136,7 +124,7 @@ void ItNetContainer001(void)
int exitCode = WEXITSTATUS(status); int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0); ASSERT_EQ(exitCode, 0);
ret = GetLocalIP(newIp); ret = NetContainerGetLocalIP("eth0", newIp, IpLen);
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
ret = strcmp(oldIp, newIp); ret = strcmp(oldIp, newIp);
......
...@@ -32,32 +32,11 @@ ...@@ -32,32 +32,11 @@
#include "It_container_test.h" #include "It_container_test.h"
using namespace std; using namespace std;
const int IpLen = 16; static const int IpLen = 16;
static const char *NETMASK = "255.255.255.0";
static int GetLocalIP(char *ip) static const char *GW = "192.168.100.1";
{ static const char *IFNAME = "veth0";
struct ifreq ifr; static const char *PEER_IP = "192.168.100.5";
int inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
if (inet_sock < 0) {
return -1;
}
int ret = strcpy_s(ifr.ifr_name, sizeof(ifr.ifr_name), "eth0");
if (ret != EOK) {
(void)close(inet_sock);
return -1;
}
ioctl(inet_sock, SIOCGIFADDR, &ifr);
ret = strcpy_s(ip, IpLen, inet_ntoa((reinterpret_cast<struct sockaddr_in *>(&ifr.ifr_addr))->sin_addr));
if (ret != EOK) {
(void)close(inet_sock);
return -1;
}
ret = close(inet_sock);
if (ret != 0) {
return -1;
}
return 0;
}
static int SetIP(char *ip) static int SetIP(char *ip)
{ {
...@@ -73,7 +52,12 @@ static int SetIP(char *ip) ...@@ -73,7 +52,12 @@ static int SetIP(char *ip)
} }
ifr.ifr_addr.sa_family = AF_INET; ifr.ifr_addr.sa_family = AF_INET;
inet_pton(AF_INET, ip, ifr.ifr_addr.sa_data + 2); /* 2: offset */ inet_pton(AF_INET, ip, ifr.ifr_addr.sa_data + 2); /* 2: offset */
ioctl(fd, SIOCSIFADDR, &ifr); ret = ioctl(fd, SIOCSIFADDR, &ifr);
if (ret != 0) {
printf("[ERR][%s:%d] ioctl SIOCSIFADDR failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
(void)close(fd);
return -1;
}
ret = close(fd); ret = close(fd);
if (ret != 0) { if (ret != 0) {
return -1; return -1;
...@@ -88,7 +72,7 @@ static int ChildFunc(void *arg) ...@@ -88,7 +72,7 @@ static int ChildFunc(void *arg)
char oldIp[IpLen] = {NULL}; char oldIp[IpLen] = {NULL};
char newIp[IpLen] = {NULL}; char newIp[IpLen] = {NULL};
ret = GetLocalIP(oldIp); ret = NetContainerGetLocalIP("eth0", oldIp, IpLen);
if (ret != 0) { if (ret != 0) {
return EXIT_CODE_ERRNO_1; return EXIT_CODE_ERRNO_1;
} }
...@@ -98,21 +82,31 @@ static int ChildFunc(void *arg) ...@@ -98,21 +82,31 @@ static int ChildFunc(void *arg)
return EXIT_CODE_ERRNO_2; return EXIT_CODE_ERRNO_2;
} }
ret = SetIP("192.168.1.234"); ret = NetContainerGetLocalIP("eth0", newIp, IpLen);
if (ret != 0) { if (ret == 0) {
return EXIT_CODE_ERRNO_3; return EXIT_CODE_ERRNO_3;
} }
ret = GetLocalIP(newIp); ret = SetIP("192.168.1.234");
if (ret != 0) { if (ret == 0) {
return EXIT_CODE_ERRNO_4; return EXIT_CODE_ERRNO_4;
} }
ret = strcmp(oldIp, newIp); ret = NetContainerResetNetAddr(IFNAME, PEER_IP, NETMASK, GW);
if (ret == 0) { if (ret != 0) {
return EXIT_CODE_ERRNO_5; return EXIT_CODE_ERRNO_5;
} }
(void)memset_s(newIp, IpLen, 0, IpLen);
ret = NetContainerGetLocalIP(IFNAME, newIp, IpLen);
if (ret != 0) {
return EXIT_CODE_ERRNO_6;
}
if (strcmp(PEER_IP, newIp) != 0) {
return EXIT_CODE_ERRNO_7;
}
printf("######## [%s:%d] %s: %s ########\n", __FUNCTION__, __LINE__, IFNAME, newIp);
return 0; return 0;
} }
...@@ -126,11 +120,10 @@ void ItNetContainer002(void) ...@@ -126,11 +120,10 @@ void ItNetContainer002(void)
EXPECT_STRNE(stack, NULL); EXPECT_STRNE(stack, NULL);
char *stackTop = stack + STACK_SIZE; char *stackTop = stack + STACK_SIZE;
ret = GetLocalIP(oldIp); ret = NetContainerGetLocalIP("eth0", oldIp, IpLen);
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
int arg = CHILD_FUNC_ARG; auto pid = clone(ChildFunc, stackTop, SIGCHLD, NULL);
auto pid = clone(ChildFunc, stackTop, SIGCHLD, &arg);
ASSERT_NE(pid, -1); ASSERT_NE(pid, -1);
int status; int status;
...@@ -140,7 +133,7 @@ void ItNetContainer002(void) ...@@ -140,7 +133,7 @@ void ItNetContainer002(void)
int exitCode = WEXITSTATUS(status); int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0); ASSERT_EQ(exitCode, 0);
ret = GetLocalIP(newIp); ret = NetContainerGetLocalIP("eth0", newIp, IpLen);
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
ret = strcmp(oldIp, newIp); ret = strcmp(oldIp, newIp);
......
...@@ -44,61 +44,6 @@ static const int DATA_LEN = 128; ...@@ -44,61 +44,6 @@ static const int DATA_LEN = 128;
static const char *SERVER_MSG = "===Hi, I'm Server.==="; static const char *SERVER_MSG = "===Hi, I'm Server.===";
static const char *PEER_MSG = "===Hi, I'm Peer.==="; static const char *PEER_MSG = "===Hi, I'm Peer.===";
static const int TRY_COUNT = 5; static const int TRY_COUNT = 5;
static const int OFFSET = 2;
static int TryResetNetaddr(const char *ifname, const char *ip, const char *netmask, const char *gw)
{
int ret;
struct ifreq ifr;
struct rtentry rt;
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd < 0) {
return -1;
}
ret = strncpy_s(ifr.ifr_name, sizeof(ifr.ifr_name), ifname, IFNAMSIZ);
if (ret != EOK) {
(void)close(fd);
return -1;
}
ifr.ifr_addr.sa_family = AF_INET;
inet_pton(AF_INET, netmask, ifr.ifr_addr.sa_data + OFFSET);
ret = ioctl(fd, SIOCSIFNETMASK, &ifr);
if (ret == 0) {
inet_pton(AF_INET, ip, ifr.ifr_addr.sa_data + OFFSET);
ret = ioctl(fd, SIOCSIFADDR, &ifr);
if (ret == 0) {
struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in *>(&rt.rt_gateway);
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(GW);
rt.rt_flags = RTF_GATEWAY;
ret = ioctl(fd, SIOCADDRT, &rt);
}
}
if (ret != 0) {
(void)close(fd);
return ret;
}
ret = close(fd);
return ret;
}
static int ResetNetaddr(const char *ifname, const char *ip, const char *netmask, const char *gw)
{
int ret;
int try_count = TRY_COUNT;
while (try_count--) {
ret = TryResetNetaddr(ifname, ip, netmask, gw);
if (ret == 0) {
break;
}
sleep(1);
}
return ret;
}
static int UdpClient(void) static int UdpClient(void)
{ {
...@@ -162,7 +107,7 @@ static int UdpClient(void) ...@@ -162,7 +107,7 @@ static int UdpClient(void)
static int ChildFunc(void *arg) static int ChildFunc(void *arg)
{ {
int ret = ResetNetaddr(IFNAME, PEER_IP, NETMASK, GW); int ret = NetContainerResetNetAddr(IFNAME, PEER_IP, NETMASK, GW);
if (ret != 0) { if (ret != 0) {
return EXIT_CODE_ERRNO_1; return EXIT_CODE_ERRNO_1;
} }
...@@ -219,9 +164,7 @@ static int UdpServer(void) ...@@ -219,9 +164,7 @@ static int UdpServer(void)
static void *UdpServerThread(void *arg) static void *UdpServerThread(void *arg)
{ {
int ret; int ret = NetContainerResetNetAddr(IFNAME, SERVER_IP, NETMASK, GW);
ret = ResetNetaddr(IFNAME, SERVER_IP, NETMASK, GW);
if (ret != 0) { if (ret != 0) {
return (void *)(intptr_t)ret; return (void *)(intptr_t)ret;
} }
......
...@@ -43,61 +43,7 @@ static const char *SERVER_MSG = "===Hi, I'm Server.==="; ...@@ -43,61 +43,7 @@ static const char *SERVER_MSG = "===Hi, I'm Server.===";
static const char *PEER_MSG = "===Hi, I'm Peer.==="; static const char *PEER_MSG = "===Hi, I'm Peer.===";
static const int TRY_COUNT = 5; static const int TRY_COUNT = 5;
static const int DATA_LEN = 128; static const int DATA_LEN = 128;
static const int IpLen = 16;
static int TryResetNetaddr(const char *ifname, const char *ip, const char *netmask, const char *gw)
{
int ret;
struct ifreq ifr;
struct rtentry rt;
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd < 0) {
return -1;
}
ret = strncpy_s(ifr.ifr_name, sizeof(ifr.ifr_name), ifname, IFNAMSIZ);
if (ret != EOK) {
close(fd);
return -1;
}
ifr.ifr_addr.sa_family = AF_INET;
inet_pton(AF_INET, netmask, ifr.ifr_addr.sa_data + 2); /* 2: offset */
ret = ioctl(fd, SIOCSIFNETMASK, &ifr);
if (ret == 0) {
inet_pton(AF_INET, ip, ifr.ifr_addr.sa_data + 2); /* 2: offset */
ret = ioctl(fd, SIOCSIFADDR, &ifr);
if (ret == 0) {
struct sockaddr_in* addr = reinterpret_cast<struct sockaddr_in *>(&rt.rt_gateway);
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(GW);
rt.rt_flags = RTF_GATEWAY;
ret = ioctl(fd, SIOCADDRT, &rt);
}
}
if (ret != 0) {
(void)close(fd);
return ret;
}
ret = close(fd);
return ret;
}
static int ResetNetaddr(const char *ifname, const char *ip, const char *netmask, const char *gw)
{
int ret;
int try_count = TRY_COUNT;
while (try_count--) {
ret = TryResetNetaddr(ifname, ip, netmask, gw);
if (ret == 0) {
break;
}
sleep(1);
}
return ret;
}
static int TcpClient(void) static int TcpClient(void)
{ {
...@@ -124,26 +70,30 @@ static int TcpClient(void) ...@@ -124,26 +70,30 @@ static int TcpClient(void)
} while (ret !=0 && (try_count--)); } while (ret !=0 && (try_count--));
if (ret < 0) { if (ret < 0) {
(void)close(peer);
printf("[ERR][%s:%d] connect failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_2; return EXIT_CODE_ERRNO_2;
} }
ret = send(peer, PEER_MSG, strlen(PEER_MSG) + 1, 0); ret = send(peer, PEER_MSG, strlen(PEER_MSG) + 1, 0);
if (ret < 0) { if (ret < 0) {
(void)close(peer);
printf("[ERR][%s:%d] send failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_3; return EXIT_CODE_ERRNO_3;
} }
ret = recv(peer, recv_data, sizeof(recv_data), 0); ret = recv(peer, recv_data, sizeof(recv_data), 0);
if (ret < 0) { if (ret < 0) {
(void)close(peer);
printf("[ERR][%s:%d] recv failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_4; return EXIT_CODE_ERRNO_4;
} }
(void)close(peer);
ret = strcmp(recv_data, SERVER_MSG); ret = strcmp(recv_data, SERVER_MSG);
if (ret != 0) { if (ret != 0) {
return EXIT_CODE_ERRNO_5; return EXIT_CODE_ERRNO_5;
} }
(void)close(peer);
return 0; return 0;
} }
...@@ -154,7 +104,6 @@ static int TcpServer(void) ...@@ -154,7 +104,6 @@ static int TcpServer(void)
int peer; int peer;
char recv_data[DATA_LEN]; char recv_data[DATA_LEN];
struct sockaddr_in server_addr; struct sockaddr_in server_addr;
socklen_t client_addr_len = sizeof(struct sockaddr);
server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (server < 0) { if (server < 0) {
...@@ -169,16 +118,19 @@ static int TcpServer(void) ...@@ -169,16 +118,19 @@ static int TcpServer(void)
ret = bind(server, const_cast<struct sockaddr *>(reinterpret_cast<struct sockaddr *>(&server_addr)), ret = bind(server, const_cast<struct sockaddr *>(reinterpret_cast<struct sockaddr *>(&server_addr)),
sizeof(struct sockaddr)); sizeof(struct sockaddr));
if (ret != 0) { if (ret != 0) {
printf("[ERR][%s:%d] bind failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_2; return EXIT_CODE_ERRNO_2;
} }
ret = listen(server, 1); ret = listen(server, 1);
if (ret < 0) { if (ret < 0) {
printf("[ERR][%s:%d] listen failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_3; return EXIT_CODE_ERRNO_3;
} }
peer = accept(server, nullptr, nullptr); peer = accept(server, nullptr, nullptr);
if (peer < 0) { if (peer < 0) {
printf("[ERR][%s:%d] accept failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_4; return EXIT_CODE_ERRNO_4;
} }
...@@ -186,6 +138,7 @@ static int TcpServer(void) ...@@ -186,6 +138,7 @@ static int TcpServer(void)
ret = recv(peer, recv_data, sizeof(recv_data), 0); ret = recv(peer, recv_data, sizeof(recv_data), 0);
if (ret < 0) { if (ret < 0) {
printf("[ERR][%s:%d] recv failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_5; return EXIT_CODE_ERRNO_5;
} }
...@@ -196,31 +149,54 @@ static int TcpServer(void) ...@@ -196,31 +149,54 @@ static int TcpServer(void)
ret = send(peer, SERVER_MSG, strlen(SERVER_MSG) + 1, 0); ret = send(peer, SERVER_MSG, strlen(SERVER_MSG) + 1, 0);
if (ret < 0) { if (ret < 0) {
printf("[ERR][%s:%d] send failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_7; return EXIT_CODE_ERRNO_7;
} }
(void)close(peer); (void)close(peer);
return 0; return 0;
} }
static void *TcpClientThread(void *arg) static void *TcpClientThread(void *arg)
{ {
int ret = ResetNetaddr(IFNAME, PEER_IP, NETMASK, GW); char newIp[IpLen] = {NULL};
if (ret == 0) { int ret = NetContainerResetNetAddr(IFNAME, PEER_IP, NETMASK, GW);
ret = TcpClient(); if (ret != 0) {
return (void *)(intptr_t)EXIT_CODE_ERRNO_1;
}
ret = NetContainerGetLocalIP(IFNAME, newIp, IpLen);
if (ret != 0) {
return (void *)(intptr_t)EXIT_CODE_ERRNO_2;
} }
if (strncmp(PEER_IP, newIp, strlen(PEER_IP)) != 0) {
return (void *)(intptr_t)EXIT_CODE_ERRNO_3;
}
printf("######## [%s:%d] %s: %s ########\n", __FUNCTION__, __LINE__, IFNAME, newIp);
ret = TcpClient();
return (void *)(intptr_t)ret; return (void *)(intptr_t)ret;
} }
static int ChildFunc(void *arg) static int ChildFunc(void *arg)
{ {
int ret = ret = ResetNetaddr(IFNAME, SERVER_IP, NETMASK, GW); char newIp[IpLen] = {NULL};
int ret = NetContainerResetNetAddr(IFNAME, SERVER_IP, NETMASK, GW);
if (ret != 0) { if (ret != 0) {
return EXIT_CODE_ERRNO_1; return EXIT_CODE_ERRNO_8;
} }
ret = NetContainerGetLocalIP(IFNAME, newIp, IpLen);
if (ret != 0) {
return EXIT_CODE_ERRNO_9;
}
if (strncmp(SERVER_IP, newIp, strlen(SERVER_IP)) != 0) {
return EXIT_CODE_ERRNO_10;
}
printf("######## [%s:%d] %s: %s ########\n", __FUNCTION__, __LINE__, IFNAME, newIp);
return TcpServer(); return TcpServer();
} }
...@@ -249,6 +225,9 @@ VOID ItNetContainer009(void) ...@@ -249,6 +225,9 @@ VOID ItNetContainer009(void)
ret = pthread_join(srv, &tret); ret = pthread_join(srv, &tret);
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
ret = (uintptr_t)tret;
ASSERT_EQ(ret, 0);
ret = pthread_attr_destroy(&attr); ret = pthread_attr_destroy(&attr);
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
......
...@@ -51,6 +51,8 @@ void ItPidContainer032(void) ...@@ -51,6 +51,8 @@ void ItPidContainer032(void)
char *array[g_arryLen] = { nullptr }; char *array[g_arryLen] = { nullptr };
char buf[g_buffSize] = { 0 }; char buf[g_buffSize] = { 0 };
sleep(2); /* 2: Wait for cache resource reclamation */
int ret = ReadFile(path.c_str(), buf); int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1); ASSERT_NE(ret, -1);
......
...@@ -55,6 +55,8 @@ void ItPidContainer033(void) ...@@ -55,6 +55,8 @@ void ItPidContainer033(void)
char buf[g_buffSize] = { 0 }; char buf[g_buffSize] = { 0 };
int status = 0; int status = 0;
sleep(2); /* 2: Wait for cache resource reclamation */
int ret = ReadFile(path.c_str(), buf); int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1); ASSERT_NE(ret, -1);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册