From 1b8e680a1bd10ba4a9b87e14637269dd648f2c4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=A2=A2=E7=BF=94?= <3579645915@qq.com> Date: Mon, 23 Dec 2024 19:45:34 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BF=AE=E6=94=B91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/netdb/full/net_netdb_test_002.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_002.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_002.cpp index 2e29594..315ba98 100644 --- a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_002.cpp +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_002.cpp @@ -64,6 +64,38 @@ static int AddrInfoTest(void) RecoveryFileEnv(pathList, file_number); return ICUNIT_SUCCESS; } +/* +以下是对这段代码的批注: + +**一、整体功能** +1. 这段代码主要是对`getaddrinfo`函数以及相关的网络地址信息查询功能进行测试。代码首先创建了一些测试文件的内容(`host_file`和`serv_file`),并将其准备到特定的文件环境(`/etc/hosts`和`/etc/services`)中,然后进行了一系列`getaddrinfo`调用并检查返回值,最后清理测试文件环境。 + +**二、代码结构与细节** + +1. **文件内容准备部分** + - 在`AddrInfoTest`函数中,定义了两个字符数组`host_file`和`serv_file`,分别用于模拟`/etc/hosts`和`/etc/services`文件中的部分内容。 + - 使用`PrepareFileEnv`函数(假设这是一个自定义函数)来将模拟的文件内容写入到实际的文件环境中。如果这个函数返回非零值,表示准备文件环境失败,此时会调用`RecoveryFileEnv`(也是自定义函数)来恢复文件环境并返回 -1。 + - 这里硬编码文件内容和路径可能在某些情况下不够灵活,如果需要在不同环境下测试,可能需要更动态的方式来指定文件内容和路径。 + +2. **`getaddrinfo`测试部分** + - 第一次调用`getaddrinfo`: + - 调用`getaddrinfo("example.com", "ftp", NULL, &addr);`是一个正常的查询操作,查询域名`example.com`对应的`ftp`服务的地址信息。使用`ICUNIT_ASSERT_EQUAL`(假设是自定义的断言宏)来验证返回值是否为0,表示查询成功。如果成功,应该释放`addrinfo`结构的内存,这里使用`freeaddrinfo(addr)`正确地释放了内存。 + - 第二次调用`getaddrinfo`: + - 调用`getaddrinfo("local", "ftp", NULL, &addr);`,这里`"local"`可能是一个无效的主机名(根据实际情况),预期返回`EAI_AGAIN`,并使用断言宏进行验证。 + - 第三次调用`getaddrinfo`: + - 调用`getaddrinfo("localhost", "fp", NULL, &addr);`,这里服务名`"fp"`可能是错误的(应该是`"ftp"`),预期返回`EAI_SERVICE`,同样使用断言宏进行验证。 + - 调用`gai_strerror(EAI_AGAIN)`获取错误信息字符串,并使用`ICUNIT_ASSERT_NOT_EQUAL`断言该字符串不为`NULL`。这是一个合理的检查,确保错误信息能够正确获取。 + +3. **文件环境恢复部分** + - 最后,无论测试过程中是否发生错误,都会调用`RecoveryFileEnv`函数来清理测试文件环境,这是一种良好的测试实践,确保测试不会对外部环境造成持久的影响。 + +**三、可能的改进点** +1. **错误处理和日志记录** + - 除了使用断言来验证结果外,可以添加更详细的错误处理和日志记录功能。例如,如果`PrepareFileEnv`或`RecoveryFileEnv`失败,可以记录更详细的错误信息,以便在测试失败时更容易定位问题。 +2. **可移植性** + - 硬编码的文件路径(`/etc/hosts`和`/etc/services`)可能在不同的操作系统或环境下不适用。可以考虑使这些路径可配置或者使用操作系统提供的标准方法来获取这些文件的位置。 +3. **测试用例的完整性** + - 可以增加更多的测试用例,例如测试不同的`hints`参数对`getaddrinfo`的影响,或者测试更多的有效和无效的主机名和服务名组合。 void NetNetDbTest002(void) { From 7b80ec07b2776878d783dbb152048f3cdc963e21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=A2=A2=E7=BF=94?= <3579645915@qq.com> Date: Mon, 23 Dec 2024 19:48:14 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/netdb/full/net_netdb_test_012.cpp | 80 +++++++++++++ .../net/netdb/full/net_netdb_test_015.cpp | 57 +++++++++ .../net/netdb/full/net_netdb_test_016.cpp | 80 +++++++++++++ .../net/netdb/full/net_netdb_test_017.cpp | 79 ++++++++++++ .../net/netdb/full/net_netdb_test_018.cpp | 113 ++++++++++++++++++ .../net/netdb/full/net_netdb_test_019.cpp | 105 ++++++++++++++++ .../net/netdb/full/net_netdb_test_020.cpp | 94 +++++++++++++++ .../net/netdb/full/net_netdb_test_021.cpp | 70 +++++++++++ .../net/netdb/full/net_netdb_test_022.cpp | 70 +++++++++++ 9 files changed, 748 insertions(+) create mode 100644 kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_012.cpp create mode 100644 kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_015.cpp create mode 100644 kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_016.cpp create mode 100644 kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_017.cpp create mode 100644 kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_018.cpp create mode 100644 kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_019.cpp create mode 100644 kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_020.cpp create mode 100644 kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_021.cpp create mode 100644 kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_022.cpp diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_012.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_012.cpp new file mode 100644 index 0000000..503948f --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_012.cpp @@ -0,0 +1,80 @@ +/* + * 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" + +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]; + + 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, "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, "tcp", -1); + ICUNIT_ASSERT_STRING_EQUAL(result->s_aliases[0], "ssh", -1); + + 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(test_port_no), "ud", &se, buf, sizeof buf, &result); + ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret); + + RecoveryFileEnv(pathList, file_number); + return ICUNIT_SUCCESS; +} + +void NetNetDbTest012(void) +{ + TEST_ADD_CASE(__FUNCTION__, GetServByPortRTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_015.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_015.cpp new file mode 100644 index 0000000..612b157 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_015.cpp @@ -0,0 +1,57 @@ +/* + * opyright (c) 2021-2021, 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 + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "lt_net_netdb.h" +#include + +static int IfNameToIndexTest(void) +{ + int ret; + char if_name[20]; + char *str = nullptr; + + ret = if_nametoindex("lo"); + ICUNIT_ASSERT_NOT_EQUAL(ret, 0, -1); + + str = if_indextoname(ret, if_name); + ICUNIT_ASSERT_NOT_EQUAL(str, nullptr, -1); + ICUNIT_ASSERT_STRING_EQUAL(if_name, "lo", -1); + + ret = if_nametoindex("eth1"); + ICUNIT_ASSERT_EQUAL(ret, 0, ret); + ICUNIT_ASSERT_EQUAL(errno, ENODEV, errno); + + return ICUNIT_SUCCESS; +} + +void NetNetDbTest015(void) +{ + TEST_ADD_CASE(__FUNCTION__, IfNameToIndexTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_016.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_016.cpp new file mode 100644 index 0000000..c9d2d67 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_016.cpp @@ -0,0 +1,80 @@ +/* + * opyright (c) 2021-2021, 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 + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "lt_net_netdb.h" + +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; + + se1 = getservbyname("discard", "tcp"); + ICUNIT_ASSERT_NOT_EQUAL(se1, NULL, -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, "discard", -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, "tcp", -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->s_aliases[0], "discard", -1); + + se1 = getservbyname("ssh", "tcp"); + ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, "ssh", -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, "tcp", -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->s_aliases[0], "ssh", -1); + + se2 = getservbyname("cho", "udp"); + ICUNIT_ASSERT_EQUAL(se2, nullptr, -1); + + se2 = getservbyname("systat", "udp"); + ICUNIT_ASSERT_EQUAL(se2, nullptr, -1); + + RecoveryFileEnv(pathList, file_number); + return ICUNIT_SUCCESS; +} + +void NetNetDbTest016(void) +{ + TEST_ADD_CASE(__FUNCTION__, GetServByNameTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_017.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_017.cpp new file mode 100644 index 0000000..a3469ff --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_017.cpp @@ -0,0 +1,79 @@ +/* + * opyright (c) 2021-2021, 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 + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "lt_net_netdb.h" + +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 }; + char buf2[2] = { 0 }; + int ret; + + errno = 0; + ret = getservbyname_r("ssh", "tcp", &se, buf1, sizeof buf1, &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, "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, "tcp", -1); + ICUNIT_ASSERT_STRING_EQUAL(result->s_aliases[0], "ssh", -1); + + ret = getservbyname_r("ssh", "tcp", &se, buf2, sizeof buf2, &result); + ICUNIT_ASSERT_EQUAL(ret, ERANGE, ret); + + ret = getservbyname_r("ssh", "tp", &se, buf1, sizeof buf1, &result); + ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret); + + ret = getservbyname_r("sh", "tcp", &se, buf1, sizeof buf1, &result); + ICUNIT_ASSERT_EQUAL(ret, ENOENT, ret); + + RecoveryFileEnv(pathList, file_number); + return ICUNIT_SUCCESS; +} + +void NetNetDbTest017(void) +{ + TEST_ADD_CASE(__FUNCTION__, GetServByNameRTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_018.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_018.cpp new file mode 100644 index 0000000..415fd08 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_018.cpp @@ -0,0 +1,113 @@ +/* + * opyright (c) 2021-2021, 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 + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "lt_net_netdb.h" + +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; + + se1 = getservent(); + 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(tcpmux_port_no), -1); + + endservent(); + se2 = getservent(); + ICUNIT_ASSERT_NOT_EQUAL(se2, nullptr, -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, se2->s_name, -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, se2->s_proto, -1); + ICUNIT_ASSERT_EQUAL(se1->s_port, se2->s_port, -1); + + setservent(0); + se3 = getservent(); + ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, se3->s_name, -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, se3->s_proto, -1); + ICUNIT_ASSERT_EQUAL(se1->s_port, se3->s_port, -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, "tcp", -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(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(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; +} + +void NetNetDbTest018(void) +{ + TEST_ADD_CASE(__FUNCTION__, GetServEntTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_019.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_019.cpp new file mode 100644 index 0000000..70ad889 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_019.cpp @@ -0,0 +1,105 @@ +/* + * opyright (c) 2021-2021, 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 + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#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; + 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, 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(); + ICUNIT_ASSERT_NOT_EQUAL(se2, nullptr, -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->h_name, se2->h_name, -1); + ICUNIT_ASSERT_EQUAL(se1->h_addrtype, se2->h_addrtype, -1); + ICUNIT_ASSERT_EQUAL(se1->h_length, se2->h_length, -1); + + sethostent(0); + se3 = gethostent(); + ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->h_name, se3->h_name, -1); + ICUNIT_ASSERT_EQUAL(se1->h_addrtype, se3->h_addrtype, -1); + ICUNIT_ASSERT_EQUAL(se1->h_length, se3->h_length, -1); + + 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, "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, "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; +} + +void NetNetDbTest019(void) +{ + TEST_ADD_CASE(__FUNCTION__, GetHostEntTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_020.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_020.cpp new file mode 100644 index 0000000..5cad29c --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_020.cpp @@ -0,0 +1,94 @@ +/* + * opyright (c) 2021-2021, 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 + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "lt_net_netdb.h" + +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; + + 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, 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(); + ICUNIT_ASSERT_NOT_EQUAL(se2, nullptr, -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, se2->n_name, -1); + ICUNIT_ASSERT_EQUAL(se1->n_addrtype, se2->n_addrtype, -1); + ICUNIT_ASSERT_EQUAL(se1->n_net, se2->n_net, -1); + + setnetent(0); + se3 = getnetent(); + ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1); + ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, se3->n_name, -1); + 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; +} + +void NetNetDbTest020(void) +{ + TEST_ADD_CASE(__FUNCTION__, GetNetEntTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_021.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_021.cpp new file mode 100644 index 0000000..9b784d5 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_021.cpp @@ -0,0 +1,70 @@ +/* + * opyright (c) 2021-2021, 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 + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "lt_net_netdb.h" + +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; + + 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, AF_INET, -1); + ICUNIT_ASSERT_EQUAL(se1->n_net, inet_network("169.254.0.0"), -1); + + se2 = getnetbyname("link"); + ICUNIT_ASSERT_EQUAL(se2, nullptr, -1); + + se3 = getnetbyname("hs"); + ICUNIT_ASSERT_EQUAL(se3, nullptr, -1); + + RecoveryFileEnv(pathList, file_number); + return ICUNIT_SUCCESS; +} + +void NetNetDbTest021(void) +{ + TEST_ADD_CASE(__FUNCTION__, GetNetBynametTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_022.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_022.cpp new file mode 100644 index 0000000..b551318 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/full/net_netdb_test_022.cpp @@ -0,0 +1,70 @@ +/* + * opyright (c) 2021-2021, 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 + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "lt_net_netdb.h" + +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; + + 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, 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); + ICUNIT_ASSERT_EQUAL(se2, nullptr, -1); + + se3 = getnetbyaddr(inet_network("169.254.0.1"), AF_INET6); + ICUNIT_ASSERT_EQUAL(se3, nullptr, -1); + + RecoveryFileEnv(pathList, file_number); + return ICUNIT_SUCCESS; +} + +void NetNetDbTest022(void) +{ + TEST_ADD_CASE(__FUNCTION__, GetNetByAddrtTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} From fd0ab82e141b1784130152ce69e646fafdc52dff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=A2=A2=E7=BF=94?= <3579645915@qq.com> Date: Mon, 23 Dec 2024 19:48:43 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/netdb/smoke/net_netdb_test_001.cpp | 58 +++++++++++++++++++ .../net/netdb/smoke/net_netdb_test_013.cpp | 48 +++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 kernel_liteos_a-master/testsuites/unittest/net/netdb/smoke/net_netdb_test_001.cpp create mode 100644 kernel_liteos_a-master/testsuites/unittest/net/netdb/smoke/net_netdb_test_013.cpp diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/smoke/net_netdb_test_001.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/smoke/net_netdb_test_001.cpp new file mode 100644 index 0000000..1e0b53a --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/smoke/net_netdb_test_001.cpp @@ -0,0 +1,58 @@ +/* + * 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" + +static int ProtoentTest(void) +{ + setprotoent(1); + + // refer to the `/etc/protocols' file. + struct protoent *prot = getprotobyname("icmp"); + ICUNIT_ASSERT_NOT_EQUAL(prot, NULL, -1); + ICUNIT_ASSERT_EQUAL(prot->p_proto, 1, prot->p_proto); + + prot = getprotobynumber(1); + ICUNIT_ASSERT_NOT_EQUAL(prot, NULL, -1); + ICUNIT_ASSERT_EQUAL(strcmp(prot->p_name, "icmp"), 0, -1); + + prot = getprotoent(); + ICUNIT_ASSERT_NOT_EQUAL(prot, NULL, -1); + + endprotoent(); + return ICUNIT_SUCCESS; +} + +void NetNetDbTest001(void) +{ + TEST_ADD_CASE(__FUNCTION__, ProtoentTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +} diff --git a/kernel_liteos_a-master/testsuites/unittest/net/netdb/smoke/net_netdb_test_013.cpp b/kernel_liteos_a-master/testsuites/unittest/net/netdb/smoke/net_netdb_test_013.cpp new file mode 100644 index 0000000..b713849 --- /dev/null +++ b/kernel_liteos_a-master/testsuites/unittest/net/netdb/smoke/net_netdb_test_013.cpp @@ -0,0 +1,48 @@ +/* + * 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" + +static int HerrorTest(void) +{ + const char *err = hstrerror(TRY_AGAIN); + ICUNIT_ASSERT_NOT_EQUAL(err, NULL, -1); + + herror(err); + + return ICUNIT_SUCCESS; +} + +void NetNetDbTest013(void) +{ + TEST_ADD_CASE(__FUNCTION__, HerrorTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION); +}