13d8536b4Sopenharmony_ci/* 23d8536b4Sopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved. 33d8536b4Sopenharmony_ci * 43d8536b4Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification, 53d8536b4Sopenharmony_ci * are permitted provided that the following conditions are met: 63d8536b4Sopenharmony_ci * 73d8536b4Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of 83d8536b4Sopenharmony_ci * conditions and the following disclaimer. 93d8536b4Sopenharmony_ci * 103d8536b4Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list 113d8536b4Sopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials 123d8536b4Sopenharmony_ci * provided with the distribution. 133d8536b4Sopenharmony_ci * 143d8536b4Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used 153d8536b4Sopenharmony_ci * to endorse or promote products derived from this software without specific prior written 163d8536b4Sopenharmony_ci * permission. 173d8536b4Sopenharmony_ci * 183d8536b4Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 193d8536b4Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 203d8536b4Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 213d8536b4Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 223d8536b4Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 233d8536b4Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 243d8536b4Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 253d8536b4Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 263d8536b4Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 273d8536b4Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 283d8536b4Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 293d8536b4Sopenharmony_ci */ 303d8536b4Sopenharmony_ci 313d8536b4Sopenharmony_ci#include "xts_net.h" 323d8536b4Sopenharmony_ci 333d8536b4Sopenharmony_ciLITE_TEST_SUIT(NET, ActsNet, ActsNetTestSuite); 343d8536b4Sopenharmony_ci 353d8536b4Sopenharmony_cistatic BOOL ActsNetTestSuiteSetUp(void) 363d8536b4Sopenharmony_ci{ 373d8536b4Sopenharmony_ci return TRUE; 383d8536b4Sopenharmony_ci} 393d8536b4Sopenharmony_ci 403d8536b4Sopenharmony_cistatic BOOL ActsNetTestSuiteTearDown(void) 413d8536b4Sopenharmony_ci{ 423d8536b4Sopenharmony_ci return TRUE; 433d8536b4Sopenharmony_ci} 443d8536b4Sopenharmony_ci 453d8536b4Sopenharmony_ci/** 463d8536b4Sopenharmony_ci * @tc.number : SUB_KERNEL_NET_0730 473d8536b4Sopenharmony_ci * @tc.name : test ioctl get and set IFHWADDR 483d8536b4Sopenharmony_ci * @tc.desc : [C- SOFTWARE -0200] 493d8536b4Sopenharmony_ci */ 503d8536b4Sopenharmony_ciLITE_TEST_CASE(ActsNetTestSuite, testIoctlIfhwAddr, Function | MediumTest | Level2) 513d8536b4Sopenharmony_ci{ 523d8536b4Sopenharmony_ci int udpFd = socket(AF_INET, SOCK_DGRAM, 0); 533d8536b4Sopenharmony_ci ICUNIT_ASSERT_NOT_EQUAL(udpFd, -1, udpFd); /* -1, common data for test, no special meaning */ 543d8536b4Sopenharmony_ci 553d8536b4Sopenharmony_ci struct ifreq ifre[5]; /* 5, common data for test, no special meaning */ 563d8536b4Sopenharmony_ci struct ifconf ifcf = {0}; 573d8536b4Sopenharmony_ci (void)memset_s(&ifcf, sizeof(struct ifconf), 0, sizeof(struct ifconf)); 583d8536b4Sopenharmony_ci ifcf.ifc_len = 5 * sizeof(struct ifreq); /* 5, common data for test, no special meaning */ 593d8536b4Sopenharmony_ci ifcf.ifc_buf = (char *)ifre; 603d8536b4Sopenharmony_ci int ret = ioctl(udpFd, SIOCGIFCONF, (char *)&ifcf); 613d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, ret); 623d8536b4Sopenharmony_ci 633d8536b4Sopenharmony_ci char rst1[18]; /* 18, common data for test, no special meaning */ 643d8536b4Sopenharmony_ci char *macPtr = NULL; 653d8536b4Sopenharmony_ci struct ifreq ifrTmp = {0}; 663d8536b4Sopenharmony_ci struct sockaddr_in *addr = NULL; 673d8536b4Sopenharmony_ci int ifrCount = ifcf.ifc_len / sizeof(struct ifreq); 683d8536b4Sopenharmony_ci ICUNIT_GOTO_EQUAL(ifrCount, 2, ifrCount, EXIT); /* 2, common data for test, no special meaning */ 693d8536b4Sopenharmony_ci ICUNIT_ASSERT_WITHIN_EQUAL(ifrCount, 2, INT_MAX, ifrCount); /* 2, common data for test, no special meaning */ 703d8536b4Sopenharmony_ciEXIT: 713d8536b4Sopenharmony_ci for (int i = 0; i < ifrCount; i++) { 723d8536b4Sopenharmony_ci addr = (struct sockaddr_in *)&ifre[i].ifr_addr; 733d8536b4Sopenharmony_ci if (strcmp("lo", ifre[i].ifr_name) != 0) { 743d8536b4Sopenharmony_ci (void)memset_s(&ifrTmp, sizeof(struct ifreq), 0, sizeof(struct ifreq)); 753d8536b4Sopenharmony_ci ret = strcpy_s(ifrTmp.ifr_name, sizeof(ifrTmp.ifr_name), ifre[i].ifr_name); 763d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, ret); 773d8536b4Sopenharmony_ci ret = ioctl(udpFd, SIOCGIFHWADDR, &ifrTmp); 783d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, ret); 793d8536b4Sopenharmony_ci macPtr = ifrTmp.ifr_hwaddr.sa_data; 803d8536b4Sopenharmony_ci ret = sprintf_s(rst1, sizeof(rst1), "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", *macPtr, *(macPtr + 1), 813d8536b4Sopenharmony_ci *(macPtr + 2), *(macPtr + 3), *(macPtr + 4), *(macPtr + 5)); /* 1, 2, 3, 4, 5, common data for test, no special meaning */ 823d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL((unsigned int)ret, strlen(rst1), (unsigned int)ret); 833d8536b4Sopenharmony_ci } 843d8536b4Sopenharmony_ci } 853d8536b4Sopenharmony_ci ret = close(udpFd); 863d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, ret); 873d8536b4Sopenharmony_ci return 0; 883d8536b4Sopenharmony_ci} 893d8536b4Sopenharmony_ci 903d8536b4Sopenharmony_ci/** 913d8536b4Sopenharmony_ci * @tc.number : SUB_KERNEL_NET_1000 923d8536b4Sopenharmony_ci * @tc.name : test socket operation 933d8536b4Sopenharmony_ci * @tc.desc : [C- SOFTWARE -0200] 943d8536b4Sopenharmony_ci */ 953d8536b4Sopenharmony_ciLITE_TEST_CASE(ActsNetTestSuite, testSocketOpt, Function | MediumTest | Level2) 963d8536b4Sopenharmony_ci{ 973d8536b4Sopenharmony_ci socklen_t len; 983d8536b4Sopenharmony_ci struct timeval timeout = {0}; 993d8536b4Sopenharmony_ci int fd = socket(AF_INET, SOCK_STREAM, 0); 1003d8536b4Sopenharmony_ci ICUNIT_ASSERT_NOT_EQUAL(fd, -1, fd); /* -1, common data for test, no special meaning */ 1013d8536b4Sopenharmony_ci 1023d8536b4Sopenharmony_ci int error = -1; /* -1, common data for test, no special meaning */ 1033d8536b4Sopenharmony_ci len = sizeof(error); 1043d8536b4Sopenharmony_ci int ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len); 1053d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, ret); 1063d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(error, 0, error); 1073d8536b4Sopenharmony_ci 1083d8536b4Sopenharmony_ci len = sizeof(timeout); 1093d8536b4Sopenharmony_ci ret = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, &len); 1103d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, ret); 1113d8536b4Sopenharmony_ci 1123d8536b4Sopenharmony_ci timeout.tv_sec = 1000; /* 1000, common data for test, no special meaning */ 1133d8536b4Sopenharmony_ci len = sizeof(timeout); 1143d8536b4Sopenharmony_ci ret = setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, len); 1153d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, ret); 1163d8536b4Sopenharmony_ci 1173d8536b4Sopenharmony_ci (void)memset_s(&timeout, len, 0, len); 1183d8536b4Sopenharmony_ci ret = getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, &len); 1193d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, ret); 1203d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(timeout.tv_sec, 1000, timeout.tv_sec); /* 1000, common data for test, no special meaning */ 1213d8536b4Sopenharmony_ci 1223d8536b4Sopenharmony_ci int flag = 1; /* 1, common data for test, no special meaning */ 1233d8536b4Sopenharmony_ci ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag)); 1243d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, ret); 1253d8536b4Sopenharmony_ci 1263d8536b4Sopenharmony_ci flag = 0; 1273d8536b4Sopenharmony_ci len = sizeof(flag); 1283d8536b4Sopenharmony_ci ret = getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, &len); 1293d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, ret); 1303d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(flag, 1, flag); /* 1, common data for test, no special meaning */ 1313d8536b4Sopenharmony_ci 1323d8536b4Sopenharmony_ci error = -1; /* -1, common data for test, no special meaning */ 1333d8536b4Sopenharmony_ci len = sizeof(error); 1343d8536b4Sopenharmony_ci ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len); 1353d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, ret); 1363d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(error, 0, error); 1373d8536b4Sopenharmony_ci 1383d8536b4Sopenharmony_ci ret = close(fd); 1393d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, ret); 1403d8536b4Sopenharmony_ci return 0; 1413d8536b4Sopenharmony_ci} 1423d8536b4Sopenharmony_ci 1433d8536b4Sopenharmony_ci/** 1443d8536b4Sopenharmony_ci * @tc.number : SUB_KERNEL_NET_1100 1453d8536b4Sopenharmony_ci * @tc.name : test getsockname and getpeername invalid input 1463d8536b4Sopenharmony_ci * @tc.desc : [C- SOFTWARE -0200] 1473d8536b4Sopenharmony_ci */ 1483d8536b4Sopenharmony_ciLITE_TEST_CASE(ActsNetTestSuite, testGetSocketNameInvalidInput, Function | MediumTest | Level3) 1493d8536b4Sopenharmony_ci{ 1503d8536b4Sopenharmony_ci struct sockaddr addr = {0}; 1513d8536b4Sopenharmony_ci socklen_t addrLen = sizeof(addr); 1523d8536b4Sopenharmony_ci int ret = getsockname(-1, &addr, &addrLen); /* -1, common data for test, no special meaning */ 1533d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ 1543d8536b4Sopenharmony_ci ret = getpeername(-1, &addr, &addrLen); /* -1, common data for test, no special meaning */ 1553d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ 1563d8536b4Sopenharmony_ci ret = getsockname(0, &addr, &addrLen); 1573d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ 1583d8536b4Sopenharmony_ci ret = getpeername(0, &addr, &addrLen); 1593d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ 1603d8536b4Sopenharmony_ci ret = getsockname(1, &addr, &addrLen); /* 1, common data for test, no special meaning */ 1613d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ 1623d8536b4Sopenharmony_ci ret = getpeername(1, &addr, &addrLen); /* 1, common data for test, no special meaning */ 1633d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ 1643d8536b4Sopenharmony_ci ret = getsockname(130, &addr, &addrLen); /* 130, common data for test, no special meaning */ 1653d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ 1663d8536b4Sopenharmony_ci ret = getpeername(130, &addr, &addrLen); /* 130, common data for test, no special meaning */ 1673d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ 1683d8536b4Sopenharmony_ci ret = getsockname(10, NULL, &addrLen); /* 10, common data for test, no special meaning */ 1693d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ 1703d8536b4Sopenharmony_ci ret = getpeername(10, NULL, &addrLen); /* 10, common data for test, no special meaning */ 1713d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ 1723d8536b4Sopenharmony_ci ret = getsockname(10, &addr, NULL); /* 10, common data for test, no special meaning */ 1733d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ 1743d8536b4Sopenharmony_ci ret = getpeername(10, &addr, NULL); /* 10, common data for test, no special meaning */ 1753d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ 1763d8536b4Sopenharmony_ci return 0; 1773d8536b4Sopenharmony_ci} 1783d8536b4Sopenharmony_ci 1793d8536b4Sopenharmony_ci/** 1803d8536b4Sopenharmony_ci * @tc.number : SUB_KERNEL_NET_2400 1813d8536b4Sopenharmony_ci * @tc.name : test convert value from host to network byte order 1823d8536b4Sopenharmony_ci * @tc.desc : [C- SOFTWARE -0200] 1833d8536b4Sopenharmony_ci */ 1843d8536b4Sopenharmony_ciLITE_TEST_CASE(ActsNetTestSuite, testHostToNetwork, Function | MediumTest | Level2) 1853d8536b4Sopenharmony_ci{ 1863d8536b4Sopenharmony_ci uint32_t intInput1 = 0; 1873d8536b4Sopenharmony_ci uint32_t intRst01 = htonl(intInput1); 1883d8536b4Sopenharmony_ci uint32_t intInput2 = 65536; /* 65536, common data for test, no special meaning */ 1893d8536b4Sopenharmony_ci uint32_t intRst02 = htonl(intInput2); 1903d8536b4Sopenharmony_ci uint16_t shortInput1 = 0; 1913d8536b4Sopenharmony_ci uint16_t shortRst01 = htons(shortInput1); 1923d8536b4Sopenharmony_ci uint16_t shortInput2 = 255; /* 255, common data for test, no special meaning */ 1933d8536b4Sopenharmony_ci uint16_t shortRst02 = htons(shortInput2); 1943d8536b4Sopenharmony_ci 1953d8536b4Sopenharmony_ci#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 1963d8536b4Sopenharmony_ci uint32_t expectZero = 0; 1973d8536b4Sopenharmony_ci uint32_t expectForIinput2 = 256; /* 255, common data for test, no special meaning */ 1983d8536b4Sopenharmony_ci uint32_t expectForSinput2 = 65280; /* 65536, common data for test, no special meaning */ 1993d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(intRst01, expectZero, intRst01); 2003d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(intRst02, expectForIinput2, intRst02); 2013d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(shortRst01, expectZero, shortRst01); 2023d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(shortRst02, expectForSinput2, shortRst02); 2033d8536b4Sopenharmony_ci#else 2043d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(intRst01, intInput1, intRst01); 2053d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(intRst02, intInput2, intRst02); 2063d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(shortRst01, shortInput1, shortRst01); 2073d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(shortRst02, shortInput2, shortRst02); 2083d8536b4Sopenharmony_ci#endif 2093d8536b4Sopenharmony_ci return 0; 2103d8536b4Sopenharmony_ci} 2113d8536b4Sopenharmony_ci 2123d8536b4Sopenharmony_ci/** 2133d8536b4Sopenharmony_ci * @tc.number : SUB_KERNEL_NET_2500 2143d8536b4Sopenharmony_ci * @tc.name : test convert value from network to host byte order 2153d8536b4Sopenharmony_ci * @tc.desc : [C- SOFTWARE -0200] 2163d8536b4Sopenharmony_ci */ 2173d8536b4Sopenharmony_ciLITE_TEST_CASE(ActsNetTestSuite, testNetworkToHost, Function | MediumTest | Level2) 2183d8536b4Sopenharmony_ci{ 2193d8536b4Sopenharmony_ci uint32_t intInput1 = 0; 2203d8536b4Sopenharmony_ci uint32_t intRst1 = ntohl(intInput1); 2213d8536b4Sopenharmony_ci uint32_t intInput2 = 65536; /* 65536, common data for test, no special meaning */ 2223d8536b4Sopenharmony_ci uint32_t intRst02 = ntohl(intInput2); 2233d8536b4Sopenharmony_ci uint16_t shortInput1 = 0; 2243d8536b4Sopenharmony_ci uint16_t shortRst01 = ntohs(shortInput1); 2253d8536b4Sopenharmony_ci uint16_t shortInput2 = 255; /* 255, common data for test, no special meaning */ 2263d8536b4Sopenharmony_ci uint16_t shortRst02 = ntohs(shortInput2); 2273d8536b4Sopenharmony_ci 2283d8536b4Sopenharmony_ci#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 2293d8536b4Sopenharmony_ci uint32_t expectZero = 0; 2303d8536b4Sopenharmony_ci uint32_t expectForIinput2 = 256; /* 255, common data for test, no special meaning */ 2313d8536b4Sopenharmony_ci uint32_t expectForSinput2 = 65280; /* 65536, common data for test, no special meaning */ 2323d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(intRst1, expectZero, intRst1); 2333d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(intRst02, expectForIinput2, intRst02); 2343d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(shortRst01, expectZero, shortRst01); 2353d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(shortRst02, expectForSinput2, shortRst02); 2363d8536b4Sopenharmony_ci#else 2373d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(intRst1, intInput1, intRst1); 2383d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(intRst02, intInput2, intRst02); 2393d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(shortRst01, shortInput1, shortRst01); 2403d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(shortRst02, shortInput2, shortRst02); 2413d8536b4Sopenharmony_ci#endif 2423d8536b4Sopenharmony_ci return 0; 2433d8536b4Sopenharmony_ci} 2443d8536b4Sopenharmony_ci 2453d8536b4Sopenharmony_ci/** 2463d8536b4Sopenharmony_ci * @tc.number : SUB_KERNEL_NET_2600 2473d8536b4Sopenharmony_ci * @tc.name : test inet_pton IPv4 normal 2483d8536b4Sopenharmony_ci * @tc.desc : [C- SOFTWARE -0200] 2493d8536b4Sopenharmony_ci */ 2503d8536b4Sopenharmony_ciLITE_TEST_CASE(ActsNetTestSuite, testInetPtonIpv4Normal, Function | MediumTest | Level2) 2513d8536b4Sopenharmony_ci{ 2523d8536b4Sopenharmony_ci int ret; 2533d8536b4Sopenharmony_ci struct in_addr rst = {0}; 2543d8536b4Sopenharmony_ci char cpAddrs[4][16] = {"10.58.212.100", "0.0.0.0", "255.0.0.0", "255.255.255.255"}; /* 4, 16, common data for test, no special meaning */ 2553d8536b4Sopenharmony_ci#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 2563d8536b4Sopenharmony_ci unsigned int expLittle[4] = {1691630090, 0, 255, 4294967295}; /* 4, common data for test, no special meaning */ 2573d8536b4Sopenharmony_ci#else 2583d8536b4Sopenharmony_ci unsigned int expBig[4] = {171627620, 0, 4278190080, 4294967295}; /* 4, common data for test, no special meaning */ 2593d8536b4Sopenharmony_ci#endif 2603d8536b4Sopenharmony_ci 2613d8536b4Sopenharmony_ci for (int i = 0; i < 4; i++) { /* 4, common data for test, no special meaning */ 2623d8536b4Sopenharmony_ci ret = inet_pton(AF_INET, cpAddrs[i], &rst); 2633d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 1, ret); 2643d8536b4Sopenharmony_ci#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 2653d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(rst.s_addr, expLittle[i], rst.s_addr); 2663d8536b4Sopenharmony_ci#else 2673d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(rst.s_addr, expBig[i], rst.s_addr); 2683d8536b4Sopenharmony_ci#endif 2693d8536b4Sopenharmony_ci } 2703d8536b4Sopenharmony_ci return 0; 2713d8536b4Sopenharmony_ci} 2723d8536b4Sopenharmony_ci 2733d8536b4Sopenharmony_ci/** 2743d8536b4Sopenharmony_ci * @tc.number : SUB_KERNEL_NET_2800 2753d8536b4Sopenharmony_ci * @tc.name : test inet_pton IPv6 normal 2763d8536b4Sopenharmony_ci * @tc.desc : [C- SOFTWARE -0200] 2773d8536b4Sopenharmony_ci */ 2783d8536b4Sopenharmony_ciLITE_TEST_CASE(ActsNetTestSuite, testInetPtonIpv6Normal, Function | MediumTest | Level2) 2793d8536b4Sopenharmony_ci{ 2803d8536b4Sopenharmony_ci int ret; 2813d8536b4Sopenharmony_ci struct in6_addr rst = {0}; 2823d8536b4Sopenharmony_ci char cpAddrs[6][40] = {"0101:0101:0101:0101:1010:1010:1010:1010", "0:0:0:0:0:0:0:0", /* 6, 40, common data for test, no special meaning */ 2833d8536b4Sopenharmony_ci "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF", "::", "1::", "0011:0011:0011:0011:11:11:11:11"}; 2843d8536b4Sopenharmony_ci for (int i = 0; i < 6; i++) { /* 6, common data for test, no special meaning */ 2853d8536b4Sopenharmony_ci ret = inet_pton(AF_INET6, cpAddrs[i], &rst); 2863d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 1, ret); /* 1, common data for test, no special meaning */ 2873d8536b4Sopenharmony_ci } 2883d8536b4Sopenharmony_ci return 0; 2893d8536b4Sopenharmony_ci} 2903d8536b4Sopenharmony_ci 2913d8536b4Sopenharmony_ci/** 2923d8536b4Sopenharmony_ci * @tc.number : SUB_KERNEL_NET_2900 2933d8536b4Sopenharmony_ci * @tc.name : test inet_pton IPv6 abnormal 2943d8536b4Sopenharmony_ci * @tc.desc : [C- SOFTWARE -0200] 2953d8536b4Sopenharmony_ci */ 2963d8536b4Sopenharmony_ciLITE_TEST_CASE(ActsNetTestSuite, testInetPtonIpv6Abnormal, Function | MediumTest | Level2) 2973d8536b4Sopenharmony_ci{ 2983d8536b4Sopenharmony_ci int ret; 2993d8536b4Sopenharmony_ci struct in6_addr rst = {0}; 3003d8536b4Sopenharmony_ci char cpAddrs[7][40] = {"127.0.0.1", "f", ":", "0:0", "1:::", ":::::::", /* 7, 40, common data for test, no special meaning */ 3013d8536b4Sopenharmony_ci "1111:1111:1111:1111:1111:1111:1111:111G"}; 3023d8536b4Sopenharmony_ci for (int i = 0; i < 7; i++) { /* 7, common data for test, no special meaning */ 3033d8536b4Sopenharmony_ci ret = inet_pton(AF_INET6, cpAddrs[i], &rst); 3043d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, 0, ret); 3053d8536b4Sopenharmony_ci } 3063d8536b4Sopenharmony_ci return 0; 3073d8536b4Sopenharmony_ci} 3083d8536b4Sopenharmony_ci 3093d8536b4Sopenharmony_ci/** 3103d8536b4Sopenharmony_ci * @tc.number : SUB_KERNEL_NET_3000 3113d8536b4Sopenharmony_ci * @tc.name : test inet_pton with invalid family 3123d8536b4Sopenharmony_ci * @tc.desc : [C- SOFTWARE -0200] 3133d8536b4Sopenharmony_ci */ 3143d8536b4Sopenharmony_ciLITE_TEST_CASE(ActsNetTestSuite, testInetPtonInvalidFamily, Function | MediumTest | Level2) 3153d8536b4Sopenharmony_ci{ 3163d8536b4Sopenharmony_ci struct in_addr rst = {0}; 3173d8536b4Sopenharmony_ci int ret = inet_pton(AF_IPX, "127.0.0.1", &rst); 3183d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ 3193d8536b4Sopenharmony_ci ret = inet_pton(-1, "127.0.0.1", &rst); 3203d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, -1, ret); /* -1, common data for test, no special meaning */ 3213d8536b4Sopenharmony_ci return 0; 3223d8536b4Sopenharmony_ci} 3233d8536b4Sopenharmony_ci 3243d8536b4Sopenharmony_ci/** 3253d8536b4Sopenharmony_ci * @tc.number : SUB_KERNEL_NET_3100 3263d8536b4Sopenharmony_ci * @tc.name : test inet_ntop IPv4 normal 3273d8536b4Sopenharmony_ci * @tc.desc : [C- SOFTWARE -0200] 3283d8536b4Sopenharmony_ci */ 3293d8536b4Sopenharmony_ciLITE_TEST_CASE(ActsNetTestSuite, testInetNtopIpv4Normal, Function | MediumTest | Level2) 3303d8536b4Sopenharmony_ci{ 3313d8536b4Sopenharmony_ci const char *ret = NULL; 3323d8536b4Sopenharmony_ci struct in_addr inputAddr = {0}; 3333d8536b4Sopenharmony_ci char rstBuff[INET_ADDRSTRLEN]; 3343d8536b4Sopenharmony_ci#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 3353d8536b4Sopenharmony_ci unsigned int inpLittle[4] = {0x64d43a0a, 0, 255, 4294967295}; /* 4, common data for test, no special meaning */ 3363d8536b4Sopenharmony_ci#else 3373d8536b4Sopenharmony_ci unsigned int inpBig[4] = {171627620, 0, 4278190080, 4294967295}; /* 4, common data for test, no special meaning */ 3383d8536b4Sopenharmony_ci#endif 3393d8536b4Sopenharmony_ci 3403d8536b4Sopenharmony_ci char expAddrs[4][16] = {"10.58.212.100", "0.0.0.0", "255.0.0.0", "255.255.255.255"}; /* 4, 16, common data for test, no special meaning */ 3413d8536b4Sopenharmony_ci for (int i = 0; i < 4; i++) { /* 4, common data for test, no special meaning */ 3423d8536b4Sopenharmony_ci#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 3433d8536b4Sopenharmony_ci inputAddr.s_addr = inpLittle[i]; 3443d8536b4Sopenharmony_ci#else 3453d8536b4Sopenharmony_ci inputAddr.s_addr = inpBig[i]; 3463d8536b4Sopenharmony_ci#endif 3473d8536b4Sopenharmony_ci ret = inet_ntop(AF_INET, &inputAddr, rstBuff, sizeof(rstBuff)); 3483d8536b4Sopenharmony_ci if (ret == NULL) { 3493d8536b4Sopenharmony_ci ICUNIT_ASSERT_NOT_EQUAL(ret, NULL, ret); 3503d8536b4Sopenharmony_ci } else { 3513d8536b4Sopenharmony_ci ICUNIT_ASSERT_STRING_EQUAL(ret, expAddrs[i], ret); 3523d8536b4Sopenharmony_ci ICUNIT_ASSERT_STRING_EQUAL(rstBuff, expAddrs[i], rstBuff); 3533d8536b4Sopenharmony_ci } 3543d8536b4Sopenharmony_ci } 3553d8536b4Sopenharmony_ci return 0; 3563d8536b4Sopenharmony_ci} 3573d8536b4Sopenharmony_ci 3583d8536b4Sopenharmony_ci/** 3593d8536b4Sopenharmony_ci * @tc.number : SUB_KERNEL_NET_3200 3603d8536b4Sopenharmony_ci * @tc.name : test inet_ntop IPv4 boundary input 3613d8536b4Sopenharmony_ci * @tc.desc : [C- SOFTWARE -0200] 3623d8536b4Sopenharmony_ci */ 3633d8536b4Sopenharmony_ciLITE_TEST_CASE(ActsNetTestSuite, testInetNtopIpv4Abnormal, Function | MediumTest | Level2) 3643d8536b4Sopenharmony_ci{ 3653d8536b4Sopenharmony_ci const char *ret = NULL; 3663d8536b4Sopenharmony_ci struct in_addr inputAddr = {0}; 3673d8536b4Sopenharmony_ci char rstBuff[INET_ADDRSTRLEN]; 3683d8536b4Sopenharmony_ci char expStr[2][16] = {"255.255.255.255", "0.0.0.0"}; /* 2, 16, common data for test, no special meaning */ 3693d8536b4Sopenharmony_ci for (int i = 0; i < 2; i++) { /* 2, common data for test, no special meaning */ 3703d8536b4Sopenharmony_ci inputAddr.s_addr = (i == 0 ? -1 : 4294967296); /* -1, 4294967296, common data for test, no special meaning */ 3713d8536b4Sopenharmony_ci ret = inet_ntop(AF_INET, &inputAddr, rstBuff, sizeof(rstBuff)); 3723d8536b4Sopenharmony_ci ICUNIT_ASSERT_NOT_EQUAL(ret, NULL, ret); 3733d8536b4Sopenharmony_ci ICUNIT_ASSERT_STRING_EQUAL(ret, expStr[i], ret); 3743d8536b4Sopenharmony_ci ICUNIT_ASSERT_STRING_EQUAL(rstBuff, expStr[i], rstBuff); 3753d8536b4Sopenharmony_ci } 3763d8536b4Sopenharmony_ci return 0; 3773d8536b4Sopenharmony_ci} 3783d8536b4Sopenharmony_ci 3793d8536b4Sopenharmony_ci/** 3803d8536b4Sopenharmony_ci * @tc.number : SUB_KERNEL_NET_3500 3813d8536b4Sopenharmony_ci * @tc.name : test inet_ntop with invalid family 3823d8536b4Sopenharmony_ci * @tc.desc : [C- SOFTWARE -0200] 3833d8536b4Sopenharmony_ci */ 3843d8536b4Sopenharmony_ciLITE_TEST_CASE(ActsNetTestSuite, testInetNtopInvalidFamily, Function | MediumTest | Level2) 3853d8536b4Sopenharmony_ci{ 3863d8536b4Sopenharmony_ci int iret; 3873d8536b4Sopenharmony_ci const char *ret = NULL; 3883d8536b4Sopenharmony_ci struct in6_addr inputAddr = {0}; 3893d8536b4Sopenharmony_ci char rstBuff[INET6_ADDRSTRLEN]; 3903d8536b4Sopenharmony_ci 3913d8536b4Sopenharmony_ci iret = inet_pton(AF_INET6, "1::", &inputAddr); 3923d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(iret, 1, iret); /* 1, common data for test, no special meaning */ 3933d8536b4Sopenharmony_ci ret = inet_ntop(AF_IPX, &inputAddr, rstBuff, sizeof(rstBuff)); 3943d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, NULL, ret); 3953d8536b4Sopenharmony_ci ret = inet_ntop(-1, &inputAddr, rstBuff, sizeof(rstBuff)); /* -1, common data for test, no special meaning */ 3963d8536b4Sopenharmony_ci ICUNIT_ASSERT_EQUAL(ret, NULL, ret); 3973d8536b4Sopenharmony_ci return 0; 3983d8536b4Sopenharmony_ci} 3993d8536b4Sopenharmony_ci 4003d8536b4Sopenharmony_ciRUN_TEST_SUITE(ActsNetTestSuite); 4013d8536b4Sopenharmony_ci 4023d8536b4Sopenharmony_civoid ActsNetTest(void) 4033d8536b4Sopenharmony_ci{ 4043d8536b4Sopenharmony_ci RUN_ONE_TESTCASE(testIoctlIfhwAddr); 4053d8536b4Sopenharmony_ci RUN_ONE_TESTCASE(testSocketOpt); 4063d8536b4Sopenharmony_ci RUN_ONE_TESTCASE(testGetSocketNameInvalidInput); 4073d8536b4Sopenharmony_ci RUN_ONE_TESTCASE(testHostToNetwork); 4083d8536b4Sopenharmony_ci RUN_ONE_TESTCASE(testNetworkToHost); 4093d8536b4Sopenharmony_ci RUN_ONE_TESTCASE(testInetPtonIpv4Normal); 4103d8536b4Sopenharmony_ci RUN_ONE_TESTCASE(testInetPtonInvalidFamily); 4113d8536b4Sopenharmony_ci RUN_ONE_TESTCASE(testInetNtopIpv4Normal); 4123d8536b4Sopenharmony_ci RUN_ONE_TESTCASE(testInetNtopIpv4Abnormal); 4133d8536b4Sopenharmony_ci#if (LWIP_IPV6 == 1) 4143d8536b4Sopenharmony_ci RUN_ONE_TESTCASE(testInetPtonIpv6Normal); 4153d8536b4Sopenharmony_ci RUN_ONE_TESTCASE(testInetPtonIpv6Abnormal); 4163d8536b4Sopenharmony_ci RUN_ONE_TESTCASE(testInetNtopInvalidFamily); 4173d8536b4Sopenharmony_ci#endif 4183d8536b4Sopenharmony_ci} 4193d8536b4Sopenharmony_ci 420