1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22
23 #include <cstdio>
24 #include <cstdlib>
25 #include <dlfcn.h>
26 #include <algorithm>
27 #include <fstream>
28 #include <iostream>
29 #include <unistd.h>
30 #include <sys/mman.h>
31 #include <netdb.h>
32 #include <gtest/gtest.h>
33
34 #include "hook.h"
35
36 using namespace testing::ext;
37 using namespace std;
38
39 extern "C"{
40 int getaddrinfo (char *__restrict, char *__restrict, struct addrinfo *__restrict, struct addrinfo **__restrict);
41 }
42
43 namespace OHOS {
44 namespace {
45
46 class GetAddrInfoTest : public testing::Test {
47 public:
48 static void TearDownTestCase(void);
49 };
50
TearDownTestCase(void)51 void GetAddrInfoTest::TearDownTestCase(void)
52 {
53 }
54
CallGetAddrInfo(int family,int sockType)55 int CallGetAddrInfo(int family, int sockType)
56 {
57 set_hook_flag(SOCKET_FLAG, true);
58
59 char host[] = "127.0.0.1";
60 struct addrinfo *result = nullptr;
61 struct addrinfo hint = {0};
62 hint.ai_flags = AI_ADDRCONFIG;
63 hint.ai_family = family;
64 hint.ai_socktype = sockType;
65 hint.ai_protocol = 0;
66
67 int ret = getaddrinfo(host, nullptr, &hint, &result);
68
69 freeaddrinfo(result);
70 result = nullptr;
71
72 set_hook_flag(SOCKET_FLAG, false);
73 return ret;
74 }
75
76 /**
77 * @tc.name: GetAddrInfoTest_001
78 * @tc.desc: test socket fail
79 * @tc.type: FUNC
80 */
81 HWTEST_F(GetAddrInfoTest, GetAddrInfoTest_001, TestSize.Level0)
82 {
83 int ret = CallGetAddrInfo(AF_INET, 0);
84 EXPECT_EQ(ret, EAI_NONAME);
85 }
86
87 /**
88 * @tc.name: GetAddrInfoTest_002
89 * @tc.desc: test socket fail
90 * @tc.type: FUNC
91 */
92 HWTEST_F(GetAddrInfoTest, GetAddrInfoTest_002, TestSize.Level0)
93 {
94 int ret = CallGetAddrInfo(AF_INET, SOCK_STREAM);
95 EXPECT_EQ(ret, EAI_NONAME);
96 }
97
98 /**
99 * @tc.name: GetAddrInfoTest_003
100 * @tc.desc: test socket fail
101 * @tc.type: FUNC
102 */
103 HWTEST_F(GetAddrInfoTest, GetAddrInfoTest_003, TestSize.Level0)
104 {
105 int ret = CallGetAddrInfo(AF_INET, SOCK_DGRAM);
106 EXPECT_EQ(ret, EAI_NONAME);
107 }
108
109 /**
110 * @tc.name: GetAddrInfoTest_004
111 * @tc.desc: test socket fail
112 * @tc.type: FUNC
113 */
114 HWTEST_F(GetAddrInfoTest, GetAddrInfoTest_004, TestSize.Level0)
115 {
116 int ret = CallGetAddrInfo(AF_INET6, 0);
117 EXPECT_EQ(ret, EAI_SYSTEM);
118 }
119
120 /**
121 * @tc.name: GetAddrInfoTest_005
122 * @tc.desc: test socket fail
123 * @tc.type: FUNC
124 */
125 HWTEST_F(GetAddrInfoTest, GetAddrInfoTest_005, TestSize.Level0)
126 {
127 int ret = CallGetAddrInfo(AF_INET6, SOCK_STREAM);
128 EXPECT_EQ(ret, EAI_SYSTEM);
129 }
130
131 /**
132 * @tc.name: GetAddrInfoTest_001
133 * @tc.desc: test socket fail
134 * @tc.type: FUNC
135 */
136 HWTEST_F(GetAddrInfoTest, GetAddrInfoTest_006, TestSize.Level0)
137 {
138 int ret = CallGetAddrInfo(AF_INET6, SOCK_DGRAM);
139 EXPECT_EQ(ret, EAI_SYSTEM);
140 }
141
142 } //namespace
143 } //namespace OHOS