1 /*
2 * Copyright (c) 2022 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 <netdb.h>
17 #include <gtest/gtest.h>
18 #include <sys/socket.h>
19
20 #include "i_net_monitor_callback.h"
21 #include "net_manager_constants.h"
22 #define private public
23 #include "net_monitor.h"
24 #undef private
25 #include "netmanager_base_common_utils.h"
26
27 namespace OHOS {
28 namespace NetManagerStandard {
29 namespace {
30 using namespace testing::ext;
31 constexpr uint32_t TEST_NETID = 999;
32 constexpr int32_t SUCCESS_CODE = 204;
33 constexpr int32_t PORTAL_CODE_MIN = 200;
34 constexpr int32_t SIM_PORTAL_CODE = 302;
35 constexpr int32_t MAX_FAILED_DETECTION_DELAY_MS = 10 * 60 * 1000;
36 constexpr int32_t ONE_URL_DETECT_NUM = 4;
37 constexpr int32_t ALL_DETECT_THREAD_NUM = 8;
38
39 class TestMonitorCallback : public INetMonitorCallback {
40 public:
OnHandleNetMonitorResult(NetDetectionStatus netDetectionState,const std::string & urlRedirect)41 inline void OnHandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect) override
42 {
43 (void)netDetectionState;
44 (void)urlRedirect;
45 }
46 };
47 } // namespace
48
49 class NetMonitorTest : public testing::Test {
50 public:
51 static void SetUpTestCase();
52 static void TearDownTestCase();
53 void SetUp();
54 void TearDown();
55 static inline std::shared_ptr<INetMonitorCallback> callback_ = std::make_shared<TestMonitorCallback>();
56 static inline NetMonitorInfo info = {true, 0};
57 static inline std::shared_ptr<NetMonitor> instance_ =
58 std::make_shared<NetMonitor>(TEST_NETID, BEARER_DEFAULT, NetLinkInfo(), callback_, info);
59 };
60
SetUpTestCase()61 void NetMonitorTest::SetUpTestCase()
62 {
63 instance_->isScreenOn_ = true;
64 instance_->Start();
65 }
66
TearDownTestCase()67 void NetMonitorTest::TearDownTestCase()
68 {
69 instance_->Stop();
70 }
71
SetUp()72 void NetMonitorTest::SetUp() {}
73
TearDown()74 void NetMonitorTest::TearDown() {}
75
76 HWTEST_F(NetMonitorTest, IsDetectingTest001, TestSize.Level1)
77 {
78 bool ret = instance_->IsDetecting();
79 EXPECT_TRUE(ret);
80 instance_->Detection();
81 instance_->Stop();
82 }
83
84 HWTEST_F(NetMonitorTest, SendHttpProbe001, TestSize.Level1)
85 {
86 std::string domain;
87 std::string urlPath;
88 NetHttpProbeResult probeResult = instance_->SendProbe();
89 EXPECT_EQ(probeResult.IsFailed(), true);
90 }
91
92 HWTEST_F(NetMonitorTest, GetHttpProbeUrlFromConfig001, TestSize.Level1)
93 {
94 instance_->GetHttpProbeUrlFromConfig();
95 EXPECT_FALSE(instance_->httpUrl_.empty());
96 EXPECT_FALSE(instance_->httpsUrl_.empty());
97 EXPECT_FALSE(instance_->fallbackHttpUrl_.empty());
98 EXPECT_FALSE(instance_->fallbackHttpsUrl_.empty());
99 }
100
101 HWTEST_F(NetMonitorTest, StartTest001, TestSize.Level1)
102 {
103 bool ret = instance_->IsDetecting();
104 EXPECT_FALSE(ret);
105 instance_->Start();
106 instance_->Stop();
107 ret = instance_->IsDetecting();
108 EXPECT_FALSE(ret);
109 }
110
111 HWTEST_F(NetMonitorTest, ProcessDetectionTest001, TestSize.Level1)
112 {
113 NetHttpProbeResult probeResult;
114 probeResult.responseCode_ = 204;
115 NetDetectionStatus result;
116 instance_->ProcessDetection(probeResult, result);
117 EXPECT_EQ(result, VERIFICATION_STATE);
118 probeResult.responseCode_ = 302;
119 instance_->netBearType_ = BEARER_CELLULAR;
120 instance_->ProcessDetection(probeResult, result);
121 EXPECT_EQ(result, CAPTIVE_PORTAL_STATE);
122 probeResult.responseCode_ = 200;
123 instance_->ProcessDetection(probeResult, result);
124 EXPECT_EQ(result, CAPTIVE_PORTAL_STATE);
125 probeResult.responseCode_ = 302;
126 instance_->netBearType_ = BEARER_WIFI;
127 instance_->detectionDelay_ = 0;
128 instance_->ProcessDetection(probeResult, result);
129 EXPECT_EQ(result, CAPTIVE_PORTAL_STATE);
130 instance_->ProcessDetection(probeResult, result);
131 EXPECT_EQ(result, CAPTIVE_PORTAL_STATE);
132 instance_->detectionDelay_ = 10 * 60 * 1000;
133 instance_->ProcessDetection(probeResult, result);
134 EXPECT_EQ(result, CAPTIVE_PORTAL_STATE);
135 instance_->isDetecting_ = true;
136 instance_->ProcessDetection(probeResult, result);
137 EXPECT_EQ(result, CAPTIVE_PORTAL_STATE);
138 instance_->isDetecting_ = false;
139 instance_->ProcessDetection(probeResult, result);
140 EXPECT_EQ(result, CAPTIVE_PORTAL_STATE);
141 }
142
143 HWTEST_F(NetMonitorTest, DetectionTest001, TestSize.Level1)
144 {
145 instance_->isDetecting_ = true;
146 instance_->Detection();
147 instance_->isDetecting_ = false;
148 instance_->Detection();
149 instance_->Stop();
150 bool ret = instance_->IsDetecting();
151 EXPECT_FALSE(ret);
152 }
153
154 HWTEST_F(NetMonitorTest, DetectionTest002, TestSize.Level1)
155 {
156 instance_->isDetecting_ = true;
157 instance_->detectionDelay_ = 0;
158 instance_->Start();
159 EXPECT_TRUE(instance_->IsDetecting());
160 instance_->detectionDelay_ = 1;
161 instance_->Start();
162 EXPECT_TRUE(instance_->IsDetecting());
163 }
164
165 HWTEST_F(NetMonitorTest, CheckIfSettingsDataReadyTest001, TestSize.Level1)
166 {
167 instance_->isDataShareReady_ = true;
168 EXPECT_EQ(instance_->CheckIfSettingsDataReady(), true);
169 }
170
171 HWTEST_F(NetMonitorTest, ProcessDetectionTest002, TestSize.Level1)
172 {
173 instance_->isDetecting_ = true;
174 instance_->detectionDelay_ = 1;
175 auto monitorCallback = instance_->netMonitorCallback_.lock();
176 instance_->netMonitorCallback_.reset();
177 instance_->Start();
178 instance_->netMonitorCallback_ = monitorCallback;
179
180 instance_->isDetecting_ = false;
181 NetHttpProbeResult probeResult;
182 probeResult.responseCode_ = SIM_PORTAL_CODE + 1;
183 NetDetectionStatus result;
184 instance_->isScreenOn_ = true;
185 instance_->ProcessDetection(probeResult, result);
186 EXPECT_EQ(result, CAPTIVE_PORTAL_STATE);
187
188 instance_->isScreenOn_ = false;
189 instance_->netBearType_ = BEARER_CELLULAR;
190 instance_->ProcessDetection(probeResult, result);
191 EXPECT_EQ(result, CAPTIVE_PORTAL_STATE);
192
193 instance_->netBearType_ = BEARER_WIFI;
194 instance_->ProcessDetection(probeResult, result);
195 EXPECT_EQ(result, CAPTIVE_PORTAL_STATE);
196
197 probeResult.responseCode_ = PORTAL_CODE_MIN - 1;
198 instance_->detectionDelay_ = MAX_FAILED_DETECTION_DELAY_MS;
199 instance_->ProcessDetection(probeResult, result);
200 EXPECT_EQ(result, INVALID_DETECTION_STATE);
201 }
202
203 HWTEST_F(NetMonitorTest, ProcessThreadDetectResultTest001, TestSize.Level1)
204 {
205 auto latch = std::make_shared<TinyCountDownLatch>(ONE_URL_DETECT_NUM);
206 auto latchAll = std::make_shared<TinyCountDownLatch>(ALL_DETECT_THREAD_NUM);
207 auto netId = instance_->netId_;
208 auto netBearType = instance_->netBearType_;
209 auto &netLinkInfo = instance_->netLinkInfo_;
210 auto httpUrl = instance_->httpUrl_;
211 auto httpsUrl = instance_->httpsUrl_;
212 auto httpProbeThread = std::make_shared<ProbeThread>(netId, netBearType,
213 netLinkInfo, latch, latchAll, ProbeType::PROBE_HTTP, httpUrl, httpsUrl);
214 auto httpsProbeThread = std::make_shared<ProbeThread>(netId, netBearType,
215 netLinkInfo, latch, latchAll, ProbeType::PROBE_HTTPS, httpUrl, httpsUrl);
216 auto backHttpThread = std::make_shared<ProbeThread>(netId, netBearType,
217 netLinkInfo, latch, latchAll, ProbeType::PROBE_HTTP_FALLBACK, httpUrl, httpsUrl);
218 auto backHttpsThread = std::make_shared<ProbeThread>(netId, netBearType,
219 netLinkInfo, latch, latchAll, ProbeType::PROBE_HTTPS_FALLBACK, httpUrl, httpsUrl);
220 httpProbeThread->httpProbe_->httpsProbeResult_.responseCode_ = PORTAL_CODE_MIN;
221 auto ret = instance_->ProcessThreadDetectResult(httpProbeThread, httpsProbeThread, backHttpThread, backHttpsThread);
222 EXPECT_EQ(ret.responseCode_, 0);
223
224 httpProbeThread->httpProbe_->httpsProbeResult_.responseCode_ = SUCCESS_CODE;
225 backHttpThread->httpProbe_->httpsProbeResult_.responseCode_ = PORTAL_CODE_MIN;
226 ret = instance_->ProcessThreadDetectResult(httpProbeThread, httpsProbeThread, backHttpThread, backHttpsThread);
227 EXPECT_EQ(ret.responseCode_, 0);
228
229 backHttpThread->httpProbe_->httpsProbeResult_.responseCode_ = SUCCESS_CODE;
230 httpsProbeThread->httpProbe_->httpsProbeResult_.responseCode_ = SUCCESS_CODE;
231 ret = instance_->ProcessThreadDetectResult(httpProbeThread, httpsProbeThread, backHttpThread, backHttpsThread);
232 EXPECT_EQ(ret.responseCode_, SUCCESS_CODE);
233
234 httpsProbeThread->httpProbe_->httpsProbeResult_.responseCode_ = PORTAL_CODE_MIN;
235 backHttpsThread->httpProbe_->httpsProbeResult_.responseCode_ = SUCCESS_CODE;
236 ret = instance_->ProcessThreadDetectResult(httpProbeThread, httpsProbeThread, backHttpThread, backHttpsThread);
237 EXPECT_EQ(ret.responseCode_, SUCCESS_CODE);
238 }
239
240 HWTEST_F(NetMonitorTest, ProcessThreadDetectResultTest002, TestSize.Level1)
241 {
242 auto latch = std::make_shared<TinyCountDownLatch>(ONE_URL_DETECT_NUM);
243 auto latchAll = std::make_shared<TinyCountDownLatch>(ALL_DETECT_THREAD_NUM);
244 auto netId = instance_->netId_;
245 auto netBearType = instance_->netBearType_;
246 auto &netLinkInfo = instance_->netLinkInfo_;
247 auto httpUrl = instance_->httpUrl_;
248 auto httpsUrl = instance_->httpsUrl_;
249 auto httpProbeThread = std::make_shared<ProbeThread>(netId, netBearType,
250 netLinkInfo, latch, latchAll, ProbeType::PROBE_HTTP, httpUrl, httpsUrl);
251 auto httpsProbeThread = std::make_shared<ProbeThread>(netId, netBearType,
252 netLinkInfo, latch, latchAll, ProbeType::PROBE_HTTPS, httpUrl, httpsUrl);
253 auto backHttpThread = std::make_shared<ProbeThread>(netId, netBearType,
254 netLinkInfo, latch, latchAll, ProbeType::PROBE_HTTP_FALLBACK, httpUrl, httpsUrl);
255 auto backHttpsThread = std::make_shared<ProbeThread>(netId, netBearType,
256 netLinkInfo, latch, latchAll, ProbeType::PROBE_HTTPS_FALLBACK, httpUrl, httpsUrl);
257 httpProbeThread->httpProbe_->httpsProbeResult_.responseCode_ = PORTAL_CODE_MIN - 1;
258 backHttpsThread->httpProbe_->httpsProbeResult_.responseCode_ = SUCCESS_CODE;
259 httpsProbeThread->httpProbe_->httpsProbeResult_.responseCode_ = PORTAL_CODE_MIN;
260 backHttpsThread->httpProbe_->httpsProbeResult_.responseCode_ = PORTAL_CODE_MIN;
261 auto ret = instance_->ProcessThreadDetectResult(httpProbeThread, httpsProbeThread, backHttpThread, backHttpsThread);
262 EXPECT_EQ(ret.responseCode_, PORTAL_CODE_MIN);
263
264 httpProbeThread->httpProbe_->httpsProbeResult_.responseCode_ = SUCCESS_CODE;
265 backHttpThread->httpProbe_->httpsProbeResult_.responseCode_ = PORTAL_CODE_MIN - 1;
266 ret = instance_->ProcessThreadDetectResult(httpProbeThread, httpsProbeThread, backHttpThread, backHttpsThread);
267 EXPECT_EQ(ret.responseCode_, PORTAL_CODE_MIN);
268
269 httpProbeThread->httpProbe_->httpsProbeResult_.responseCode_ = SUCCESS_CODE;
270 backHttpThread->httpProbe_->httpsProbeResult_.responseCode_ = SUCCESS_CODE;
271 ret = instance_->ProcessThreadDetectResult(httpProbeThread, httpsProbeThread, backHttpThread, backHttpsThread);
272 EXPECT_NE(ret.responseCode_, SUCCESS_CODE);
273 }
274
275 HWTEST_F(NetMonitorTest, GetHttpProbeUrlFromConfigTest002, TestSize.Level1)
276 {
277 auto isNeedSuffix = instance_->isNeedSuffix_;
278 instance_->isNeedSuffix_ = false;
279 instance_->GetHttpProbeUrlFromConfig();
280 EXPECT_FALSE(instance_->httpsUrl_.empty());
281 EXPECT_FALSE(instance_->fallbackHttpUrl_.empty());
282 EXPECT_FALSE(instance_->fallbackHttpsUrl_.empty());
283 instance_->isNeedSuffix_ = isNeedSuffix;
284 }
285
286 HWTEST_F(NetMonitorTest, CreateProbeThreadTest001, TestSize.Level1)
287 {
288 std::shared_ptr<TinyCountDownLatch> latch = std::make_shared<TinyCountDownLatch>(1);
289 std::shared_ptr<TinyCountDownLatch> latchAll = std::make_shared<TinyCountDownLatch>(2);
290 std::shared_ptr<ProbeThread> httpThread = nullptr;
291 std::shared_ptr<ProbeThread> httpsThread = nullptr;
292 instance_->netBearType_ = BEARER_CELLULAR;
293 EXPECT_NO_THROW(instance_->CreateProbeThread(httpThread, httpsThread, latch, latchAll, true));
294 EXPECT_NO_THROW(instance_->CreateProbeThread(httpThread, httpsThread, latch, latchAll, false));
295 instance_->netBearType_ = BEARER_WIFI;
296 EXPECT_NO_THROW(instance_->CreateProbeThread(httpThread, httpsThread, latch, latchAll, true));
297 EXPECT_NO_THROW(instance_->CreateProbeThread(httpThread, httpsThread, latch, latchAll, false));
298 }
299
300 HWTEST_F(NetMonitorTest, StartProbeTest001, TestSize.Level1)
301 {
302 instance_->netBearType_ = BEARER_CELLULAR;
303 EXPECT_NO_THROW(instance_->Detection());
304 instance_->netBearType_ = BEARER_WIFI;
305 EXPECT_NO_THROW(instance_->Detection());
306 }
307
308 HWTEST_F(NetMonitorTest, DetectionDelayWhenScreenOffTest001, TestSize.Level1)
309 {
310 instance_->isScreenOn_ = false;
311 instance_->isDetecting_ = false;
312 instance_->lastDetectTimestamp_ = CommonUtils::GetCurrentMilliSecond();
313 sleep(1);
314 instance_->Start();
315 EXPECT_TRUE(instance_->isDetecting_);
316 sleep(1);
317 instance_->Stop();
318 }
319
320 } // namespace NetManagerStandard
321 } // namespace OHOS