Lvwenxuan 7 months ago
commit feebadef06

@ -32,35 +32,53 @@
#include "lt_net_netdb.h"
// 测试函数AddrInfoTest的定义
static int AddrInfoTest(void)
{
// Prerequisite: correct DNS servers must be configured.
// 假设DNS服务器配置正确
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;
}
// 声明addrinfo结构体指针
struct addrinfo *addr = NULL;
// 使用getaddrinfo函数解析域名和端口
int ret = getaddrinfo("example.com", "ftp", NULL, &addr);
// 断言getaddrinfo函数返回0表示成功
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
// 释放addrinfo结构体
freeaddrinfo(addr);
// 尝试解析一个不存在的域名和端口
ret = getaddrinfo("local", "ftp", NULL, &addr);
// 断言getaddrinfo函数返回EAI_AGAIN表示临时失败
ICUNIT_ASSERT_EQUAL(ret, EAI_AGAIN, ret);
// 尝试解析一个不存在的服务名称
ret = getaddrinfo("localhost", "fp", NULL, &addr);
// 断言getaddrinfo函数返回EAI_SERVICE表示服务名称错误
ICUNIT_ASSERT_EQUAL(ret, EAI_SERVICE, ret);
// 获取错误信息字符串
const char *p = gai_strerror(EAI_AGAIN);
// 断言错误信息字符串不为空
ICUNIT_ASSERT_NOT_EQUAL(p, NULL, -1);
// 恢复文件环境
RecoveryFileEnv(pathList, file_number);
return ICUNIT_SUCCESS;
}
@ -97,7 +115,9 @@ static int AddrInfoTest(void)
3. ****
- `hints``getaddrinfo`
// 测试用例NetNetDbTest002的定义
void NetNetDbTest002(void)
{
// 添加测试用例
TEST_ADD_CASE(__FUNCTION__, AddrInfoTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}

@ -28,24 +28,31 @@
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//123123
#include "lt_net_netdb.h"
// 测试函数IfAddrsTest的定义
static int IfAddrsTest(void)
{
// Prerequisite: correct DNS servers must be configured.
// 假设DNS服务器配置正确
struct ifaddrs *addr = NULL;
// 使用getifaddrs函数获取网络接口地址信息
int ret = getifaddrs(&addr);
// 断言getifaddrs函数返回0表示成功
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
// 断言返回的地址信息不为空
ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1);
// 释放ifaddrs结构体
freeifaddrs(addr);
return ICUNIT_SUCCESS;
}
// 测试用例NetNetDbTest003的定义
void NetNetDbTest003(void)
{
// 添加测试用例
TEST_ADD_CASE(__FUNCTION__, IfAddrsTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}

@ -2,74 +2,74 @@
* 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 "lt_net_netdb.h"
// 测试函数用于测试gethostbyaddr函数
static int GetHostByAddrTest(void)
{
// 定义一个模拟的hosts文件内容
char host_file[] = "127.0.0.1 localhost\n100.0.0.0 example.com example\n";
// 定义hosts文件的路径列表
char *pathList[] = {"/etc/hosts"};
// 定义模拟的hosts文件流列表
char *streamList[] = {host_file};
// 定义模拟的hosts文件流长度列表
int streamLen[] = {sizeof(host_file)};
// 定义文件数量
const int file_number = 1;
// 准备文件环境将模拟的hosts文件内容设置为系统的hosts文件
int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
// 如果准备失败,恢复原始文件环境
if (flag != 0) {
RecoveryFileEnv(pathList, file_number);
return -1;
return -1; // 返回错误码
}
// 定义in_addr结构体用于存储IP地址
struct in_addr ia;
int length = 4; // the address length is 4;
// 定义地址长度
int length = 4; // IPv4地址长度为4
// 将点分十进制IP地址转换为网络字节序的整数值
ia.s_addr = inet_addr("127.0.0.1");
// 使用gethostbyaddr获取hostent结构体指针
struct hostent *addr = gethostbyaddr(&ia, sizeof ia, AF_INET);
// 断言addr不为空即gethostbyaddr成功
ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1);
// 断言返回的地址类型为AF_INET
ICUNIT_ASSERT_EQUAL(addr->h_addrtype, AF_INET, addr->h_addrtype);
// 断言返回的主机名为"localhost"
ICUNIT_ASSERT_STRING_EQUAL(addr->h_name, "localhost", -1);
// 断言返回的地址长度为4
ICUNIT_ASSERT_EQUAL(addr->h_length, length, -1);
// 测试另一个IP地址
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, "example.com", -1);
// 测试一个不存在的IP地址
errno = 0;
ia.s_addr = inet_addr("127.0.0.0");
addr = gethostbyaddr(&ia, sizeof ia, AF_INET);
// 断言errno为ENOENT表示没有找到对应的主机名
ICUNIT_ASSERT_EQUAL(errno, ENOENT, errno);
// 恢复原始文件环境
RecoveryFileEnv(pathList, file_number);
return ICUNIT_SUCCESS;
return ICUNIT_SUCCESS; // 返回成功码
}
// 定义测试用例
void NetNetDbTest004(void)
{
// 添加测试用例,指定测试函数、测试类型、测试级别等信息
TEST_ADD_CASE(__FUNCTION__, GetHostByAddrTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}
}

@ -28,11 +28,14 @@
* 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 GetHostByAddrRTest(void)
{
// 定义一个静态函数用于测试gethostbyaddr_r函数。
struct in_addr ia;
struct hostent addr, *result = NULL;
char buf[1024];
@ -40,29 +43,47 @@ static int GetHostByAddrRTest(void)
int err, ret;
int length = 4;
// 定义了一些变量包括用于存储IP地址的in_addr结构体hostent结构体用于存储查询结果以及其他辅助变量。
ia.s_addr = inet_addr("127.0.0.1");
// 将字符串形式的IP地址"127.0.0.1"转换为网络字节序的二进制形式并存储在ia结构体中。
ret = gethostbyaddr_r(&ia, sizeof ia, AF_INET, &addr, buf, sizeof buf, &result, &err);
// 使用gethostbyaddr_r函数根据IP地址查询主机信息并将结果存储在result指向的hostent结构体中。
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
// 断言函数返回值应为0表示成功。
ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1);
// 断言result不应为空表示查询到了主机信息。
ICUNIT_ASSERT_EQUAL(result->h_addrtype, AF_INET, result->h_addrtype);
// 断言查询到的主机地址类型应为AF_INETIPv4
ICUNIT_ASSERT_STRING_EQUAL(result->h_name, "localhost", -1);
// 断言查询到的主机名应为"localhost"。
ICUNIT_ASSERT_EQUAL(result->h_length, length, -1);
// 断言查询到的主机地址长度应为4IPv4地址长度
errno = 0;
// 清空errno用于检测接下来的函数调用是否设置了errno。
ia.s_addr = inet_addr("127.0.0.0");
// 将字符串形式的IP地址"127.0.0.0"转换为网络字节序的二进制形式并存储在ia结构体中。
ret = gethostbyaddr_r(&ia, sizeof ia, AF_INET, &addr, buf, sizeof buf, &result, &err);
// 再次调用gethostbyaddr_r函数但这次传入的是一个无效的IP地址。
ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
// 断言errno应被设置为EINVAL表示无效参数。
ret = gethostbyaddr_r(&ia, sizeof ia, AF_INET, &addr, buf, sizeof buf, &result, &err);
// 再次调用函数但errno已经被之前的调用设置这里检查是否保持不变。
ICUNIT_ASSERT_EQUAL(errno, EINVAL, errno);
// 断言errno仍然为EINVAL。
ret = gethostbyaddr_r(&ia, sizeof ia, AF_INET, &addr, buf1, sizeof buf1, &result, &err);
// 调用gethostbyaddr_r函数但这次传入的缓冲区buf1大小不足以存储结果。
ICUNIT_ASSERT_EQUAL(ret, ERANGE, ret);
// 断言函数返回值应为ERANGE表示缓冲区太小。
return ICUNIT_SUCCESS;
// 返回测试成功。
}
void NetNetDbTest005(void)
{
TEST_ADD_CASE(__FUNCTION__, GetHostByAddrRTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}
// 将当前测试函数添加到测试用例中,指定测试类型、协议、级别和函数。
}

