diff --git a/testsuites/unittest/container/It_container_test.cpp b/testsuites/unittest/container/It_container_test.cpp index c9d771da81e1f6e8ac000c2ac2a8ed04cdb271db..f0b37474968eb8652a611bea6203efef07c25f88 100644 --- a/testsuites/unittest/container/It_container_test.cpp +++ b/testsuites/unittest/container/It_container_test.cpp @@ -30,6 +30,10 @@ #include #include #include +#include +#include +#include +#include #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(&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(&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 diff --git a/testsuites/unittest/container/It_container_test.h b/testsuites/unittest/container/It_container_test.h index 09c7b3afb4e5304411a6bbf5b7c1a0b55a0bcba8..18ecbe5c4f5673df4505f862117c7479bfd60e2f 100644 --- a/testsuites/unittest/container/It_container_test.h +++ b/testsuites/unittest/container/It_container_test.h @@ -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); diff --git a/testsuites/unittest/container/config.gni b/testsuites/unittest/container/config.gni index 8dec07abd39f9fdbd1f03241ff6e3d037ce23673..b5f407500e19a30c83ab22e3421479d9d4ee0bb6 100644 --- a/testsuites/unittest/container/config.gni +++ b/testsuites/unittest/container/config.gni @@ -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" ] } diff --git a/testsuites/unittest/container/smoke/It_net_container_010.cpp b/testsuites/unittest/container/full/It_net_container_010.cpp similarity index 97% rename from testsuites/unittest/container/smoke/It_net_container_010.cpp rename to testsuites/unittest/container/full/It_net_container_010.cpp index f46902564bd6830a697e49652e367ec2e1f07e5f..11993fd671299aae8e6a762a50adc4cb5bf0f9fb 100644 --- a/testsuites/unittest/container/smoke/It_net_container_010.cpp +++ b/testsuites/unittest/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 */ } diff --git a/testsuites/unittest/container/full/It_pid_container_003.cpp b/testsuites/unittest/container/full/It_pid_container_003.cpp index ee181dff160b248e5ae16c8b5026dd70424b299e..8bc8ab52e204d2119d6e32701c069bb060e4b9c1 100644 --- a/testsuites/unittest/container/full/It_pid_container_003.cpp +++ b/testsuites/unittest/container/full/It_pid_container_003.cpp @@ -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 */ } diff --git a/testsuites/unittest/container/full/It_pid_container_009.cpp b/testsuites/unittest/container/full/It_pid_container_009.cpp index 8cc0484eee69ce5a2dba5ca9a4f9338b19cc617c..476726038049c22ed29d5fd3f672fb583c6d9859 100644 --- a/testsuites/unittest/container/full/It_pid_container_009.cpp +++ b/testsuites/unittest/container/full/It_pid_container_009.cpp @@ -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 */ } diff --git a/testsuites/unittest/container/full/It_pid_container_020.cpp b/testsuites/unittest/container/full/It_pid_container_020.cpp index 238a8c476ba54f332c8c56d0386fbe0fe2d8838c..f598bb22578d37a4cd18a22eedb06c8470788f0b 100644 --- a/testsuites/unittest/container/full/It_pid_container_020.cpp +++ b/testsuites/unittest/container/full/It_pid_container_020.cpp @@ -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 */ } diff --git a/testsuites/unittest/container/full/It_pid_container_024.cpp b/testsuites/unittest/container/full/It_pid_container_024.cpp index 1a64c7ba3b5f760f5b20d05eeca2f4ca12f91654..d103af479048e31ac555cd0ed100e19e9c3ef2eb 100644 --- a/testsuites/unittest/container/full/It_pid_container_024.cpp +++ b/testsuites/unittest/container/full/It_pid_container_024.cpp @@ -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 */ } diff --git a/testsuites/unittest/container/full/It_user_container_005.cpp b/testsuites/unittest/container/full/It_user_container_005.cpp index e3e91087ac25d56f44c1561f3ffe071465a5599b..297fa9fed7ca694f9e5bf6a2fc651781ec7c1530 100644 --- a/testsuites/unittest/container/full/It_user_container_005.cpp +++ b/testsuites/unittest/container/full/It_user_container_005.cpp @@ -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 */ } diff --git a/testsuites/unittest/container/full/It_uts_container_003.cpp b/testsuites/unittest/container/full/It_uts_container_003.cpp index 7adcadc3b91a9745eb32735d1552c353d7f9088a..1338441c94f9a421e7aaf29dfac35842bbecc502 100644 --- a/testsuites/unittest/container/full/It_uts_container_003.cpp +++ b/testsuites/unittest/container/full/It_uts_container_003.cpp @@ -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 */ } diff --git a/testsuites/unittest/container/smoke/It_net_container_001.cpp b/testsuites/unittest/container/smoke/It_net_container_001.cpp index b145e2ca8ae17897e868238ecbbc7b699893166a..e6d795164556b594927627e7582e7fc3af5c1cb9 100644 --- a/testsuites/unittest/container/smoke/It_net_container_001.cpp +++ b/testsuites/unittest/container/smoke/It_net_container_001.cpp @@ -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(&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); diff --git a/testsuites/unittest/container/smoke/It_net_container_002.cpp b/testsuites/unittest/container/smoke/It_net_container_002.cpp index 4f6b979b91e3298c9445b70cbadc0816410ed5a7..1771d8f822c528e82c9c57844de9786a86db936b 100644 --- a/testsuites/unittest/container/smoke/It_net_container_002.cpp +++ b/testsuites/unittest/container/smoke/It_net_container_002.cpp @@ -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(&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); diff --git a/testsuites/unittest/container/smoke/It_net_container_005.cpp b/testsuites/unittest/container/smoke/It_net_container_005.cpp index 9a2d6540eaf63839411943f5a3a1b70703ade0ad..c35c43895aac0b72e491836c4e2a2e740d1931c9 100644 --- a/testsuites/unittest/container/smoke/It_net_container_005.cpp +++ b/testsuites/unittest/container/smoke/It_net_container_005.cpp @@ -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(&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; } diff --git a/testsuites/unittest/container/smoke/It_net_container_009.cpp b/testsuites/unittest/container/smoke/It_net_container_009.cpp index 40b78bd5f986cc96c5f1e321de944e9aebd2a886..fcb398ed10245b7a2fb07029806a6997449db432 100644 --- a/testsuites/unittest/container/smoke/It_net_container_009.cpp +++ b/testsuites/unittest/container/smoke/It_net_container_009.cpp @@ -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(&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(reinterpret_cast(&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); diff --git a/testsuites/unittest/container/smoke/It_pid_container_032.cpp b/testsuites/unittest/container/smoke/It_pid_container_032.cpp index 4ed7670e75d1664817a35153d97ffce059a9636c..f78f0e7d9cbf6f4e2bd34547b63ea91fbbd9b189 100644 --- a/testsuites/unittest/container/smoke/It_pid_container_032.cpp +++ b/testsuites/unittest/container/smoke/It_pid_container_032.cpp @@ -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); diff --git a/testsuites/unittest/container/smoke/It_pid_container_033.cpp b/testsuites/unittest/container/smoke/It_pid_container_033.cpp index 4eae6c73e2fc3d49373a6b665c5060e65b388127..04ee6ed742a8d36f90faa644dbcab85d1306f936 100644 --- a/testsuites/unittest/container/smoke/It_pid_container_033.cpp +++ b/testsuites/unittest/container/smoke/It_pid_container_033.cpp @@ -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);