|  |  |  | @ -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_INET(IPv4)。
 | 
			
		
	
		
			
				
					|  |  |  |  |     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_INET6(IPv6)。
 | 
			
		
	
		
			
				
					|  |  |  |  |     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_INET(IPv4)。
 | 
			
		
	
		
			
				
					|  |  |  |  |     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函数注册为一个测试案例。
 |