@ -28,42 +28,60 @@
* 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 GetHostByNameTest(void)
{
// 定义一个模拟的hosts文件内容
char host_file[] = "127.0.0.1 localhost\n";
// 定义hosts文件的路径列表
char *pathList[] = {"/etc/hosts"};
// 定义模拟的hosts文件流列表
char *streamList[] = {host_file};
// 定义模拟的hosts文件流长度列表
int streamLen[] = {sizeof(host_file)};
// 定义文件数量
int flag = PrepareFileEnv(pathList, streamList, streamLen, 1);
// 如果准备文件环境失败,则恢复原始文件环境并返回错误码
if (flag != 0) {
RecoveryFileEnv(pathList, 1);
return -1;
}
// 使用gethostbyname函数根据主机名获取hostent结构体指针
struct hostent *addr = gethostbyname("localhost");
// 断言addr不为空即gethostbyname成功
ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1);
// 断言返回的主机名与输入的主机名相同
ICUNIT_ASSERT_EQUAL(strcmp(addr->h_name, "localhost"), 0, -1);
// 断言返回的地址类型为AF_INET
ICUNIT_ASSERT_EQUAL(addr->h_addrtype, AF_INET, addr->h_addrtype);
// 断言返回的地址长度不为0
ICUNIT_ASSERT_NOT_EQUAL(addr->h_length, 0, addr->h_length);
// 测试gethostbyname函数传入IP地址字符串作为主机名
addr = gethostbyname("127.0.0.1");
ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1);
ICUNIT_ASSERT_EQUAL(strcmp(addr->h_name, "127.0.0.1"), 0, -1);
ICUNIT_ASSERT_EQUAL(addr->h_addrtype, AF_INET, addr->h_addrtype);
ICUNIT_ASSERT_NOT_EQUAL(addr->h_length, 0, addr->h_length);
// 测试一个不存在的主机名
addr = gethostbyname("lo");
// 断言addr为空即gethostbyname失败
ICUNIT_ASSERT_EQUAL(addr, NULL, -1);
// 恢复原始文件环境
RecoveryFileEnv(pathList, 1);
return ICUNIT_SUCCESS;
}
// 定义测试用例
void NetNetDbTest006(void)
{
// 添加测试用例,指定测试函数、测试类型、测试级别等信息
TEST_ADD_CASE(__FUNCTION__, GetHostByNameTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}
}

