• 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 <gtest/gtest.h>
17 
18 #include "net_detection_callback_stub.h"
19 #include "net_manager_constants.h"
20 #include "network.h"
21 
22 namespace OHOS {
23 namespace NetManagerStandard {
24 namespace {
25 using namespace testing::ext;
26 constexpr int32_t TEST_NETID = 12;
27 constexpr uint32_t TEST_SUPPLIERID = 214;
28 constexpr const char *TEST_RUNNER_CREATE = "RUNNER_CREATE";
29 class NetDetectionCallbackTest : public NetDetectionCallbackStub {
30 public:
OnNetDetectionResultChanged(NetDetectionResultCode detectionResult,const std::string & urlRedirect)31     inline int32_t OnNetDetectionResultChanged(NetDetectionResultCode detectionResult,
32                                                const std::string &urlRedirect) override
33     {
34         return NETMANAGER_SUCCESS;
35     }
36 };
37 } // namespace
38 
39 class NetworkTest : public testing::Test {
40 public:
41     static void SetUpTestCase();
42     static void TearDownTestCase();
43     void SetUp();
44     void TearDown();
45     static inline std::shared_ptr<Network> instance_ = nullptr;
46     static inline sptr<INetDetectionCallback> callabck_ = new (std::nothrow) NetDetectionCallbackTest();
47 };
48 
SetUpTestCase()49 void NetworkTest::SetUpTestCase()
50 {
51     NetDetectionHandler detectionHandler = [](uint32_t supplierId, bool ifValid) {
52         std::cout << "supplierId:" << supplierId;
53         std::cout << "IfValid:" << ifValid << std::endl;
54     };
55     auto runner = AppExecFwk::EventRunner::Create(TEST_RUNNER_CREATE);
56     auto eventHandler = std::make_shared<NetConnEventHandler>(runner);
57     instance_ = std::make_shared<Network>(TEST_NETID, TEST_SUPPLIERID, detectionHandler, NetBearType::BEARER_ETHERNET,
58                                           eventHandler);
59 }
60 
TearDownTestCase()61 void NetworkTest::TearDownTestCase() {}
62 
SetUp()63 void NetworkTest::SetUp() {}
64 
TearDown()65 void NetworkTest::TearDown() {}
66 
67 HWTEST_F(NetworkTest, operatorTest001, TestSize.Level1)
68 {
69     int32_t netId1 = 2;
70     int32_t netId2 = 3;
71     uint32_t supplierId = 4445;
72     Network work0(netId1, supplierId, nullptr, NetBearType::BEARER_ETHERNET, nullptr);
73     Network work1(netId1, TEST_SUPPLIERID, nullptr, NetBearType::BEARER_ETHERNET, nullptr);
74     EXPECT_TRUE(work0 == work1);
75     Network work2(netId2, TEST_SUPPLIERID, nullptr, NetBearType::BEARER_ETHERNET, nullptr);
76     EXPECT_FALSE(work1 == work2);
77 }
78 HWTEST_F(NetworkTest, GetNetIdTest001, TestSize.Level1)
79 {
80     int32_t ret = instance_->GetNetId();
81     EXPECT_EQ(ret, TEST_NETID);
82 }
83 
84 HWTEST_F(NetworkTest, UpdateBasicNetworkTest001, TestSize.Level1)
85 {
86     bool ret = instance_->UpdateBasicNetwork(true);
87     EXPECT_TRUE(ret);
88     ret = instance_->UpdateBasicNetwork(true);
89     EXPECT_TRUE(ret);
90 }
91 
92 HWTEST_F(NetworkTest, UpdateBasicNetworkTest002, TestSize.Level1)
93 {
94     bool ret = instance_->UpdateBasicNetwork(false);
95     EXPECT_TRUE(ret);
96     ret = instance_->UpdateBasicNetwork(false);
97     EXPECT_TRUE(ret);
98 }
99 
100 HWTEST_F(NetworkTest, UpdateNetLinkInfoTest001, TestSize.Level1)
101 {
102     NetLinkInfo info;
103     bool ret = instance_->UpdateNetLinkInfo(info);
104     EXPECT_TRUE(ret);
105 }
106 HWTEST_F(NetworkTest, GetNetLinkInfoTest001, TestSize.Level1)
107 {
108     NetLinkInfo ret = instance_->GetNetLinkInfo();
109     EXPECT_FALSE(ret.ToString("").empty());
110 }
111 HWTEST_F(NetworkTest, UpdateTest001, TestSize.Level1)
112 {
113     NetLinkInfo info;
114     instance_->UpdateIpAddrs(info);
115     instance_->UpdateInterfaces(info);
116     instance_->UpdateRoutes(info);
117     instance_->UpdateDns(info);
118     instance_->UpdateMtu(info);
119     instance_->RegisterNetDetectionCallback(callabck_);
120     int32_t ret = instance_->UnRegisterNetDetectionCallback(callabck_);
121     EXPECT_EQ(ret, NETMANAGER_SUCCESS);
122 }
123 HWTEST_F(NetworkTest, StartNetDetectionTest001, TestSize.Level1)
124 {
125     instance_->StartNetDetection(true);
126     instance_->StartNetDetection(false);
127     instance_->SetDefaultNetWork();
128     instance_->ClearDefaultNetWorkNetId();
129     instance_->UpdateNetConnState(NetConnState::NET_CONN_STATE_CONNECTING);
130     bool ret = instance_->IsConnecting();
131     EXPECT_TRUE(ret);
132     instance_->UpdateNetConnState(NetConnState::NET_CONN_STATE_CONNECTED);
133     ret = instance_->IsConnected();
134     EXPECT_TRUE(ret);
135     instance_->UpdateNetConnState(NetConnState::NET_CONN_STATE_DISCONNECTING);
136     ret = instance_->IsConnecting();
137     EXPECT_FALSE(ret);
138     instance_->UpdateNetConnState(NetConnState::NET_CONN_STATE_DISCONNECTED);
139     ret = instance_->IsConnected();
140     EXPECT_FALSE(ret);
141     instance_->UpdateNetConnState(NetConnState::NET_CONN_STATE_CONNECTING);
142     std::string urlRedirect = "test_redirect";
143     instance_->OnHandleNetMonitorResult(NetDetectionStatus::INVALID_DETECTION_STATE, urlRedirect);
144 }
145 } // namespace NetManagerStandard
146 } // namespace OHOS