1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #ifdef GTEST_API_
19 #define private public
20 #endif
21 #include "net_http_probe.h"
22 #include "net_http_probe_result.h"
23 #include "net_link_info.h"
24 #include "net_manager_constants.h"
25
26 namespace OHOS {
27 namespace NetManagerStandard {
28 namespace {
29 using namespace testing::ext;
30 constexpr int32_t TEST_NETID = 999;
31 constexpr const char *TEST_PROXY_HOST = "testHttpProxy";
32 constexpr const char *TEST_STRING = "testString";
33 constexpr const char *TEST_HTTP_URL = "http://connectivitycheck.platform.hicloud.com/generate_204";
34 constexpr const char *TEST_HTTPS_URL = "https://connectivitycheck.platform.hicloud.com/generate_204";
35
36 class NetHttpProbeTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 static inline std::shared_ptr<NetHttpProbe> instance_ = nullptr;
43 };
44
SetUpTestCase()45 void NetHttpProbeTest::SetUpTestCase()
46 {
47 instance_ = std::make_shared<NetHttpProbe>(TEST_NETID, NetBearType::BEARER_DEFAULT, NetLinkInfo(), PROBE_HTTP);
48 }
49
TearDownTestCase()50 void NetHttpProbeTest::TearDownTestCase() {}
51
SetUp()52 void NetHttpProbeTest::SetUp() {}
53
TearDown()54 void NetHttpProbeTest::TearDown() {}
55
56 HWTEST_F(NetHttpProbeTest, SendProbeTest001, TestSize.Level1)
57 {
58 instance_->GetHttpProbeResult();
59 instance_->GetHttpsProbeResult();
60 HttpProxy httpProxy = {TEST_PROXY_HOST, 0, {}};
61 NetLinkInfo info;
62 instance_->UpdateNetLinkInfo(info);
63 instance_->UpdateGlobalHttpProxy(httpProxy);
64 int32_t ret = instance_->SendProbe(PROBE_HTTP_HTTPS, TEST_HTTP_URL, TEST_HTTPS_URL);
65 EXPECT_EQ(ret, NETMANAGER_ERR_INTERNAL);
66 }
67
68 HWTEST_F(NetHttpProbeTest, NetHttpProbeBranchTest001, TestSize.Level1)
69 {
70 instance_->SendHttpProbeRequest();
71 instance_->RecvHttpProbeResponse();
72
73 std::string domain = "";
74 std::string result = instance_->GetAddrInfo(domain);
75 ASSERT_TRUE(result.empty());
76
77 int32_t port = 0;
78 bool ret = instance_->SetResolveOption(ProbeType::PROBE_HTTP, domain, TEST_STRING, port);
79 ASSERT_FALSE(ret);
80
81 ret = instance_->SetResolveOption(ProbeType::PROBE_HTTP, TEST_STRING, TEST_STRING, port);
82 ASSERT_FALSE(ret);
83
84 ret = instance_->SetResolveOption(ProbeType::PROBE_HTTPS, TEST_STRING, TEST_STRING, port);
85 ASSERT_FALSE(ret);
86
87 bool useProxy = true;
88 ret = instance_->SendDnsProbe(ProbeType::PROBE_HTTPS, TEST_STRING, TEST_STRING, useProxy);
89 ASSERT_TRUE(ret);
90
91 ret = instance_->SendDnsProbe(ProbeType::PROBE_HTTPS, TEST_STRING, TEST_STRING, useProxy);
92 ASSERT_TRUE(ret);
93 }
94
95 HWTEST_F(NetHttpProbeTest, CheckCurlGlobalInitState001, TestSize.Level1)
96 {
97 instance_->isCurlInit_ = false;
98 bool ret = instance_->CheckCurlGlobalInitState();
99 ASSERT_TRUE(ret);
100 }
101
102 HWTEST_F(NetHttpProbeTest, SetResolveOption001, TestSize.Level1)
103 {
104 std::string domain = "http:";
105 std::string ipAddress = "127.0.0.1";
106 int32_t port = 0;
107
108 bool ret = instance_->SetResolveOption(ProbeType::PROBE_HTTP, domain, ipAddress, port);
109 ASSERT_FALSE(ret);
110
111 instance_->CleanHttpCurl();
112 ASSERT_TRUE(instance_->httpResolveList_ == nullptr);
113
114 ret = instance_->SetResolveOption(ProbeType::PROBE_HTTPS, domain, ipAddress, port);
115 ASSERT_FALSE(ret);
116
117 instance_->CleanHttpCurl();
118 ASSERT_TRUE(instance_->httpsResolveList_ == nullptr);
119 }
120
121 HWTEST_F(NetHttpProbeTest, ExtractDomainFormUrl001, TestSize.Level1)
122 {
123 string url = "";
124
125 std::string ret = instance_->ExtractDomainFormUrl(url);
126 EXPECT_EQ(ret, std::string());
127 ret = instance_->ExtractDomainFormUrl(TEST_STRING);
128 EXPECT_EQ(ret, TEST_STRING);
129 }
130
131 HWTEST_F(NetHttpProbeTest, SetHttpOptions001, TestSize.Level1)
132 {
133 ProbeType probeType = ProbeType::PROBE_HTTP_HTTPS;
134 CURL *curl = nullptr;
135 std::string url = "";
136
137 bool ret = instance_->SetHttpOptions(probeType, curl, url);
138 ASSERT_FALSE(ret);
139 ret = instance_->SetHttpOptions(probeType, instance_->httpCurl_, url);
140 ASSERT_FALSE(ret);
141 ret = instance_->InitHttpCurl(ProbeType::PROBE_HTTP);
142 ASSERT_TRUE(ret);
143 ret = instance_->SetHttpOptions(ProbeType::PROBE_HTTP, instance_->httpCurl_, TEST_HTTPS_URL);
144 ASSERT_TRUE(ret);
145 }
146
147 HWTEST_F(NetHttpProbeTest, SetProxyOptionTest001, TestSize.Level1)
148 {
149 ProbeType probeType = ProbeType::PROBE_HTTP;
150 HttpProxy httpProxy = {"", 0, {}};
151 bool useHttpProxy = false;
152 instance_->defaultUseGlobalHttpProxy_ = true;
153
154 instance_->UpdateGlobalHttpProxy(httpProxy);
155 bool ret = instance_->SetProxyOption(probeType, useHttpProxy);
156 ASSERT_TRUE(ret);
157
158 instance_->CleanHttpCurl();
159 httpProxy = {"httpProxy", 0, {}};
160 instance_->UpdateGlobalHttpProxy(httpProxy);
161 ret = instance_->SetProxyOption(probeType, useHttpProxy);
162 ASSERT_TRUE(ret);
163
164 ret = instance_->InitHttpCurl(probeType);
165 ASSERT_TRUE(ret);
166 ret = instance_->SetProxyOption(probeType, useHttpProxy);
167 ASSERT_TRUE(ret);
168
169 probeType = ProbeType::PROBE_HTTPS;
170 instance_->CleanHttpCurl();
171 ret = instance_->SetProxyOption(probeType, useHttpProxy);
172 ASSERT_TRUE(ret);
173
174 ret = instance_->InitHttpCurl(probeType);
175 ASSERT_TRUE(ret);
176 ret = instance_->SetProxyOption(probeType, useHttpProxy);
177 ASSERT_TRUE(ret);
178 }
179
180 HWTEST_F(NetHttpProbeTest, SendDnsProbeTest001, TestSize.Level1)
181 {
182 bool useProxy = false;
183 std::string httpUrl = "";
184 std::string httpsUrl = "";
185 bool ret = instance_->SendDnsProbe(ProbeType::PROBE_HTTP, httpUrl, TEST_STRING, useProxy);
186 ASSERT_FALSE(ret);
187
188 ret = instance_->SendDnsProbe(ProbeType::PROBE_HTTPS, TEST_STRING, httpsUrl, useProxy);
189 ASSERT_FALSE(ret);
190 }
191
192 HWTEST_F(NetHttpProbeTest, SendHttpProbeRequestTest001, TestSize.Level1)
193 {
194 instance_->CleanHttpCurl();
195 instance_->SendHttpProbeRequest();
196 ASSERT_TRUE(instance_->curlMulti_ == nullptr);
197 instance_->RecvHttpProbeResponse();
198 ASSERT_TRUE(instance_->curlMulti_ == nullptr);
199
200 bool ret = instance_->InitHttpCurl(ProbeType::PROBE_HTTP);
201 ASSERT_TRUE(ret);
202 instance_->SendHttpProbeRequest();
203 ASSERT_TRUE(instance_->curlMulti_ != nullptr);
204 instance_->RecvHttpProbeResponse();
205 ASSERT_TRUE(instance_->curlMulti_ != nullptr);
206 }
207
208 HWTEST_F(NetHttpProbeTest, LoadProxyTest001, TestSize.Level1)
209 {
210 ProbeType probeType = ProbeType::PROBE_HTTP;
211 instance_->defaultUseGlobalHttpProxy_ = true;
212 HttpProxy httpProxy = {"httpProxy", 0, {}};
213 std::string proxyHost = "proxyHost";
214 int32_t proxyPort = 0;
215
216 instance_->UpdateGlobalHttpProxy(httpProxy);
217 bool ret = instance_->LoadProxy(proxyHost, proxyPort);
218 ASSERT_TRUE(ret);
219
220 instance_->defaultUseGlobalHttpProxy_ = false;
221 NetLinkInfo netLinkInfo;
222 netLinkInfo.httpProxy_ = {"127.0.0.1", 80, {"localhost"}};
223 instance_->UpdateNetLinkInfo(netLinkInfo);
224 ret = instance_->LoadProxy(proxyHost, proxyPort);
225 ASSERT_TRUE(ret);
226 }
227 } // namespace
228 } // namespace NetManagerStandard
229 } // namespace OHOS
230