@ -31,33 +31,51 @@
#include "lt_net_netdb.h"
// 引入头文件,可能包含了网络数据库相关的函数声明和宏定义。
static int GetHostByNameRTest(void)
{
char host_file[] = "127.0.0.1 localhost\n";
// 定义一个模拟的hosts文件内容包含localhost映射到127.0.0.1的条目。
char *pathList[] = {"/etc/hosts"};
// 定义hosts文件的路径列表这里只有一个路径指向系统的hosts文件。
char *streamList[] = {static_cast<char *>(host_file)};
// 定义模拟的hosts文件流列表这里将模拟的hosts文件内容转换为char*类型。
int streamLen[] = {sizeof(host_file)};
// 定义模拟的hosts文件流长度列表。
const int file_number = 1;
// 定义文件数量。
int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
// 调用PrepareFileEnv函数准备文件环境模拟hosts文件。
if (flag != 0) {
RecoveryFileEnv(pathList, file_number);
return -1;
}
// 如果准备文件环境失败,则恢复原始文件环境并返回错误码。
struct hostent addr, *result = NULL;
// 定义hostent结构体变量addr用于存储gethostbyname_r函数的结果。
char buf[1024];
// 定义一个足够大的缓冲区buf用于gethostbyname_r函数。
char buf1[1];
// 定义一个非常小的缓冲区buf1用于测试gethostbyname_r函数的边界条件。
int err, ret;
ret = gethostbyname_r("localhost", &addr, buf, sizeof buf, &result, &err);
// 使用gethostbyname_r函数根据主机名"localhost"获取hostent结构体指针。
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
// 断言返回值ret为0表示gethostbyname_r函数调用成功。
ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1);
// 断言result不为空即gethostbyname_r成功。
ICUNIT_ASSERT_EQUAL(strcmp(result->h_name, "localhost"), 0, -1);
// 断言返回的主机名与输入的主机名相同。
ICUNIT_ASSERT_EQUAL(result->h_addrtype, AF_INET, result->h_addrtype);
// 断言返回的地址类型为AF_INET。
ICUNIT_ASSERT_NOT_EQUAL(result->h_length, 0, result->h_length);
// 断言返回的地址长度不为0。
ret = gethostbyname_r("127.0.0.1", &addr, buf, sizeof buf, &result, &err);
// 使用gethostbyname_r函数根据IP地址字符串"127.0.0.1"获取hostent结构体指针。
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1);
ICUNIT_ASSERT_EQUAL(strcmp(result->h_name, "127.0.0.1"), 0, -1);
@ -65,17 +83,26 @@ static int GetHostByNameRTest(void)
ICUNIT_ASSERT_NOT_EQUAL(result->h_length, 0, result->h_length);
ret = gethostbyname_r("127.0.0.1", &addr, buf1, sizeof buf1, &result, &err);
// 测试gethostbyname_r函数的边界条件传入的缓冲区太小。
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
// 断言返回值ret不为0表示gethostbyname_r函数调用失败。
ret = gethostbyname_r("127.0.0.0.0", &addr, buf, sizeof buf, &result, &err);
// 测试一个不存在的IP地址。
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
// 断言返回值ret不为0表示gethostbyname_r函数调用失败。
ret = gethostbyname_r("lo", &addr, buf, sizeof buf, &result, &err);
// 测试一个不存在的主机名。
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
// 断言返回值ret不为0表示gethostbyname_r函数调用失败。
RecoveryFileEnv(pathList, file_number);
// 恢复原始文件环境。
return ICUNIT_SUCCESS;
// 返回测试成功。
}
void NetNetDbTest007(void)
{
TEST_ADD_CASE(__FUNCTION__, GetHostByNameRTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}
// 定义测试用例将GetHostByNameRTest函数注册为一个测试案例。

@ -31,42 +31,61 @@
#include "lt_net_netdb.h"
// 引入头文件,可能包含了网络数据库相关的函数声明和宏定义。
static int GetHostByName2Test(void)
{
char host_file[] = "127.0.0.1 localhost\n";
// 定义一个模拟的hosts文件内容包含localhost映射到127.0.0.1的条目。
char *pathList[] = {"/etc/hosts"};
// 定义hosts文件的路径列表这里只有一个路径指向系统的hosts文件。
char *streamList[] = {static_cast<char *>(host_file)};
// 定义模拟的hosts文件流列表这里将模拟的hosts文件内容转换为char*类型。
int streamLen[] = {sizeof(host_file)};
// 定义模拟的hosts文件流长度列表。
const int file_number = 1;
// 定义文件数量。
int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
// 调用PrepareFileEnv函数准备文件环境模拟hosts文件。
if (flag != 0) {
RecoveryFileEnv(pathList, file_number);
return -1;
}
// 如果准备文件环境失败,则恢复原始文件环境并返回错误码。
struct hostent *addr = gethostbyname2("localhost", AF_INET);
// 使用gethostbyname2函数根据主机名"localhost"和地址族AF_INET获取hostent结构体指针。
ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1);
// 断言addr不为空即gethostbyname2成功。
ICUNIT_ASSERT_EQUAL(strcmp(addr->h_name, "localhost"), 0, -1);
// 断言返回的主机名与输入的主机名相同。
ICUNIT_ASSERT_EQUAL(addr->h_addrtype, AF_INET, addr->h_addrtype);
// 断言返回的地址类型为AF_INET。
ICUNIT_ASSERT_NOT_EQUAL(addr->h_length, 0, addr->h_length);
// 断言返回的地址长度不为0。
addr = gethostbyname2("127.0.0.1", AF_INET);
// 使用gethostbyname2函数根据IP地址字符串"127.0.0.1"和地址族AF_INET获取hostent结构体指针。
ICUNIT_ASSERT_NOT_EQUAL(addr, NULL, -1);
ICUNIT_ASSERT_EQUAL(strcmp(addr->h_name, "127.0.0.1"), 0, -1);
ICUNIT_ASSERT_EQUAL(addr->h_addrtype, AF_INET, addr->h_addrtype);
ICUNIT_ASSERT_NOT_EQUAL(addr->h_length, 0, addr->h_length);
addr = gethostbyname2("127.0.0.1", AF_INET6);
// 尝试使用gethostbyname2函数根据IP地址字符串"127.0.0.1"和地址族AF_INET6获取hostent结构体指针预期失败。
ICUNIT_ASSERT_EQUAL(addr, NULL, -1);
addr = gethostbyname2("localh", AF_INET);
// 尝试使用gethostbyname2函数根据不完整的主机名"localh"和地址族AF_INET获取hostent结构体指针预期失败。
ICUNIT_ASSERT_EQUAL(addr, NULL, -1);
RecoveryFileEnv(pathList, file_number);
// 恢复原始文件环境。
return ICUNIT_SUCCESS;
// 返回测试成功。
}
void NetNetDbTest008(void)
{
TEST_ADD_CASE(__FUNCTION__, GetHostByName2Test, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}
// 定义测试用例将GetHostByName2Test函数注册为一个测试案例。

@ -30,33 +30,51 @@
*/
#include "lt_net_netdb.h"
// 引入头文件,可能包含了网络数据库相关的函数声明和宏定义。
static int GetHostByName2RTest(void)
{
char host_file[] = "127.0.0.1 localhost\n";
// 定义一个模拟的hosts文件内容包含localhost映射到127.0.0.1的条目。
char *pathList[] = {"/etc/hosts"};
// 定义hosts文件的路径列表这里只有一个路径指向系统的hosts文件。
char *streamList[] = {host_file};
// 定义模拟的hosts文件流列表这里直接使用host_file。
int streamLen[] = {sizeof(host_file)};
// 定义模拟的hosts文件流长度列表。
const int file_number = 1;
// 定义文件数量。
int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
// 调用PrepareFileEnv函数准备文件环境模拟hosts文件。
if (flag != 0) {
RecoveryFileEnv(pathList, file_number);
return -1;
}
// 如果准备文件环境失败,则恢复原始文件环境并返回错误码。
struct hostent addr, *result = NULL;
// 定义hostent结构体变量addr用于存储gethostbyname2_r函数的结果。
char buf[1024];
// 定义一个足够大的缓冲区buf用于gethostbyname2_r函数。
char buf1[1];
// 定义一个非常小的缓冲区buf1用于测试gethostbyname2_r函数的边界条件。
int err, ret;
ret = gethostbyname2_r("localhost", AF_INET, &addr, buf, sizeof buf, &result, &err);
// 使用gethostbyname2_r函数根据主机名"localhost"、地址族AF_INET获取hostent结构体指针。
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
// 断言返回值ret为0表示gethostbyname2_r函数调用成功。
ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1);
// 断言result不为空即gethostbyname2_r成功。
ICUNIT_ASSERT_EQUAL(strcmp(result->h_name, "localhost"), 0, -1);
// 断言返回的主机名与输入的主机名相同。
ICUNIT_ASSERT_EQUAL(result->h_addrtype, AF_INET, result->h_addrtype);
// 断言返回的地址类型为AF_INET。
ICUNIT_ASSERT_NOT_EQUAL(result->h_length, 0, result->h_length);
// 断言返回的地址长度不为0。
ret = gethostbyname2_r("127.0.0.1", AF_INET, &addr, buf, sizeof buf, &result, &err);
// 使用gethostbyname2_r函数根据IP地址字符串"127.0.0.1"、地址族AF_INET获取hostent结构体指针。
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1);
ICUNIT_ASSERT_EQUAL(strcmp(result->h_name, "127.0.0.1"), 0, -1);
@ -64,17 +82,26 @@ static int GetHostByName2RTest(void)
ICUNIT_ASSERT_NOT_EQUAL(result->h_length, 0, result->h_length);
ret = gethostbyname2_r("127.0.0.1", AF_INET, &addr, buf1, sizeof buf1, &result, &err);
// 测试gethostbyname2_r函数的边界条件传入的缓冲区太小。
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
// 断言返回值ret不为0表示gethostbyname2_r函数调用失败。
ret = gethostbyname2_r("127.0.0.1.1", AF_INET, &addr, buf, sizeof buf, &result, &err);
// 测试一个不存在的IP地址。
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
// 断言返回值ret不为0表示gethostbyname2_r函数调用失败。
ret = gethostbyname2_r("lo", AF_INET, &addr, buf, sizeof buf, &result, &err);
// 测试一个不存在的主机名。
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);
// 断言返回值ret不为0表示gethostbyname2_r函数调用失败。
RecoveryFileEnv(pathList, file_number);
// 恢复原始文件环境。
return ICUNIT_SUCCESS;
// 返回测试成功。
}
void NetNetDbTest009(void)
{
TEST_ADD_CASE(__FUNCTION__, GetHostByName2RTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}
// 定义测试用例将GetHostByName2RTest函数注册为一个测试案例。

@ -31,21 +31,31 @@
#include "lt_net_netdb.h"
// 引入头文件,可能包含了网络数据库相关的函数声明和宏定义。
static int NameInfoTest(void)
{
char host[256], serv[256];
// 定义两个数组用于存储getnameinfo函数返回的主机名和服务器名。
struct sockaddr_in addr;
// 定义一个sockaddr_in结构体用于存储IPv4地址信息。
addr.sin_family = AF_INET;
// 设置地址族为IPv4。
addr.sin_port = htons(22);
// 设置端口号为22例如SSH端口htons函数用于将主机字节序转换为网络字节序。
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
// 设置IP地址为127.0.0.1inet_addr函数将点分十进制IP地址转换为网络字节序的整数。
int ret = getnameinfo((struct sockaddr*)&addr, sizeof addr, host, sizeof host, serv, sizeof serv, 0);
// 调用getnameinfo函数传入地址信息、主机名和服务器名的缓冲区尝试获取主机名和服务器名。
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
// 断言getnameinfo函数调用成功返回值为0。
return ICUNIT_SUCCESS;
// 返回测试成功。
}
void NetNetDbTest010(void)
{
TEST_ADD_CASE(__FUNCTION__, NameInfoTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}
// 定义测试用例将NameInfoTest函数注册为一个测试案例。

@ -31,37 +31,57 @@
#include "lt_net_netdb.h"
// 引入头文件,可能包含了网络数据库相关的函数声明和宏定义。
static int GetServByPortTest(void)
{
// refer to the `/etc/services' file.
char serv_file[] = "ssh 22/tcp\n";
// 定义一个模拟的services文件内容包含ssh服务的条目。
char *pathList[] = {"/etc/services"};
// 定义services文件的路径列表这里只有一个路径指向系统的services文件。
char *streamList[] = {static_cast<char *>(serv_file)};
// 定义模拟的services文件流列表这里将模拟的services文件内容转换为char*类型。
int streamLen[] = {sizeof(serv_file)};
// 定义模拟的services文件流长度列表。
const int file_number = 1;
// 定义文件数量。
int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
// 调用PrepareFileEnv函数准备文件环境模拟services文件。
if (flag != 0) {
RecoveryFileEnv(pathList, file_number);
return -1;
}
// 如果准备文件环境失败,则恢复原始文件环境并返回错误码。
const int test_port_no = 22; // ssh port number is 22
// 定义测试的端口号这里是22即SSH服务的标准端口号。
struct servent *se1 = nullptr;
// 定义servent结构体指针变量se1初始化为nullptr。
struct servent *se = getservbyport(htons(test_port_no), "tcp");
// 使用getservbyport函数根据端口号和协议名"tcp"获取servent结构体指针。
ICUNIT_ASSERT_NOT_EQUAL(se, NULL, -1);
// 断言se不为空即getservbyport成功。
ICUNIT_ASSERT_STRING_EQUAL(se->s_name, "ssh", -1);
// 断言返回的服务名称与"ssh"相同。
ICUNIT_ASSERT_STRING_EQUAL(se->s_proto, "tcp", -1);
// 断言返回的服务协议与"tcp"相同。
ICUNIT_ASSERT_STRING_EQUAL(se->s_aliases[0], "ssh", -1);
// 断言返回的服务别名与"ssh"相同。
se1 = getservbyport(htons(test_port_no), "tp");
// 尝试使用getservbyport函数根据端口号和错误的协议名"tp"获取servent结构体指针预期失败。
ICUNIT_ASSERT_EQUAL(se1, nullptr, -1);
// 断言se1为空即getservbyport失败。
RecoveryFileEnv(pathList, file_number);
// 恢复原始文件环境。
return ICUNIT_SUCCESS;
// 返回测试成功。
}
void NetNetDbTest011(void)
{
TEST_ADD_CASE(__FUNCTION__, GetServByPortTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}
// 定义测试用例将GetServByPortTest函数注册为一个测试案例。

@ -31,50 +31,80 @@
#include "lt_net_netdb.h"
// 引入头文件,可能包含了网络数据库相关的函数声明和宏定义。
static int GetServByPortRTest(void)
{
// refer to the `/etc/services' file.
char serv_file[] = "ssh 22/tcp\n";
// 定义一个模拟的services文件内容包含ssh服务的条目。
char *pathList[] = {"/etc/services"};
// 定义services文件的路径列表这里只有一个路径指向系统的services文件。
char *streamList[] = {static_cast<char *>(serv_file)};
// 定义模拟的services文件流列表这里将模拟的services文件内容转换为char*类型。
int streamLen[] = {sizeof(serv_file)};
// 定义模拟的services文件流长度列表。
const int file_number = 1;
// 定义文件数量。
int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
// 调用PrepareFileEnv函数准备文件环境模拟services文件。
if (flag != 0) {
RecoveryFileEnv(pathList, file_number);
return -1;
}
// 如果准备文件环境失败,则恢复原始文件环境并返回错误码。
struct servent se, *result = NULL;
// 定义servent结构体变量se用于存储getservbyport_r函数的结果。
char buf[1024];
// 定义一个足够大的缓冲区buf用于getservbyport_r函数。
char buf1[2];
// 定义一个非常小的缓冲区buf1用于测试getservbyport_r函数的边界条件。
const int test_port_no = 22; // ssh port number is 22
// 定义测试的端口号这里是22即SSH服务的标准端口号。
int ret = getservbyport_r(htons(test_port_no), "tcp", &se, buf, sizeof buf, &result);
// 使用getservbyport_r函数根据端口号和协议名"tcp"获取servent结构体指针。
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
// 断言返回值ret为0表示getservbyport_r函数调用成功。
ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1);
// 断言result不为空即getservbyport_r成功。
ICUNIT_ASSERT_STRING_EQUAL(se.s_name, "ssh", -1);
// 断言返回的服务名称与"ssh"相同。
ICUNIT_ASSERT_STRING_EQUAL(se.s_proto, "tcp", -1);
// 断言返回的服务协议与"tcp"相同。
ICUNIT_ASSERT_STRING_EQUAL(se.s_aliases[0], "ssh", -1);
// 断言返回的服务别名与"ssh"相同。
ICUNIT_ASSERT_STRING_EQUAL(result->s_name, "ssh", -1);
// 断言result结构体中的服务名称与"ssh"相同。
ICUNIT_ASSERT_STRING_EQUAL(result->s_proto, "tcp", -1);
// 断言result结构体中的服务协议与"tcp"相同。
ICUNIT_ASSERT_STRING_EQUAL(result->s_aliases[0], "ssh", -1);
// 断言result结构体中的服务别名与"ssh"相同。
ret = getservbyport_r(htons(test_port_no), "udp", &se, buf, sizeof buf, &result);
// 尝试使用getservbyport_r函数根据端口号和协议名"udp"获取servent结构体指针预期失败。
ICUNIT_ASSERT_EQUAL(ret, ENOENT, -1);
// 断言返回值ret为ENOENT表示未找到对应服务。
ret = getservbyport_r(htons(test_port_no), "udp", &se, buf1, sizeof buf1, &result);
// 测试getservbyport_r函数的边界条件传入的缓冲区太小。
ICUNIT_ASSERT_EQUAL(ret, ERANGE, ret);
// 断言返回值ret为ERANGE表示缓冲区太小。
ret = getservbyport_r(htons(test_port_no), "ud", &se, buf, sizeof buf, &result);
// 尝试使用getservbyport_r函数根据端口号和不完整的协议名"ud"获取servent结构体指针预期失败。
ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
// 断言返回值ret为EINVAL表示提供的协议名不合法。
RecoveryFileEnv(pathList, file_number);
// 恢复原始文件环境。
return ICUNIT_SUCCESS;
// 返回测试成功。
}
void NetNetDbTest012(void)
{
TEST_ADD_CASE(__FUNCTION__, GetServByPortRTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}
// 定义测试用例将GetServByPortRTest函数注册为一个测试案例。

@ -1,5 +1,5 @@
/*
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (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,
@ -30,6 +30,7 @@
#include "lt_net_netdb.h"
#include <net/if.h>
// 引入头文件,包括网络数据库相关的函数声明和宏定义,以及网络接口相关的函数和宏定义。
static int IfNameToIndexTest(void)
{
@ -38,20 +39,30 @@ static int IfNameToIndexTest(void)
char *str = nullptr;
ret = if_nametoindex("lo");
// 调用if_nametoindex函数将接口名称"lo"转换为系统索引。
ICUNIT_ASSERT_NOT_EQUAL(ret, 0, -1);
// 断言转换成功返回的索引值不为0。
str = if_indextoname(ret, if_name);
// 调用if_indextoname函数将系统索引转换回接口名称。
ICUNIT_ASSERT_NOT_EQUAL(str, nullptr, -1);
// 断言转换成功返回的字符串指针不为nullptr。
ICUNIT_ASSERT_STRING_EQUAL(if_name, "lo", -1);
// 断言转换回的接口名称为"lo"。
ret = if_nametoindex("eth1");
// 尝试调用if_nametoindex函数将接口名称"eth1"转换为系统索引。
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
// 断言转换失败返回的索引值为0。
ICUNIT_ASSERT_EQUAL(errno, ENODEV, errno);
// 断言errno被设置为ENODEV表示没有找到对应的设备。
return ICUNIT_SUCCESS;
// 返回测试成功。
}
void NetNetDbTest015(void)
{
TEST_ADD_CASE(__FUNCTION__, IfNameToIndexTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}
// 定义测试用例将IfNameToIndexTest函数注册为一个测试案例。

@ -1,5 +1,5 @@
/*
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (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,
@ -29,6 +29,7 @@
*/
#include "lt_net_netdb.h"
// 引入头文件,可能包含了网络数据库相关的函数声明和宏定义。
static int GetServByNameTest(void)
{
@ -38,43 +39,65 @@ static int GetServByNameTest(void)
"discard 9/udp sink null\n"
"systat 11/tcp users\n"
"ssh 22/tcp\n";
// 定义一个模拟的services文件内容包含多个服务的条目。
char *pathList[] = {"/etc/services"};
// 定义services文件的路径列表这里只有一个路径指向系统的services文件。
char *streamList[] = {static_cast<char *>(serv_file)};
// 定义模拟的services文件流列表这里将模拟的services文件内容转换为char*类型。
int streamLen[] = {sizeof(serv_file)};
// 定义模拟的services文件流长度列表。
const int file_number = 1;
// 定义文件数量。
int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
// 调用PrepareFileEnv函数准备文件环境模拟services文件。
if (flag != 0) {
RecoveryFileEnv(pathList, file_number);
return -1;
}
// 如果准备文件环境失败,则恢复原始文件环境并返回错误码。
struct servent *se1 = nullptr;
struct servent *se2 = nullptr;
se1 = getservbyname("discard", "tcp");
// 使用getservbyname函数根据服务名"discard"和协议名"tcp"获取servent结构体指针。
ICUNIT_ASSERT_NOT_EQUAL(se1, NULL, -1);
// 断言se1不为空即getservbyname成功。
ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, "discard", -1);
// 断言返回的服务名称与"discard"相同。
ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, "tcp", -1);
// 断言返回的服务协议与"tcp"相同。
ICUNIT_ASSERT_STRING_EQUAL(se1->s_aliases[0], "discard", -1);
// 断言返回的服务别名与"discard"相同。
se1 = getservbyname("ssh", "tcp");
// 使用getservbyname函数根据服务名"ssh"和协议名"tcp"获取servent结构体指针。
ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1);
// 断言se1不为空即getservbyname成功。
ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, "ssh", -1);
// 断言返回的服务名称与"ssh"相同。
ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, "tcp", -1);
// 断言返回的服务协议与"tcp"相同。
ICUNIT_ASSERT_STRING_EQUAL(se1->s_aliases[0], "ssh", -1);
// 断言返回的服务别名与"ssh"相同。
se2 = getservbyname("cho", "udp");
// 尝试使用getservbyname函数根据错误的服务名"cho"和协议名"udp"获取servent结构体指针预期失败。
ICUNIT_ASSERT_EQUAL(se2, nullptr, -1);
se2 = getservbyname("systat", "udp");
// 尝试使用getservbyname函数根据服务名"systat"和协议名"udp"获取servent结构体指针预期失败。
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);
}
// 定义测试用例将GetServByNameTest函数注册为一个测试案例。

