提交 8f937c87 编写于 作者: Z zhushengle

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

1.修复网络容器测试失败问题
2.调整测试用例之间的顺序,提高测试用例的稳定性
3.极限压测用例增加延时,等待系统资源回收,增加用例稳定性
4.优化网络容器测试用例重复代码

Close #I6L7VF
Signed-off-by: Nzhushengle <zhushengle@huawei.com>
Change-Id: If800793430c872c432f5fdf2d9c2d968fafb3973
上级 a104497e
......@@ -30,6 +30,10 @@
#include <climits>
#include <gtest/gtest.h>
#include <cstdio>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/route.h>
#include "It_container_test.h"
const char *USERDATA_DIR_NAME = "/userdata";
......@@ -41,6 +45,8 @@ const char *FS_TYPE = "vfat";
const int BIT_ON_RETURN_VALUE = 8;
const int STACK_SIZE = 1024 * 1024;
const int CHILD_FUNC_ARG = 0x2088;
static const int TRY_COUNT = 5;
static const int OFFSET = 2;
int ChildFunction(void *args)
{
......@@ -145,6 +151,98 @@ int GetLine(char *buf, int count, int maxLen, char **array)
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::ostringstream buf;
......@@ -290,18 +388,6 @@ HWTEST_F(ContainerTest, ItNetContainer009, TestSize.Level0)
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.desc: uts container function test case
......@@ -400,6 +486,30 @@ HWTEST_F(ContainerTest, ItUserContainer007, TestSize.Level0)
}
#endif
#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.desc: pid container function test case
......@@ -495,30 +605,6 @@ HWTEST_F(ContainerTest, ItPidContainer031, TestSize.Level0)
{
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
#if defined(LOSCFG_USER_TEST_UTS_CONTAINER)
/**
......@@ -1279,6 +1365,19 @@ HWTEST_F(ContainerTest, ItUserContainer005, TestSize.Level0)
ItUserContainer005();
}
#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
} // namespace OHOS
......
......@@ -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 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);
extern std::string ReadlinkContainer(int pid, const std::string& containerType);
......
......@@ -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_008.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_012.cpp",
]
sources_full +=
[ "$TEST_UNITTEST_DIR/container/full/It_net_container_010.cpp" ]
}
......@@ -77,4 +77,6 @@ void ItNetContainer010(void)
ASSERT_EQ(ret, pid);
status = WEXITSTATUS(status);
ASSERT_EQ(status, EXIT_CODE_ERRNO_5);
sleep(5); /* 5: Wait for process resources to be reclaimed */
}
......@@ -99,4 +99,6 @@ void ItPidContainer003(void)
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
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)
for (int i = 0; i < count; i++) {
pid = fork();
if (pid == 0) {
sleep(5); /* delay 5s */
sleep(2); /* delay 2s */
exit(0);
} else if (pid < 0) {
if (errno != EAGAIN) {
......@@ -91,4 +91,6 @@ void ItPidContainer009(void)
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, EXIT_CODE_ERRNO_255);
sleep(5); /* 5: Wait for process resources to be reclaimed */
}
......@@ -112,4 +112,6 @@ void ItPidContainer020(void)
ASSERT_NE(ret, 0);
ret = WEXITSTATUS(status);
ASSERT_EQ(ret, 0);
sleep(5); /* 5: Wait for process resources to be reclaimed */
}
......@@ -77,4 +77,6 @@ void ItPidContainer024(void)
ASSERT_EQ(ret, pid);
status = WEXITSTATUS(status);
ASSERT_EQ(status, EXIT_CODE_ERRNO_5);
sleep(5); /* 5: Wait for process resources to be reclaimed */
}
......@@ -85,4 +85,6 @@ void ItUserContainer005(void)
status = WEXITSTATUS(status);
ASSERT_EQ(status, EXIT_TRUE_CODE);
sleep(5); /* 5: Wait for process resources to be reclaimed */
}
......@@ -78,4 +78,6 @@ void ItUtsContainer003(void)
ASSERT_EQ(ret, pid);
status = WEXITSTATUS(status);
ASSERT_EQ(status, EXIT_CODE_ERRNO_5);
sleep(5); /* 5: Wait for process resources to be reclaimed */
}
......@@ -32,32 +32,11 @@
#include "It_container_test.h"
using namespace std;
const int IpLen = 16;
static int GetLocalIP(char *ip)
{
struct ifreq ifr;
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 const int IpLen = 16;
static const char *NETMASK = "255.255.255.0";
static const char *GW = "192.168.100.1";
static const char *IFNAME = "veth0";
static const char *PEER_IP = "192.168.100.5";
static int SetIP(char *ip)
{
......@@ -73,7 +52,13 @@ static int SetIP(char *ip)
}
ifr.ifr_addr.sa_family = AF_INET;
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);
if (ret != 0) {
return -1;
......@@ -88,27 +73,31 @@ static int ChildFunc(void *arg)
char oldIp[IpLen] = {NULL};
char newIp[IpLen] = {NULL};
ret = GetLocalIP(oldIp);
if (ret != 0) {
ret = NetContainerGetLocalIP("eth0", oldIp, IpLen);
if (ret == 0) {
return EXIT_CODE_ERRNO_1;
}
ret = SetIP("192.168.1.233");
if (ret != 0) {
if (ret == 0) {
return EXIT_CODE_ERRNO_2;
}
ret = GetLocalIP(newIp);
ret = NetContainerResetNetAddr(IFNAME, PEER_IP, NETMASK, GW);
if (ret != 0) {
return EXIT_CODE_ERRNO_3;
}
ret = strcmp(oldIp, newIp);
if (ret == 0) {
(void)memset_s(newIp, IpLen, 0, IpLen);
ret = NetContainerGetLocalIP(IFNAME, newIp, IpLen);
if (ret != 0) {
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;
}
......@@ -122,11 +111,10 @@ void ItNetContainer001(void)
EXPECT_STRNE(stack, NULL);
char *stackTop = stack + STACK_SIZE;
ret = GetLocalIP(oldIp);
ret = NetContainerGetLocalIP("eth0", oldIp, IpLen);
ASSERT_EQ(ret, 0);
int arg = CHILD_FUNC_ARG;
auto pid = clone(ChildFunc, stackTop, SIGCHLD | CLONE_NEWNET, &arg);
auto pid = clone(ChildFunc, stackTop, SIGCHLD | CLONE_NEWNET, NULL);
ASSERT_NE(pid, -1);
int status;
......@@ -136,7 +124,7 @@ void ItNetContainer001(void)
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
ret = GetLocalIP(newIp);
ret = NetContainerGetLocalIP("eth0", newIp, IpLen);
ASSERT_EQ(ret, 0);
ret = strcmp(oldIp, newIp);
......
......@@ -32,32 +32,11 @@
#include "It_container_test.h"
using namespace std;
const int IpLen = 16;
static int GetLocalIP(char *ip)
{
struct ifreq ifr;
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 const int IpLen = 16;
static const char *NETMASK = "255.255.255.0";
static const char *GW = "192.168.100.1";
static const char *IFNAME = "veth0";
static const char *PEER_IP = "192.168.100.5";
static int SetIP(char *ip)
{
......@@ -73,7 +52,12 @@ static int SetIP(char *ip)
}
ifr.ifr_addr.sa_family = AF_INET;
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);
if (ret != 0) {
return -1;
......@@ -88,7 +72,7 @@ static int ChildFunc(void *arg)
char oldIp[IpLen] = {NULL};
char newIp[IpLen] = {NULL};
ret = GetLocalIP(oldIp);
ret = NetContainerGetLocalIP("eth0", oldIp, IpLen);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
......@@ -98,21 +82,31 @@ static int ChildFunc(void *arg)
return EXIT_CODE_ERRNO_2;
}
ret = SetIP("192.168.1.234");
if (ret != 0) {
ret = NetContainerGetLocalIP("eth0", newIp, IpLen);
if (ret == 0) {
return EXIT_CODE_ERRNO_3;
}
ret = GetLocalIP(newIp);
if (ret != 0) {
ret = SetIP("192.168.1.234");
if (ret == 0) {
return EXIT_CODE_ERRNO_4;
}
ret = strcmp(oldIp, newIp);
if (ret == 0) {
ret = NetContainerResetNetAddr(IFNAME, PEER_IP, NETMASK, GW);
if (ret != 0) {
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;
}
......@@ -126,11 +120,10 @@ void ItNetContainer002(void)
EXPECT_STRNE(stack, NULL);
char *stackTop = stack + STACK_SIZE;
ret = GetLocalIP(oldIp);
ret = NetContainerGetLocalIP("eth0", oldIp, IpLen);
ASSERT_EQ(ret, 0);
int arg = CHILD_FUNC_ARG;
auto pid = clone(ChildFunc, stackTop, SIGCHLD, &arg);
auto pid = clone(ChildFunc, stackTop, SIGCHLD, NULL);
ASSERT_NE(pid, -1);
int status;
......@@ -140,7 +133,7 @@ void ItNetContainer002(void)
int exitCode = WEXITSTATUS(status);
ASSERT_EQ(exitCode, 0);
ret = GetLocalIP(newIp);
ret = NetContainerGetLocalIP("eth0", newIp, IpLen);
ASSERT_EQ(ret, 0);
ret = strcmp(oldIp, newIp);
......
......@@ -44,61 +44,6 @@ static const int DATA_LEN = 128;
static const char *SERVER_MSG = "===Hi, I'm Server.===";
static const char *PEER_MSG = "===Hi, I'm Peer.===";
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)
{
......@@ -162,7 +107,7 @@ static int UdpClient(void)
static int ChildFunc(void *arg)
{
int ret = ResetNetaddr(IFNAME, PEER_IP, NETMASK, GW);
int ret = NetContainerResetNetAddr(IFNAME, PEER_IP, NETMASK, GW);
if (ret != 0) {
return EXIT_CODE_ERRNO_1;
}
......@@ -219,9 +164,7 @@ static int UdpServer(void)
static void *UdpServerThread(void *arg)
{
int ret;
ret = ResetNetaddr(IFNAME, SERVER_IP, NETMASK, GW);
int ret = NetContainerResetNetAddr(IFNAME, SERVER_IP, NETMASK, GW);
if (ret != 0) {
return (void *)(intptr_t)ret;
}
......
......@@ -43,61 +43,7 @@ static const char *SERVER_MSG = "===Hi, I'm Server.===";
static const char *PEER_MSG = "===Hi, I'm Peer.===";
static const int TRY_COUNT = 5;
static const int DATA_LEN = 128;
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 const int IpLen = 16;
static int TcpClient(void)
{
......@@ -124,26 +70,30 @@ static int TcpClient(void)
} while (ret !=0 && (try_count--));
if (ret < 0) {
(void)close(peer);
printf("[ERR][%s:%d] connect failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_2;
}
ret = send(peer, PEER_MSG, strlen(PEER_MSG) + 1, 0);
if (ret < 0) {
(void)close(peer);
printf("[ERR][%s:%d] send failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_3;
}
ret = recv(peer, recv_data, sizeof(recv_data), 0);
if (ret < 0) {
(void)close(peer);
printf("[ERR][%s:%d] recv failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_4;
}
(void)close(peer);
ret = strcmp(recv_data, SERVER_MSG);
if (ret != 0) {
return EXIT_CODE_ERRNO_5;
}
(void)close(peer);
return 0;
}
......@@ -154,7 +104,6 @@ static int TcpServer(void)
int peer;
char recv_data[DATA_LEN];
struct sockaddr_in server_addr;
socklen_t client_addr_len = sizeof(struct sockaddr);
server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (server < 0) {
......@@ -169,16 +118,19 @@ static int TcpServer(void)
ret = bind(server, const_cast<struct sockaddr *>(reinterpret_cast<struct sockaddr *>(&server_addr)),
sizeof(struct sockaddr));
if (ret != 0) {
printf("[ERR][%s:%d] bind failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_2;
}
ret = listen(server, 1);
if (ret < 0) {
printf("[ERR][%s:%d] listen failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_3;
}
peer = accept(server, nullptr, nullptr);
if (peer < 0) {
printf("[ERR][%s:%d] accept failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_4;
}
......@@ -186,6 +138,7 @@ static int TcpServer(void)
ret = recv(peer, recv_data, sizeof(recv_data), 0);
if (ret < 0) {
printf("[ERR][%s:%d] recv failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_5;
}
......@@ -196,31 +149,54 @@ static int TcpServer(void)
ret = send(peer, SERVER_MSG, strlen(SERVER_MSG) + 1, 0);
if (ret < 0) {
printf("[ERR][%s:%d] send failed, %s!\n", __FUNCTION__, __LINE__, strerror(errno));
return EXIT_CODE_ERRNO_7;
}
(void)close(peer);
return 0;
}
static void *TcpClientThread(void *arg)
{
int ret = ResetNetaddr(IFNAME, PEER_IP, NETMASK, GW);
if (ret == 0) {
ret = TcpClient();
char newIp[IpLen] = {NULL};
int ret = NetContainerResetNetAddr(IFNAME, PEER_IP, NETMASK, GW);
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;
}
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) {
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();
}
......@@ -249,6 +225,9 @@ VOID ItNetContainer009(void)
ret = pthread_join(srv, &tret);
ASSERT_EQ(ret, 0);
ret = (uintptr_t)tret;
ASSERT_EQ(ret, 0);
ret = pthread_attr_destroy(&attr);
ASSERT_EQ(ret, 0);
......
......@@ -51,6 +51,8 @@ void ItPidContainer032(void)
char *array[g_arryLen] = { nullptr };
char buf[g_buffSize] = { 0 };
sleep(2); /* 2: Wait for cache resource reclamation */
int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
......
......@@ -55,6 +55,8 @@ void ItPidContainer033(void)
char buf[g_buffSize] = { 0 };
int status = 0;
sleep(2); /* 2: Wait for cache resource reclamation */
int ret = ReadFile(path.c_str(), buf);
ASSERT_NE(ret, -1);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册