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