@ -1,5 +1,5 @@
/*
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (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,
@ -29,19 +29,27 @@
*/
#include "lt_net_netdb.h"
// 引入头文件,可能包含了网络数据库相关的函数声明和宏定义。
static int GetServByNameRTest(void)
{
char serv_file[] = "ssh 22/tcp\n";
// 定义一个模拟的services文件内容包含ssh服务的条目。
char *pathList[] = {"/etc/services"};
// 定义services文件的路径列表这里只有一个路径指向系统的services文件。
char *streamList[] = {static_cast<char *>(serv_file)};
// 定义模拟的services文件流列表这里将模拟的services文件内容转换为char*类型。
int streamLen[] = {sizeof(serv_file)};
// 定义模拟的services文件流长度列表。
const int file_number = 1;
// 定义文件数量。
int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
// 调用PrepareFileEnv函数准备文件环境模拟services文件。
if (flag != 0) {
RecoveryFileEnv(pathList, file_number);
return -1;
}
// 如果准备文件环境失败,则恢复原始文件环境并返回错误码。
struct servent se;
struct servent *result = NULL;
@ -51,29 +59,47 @@ static int GetServByNameRTest(void)
errno = 0;
ret = getservbyname_r("ssh", "tcp", &se, buf1, sizeof buf1, &result);
// 使用getservbyname_r函数根据服务名"ssh"和协议名"tcp"获取servent结构体指针。
ICUNIT_ASSERT_EQUAL(ret, 0, ret);
// 断言返回值ret为0表示getservbyname_r函数调用成功。
ICUNIT_ASSERT_NOT_EQUAL(result, NULL, -1);
// 断言result不为空即getservbyname_r成功。
ICUNIT_ASSERT_STRING_EQUAL(se.s_name, "ssh", -1);
// 断言返回的服务名称与"ssh"相同。
ICUNIT_ASSERT_STRING_EQUAL(se.s_proto, "tcp", -1);
// 断言返回的服务协议与"tcp"相同。
ICUNIT_ASSERT_STRING_EQUAL(se.s_aliases[0], "ssh", -1);
// 断言返回的服务别名与"ssh"相同。
ICUNIT_ASSERT_STRING_EQUAL(result->s_name, "ssh", -1);
// 断言result结构体中的服务名称与"ssh"相同。
ICUNIT_ASSERT_STRING_EQUAL(result->s_proto, "tcp", -1);
// 断言result结构体中的服务协议与"tcp"相同。
ICUNIT_ASSERT_STRING_EQUAL(result->s_aliases[0], "ssh", -1);
// 断言result结构体中的服务别名与"ssh"相同。
ret = getservbyname_r("ssh", "tcp", &se, buf2, sizeof buf2, &result);
// 测试getservbyname_r函数的边界条件传入的缓冲区太小。
ICUNIT_ASSERT_EQUAL(ret, ERANGE, ret);
// 断言返回值ret为ERANGE表示缓冲区太小。
ret = getservbyname_r("ssh", "tp", &se, buf1, sizeof buf1, &result);
// 尝试使用getservbyname_r函数根据服务名"ssh"和错误的协议名"tp"获取servent结构体指针预期失败。
ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
// 断言返回值ret为EINVAL表示提供的协议名不合法。
ret = getservbyname_r("sh", "tcp", &se, buf1, sizeof buf1, &result);
// 尝试使用getservbyname_r函数根据不完整的服务名"sh"和协议名"tcp"获取servent结构体指针预期失败。
ICUNIT_ASSERT_EQUAL(ret, ENOENT, ret);
// 断言返回值ret为ENOENT表示未找到对应服务。
RecoveryFileEnv(pathList, file_number);
// 恢复原始文件环境。
return ICUNIT_SUCCESS;
// 返回测试成功。
}
void NetNetDbTest017(void)
{
TEST_ADD_CASE(__FUNCTION__, GetServByNameRTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}
// 定义测试用例将GetServByNameRTest函数注册为一个测试案例。

@ -1,5 +1,5 @@
/*
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (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,
@ -29,6 +29,7 @@
*/
#include "lt_net_netdb.h"
// 引入头文件,可能包含了网络数据库相关的函数声明和宏定义。
static int GetServEntTest(void)
{
@ -39,15 +40,22 @@ static int GetServEntTest(void)
"ssh 100000/tcp\n"
"ssh /tcp\n"
"ssh 22/";
// 定义一个模拟的services文件内容包含多个服务的条目。
char *pathList[] = {"/etc/services"};
// 定义services文件的路径列表这里只有一个路径指向系统的services文件。
char *streamList[] = {static_cast<char *>(serv_file)};
// 定义模拟的services文件流列表这里将模拟的services文件内容转换为char*类型。
int streamLen[] = {sizeof(serv_file)};
// 定义模拟的services文件流长度列表。
const int file_number = 1;
// 定义文件数量。
int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
// 调用PrepareFileEnv函数准备文件环境模拟services文件。
if (flag != 0) {
RecoveryFileEnv(pathList, file_number);
return -1;
}
// 如果准备文件环境失败,则恢复原始文件环境并返回错误码。
/* tcpmux,echo,discard port number is 1,7,9 */
const int tcpmux_port_no = 1;
@ -59,55 +67,96 @@ static int GetServEntTest(void)
struct servent *se3 = nullptr;
se1 = getservent();
// 调用getservent函数获取services数据库中的第一个服务条目。
ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1);
// 断言se1不为空即getservent成功。
ICUNIT_ASSERT_STRING_EQUAL(se1->s_name, "tcpmux", -1);
// 断言返回的服务名称与"tcpmux"相同。
ICUNIT_ASSERT_STRING_EQUAL(se1->s_proto, "tcp", -1);
// 断言返回的服务协议与"tcp"相同。
ICUNIT_ASSERT_EQUAL(se1->s_port, ntohs(tcpmux_port_no), -1);
// 断言返回的服务端口号与转换为网络字节序的1相同。
endservent();
// 调用endservent函数结束服务数据库的搜索。
se2 = getservent();
// 再次调用getservent函数获取services数据库中的第一个服务条目。
ICUNIT_ASSERT_NOT_EQUAL(se2, nullptr, -1);
// 断言se2不为空即getservent成功。
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);
// 调用setservent函数设置服务数据库的搜索位置到开始。
se3 = getservent();
// 调用getservent函数获取services数据库中的第一个服务条目。
ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
// 断言se3不为空即getservent成功。
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);
// 断言se3不为空即getservent成功。
ICUNIT_ASSERT_STRING_EQUAL(se3->s_name, "echo", -1);
// 断言返回的服务名称与"echo"相同。
ICUNIT_ASSERT_STRING_EQUAL(se3->s_proto, "tcp", -1);
// 断言返回的服务协议与"tcp"相同。
ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(echo_port_no), -1);
// 断言返回的服务端口号与转换为网络字节序的7相同。
se3 = getservent();
// 获取下一个服务条目。
ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
// 断言se3不为空即getservent成功。
ICUNIT_ASSERT_STRING_EQUAL(se3->s_name, "echo", -1);
// 断言返回的服务名称与"echo"相同。
ICUNIT_ASSERT_STRING_EQUAL(se3->s_proto, "udp", -1);
// 断言返回的服务协议与"udp"相同。
ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(echo_port_no), -1);
// 断言返回的服务端口号与转换为网络字节序的7相同。
se3 = getservent();
// 获取下一个服务条目。
ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
// 断言se3不为空即getservent成功。
ICUNIT_ASSERT_STRING_EQUAL(se3->s_name, "discard", -1);
// 断言返回的服务名称与"discard"相同。
ICUNIT_ASSERT_STRING_EQUAL(se3->s_proto, "tcp", -1);
// 断言返回的服务协议与"tcp"相同。
ICUNIT_ASSERT_EQUAL(se3->s_port, ntohs(discard_port_no), -1);
// 断言返回的服务端口号与转换为网络字节序的9相同。
ICUNIT_ASSERT_STRING_EQUAL(se3->s_aliases[0], "sink", -1);
// 断言返回的服务别名列表中的第一个别名与"sink"相同。
ICUNIT_ASSERT_STRING_EQUAL(se3->s_aliases[1], "null", -1);
// 断言返回的服务别名列表中的第二个别名与"null"相同。
ICUNIT_ASSERT_EQUAL(se3->s_aliases[2], nullptr, -1);
// 断言返回的服务别名列表中的第三个别名为nullptr表示别名列表结束。
se3 = getservent();
// 获取下一个服务条目。
ICUNIT_ASSERT_EQUAL(se3, nullptr, -1);
// 断言se3为空即已经到达services数据库的末尾。
endservent();
// 调用endservent函数结束服务数据库的搜索。
RecoveryFileEnv(pathList, file_number);
// 恢复原始文件环境。
return ICUNIT_SUCCESS;
// 返回测试成功。
}
void NetNetDbTest018(void)
{
TEST_ADD_CASE(__FUNCTION__, GetServEntTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}
// 定义测试用例将GetServEntTest函数注册为一个测试案例。

@ -1,5 +1,5 @@
/*
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (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,
@ -29,7 +29,8 @@
*/
#include "lt_net_netdb.h"
#include<arpa/nameser.h>
#include <arpa/nameser.h>
// 引入头文件包括网络数据库相关的函数声明和宏定义以及Nameser.h中定义的与DNS相关的宏和结构。
static int GetHostEntTest(void)
{
@ -38,15 +39,22 @@ static int GetHostEntTest(void)
"10.0.0.0 example example.com example.cn\n"
"10.0.0.0\n"
"10.0.0 example.com";
// 定义一个模拟的hosts文件内容包含IPv4和IPv6的条目。
char *pathList[] = {"/etc/hosts"};
// 定义hosts文件的路径列表这里只有一个路径指向系统的hosts文件。
char *streamList[] = {static_cast<char *>(host_file)};
// 定义模拟的hosts文件流列表这里将模拟的hosts文件内容转换为char*类型。
int streamLen[] = {sizeof(host_file)};
// 定义模拟的hosts文件流长度列表。
const int file_number = 1;
// 定义文件数量。
int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
// 调用PrepareFileEnv函数准备文件环境模拟hosts文件。
if (flag != 0) {
RecoveryFileEnv(pathList, file_number);
return -1;
}
// 如果准备文件环境失败,则恢复原始文件环境并返回错误码。
struct hostent *se1 = nullptr;
struct hostent *se2 = nullptr;
@ -54,52 +62,91 @@ static int GetHostEntTest(void)
char addr[INET6_ADDRSTRLEN];
se1 = gethostent();
// 调用gethostent函数获取hosts数据库中的第一个主机条目。
ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1);
// 断言se1不为空即gethostent成功。
ICUNIT_ASSERT_STRING_EQUAL(se1->h_name, "localhost", -1);
// 断言返回的主机名与"localhost"相同。
ICUNIT_ASSERT_EQUAL(se1->h_addrtype, AF_INET, -1);
// 断言返回的地址类型为AF_INETIPv4
ICUNIT_ASSERT_EQUAL(se1->h_length, INADDRSZ, -1);
// 断言返回的地址长度为IPv4地址长度。
ICUNIT_ASSERT_STRING_EQUAL("127.0.0.1", inet_ntop(AF_INET, se1->h_addr_list[0], addr, INET_ADDRSTRLEN), -1);
// 断言返回的第一个地址为"127.0.0.1"。
ICUNIT_ASSERT_EQUAL(se1->h_aliases[0], nullptr, -1);
// 断言别名列表中的第一个别名为nullptr表示别名列表结束。
endhostent();
// 调用endhostent函数结束主机数据库的搜索。
se2 = gethostent();
// 再次调用gethostent函数获取hosts数据库中的第一个主机条目。
ICUNIT_ASSERT_NOT_EQUAL(se2, nullptr, -1);
// 断言se2不为空即gethostent成功。
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);
// 调用sethostent函数设置主机数据库的搜索位置到开始。
se3 = gethostent();
// 调用gethostent函数获取hosts数据库中的第一个主机条目。
ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
// 断言se3不为空即gethostent成功。
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);
// 断言se3不为空即gethostent成功。
ICUNIT_ASSERT_EQUAL(se3->h_addrtype, AF_INET6, -1);
// 断言返回的地址类型为AF_INET6IPv6
ICUNIT_ASSERT_STRING_EQUAL(se3->h_name, "ip6-localhost", -1);
// 断言返回的主机名为"ip6-localhost"。
ICUNIT_ASSERT_EQUAL(se3->h_length, IN6ADDRSZ, -1);
// 断言返回的地址长度为IPv6地址长度。
ICUNIT_ASSERT_STRING_EQUAL("::1", inet_ntop(AF_INET6, se3->h_addr_list[0], addr, INET6_ADDRSTRLEN), -1);
// 断言返回的第一个地址为"::1"。
se3 = gethostent();
// 获取下一个主机条目。
ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
// 断言se3不为空即gethostent成功。
ICUNIT_ASSERT_EQUAL(se3->h_addrtype, AF_INET, -1);
// 断言返回的地址类型为AF_INETIPv4
ICUNIT_ASSERT_STRING_EQUAL(se3->h_name, "example", -1);
// 断言返回的主机名为"example"。
ICUNIT_ASSERT_STRING_EQUAL("10.0.0.0", inet_ntop(AF_INET, se3->h_addr_list[0], addr, INET_ADDRSTRLEN), -1);
// 断言返回的第一个地址为"10.0.0.0"。
ICUNIT_ASSERT_STRING_EQUAL(se3->h_aliases[0], "example.com", -1);
// 断言别名列表中的第一个别名为"example.com"。
ICUNIT_ASSERT_STRING_EQUAL(se3->h_aliases[1], "example.cn", -1);
// 断言别名列表中的第二个别名为"example.cn"。
ICUNIT_ASSERT_EQUAL(se3->h_aliases[2], nullptr, -1);
// 断言别名列表中的第三个别名为nullptr表示别名列表结束。
se3 = gethostent();
// 获取下一个主机条目。
ICUNIT_ASSERT_EQUAL(se3, nullptr, -1);
// 断言se3为空即已经到达hosts数据库的末尾。
endhostent();
// 调用endhostent函数结束主机数据库的搜索。
RecoveryFileEnv(pathList, file_number);
// 恢复原始文件环境。
return ICUNIT_SUCCESS;
// 返回测试成功。
}
void NetNetDbTest019(void)
{
TEST_ADD_CASE(__FUNCTION__, GetHostEntTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}
// 定义测试用例将GetHostEntTest函数注册为一个测试案例。

@ -1,5 +1,5 @@
/*
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (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,
@ -29,6 +29,7 @@
*/
#include "lt_net_netdb.h"
// 引入头文件,包含了网络数据库相关的函数声明和宏定义。
static int GetNetEntTest(void)
{
@ -36,56 +37,94 @@ static int GetNetEntTest(void)
"link-local 169.254.0.0\n"
"example 192.168.1.0 network example-network\n"
"test1";
// 定义一个模拟的networks文件内容包含网络名称和对应的地址。
char *pathList[] = {"/etc/networks"};
// 定义networks文件的路径列表这里只有一个路径指向系统的networks文件。
char *streamList[] = {static_cast<char *>(network_file)};
// 定义模拟的networks文件流列表这里将模拟的networks文件内容转换为char*类型。
int streamLen[] = {sizeof(network_file)};
// 定义模拟的networks文件流长度列表。
const int file_number = 1;
// 定义文件数量。
int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
// 调用PrepareFileEnv函数准备文件环境模拟networks文件。
if (flag != 0) {
RecoveryFileEnv(pathList, file_number);
return -1;
}
// 如果准备文件环境失败,则恢复原始文件环境并返回错误码。
struct netent *se1 = nullptr;
struct netent *se2 = nullptr;
struct netent *se3 = nullptr;
se1 = getnetent();
// 调用getnetent函数获取networks数据库中的第一个网络条目。
ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1);
// 断言se1不为空即getnetent成功。
ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, "link-local", -1);
// 断言返回的网络名称与"link-local"相同。
ICUNIT_ASSERT_EQUAL(se1->n_addrtype, AF_INET, -1);
// 断言返回的地址类型为AF_INETIPv4
ICUNIT_ASSERT_EQUAL(se1->n_net, inet_network("169.254.0.0"), -1);
// 断言返回的网络地址与"169.254.0.0"相同。
ICUNIT_ASSERT_EQUAL(se1->n_aliases[0], nullptr, -1);
// 断言别名列表中的第一个别名为nullptr表示别名列表结束。
endnetent();
// 调用endnetent函数结束网络数据库的搜索。
se2 = getnetent();
// 再次调用getnetent函数获取networks数据库中的第一个网络条目。
ICUNIT_ASSERT_NOT_EQUAL(se2, nullptr, -1);
// 断言se2不为空即getnetent成功。
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);
// 调用setnetent函数设置网络数据库的搜索位置到开始。
se3 = getnetent();
// 调用getnetent函数获取networks数据库中的第一个网络条目。
ICUNIT_ASSERT_NOT_EQUAL(se3, nullptr, -1);
// 断言se3不为空即getnetent成功。
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);
// 断言se1不为空即getnetent成功。
ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, "example", -1);
// 断言返回的网络名称与"example"相同。
ICUNIT_ASSERT_EQUAL(se1->n_addrtype, AF_INET, -1);
// 断言返回的地址类型为AF_INETIPv4
ICUNIT_ASSERT_EQUAL(se1->n_net, inet_network("192.168.1.0"), -1);
// 断言返回的网络地址与"192.168.1.0"相同。
ICUNIT_ASSERT_STRING_EQUAL(se1->n_aliases[0], "network", -1);
// 断言别名列表中的第一个别名为"network"。
ICUNIT_ASSERT_STRING_EQUAL(se1->n_aliases[1], "example-network", -1);
// 断言别名列表中的第二个别名为"example-network"。
ICUNIT_ASSERT_EQUAL(se1->n_aliases[2], nullptr, -1);
// 断言别名列表中的第三个别名为nullptr表示别名列表结束。
se1 = getnetent();
// 获取下一个网络条目。
ICUNIT_ASSERT_EQUAL(se1, nullptr, -1);
// 断言se1为空即已经到达networks数据库的末尾。
endnetent();
// 调用endnetent函数结束网络数据库的搜索。
RecoveryFileEnv(pathList, file_number);
// 恢复原始文件环境。
return ICUNIT_SUCCESS;
// 返回测试成功。
}
void NetNetDbTest020(void)

