diff --git a/testsuites/unittest/net/netdb/config.gni b/testsuites/unittest/net/netdb/config.gni index 32a188fc53743634349778c203db7c9a079f66b8..2c1702ad121d275f585270087df6f658d1f75d5c 100644 --- a/testsuites/unittest/net/netdb/config.gni +++ b/testsuites/unittest/net/netdb/config.gni @@ -49,7 +49,6 @@ netdb_sources_full = [ "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_010.cpp", "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_011.cpp", "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_012.cpp", - "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_014.cpp", "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_015.cpp", "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_016.cpp", "$TEST_UNITTEST_DIR/net/netdb/full/net_netdb_test_017.cpp", diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_002.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_002.cpp index bb124c5c364b34026fea8f1c4dc3d67a04e76aa4..2e295946f4a1407da0e6038207040e99060bcb1d 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_002.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_002.cpp @@ -35,6 +35,17 @@ static int AddrInfoTest(void) { // Prerequisite: correct DNS servers must be configured. + char host_file[] = "127.0.0.2 example.com\n", serv_file[] = "ftp 21/tcp\n"; + char *pathList[] = {"/etc/hosts", "/etc/services"}; + char *streamList[] = {host_file, serv_file}; + int streamLen[] = {sizeof(host_file), sizeof(serv_file)}; + const int file_number = 2; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + struct addrinfo *addr = NULL; int ret = getaddrinfo("example.com", "ftp", NULL, &addr); ICUNIT_ASSERT_EQUAL(ret, 0, ret); @@ -50,6 +61,7 @@ static int AddrInfoTest(void) const char *p = gai_strerror(EAI_AGAIN); ICUNIT_ASSERT_NOT_EQUAL(p, NULL, -1); + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_004.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_004.cpp index aeea1cc32037d724a828599edc9c7e5752f4eb2c..957874509b0733ba24ac45437c2384acb65168a6 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_004.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_004.cpp @@ -34,8 +34,19 @@ static int GetHostByAddrTest(void) { + char host_file[] = "127.0.0.1 localhost\n100.0.0.0 example.com example\n"; + char *pathList[] = {"/etc/hosts"}; + char *streamList[] = {host_file}; + int streamLen[] = {sizeof(host_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + struct in_addr ia; - int length = 4; + int length = 4; // the address length is 4; ia.s_addr = inet_addr("127.0.0.1"); struct hostent *addr = gethostbyaddr(&ia, sizeof ia, AF_INET); ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1); @@ -43,19 +54,18 @@ static int GetHostByAddrTest(void) ICUNIT_ASSERT_STRING_EQUAL(addr->h_name, "localhost", -1); ICUNIT_ASSERT_EQUAL(addr->h_length, length, -1); - ia.s_addr = inet_addr("100.109.180.184"); + ia.s_addr = inet_addr("100.0.0.0"); addr = gethostbyaddr(&ia, sizeof ia, AF_INET); ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1); ICUNIT_ASSERT_EQUAL(addr->h_addrtype, AF_INET, addr->h_addrtype); - ICUNIT_ASSERT_STRING_EQUAL(addr->h_name, "szvphisprb93341", -1); - ICUNIT_ASSERT_STRING_EQUAL(addr->h_aliases[0], "szvphisprb93341", -1); + ICUNIT_ASSERT_STRING_EQUAL(addr->h_name, "example.com", -1); errno = 0; ia.s_addr = inet_addr("127.0.0.0"); addr = gethostbyaddr(&ia, sizeof ia, AF_INET); ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno); - return ICUNIT_SUCCESS; +    RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_006.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_006.cpp index b9932e8105d80fa93ebc0e85148c7f6f76dacb8c..9cc7c8293375b40ba0a1dfc6f2f84e06bf462d08 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_006.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_006.cpp @@ -34,6 +34,16 @@ static int GetHostByNameTest(void) { + char host_file[] = "127.0.0.1 localhost\n"; + char *pathList[] = {"/etc/hosts"}; + char *streamList[] = {host_file}; + int streamLen[] = {sizeof(host_file)}; + int flag = PrepareFileEnv(pathList, streamList, streamLen, 1); + if (flag != 0) { + RecoveryFileEnv(pathList, 1); + return -1; + } + struct hostent *addr = gethostbyname("localhost"); ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1); ICUNIT_ASSERT_EQUAL(strcmp(addr->h_name, "localhost"), 0, -1); @@ -49,6 +59,7 @@ static int GetHostByNameTest(void) addr = gethostbyname("lo"); ICUNIT_ASSERT_EQUAL(addr, NULL, -1); + RecoveryFileEnv(pathList, 1); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_007.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_007.cpp index 712e5b97b2089ea6aef55790c2ab28788d092715..42ef5c50445656685c76aeaf5c1257376b6037e8 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_007.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_007.cpp @@ -34,6 +34,17 @@ static int GetHostByNameRTest(void) { + char host_file[] = "127.0.0.1 localhost\n"; + char *pathList[] = {"/etc/hosts"}; + char *streamList[] = {static_cast(host_file)}; + int streamLen[] = {sizeof(host_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + struct hostent addr, *result = NULL; char buf[1024]; char buf1[1]; @@ -60,6 +71,7 @@ static int GetHostByNameRTest(void) ret = gethostbyname_r("lo", &addr, buf, sizeof buf, &result, &err); ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret); + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_008.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_008.cpp index 6a9d44e6a003b1abb318ae2fc34b028f7baceff6..7819b3b629746de6d887e8e0c0f7183335800bfd 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_008.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_008.cpp @@ -34,6 +34,17 @@ static int GetHostByName2Test(void) { + char host_file[] = "127.0.0.1 localhost\n"; + char *pathList[] = {"/etc/hosts"}; + char *streamList[] = {static_cast(host_file)}; + int streamLen[] = {sizeof(host_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + struct hostent *addr = gethostbyname2("localhost", AF_INET); ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1); ICUNIT_ASSERT_EQUAL(strcmp(addr->h_name, "localhost"), 0, -1); @@ -51,6 +62,7 @@ static int GetHostByName2Test(void) addr = gethostbyname2("localh", AF_INET); ICUNIT_ASSERT_EQUAL(addr, NULL, -1); + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_009.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_009.cpp index c14bc9b7d196e4109aca86943c04ee1a070f7dfe..7a9ab53195e67544e8128201c73975bdf6e679b9 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_009.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_009.cpp @@ -33,6 +33,17 @@ static int GetHostByName2RTest(void) { + char host_file[] = "127.0.0.1 localhost\n"; + char *pathList[] = {"/etc/hosts"}; + char *streamList[] = {host_file}; + int streamLen[] = {sizeof(host_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + struct hostent addr, *result = NULL; char buf[1024]; char buf1[1]; @@ -59,6 +70,7 @@ static int GetHostByName2RTest(void) ret = gethostbyname2_r("lo", AF_INET, &addr, buf, sizeof buf, &result, &err); ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret); + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_011.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_011.cpp index b6b7f191ed62b270cf74982f1ba55b6647e4e35f..32f7d434a228905f0443726fc963d1a099cde063 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_011.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_011.cpp @@ -35,16 +35,29 @@ static int GetServByPortTest(void) { // refer to the `/etc/services' file. + char serv_file[] = "ssh 22/tcp\n"; + char *pathList[] = {"/etc/services"}; + char *streamList[] = {static_cast(serv_file)}; + int streamLen[] = {sizeof(serv_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + + const int test_port_no = 22; // ssh port number is 22 struct servent *se1 = nullptr; - struct servent *se = getservbyport(htons(22), "tcp"); + struct servent *se = getservbyport(htons(test_port_no), "tcp"); ICUNIT_ASSERT_NOT_EQUAL(se, NULL, -1); ICUNIT_ASSERT_STRING_EQUAL(se->s_name, "ssh", -1); ICUNIT_ASSERT_STRING_EQUAL(se->s_proto, "tcp", -1); ICUNIT_ASSERT_STRING_EQUAL(se->s_aliases[0], "ssh", -1); - se1 = getservbyport(htons(22), "tp"); + se1 = getservbyport(htons(test_port_no), "tp"); ICUNIT_ASSERT_EQUAL(se1, nullptr, -1); + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_012.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_012.cpp index 0e9ad90e2e7afa4b4bb9549b04c48d8602aa19b9..503948f91aa1c5e1e00eba57b06c61a25e50f4a2 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_012.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_012.cpp @@ -35,26 +35,42 @@ static int GetServByPortRTest(void) { // refer to the `/etc/services' file. + char serv_file[] = "ssh 22/tcp\n"; + char *pathList[] = {"/etc/services"}; + char *streamList[] = {static_cast(serv_file)}; + int streamLen[] = {sizeof(serv_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + struct servent se, *result = NULL; char buf[1024]; char buf1[2]; - int ret = getservbyport_r(htons(22), "udp", &se, buf, sizeof buf, &result); + const int test_port_no = 22; // ssh port number is 22 + int ret = getservbyport_r(htons(test_port_no), "tcp", &se, buf, sizeof buf, &result); ICUNIT_ASSERT_EQUAL(ret, 0, ret); ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1); ICUNIT_ASSERT_STRING_EQUAL(se.s_name, "ssh", -1); - ICUNIT_ASSERT_STRING_EQUAL(se.s_proto, "udp", -1); + ICUNIT_ASSERT_STRING_EQUAL(se.s_proto, "tcp", -1); ICUNIT_ASSERT_STRING_EQUAL(se.s_aliases[0], "ssh", -1); ICUNIT_ASSERT_STRING_EQUAL(result->s_name, "ssh", -1); - ICUNIT_ASSERT_STRING_EQUAL(result->s_proto, "udp", -1); + ICUNIT_ASSERT_STRING_EQUAL(result->s_proto, "tcp", -1); ICUNIT_ASSERT_STRING_EQUAL(result->s_aliases[0], "ssh", -1); - ret = getservbyport_r(htons(22), "udp", &se, buf1, sizeof buf1, &result); + ret = getservbyport_r(htons(test_port_no), "udp", &se, buf, sizeof buf, &result); + ICUNIT_ASSERT_EQUAL(ret, ENOENT, -1); + + ret = getservbyport_r(htons(test_port_no), "udp", &se, buf1, sizeof buf1, &result); ICUNIT_ASSERT_EQUAL(ret, ERANGE, ret); - ret = getservbyport_r(htons(22), "ud", &se, buf, sizeof buf, &result); + ret = getservbyport_r(htons(test_port_no), "ud", &se, buf, sizeof buf, &result); ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret); + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_014.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_014.cpp deleted file mode 100644 index d4f0caaa932c0eda4f588117b4c1e26d0f7f25ee..0000000000000000000000000000000000000000 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_014.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "lt_net_netdb.h" -#include - -static int IfNameIndexTest(void) -{ - struct if_nameindex *idx = if_nameindex(); - ICUNIT_ASSERT_NOT_EQUAL(idx, NULL, -1); - - if_freenameindex(idx); - - return ICUNIT_SUCCESS; -} - -void NetNetDbTest014(void) -{ - TEST_ADD_CASE(__FUNCTION__, IfNameIndexTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); -} diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_015.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_015.cpp index 95e675b4221b93cca5c3df9234f0c51673da5c84..612b157535a160b3a452d855d1b33162c873deed 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_015.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_015.cpp @@ -44,10 +44,6 @@ static int IfNameToIndexTest(void) ICUNIT_ASSERT_NOT_EQUAL(str, nullptr, -1); ICUNIT_ASSERT_STRING_EQUAL(if_name, "lo", -1); - str = if_indextoname(3, if_name); - ICUNIT_ASSERT_EQUAL(str, nullptr, -1); - ICUNIT_ASSERT_EQUAL(errno, ENXIO, errno); - ret = if_nametoindex("eth1"); ICUNIT_ASSERT_EQUAL(ret, 0, ret); ICUNIT_ASSERT_EQUAL(errno, ENODEV, errno); diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_016.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_016.cpp index 652db1ed3861cf48a693d7efd26c1d68a9f53a85..c9d2d67ba0e2abfb1f81966bfe000b066754e35e 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_016.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_016.cpp @@ -32,6 +32,23 @@ static int GetServByNameTest(void) { + char serv_file[] = "echo 7/tcp\n" + "echo 7/udp\n" + "discard 9/tcp sink null\n" + "discard 9/udp sink null\n" + "systat 11/tcp users\n" + "ssh 22/tcp\n"; + + char *pathList[] = {"/etc/services"}; + char *streamList[] = {static_cast(serv_file)}; + int streamLen[] = {sizeof(serv_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + struct servent *se1 = nullptr; struct servent *se2 = nullptr; @@ -53,6 +70,7 @@ static int GetServByNameTest(void) se2 = getservbyname("systat", "udp"); ICUNIT_ASSERT_EQUAL(se2, nullptr, -1); + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_017.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_017.cpp index 81908c5599b68019384167b4febf7b5586bd3710..a3469ff691ba3252a865e845426b3f9128ce3bfc 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_017.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_017.cpp @@ -32,6 +32,17 @@ static int GetServByNameRTest(void) { + char serv_file[] = "ssh 22/tcp\n"; + char *pathList[] = {"/etc/services"}; + char *streamList[] = {static_cast(serv_file)}; + int streamLen[] = {sizeof(serv_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + struct servent se; struct servent *result = NULL; char buf1[1024] = { 0 }; @@ -58,6 +69,7 @@ static int GetServByNameRTest(void) ret = getservbyname_r("sh", "tcp", &se, buf1, sizeof buf1, &result); ICUNIT_ASSERT_EQUAL(ret, ENOENT, ret); + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_018.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_018.cpp index 3f816dc8d6f895bf7f510d8789512047d5b38b08..415fd0862ce2f886bd66143810e1b5d342fc9c76 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_018.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_018.cpp @@ -32,6 +32,28 @@ static int GetServEntTest(void) { + char serv_file[] = "tcpmux 1/tcp # TCP port service multiplexer\n" + "echo 7/tcp\n" + "echo 7/udp\n" + "discard 9/tcp sink null\n" + "ssh 100000/tcp\n" + "ssh /tcp\n" + "ssh 22/"; + char *pathList[] = {"/etc/services"}; + char *streamList[] = {static_cast(serv_file)}; + int streamLen[] = {sizeof(serv_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + + /* tcpmux,echo,discard port number is 1,7,9 */ + const int tcpmux_port_no = 1; + const int echo_port_no = 7; + const int discard_port_no = 9; + struct servent *se1 = nullptr; struct servent *se2 = nullptr; struct servent *se3 = nullptr; @@ -40,7 +62,7 @@ static int GetServEntTest(void) ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1); ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, "tcpmux", -1); ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, "tcp", -1); - ICUNIT_ASSERT_EQUAL(se1->s_port, ntohs(1), -1); + ICUNIT_ASSERT_EQUAL(se1->s_port, ntohs(tcpmux_port_no), -1); endservent(); se2 = getservent(); @@ -60,20 +82,28 @@ static int GetServEntTest(void) ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1); ICUNIT_ASSERT_STRING_EQUAL(se3->s_name, "echo", -1); ICUNIT_ASSERT_STRING_EQUAL(se3->s_proto, "tcp", -1); - ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(7), -1); + ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(echo_port_no), -1); se3 = getservent(); ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1); ICUNIT_ASSERT_STRING_EQUAL(se3->s_name, "echo", -1); ICUNIT_ASSERT_STRING_EQUAL(se3->s_proto, "udp", -1); - ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(7), -1); + ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(echo_port_no), -1); se3 = getservent(); ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1); ICUNIT_ASSERT_STRING_EQUAL(se3->s_name, "discard", -1); ICUNIT_ASSERT_STRING_EQUAL(se3->s_proto, "tcp", -1); - ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(9), -1); + ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(discard_port_no), -1); ICUNIT_ASSERT_STRING_EQUAL(se3->s_aliases[0], "sink", -1); + ICUNIT_ASSERT_STRING_EQUAL(se3->s_aliases[1], "null", -1); + ICUNIT_ASSERT_EQUAL(se3->s_aliases[2], nullptr, -1); + + se3 = getservent(); + ICUNIT_ASSERT_EQUAL(se3, nullptr, -1); + endservent(); + + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_019.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_019.cpp index 53c9b08a7b6564d6ce7fd9c275e5a4e6c43f35eb..70ad889aa988d6f6ede9a5cd034a0c00eb86ee4f 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_019.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_019.cpp @@ -29,21 +29,37 @@ */ #include "lt_net_netdb.h" +#include static int GetHostEntTest(void) { + char host_file[] = "127.0.0.1 localhost #localhost\n" + "::1 ip6-localhost\n" + "10.0.0.0 example example.com example.cn\n" + "10.0.0.0\n" + "10.0.0 example.com"; + char *pathList[] = {"/etc/hosts"}; + char *streamList[] = {static_cast(host_file)}; + int streamLen[] = {sizeof(host_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + struct hostent *se1 = nullptr; struct hostent *se2 = nullptr; struct hostent *se3 = nullptr; - int type = 2; - int length1 = 4; - int length2 = 16; + char addr[INET6_ADDRSTRLEN]; se1 = gethostent(); ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1); ICUNIT_ASSERT_STRING_EQUAL(se1->h_name, "localhost", -1); - ICUNIT_ASSERT_EQUAL(se1->h_addrtype, type, -1); - ICUNIT_ASSERT_EQUAL(se1->h_length, length1, -1); + ICUNIT_ASSERT_EQUAL(se1->h_addrtype, AF_INET, -1); + ICUNIT_ASSERT_EQUAL(se1->h_length, INADDRSZ, -1); + ICUNIT_ASSERT_STRING_EQUAL("127.0.0.1", inet_ntop(AF_INET, se1->h_addr_list[0], addr, INET_ADDRSTRLEN), -1); + ICUNIT_ASSERT_EQUAL(se1->h_aliases[0], nullptr, -1); endhostent(); se2 = gethostent(); @@ -62,15 +78,24 @@ static int GetHostEntTest(void) se3 = gethostent(); ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1); ICUNIT_ASSERT_EQUAL(se3->h_addrtype, AF_INET6, -1); - ICUNIT_ASSERT_STRING_EQUAL(se3->h_name, "localhost", -1); - ICUNIT_ASSERT_EQUAL(se3->h_length, length2, -1); + ICUNIT_ASSERT_STRING_EQUAL(se3->h_name, "ip6-localhost", -1); + ICUNIT_ASSERT_EQUAL(se3->h_length, IN6ADDRSZ, -1); + ICUNIT_ASSERT_STRING_EQUAL("::1", inet_ntop(AF_INET6, se3->h_addr_list[0], addr, INET6_ADDRSTRLEN), -1); se3 = gethostent(); ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1); ICUNIT_ASSERT_EQUAL(se3->h_addrtype, AF_INET, -1); - ICUNIT_ASSERT_STRING_EQUAL(se3->h_name, "szvphisprb93341", -1); - ICUNIT_ASSERT_STRING_EQUAL(se3->h_aliases[0], "szvphisprb93341.huawei.com", -1); + ICUNIT_ASSERT_STRING_EQUAL(se3->h_name, "example", -1); + ICUNIT_ASSERT_STRING_EQUAL("10.0.0.0", inet_ntop(AF_INET, se3->h_addr_list[0], addr, INET_ADDRSTRLEN), -1); + ICUNIT_ASSERT_STRING_EQUAL(se3->h_aliases[0], "example.com", -1); + ICUNIT_ASSERT_STRING_EQUAL(se3->h_aliases[1], "example.cn", -1); + ICUNIT_ASSERT_EQUAL(se3->h_aliases[2], nullptr, -1); + + se3 = gethostent(); + ICUNIT_ASSERT_EQUAL(se3, nullptr, -1); + endhostent(); + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_020.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_020.cpp index a08a22f0770df200f9055e890383c66226f72023..5cad29c666859333ea88b87407ea3773356c95e0 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_020.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_020.cpp @@ -32,6 +32,20 @@ static int GetNetEntTest(void) { + char network_file[] = "# symbolic names for networks, see networks(5) for more information\n" + "link-local 169.254.0.0\n" + "example 192.168.1.0 network example-network\n" + "test1"; + char *pathList[] = {"/etc/networks"}; + char *streamList[] = {static_cast(network_file)}; + int streamLen[] = {sizeof(network_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + struct netent *se1 = nullptr; struct netent *se2 = nullptr; struct netent *se3 = nullptr; @@ -39,8 +53,9 @@ static int GetNetEntTest(void) se1 = getnetent(); ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1); ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, "link-local", -1); - ICUNIT_ASSERT_EQUAL(se1->n_addrtype, 2, -1); + ICUNIT_ASSERT_EQUAL(se1->n_addrtype, AF_INET, -1); ICUNIT_ASSERT_EQUAL(se1->n_net, inet_network("169.254.0.0"), -1); + ICUNIT_ASSERT_EQUAL(se1->n_aliases[0], nullptr, -1); endnetent(); se2 = getnetent(); @@ -56,6 +71,20 @@ static int GetNetEntTest(void) ICUNIT_ASSERT_EQUAL(se1->n_addrtype, se3->n_addrtype, -1); ICUNIT_ASSERT_EQUAL(se1->n_net, se3->n_net, -1); + se1 = getnetent(); + ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, "example", -1); + ICUNIT_ASSERT_EQUAL(se1->n_addrtype, AF_INET, -1); + ICUNIT_ASSERT_EQUAL(se1->n_net, inet_network("192.168.1.0"), -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->n_aliases[0], "network", -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->n_aliases[1], "example-network", -1); + ICUNIT_ASSERT_EQUAL(se1->n_aliases[2], nullptr, -1); + + se1 = getnetent(); + ICUNIT_ASSERT_EQUAL(se1, nullptr, -1); + endnetent(); + + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_021.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_021.cpp index 0e4254b3932ac15f69be598520d3a30932a7f577..9b784d514d8d8cd902e864d4e1a37a18a3a1b91a 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_021.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_021.cpp @@ -32,6 +32,18 @@ static int GetNetBynametTest(void) { + char network_file[] = "# symbolic names for networks, see networks(5) for more information\n" + "link-local 169.254.0.0\n"; + char *pathList[] = {"/etc/networks"}; + char *streamList[] = {static_cast(network_file)}; + int streamLen[] = {sizeof(network_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + struct netent *se1 = nullptr; struct netent *se2 = nullptr; struct netent *se3 = nullptr; @@ -39,7 +51,7 @@ static int GetNetBynametTest(void) se1 = getnetbyname("link-local"); ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1); ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, "link-local", -1); - ICUNIT_ASSERT_EQUAL(se1->n_addrtype, 2, -1); + ICUNIT_ASSERT_EQUAL(se1->n_addrtype, AF_INET, -1); ICUNIT_ASSERT_EQUAL(se1->n_net, inet_network("169.254.0.0"), -1); se2 = getnetbyname("link"); @@ -48,6 +60,7 @@ static int GetNetBynametTest(void) se3 = getnetbyname("hs"); ICUNIT_ASSERT_EQUAL(se3, nullptr, -1); + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/full/net_netdb_test_022.cpp b/testsuites/unittest/net/netdb/full/net_netdb_test_022.cpp index 3bf0f9b11a39c0e4325073e8486458d18d3474a6..b551318ec147d528ee64cd410e7b5fdea2882b28 100644 --- a/testsuites/unittest/net/netdb/full/net_netdb_test_022.cpp +++ b/testsuites/unittest/net/netdb/full/net_netdb_test_022.cpp @@ -32,6 +32,18 @@ static int GetNetByAddrtTest(void) { + char network_file[] = "# symbolic names for networks, see networks(5) for more information\n" + "link-local 169.254.0.0\n"; + char *pathList[] = {"/etc/networks"}; + char *streamList[] = {static_cast(network_file)}; + int streamLen[] = {sizeof(network_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + struct netent *se1 = nullptr; struct netent *se2 = nullptr; struct netent *se3 = nullptr; @@ -39,7 +51,7 @@ static int GetNetByAddrtTest(void) se1 = getnetbyaddr(inet_network("169.254.0.0"), AF_INET); ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1); ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, "link-local", -1); - ICUNIT_ASSERT_EQUAL(se1->n_addrtype, 2, -1); + ICUNIT_ASSERT_EQUAL(se1->n_addrtype, AF_INET, -1); ICUNIT_ASSERT_EQUAL(se1->n_net, inet_network("169.254.0.0"), -1); se2 = getnetbyaddr(inet_network("169.254.0.1"), AF_INET); @@ -48,6 +60,7 @@ static int GetNetByAddrtTest(void) se3 = getnetbyaddr(inet_network("169.254.0.1"), AF_INET6); ICUNIT_ASSERT_EQUAL(se3, nullptr, -1); + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/netdb/lt_net_netdb.h b/testsuites/unittest/net/netdb/lt_net_netdb.h index d60a83cc7e19d6dd0efb61bde09cf8ac5428f234..1ef994ee680f47a8d8d0250bfce510602806007d 100644 --- a/testsuites/unittest/net/netdb/lt_net_netdb.h +++ b/testsuites/unittest/net/netdb/lt_net_netdb.h @@ -52,7 +52,6 @@ void NetNetDbTest010(void); void NetNetDbTest011(void); void NetNetDbTest012(void); void NetNetDbTest013(void); -void NetNetDbTest014(void); void NetNetDbTest015(void); void NetNetDbTest016(void); void NetNetDbTest017(void); diff --git a/testsuites/unittest/net/netdb/net_netdb_test.cpp b/testsuites/unittest/net/netdb/net_netdb_test.cpp index 84761e3f0635f969a4327ab3e099a1e9a716a1d9..aa0e2dea111f9306c6a45694f98f46c48a8d074b 100644 --- a/testsuites/unittest/net/netdb/net_netdb_test.cpp +++ b/testsuites/unittest/net/netdb/net_netdb_test.cpp @@ -72,5 +72,206 @@ HWTEST_F(NetDbTest, NetNetDbTest013, TestSize.Level0) NetNetDbTest013(); } +#endif + +#if defined(LOSCFG_USER_TEST_FULL) +/* * + * @tc.name: NetNetDbTest018 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest018, TestSize.Level0) +{ + NetNetDbTest018(); +} + +/* * + * @tc.name: NetNetDbTest002 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest002, TestSize.Level0) +{ + NetNetDbTest002(); +} + +/* * + * @tc.name: NetNetDbTest003 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest003, TestSize.Level0) +{ + NetNetDbTest003(); +} + +/* * + * @tc.name: NetNetDbTest004 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest004, TestSize.Level0) +{ + NetNetDbTest004(); +} + +/* * + * @tc.name: NetNetDbTest006 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest006, TestSize.Level0) +{ + NetNetDbTest006(); +} + +/* * + * @tc.name: NetNetDbTest007 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest007, TestSize.Level0) +{ + NetNetDbTest007(); +} + +/* * + * @tc.name: NetNetDbTest008 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest008, TestSize.Level0) +{ + NetNetDbTest008(); +} + +/* * + * @tc.name: NetNetDbTest009 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest009, TestSize.Level0) +{ + NetNetDbTest009(); +} + +/* * + * @tc.name: NetNetDbTest010 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest010, TestSize.Level0) +{ + NetNetDbTest010(); +} + +/* * + * @tc.name: NetNetDbTest011 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest011, TestSize.Level0) +{ + NetNetDbTest011(); +} + +/* * + * @tc.name: NetNetDbTest012 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest012, TestSize.Level0) +{ + NetNetDbTest012(); +} + +/* * + * @tc.name: NetNetDbTest015 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest015, TestSize.Level0) +{ + NetNetDbTest015(); +} + +/* * + * @tc.name: NetNetDbTest016 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest016, TestSize.Level0) +{ + NetNetDbTest016(); +} + +/* * + * @tc.name: NetNetDbTest017 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest017, TestSize.Level0) +{ + NetNetDbTest017(); +} + +/* * + * @tc.name: NetNetDbTest019 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest019, TestSize.Level0) +{ + NetNetDbTest019(); +} + +/* * + * @tc.name: NetNetDbTest020 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest020, TestSize.Level0) +{ + NetNetDbTest020(); +} + +/* * + * @tc.name: NetNetDbTest021 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest021, TestSize.Level0) +{ + NetNetDbTest021(); +} + +/* * + * @tc.name: NetNetDbTest022 + * @tc.desc: function for NetDbTest + * @tc.type: FUNC + * @tc.require: AR000EEMQ9 + */ +HWTEST_F(NetDbTest, NetNetDbTest022, TestSize.Level0) +{ + NetNetDbTest022(); +} + #endif } diff --git a/testsuites/unittest/net/resolv/full/net_resolv_test_004.cpp b/testsuites/unittest/net/resolv/full/net_resolv_test_004.cpp index e8cb672afd1a8ab6f5f12d303e57d718cc3eca8f..ea7b2ed748b0b390078873f7c97a1f23aeb42017 100644 --- a/testsuites/unittest/net/resolv/full/net_resolv_test_004.cpp +++ b/testsuites/unittest/net/resolv/full/net_resolv_test_004.cpp @@ -34,6 +34,17 @@ static int EtherHosttonTest(void) { // suppose "0:0:0:0:0:0 localhost" in `/etc/ethers' file. + char ether_file[] = "0:0:0:0:0:0 localhost"; + char *pathList[] = {"/etc/ethers"}; + char *streamList[] = {static_cast(ether_file)}; + int streamLen[] = {sizeof(ether_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + struct ether_addr addr, *eaddr = &addr; int ret = ether_hostton("localhost", eaddr); @@ -45,6 +56,7 @@ static int EtherHosttonTest(void) ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[4], 0x00, eaddr->ether_addr_octet[4]); ICUNIT_ASSERT_EQUAL(eaddr->ether_addr_octet[5], 0x00, eaddr->ether_addr_octet[5]); + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } diff --git a/testsuites/unittest/net/resolv/full/net_resolv_test_008.cpp b/testsuites/unittest/net/resolv/full/net_resolv_test_008.cpp index baf77fbefb73b16a3158392137121ba2469bace9..2fe4a3039811892a34d94069290850ce577e1ae8 100644 --- a/testsuites/unittest/net/resolv/full/net_resolv_test_008.cpp +++ b/testsuites/unittest/net/resolv/full/net_resolv_test_008.cpp @@ -35,6 +35,17 @@ static int EtherNtohostTest(void) { // suppose "0:0:0:0:0:0 localhost" in `/etc/ethers' file. + char ether_file[] = "0:0:0:0:0:0 localhost"; + char *pathList[] = {"/etc/ethers"}; + char *streamList[] = {static_cast(ether_file)}; + int streamLen[] = {sizeof(ether_file)}; + const int file_number = 1; + int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number); + if (flag != 0) { + RecoveryFileEnv(pathList, file_number); + return -1; + } + struct ether_addr addr = {{0,0,0,0,0,0}}, *eaddr = &addr; char buf[100]; int ret = ether_ntohost(buf, eaddr); @@ -42,6 +53,7 @@ static int EtherNtohostTest(void) ICUNIT_ASSERT_EQUAL(ret, 0, -1); ICUNIT_ASSERT_EQUAL(strcmp("localhost", buf), 0, -1); + RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; }