• 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_);
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 } // namespace NetManagerStandard
92 } // namespace OHOS