@ -1,5 +1,5 @@
/*
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (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,
@ -29,42 +29,60 @@
*/
#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";
// 定义一个模拟的networks文件内容包含网络名称和对应的地址。
char *pathList[] = {"/etc/networks"};
// 定义networks文件的路径列表这里只有一个路径指向系统的networks文件。
char *streamList[] = {static_cast<char *>(network_file)};
// 定义模拟的networks文件流列表这里将模拟的networks文件内容转换为char*类型。
int streamLen[] = {sizeof(network_file)};
// 定义模拟的networks文件流长度列表。
const int file_number = 1;
// 定义文件数量。
int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
// 调用PrepareFileEnv函数准备文件环境模拟networks文件。
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");
// 使用getnetbyname函数根据网络名称"link-local"获取netent结构体指针。
ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1);
// 断言se1不为空即getnetbyname成功。
ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, "link-local", -1);
// 断言返回的网络名称与"link-local"相同。
ICUNIT_ASSERT_EQUAL(se1->n_addrtype, AF_INET, -1);
// 断言返回的地址类型为AF_INETIPv4
ICUNIT_ASSERT_EQUAL(se1->n_net, inet_network("169.254.0.0"), -1);
// 断言返回的网络地址与"169.254.0.0"相同。
se2 = getnetbyname("link");
// 尝试使用getnetbyname函数根据不完整的网络名称"link"获取netent结构体指针预期失败。
ICUNIT_ASSERT_EQUAL(se2, nullptr, -1);
se3 = getnetbyname("hs");
// 尝试使用getnetbyname函数根据不存在的网络名称"hs"获取netent结构体指针预期失败。
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);
}
// 定义测试用例将GetNetBynametTest函数注册为一个测试案例。

