• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 namespace {
29 using namespace testing::ext;
30 constexpr uint32_t TEST_NETID = 999;
31 
32 class TestMonitorCallback : public INetMonitorCallback {
33 public:
OnHandleNetMonitorResult(NetDetectionStatus netDetectionState,const std::string & urlRedirect)34     inline void OnHandleNetMonitorResult(NetDetectionStatus netDetectionState, const std::string &urlRedirect) override
35     {
36         (void)netDetectionState;
37         (void)urlRedirect;
38     }
39 };
40 } // namespace
41 
42 class NetMonitorTest : public testing::Test {
43 public:
44     static void SetUpTestCase();
45     static void TearDownTestCase();
46     void SetUp();
47     void TearDown();
48     static inline std::shared_ptr<INetMonitorCallback> callback_ = std::make_shared<TestMonitorCallback>();
49     static inline std::shared_ptr<NetMonitor> instance_ =
50         std::make_shared<NetMonitor>(TEST_NETID, BEARER_DEFAULT, NetLinkInfo(), callback_, true);
51 };
52 
SetUpTestCase()53 void NetMonitorTest::SetUpTestCase()
54 {
55     instance_->Start();
56 }
57 
TearDownTestCase()58 void NetMonitorTest::TearDownTestCase()
59 {
60     instance_->Stop();
61 }
62 
SetUp()63 void NetMonitorTest::SetUp() {}
64 
TearDown()65 void NetMonitorTest::TearDown() {}
66 
67 HWTEST_F(NetMonitorTest, IsDetectingTest001, TestSize.Level1)
68 {
69     bool ret = instance_->IsDetecting();
70     EXPECT_TRUE(ret);
71     instance_->Detection();
72     instance_->Stop();
73 }
74 
75 HWTEST_F(NetMonitorTest, SendHttpProbe001, TestSize.Level1)
76 {
77     std::string domain;
78     std::string urlPath;
79     NetHttpProbeResult probeResult = instance_->SendProbe();
80     EXPECT_EQ(probeResult.IsFailed(), true);
81 }
82 
83 HWTEST_F(NetMonitorTest, GetHttpProbeUrlFromConfig001, TestSize.Level1)
84 {
85     instance_->GetHttpProbeUrlFromConfig();
86     EXPECT_FALSE(instance_->httpUrl_.empty());
87     EXPECT_FALSE(instance_->httpsUrl_.empty());
88     EXPECT_FALSE(instance_->fallbackHttpUrl_.empty());
89     EXPECT_FALSE(instance_->fallbackHttpsUrl_.empty());
90 }
91 
92 HWTEST_F(NetMonitorTest, StartTest001, TestSize.Level1)
93 {
94     bool ret = instance_->IsDetecting();
95     EXPECT_FALSE(ret);
96     instance_->Start();
97     instance_->Stop();
98     ret = instance_->IsDetecting();
99     EXPECT_FALSE(ret);
100 }
101 
102 HWTEST_F(NetMonitorTest, ProcessDetectionTest001, TestSize.Level1)
103 {
104     NetHttpProbeResult probeResult;
105     probeResult.responseCode_ = 204;
106     NetDetectionStatus result;
107     instance_->ProcessDetection(probeResult, result);
108     EXPECT_EQ(result, VERIFICATION_STATE);
109     probeResult.responseCode_ = 302;
110     instance_->netBearType_ = BEARER_CELLULAR;
111     instance_->ProcessDetection(probeResult, result);
112     EXPECT_EQ(result, CAPTIVE_PORTAL_STATE);
113     probeResult.responseCode_ = 200;
114     instance_->ProcessDetection(probeResult, result);
115     EXPECT_EQ(result, CAPTIVE_PORTAL_STATE);
116     probeResult.responseCode_ = 302;
117     instance_->netBearType_ = BEARER_WIFI;
118     instance_->detectionDelay_ = 0;
119     instance_->ProcessDetection(probeResult, result);
120     EXPECT_NE(result, INVALID_DETECTION_STATE);
121     instance_->ProcessDetection(probeResult, result);
122     EXPECT_NE(result, INVALID_DETECTION_STATE);
123     instance_->detectionDelay_ = 10 * 60 * 1000;
124     instance_->ProcessDetection(probeResult, result);
125     EXPECT_NE(result, INVALID_DETECTION_STATE);
126     instance_->isDetecting_ = true;
127     instance_->ProcessDetection(probeResult, result);
128     EXPECT_NE(result, INVALID_DETECTION_STATE);
129     instance_->isDetecting_ = false;
130     instance_->ProcessDetection(probeResult, result);
131     EXPECT_NE(result, INVALID_DETECTION_STATE);
132 }
133 
134 HWTEST_F(NetMonitorTest, DetectionTest001, TestSize.Level1)
135 {
136     instance_->isDetecting_ = true;
137     instance_->Detection();
138     instance_->isDetecting_ = false;
139     instance_->Detection();
140     instance_->Stop();
141     bool ret = instance_->IsDetecting();
142     EXPECT_FALSE(ret);
143 }
144 
145 } // namespace NetManagerStandard
146 } // namespace OHOS