@ -1,5 +1,5 @@
/*
* opyright (c) 2021-2021, Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (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,
@ -29,39 +29,56 @@
*/
#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";
// 定义一个模拟的networks文件内容包含网络名称和对应的地址。
char *pathList[] = {"/etc/networks"};
// 定义networks文件的路径列表这里只有一个路径指向系统的networks文件。
char *streamList[] = {static_cast<char *>(network_file)};
// 定义模拟的networks文件流列表这里将模拟的networks文件内容转换为char*类型。
int streamLen[] = {sizeof(network_file)};
// 定义模拟的networks文件流长度列表。
const int file_number = 1;
// 定义文件数量。
int flag = PrepareFileEnv(pathList, streamList, streamLen, file_number);
// 调用PrepareFileEnv函数准备文件环境模拟networks文件。
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);
// 使用getnetbyaddr函数根据网络地址"169.254.0.0"和地址族AF_INET获取netent结构体指针。
ICUNIT_ASSERT_NOT_EQUAL(se1, nullptr, -1);
// 断言se1不为空即getnetbyaddr成功。
ICUNIT_ASSERT_STRING_EQUAL(se1->n_name, "link-local", -1);
// 断言返回的网络名称与"link-local"相同。
ICUNIT_ASSERT_EQUAL(se1->n_addrtype, AF_INET, -1);
// 断言返回的地址类型为AF_INETIPv4
ICUNIT_ASSERT_EQUAL(se1->n_net, inet_network("169.254.0.0"), -1);
// 断言返回的网络地址与"169.254.0.0"相同。
se2 = getnetbyaddr(inet_network("169.254.0.1"), AF_INET);
// 尝试使用getnetbyaddr函数根据网络地址"169.254.0.1"和地址族AF_INET获取netent结构体指针预期失败。
ICUNIT_ASSERT_EQUAL(se2, nullptr, -1);
se3 = getnetbyaddr(inet_network("169.254.0.1"), AF_INET6);
// 尝试使用getnetbyaddr函数根据网络地址"169.254.0.1"和地址族AF_INET6获取netent结构体指针预期失败。
ICUNIT_ASSERT_EQUAL(se3, nullptr, -1);
RecoveryFileEnv(pathList, file_number);
// 恢复原始文件环境。
return ICUNIT_SUCCESS;
// 返回测试成功。
}
void NetNetDbTest022(void)

@ -2,58 +2,46 @@
* 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 "lt_net_netdb.h"
// 定义一个测试函数,用于测试协议数据库的功能
static int ProtoentTest(void)
{
setprotoent(1);
setprotoent(1); // 打开协议数据库文件参数1表示重置文件指针到文件开始
// refer to the `/etc/protocols' file.
// 引用/etc/protocols文件获取名为"icmp"的协议条目
struct protoent *prot = getprotobyname("icmp");
// 断言:确保获取到的协议条目不为空
ICUNIT_ASSERT_NOT_EQUAL(prot, NULL, -1);
// 断言确保获取到的协议编号是1ICMP协议的编号
ICUNIT_ASSERT_EQUAL(prot->p_proto, 1, prot->p_proto);
// 获取编号为1的协议条目也是ICMP协议
prot = getprotobynumber(1);
// 断言:确保获取到的协议条目不为空
ICUNIT_ASSERT_NOT_EQUAL(prot, NULL, -1);
ICUNIT_ASSERT_EQUAL(strcmp(prot->p_name, "icmp"), 0, -1);
// 断言:确保获取到的协议名称是"icmp"
ICUNIT_ASSERT_EQUAL(strcmp(prot->p_name, "icmp"), 0, -1); // strcmp返回0表示字符串相等
// 获取协议数据库中的下一个协议条目
prot = getprotoent();
// 断言:确保获取到的协议条目不为空
ICUNIT_ASSERT_NOT_EQUAL(prot, NULL, -1);
// 关闭协议数据库文件
endprotoent();
// 返回测试成功标志
return ICUNIT_SUCCESS;
}
// 定义一个测试注册函数用于将ProtoentTest测试添加到测试套件中
void NetNetDbTest001(void)
{
// 添加测试用例到测试框架中,包括测试用例名称、测试函数、测试类别、测试协议、测试级别和测试类型
TEST_ADD_CASE(__FUNCTION__, ProtoentTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}

@ -1,48 +1,34 @@
/*
* 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 "lt_net_netdb.h"
// 定义一个测试函数用于测试hstrerror和herror函数的功能
static int HerrorTest(void)
{
// 调用hstrerror函数传入一个错误码TRY_AGAIN是一个宏通常代表一个网络相关的错误码
// hstrerror函数用于将错误码转换为对应的错误描述字符串
const char *err = hstrerror(TRY_AGAIN);
// 断言确保hstrerror返回的错误描述字符串不为空
// 如果为空,则测试失败,返回-1作为错误码
ICUNIT_ASSERT_NOT_EQUAL(err, NULL, -1);
// 调用herror函数传入hstrerror返回的错误描述字符串
// herror函数通常用于将错误描述打印到标准错误输出stderr
herror(err);
// 返回测试成功标志
return ICUNIT_SUCCESS;
}
// 定义一个测试注册函数用于将HerrorTest测试添加到测试套件中
void NetNetDbTest013(void)
{
// 使用TEST_ADD_CASE宏添加测试用例到测试框架中
// 宏的参数包括测试用例的名称(当前函数名)、测试函数、测试类别、测试协议、测试级别和测试类型
// 这些参数帮助测试框架组织和执行测试用例
TEST_ADD_CASE(__FUNCTION__, HerrorTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
}
}

@ -28,6 +28,242 @@
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// 包含标准输入输出头文件
#include "stdio.h"
// 包含C标准库中的极限定义
#include <climits>
// 包含Google Test测试框架头文件
#include <gtest/gtest.h>
// 包含自定义的pthread测试头文件
#include "it_pthread_test.h"
// 包含系统资源库头文件
#include <sys/resource.h>
// 使用Google Test框架的扩展命名空间
using namespace testing::ext;
// 定义一个名为OHOS的命名空间用于封装测试代码
namespace OHOS {
// 定义一个测试类ProcessPthreadTest它继承自Google Test框架的Test类
class ProcessPthreadTest : public testing::Test {
public:
// 测试套件开始前调用的静态成员函数,用于设置测试环境
static void SetUpTestCase(void) {
struct sched_param param = { 0 }; // 定义调度参数结构体
int currThreadPolicy, ret; // 定义当前线程策略和返回值变量
// 获取当前线程的调度参数
ret = pthread_getschedparam(pthread_self(), &currThreadPolicy, &param);
// 断言返回值为0如果不为0则用-ret作为错误码
ICUNIT_ASSERT_EQUAL_VOID(ret, 0, -ret);
// 设置线程的调度优先级
param.sched_priority = TASK_PRIO_TEST;
// 设置当前线程的调度参数
ret = pthread_setschedparam(pthread_self(), SCHED_RR, &param);
// 断言返回值为0如果不为0则用-ret作为错误码
ICUNIT_ASSERT_EQUAL_VOID(ret, 0, -ret);
}
// 测试套件结束后调用的静态成员函数,用于清理测试环境(这里为空实现)
static void TearDownTestCase(void) {}
};
#if defined(LOSCFG_USER_TEST_SMOKE)
// 如果定义了LOSCFG_USER_TEST_SMOKE则编译并执行以下测试用例
/* *
* @tc.name: it_test_pthread_003
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
*/
// 定义测试用例ItTestPthread003
HWTEST_F(ProcessPthreadTest, ItTestPthread003, TestSize.Level0) {
ItTestPthread003();
}
#endif
#ifndef LOSCFG_USER_TEST_SMP
// 如果未定义LOSCFG_USER_TEST_SMP则编译并执行以下测试用例
/* *
* @tc.name: it_test_pthread_006
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
*/
// 定义测试用例ItTestPthread006
HWTEST_F(ProcessPthreadTest, ItTestPthread006, TestSize.Level0) {
ItTestPthread006();
}
#endif
// 以下是其他测试用例的定义,每个测试用例都有相应的注释说明其功能和类型
/* *
* @tc.name: it_test_pthread_007
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
*/
HWTEST_F(ProcessPthreadTest, ItTestPthread007, TestSize.Level0) {
ItTestPthread007();
}
/* *
* @tc.name: it_test_pthread_008
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
*/
HWTEST_F(ProcessPthreadTest, ItTestPthread008, TestSize.Level0) {
ItTestPthread008();
}
/* *
* @tc.name: it_test_pthread_009
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
*/
HWTEST_F(ProcessPthreadTest, ItTestPthread009, TestSize.Level0) {
ItTestPthread009();
}
#ifndef LOSCFG_USER_TEST_SMP
/* *
* @tc.name: it_test_pthread_010
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
*/
HWTEST_F(ProcessPthreadTest, ItTestPthread010, TestSize.Level0) {
ItTestPthread010();
}
#endif
/* *
* @tc.name: it_test_pthread_011
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
*/
HWTEST_F(ProcessPthreadTest, ItTestPthread011, TestSize.Level0) {
ItTestPthread011();
}
/* *
* @tc.name: it_test_pthread_012
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
*/
HWTEST_F(ProcessPthreadTest, ItTestPthread012, TestSize.Level0) {
ItTestPthread012();
}
/* *
* @tc.name: it_test_pthread_013
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
*/
HWTEST_F(ProcessPthreadTest, ItTestPthread013, TestSize.Level0) {
ItTestPthread013();
}
/* *
* @tc.name: it_test_pthread_015
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
*/
HWTEST_F(ProcessPthreadTest, ItTestPthread015, TestSize.Level0) {
ItTestPthread015();
}
/* *
* @tc.name: it_test_pthread_016
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
*/
HWTEST_F(ProcessPthreadTest, ItTestPthread016, TestSize.Level0) {
ItTestPthread016();
}
/* *
* @tc.name: it_test_pthread_018
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
*/
HWTEST_F(ProcessPthreadTest, ItTestPthread018, TestSize.Level0) {
ItTestPthread018();
}
/* *
* @tc.name: it_test_pthread_019
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
*/
HWTEST_F(ProcessPthreadTest, ItTestPthread019, TestSize.Level0) {
ItTestPthread019();
}
/* *
* @tc.name: it_test_pthread_020
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
* @tc.require: issueI6T3P3
* @tc.author:
*/
HWTEST_F(ProcessPthreadTest, ItTestPthread020, TestSize.Level0) {
ItTestPthread020();
}
/* *
* @tc.name: it_test_pthread_021
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
* @tc.require: issueI6T3P3
* @tc.author:
*/
HWTEST_F(ProcessPthreadTest, ItTestPthread021, TestSize.Level0) {
ItTestPthread021();
}
/* *
* @tc.name: it_test_pthread_022
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
* @tc.require: issueI6T3P3
* @tc.author:
*/
HWTEST_F(ProcessPthreadTest, ItTestPthread022, TestSize.Level0) {
ItTestPthread022();
}
/* *
* @tc.name: it_test_pthread_024
* @tc.desc: function for ProcessPthreadTest
* @tc.type: FUNC
* @tc.require: issueI6T3P3
* @tc.author:
*/
HWTEST_F(ProcessPthreadTest, ItTestP/*
* 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 "stdio.h" // 引入标准输入输出头文件
#include <climits> // 引入字符类型限制头文件

Loading…
Cancel
Save