• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 #include <memory>
18 
19 #include "common_net_conn_callback_test.h"
20 #include "net_conn_callback_stub.h"
21 #include "net_manager_constants.h"
22 #define private public
23 #include "net_activate.h"
24 #undef private
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 namespace {
29 using namespace testing::ext;
30 constexpr uint32_t TEST_TIMEOUT_MS = 1000;
31 constexpr uint32_t TEST_REQUEST_ID = 54656;
32 constexpr const char *TEST_IDENT = "testIdent";
33 
34 class NetActivateCallbackTest : public INetActivateCallback {
OnNetActivateTimeOut(uint32_t reqId)35     void OnNetActivateTimeOut(uint32_t reqId) override
36     {
37         std::cout << "Activate network request " << reqId << " timeout." << std::endl;
38     }
39 };
40 } // namespace
41 
42 class NetActivateTest : 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<AppExecFwk::EventRunner> netActEventRunner_ = nullptr;
49     static inline std::shared_ptr<AppExecFwk::EventHandler> netActEventHandler_ = nullptr;
50     static inline std::shared_ptr<NetActivate> instance_ = nullptr;
51     static inline sptr<INetConnCallback> callback_ = nullptr;
52     static inline sptr<NetSpecifier> specifier_ = nullptr;
53     static inline std::shared_ptr<INetActivateCallback> timeoutCallback_ = nullptr;
54 };
55 
SetUpTestCase()56 void NetActivateTest::SetUpTestCase()
57 {
58     callback_ = new (std::nothrow) NetConnCallbackStubCb();
59     specifier_ = new (std::nothrow) NetSpecifier();
60     timeoutCallback_ = std::make_shared<NetActivateCallbackTest>();
61     netActEventRunner_ = AppExecFwk::EventRunner::Create("NET_ACTIVATE_WORK_THREAD");
62     netActEventHandler_ = std::make_shared<AppExecFwk::EventHandler>(netActEventRunner_);
63     instance_ =
64         std::make_shared<NetActivate>(specifier_, callback_, timeoutCallback_, TEST_TIMEOUT_MS, netActEventHandler_);
65 }
66 
TearDownTestCase()67 void NetActivateTest::TearDownTestCase()
68 {
69     instance_ = nullptr;
70 }
71 
SetUp()72 void NetActivateTest::SetUp() {}
73 
TearDown()74 void NetActivateTest::TearDown() {}
75 
76 HWTEST_F(NetActivateTest, MatchRequestAndNetworkTest001, TestSize.Level1)
77 {
78     std::set<NetCap> netCaps;
79     netCaps.insert(NET_CAPABILITY_INTERNET);
80     sptr<NetSupplier> supplier = new (std::nothrow) NetSupplier(NetBearType::BEARER_ETHERNET, TEST_IDENT, netCaps);
81     bool ret = instance_->MatchRequestAndNetwork(supplier);
82     EXPECT_TRUE(ret);
83     sptr<NetSupplier> supplier001 = nullptr;
84     ret = instance_->MatchRequestAndNetwork(supplier001);
85     EXPECT_FALSE(ret);
86     std::string test;
87     sptr<NetSupplier> supplier002 = new (std::nothrow) NetSupplier(NetBearType::BEARER_ETHERNET, test, netCaps);
88     ret = instance_->MatchRequestAndNetwork(supplier002);
89     EXPECT_TRUE(ret);
90     std::set<NetCap> netCaps1;
91     netCaps1.insert(NET_CAPABILITY_INTERNET);
92     sptr<NetSupplier> supplier003 = new (std::nothrow) NetSupplier(NetBearType::BEARER_ETHERNET, TEST_IDENT, netCaps1);
93     ret = instance_->MatchRequestAndNetwork(supplier003);
94     EXPECT_TRUE(ret);
95     sptr<NetSupplier> supplier004 = new (std::nothrow) NetSupplier(NetBearType::BEARER_CELLULAR, TEST_IDENT, netCaps);
96     ret = instance_->MatchRequestAndNetwork(supplier004);
97     EXPECT_TRUE(ret);
98 }
99 
100 HWTEST_F(NetActivateTest, MatchRequestAndNetworkTest002, TestSize.Level1)
101 {
102     std::set<NetCap> netCaps;
103     netCaps.insert(NET_CAPABILITY_INTERNET);
104     sptr<NetSupplier> supplier = nullptr;
105     bool ret = instance_->MatchRequestAndNetwork(supplier);
106     EXPECT_EQ(ret, false);
107     supplier = new (std::nothrow) NetSupplier(NetBearType::BEARER_ETHERNET, TEST_IDENT, netCaps);
108     ret = instance_->MatchRequestAndNetwork(supplier);
109     EXPECT_TRUE(ret);
110 }
111 
112 HWTEST_F(NetActivateTest, SetGetRequestIdTest001, TestSize.Level1)
113 {
114     instance_->SetRequestId(TEST_REQUEST_ID);
115     uint32_t requestId = instance_->GetRequestId();
116     EXPECT_EQ(requestId, TEST_REQUEST_ID);
117 }
118 
119 HWTEST_F(NetActivateTest, SetGetServiceSupplyTest001, TestSize.Level1)
120 {
121     std::set<NetCap> netCaps;
122     sptr<NetSupplier> supplier = new (std::nothrow) NetSupplier(NetBearType::BEARER_ETHERNET, TEST_IDENT, netCaps);
123     instance_->SetServiceSupply(supplier);
124     auto result = instance_->GetServiceSupply();
125     EXPECT_EQ(result, supplier);
126 }
127 
128 HWTEST_F(NetActivateTest, GetNetCallbackTest001, TestSize.Level1)
129 {
130     auto result = instance_->GetNetCallback();
131     EXPECT_EQ(result, callback_);
132 }
133 
134 HWTEST_F(NetActivateTest, GetNetSpecifierTest001, TestSize.Level1)
135 {
136     auto result = instance_->GetNetSpecifier();
137     EXPECT_EQ(result, specifier_);
138 }
139 
140 HWTEST_F(NetActivateTest, CompareByNetworkIdentTest001, TestSize.Level1)
141 {
142     auto result = instance_->GetNetSpecifier();
143     EXPECT_EQ(result, specifier_);
144 }
145 
146 HWTEST_F(NetActivateTest, CompareByNetworkIdent001, TestSize.Level1)
147 {
148     std::string ident;
149     bool ret = instance_->CompareByNetworkIdent(ident, BEARER_DEFAULT, false);
150     EXPECT_EQ(ret, true);
151 
152     ident = "test1234";
153     ret = instance_->CompareByNetworkIdent(ident, BEARER_DEFAULT, false);
154     EXPECT_EQ(ret, true);
155 
156     instance_->netSpecifier_->ident_ = "test1234";
157     ret = instance_->CompareByNetworkIdent(ident, BEARER_DEFAULT, false);
158     EXPECT_EQ(ret, true);
159 
160     ident = "test5678";
161     ret = instance_->CompareByNetworkIdent(ident, BEARER_DEFAULT, false);
162     EXPECT_EQ(ret, false);
163 
164     instance_->netSpecifier_->ident_ = "wifi";
165     ret = instance_->CompareByNetworkIdent(ident, BEARER_DEFAULT, true);
166     EXPECT_EQ(ret, true);
167 
168     ret = instance_->CompareByNetworkIdent(ident, BEARER_WIFI, true);
169     EXPECT_EQ(ret, true);
170 }
171 
172 HWTEST_F(NetActivateTest, CompareByNetworkCapabilities001, TestSize.Level1)
173 {
174     NetCaps netCaps;
175     netCaps.InsertNetCap(NetCap::NET_CAPABILITY_INTERNET);
176     bool ret = instance_->CompareByNetworkCapabilities(netCaps);
177     EXPECT_EQ(ret, true);
178 
179     sptr<INetConnCallback> callback = new (std::nothrow) NetConnCallbackStubCb();
180     sptr<NetSpecifier> specifier = nullptr;
181     std::weak_ptr<INetActivateCallback> timeoutCallback = std::make_shared<NetActivateCallbackTest>();
182     std::shared_ptr<NetActivate> testNetActivate =
183         std::make_shared<NetActivate>(specifier, callback, timeoutCallback, TEST_TIMEOUT_MS, netActEventHandler_);
184 
185     ret = testNetActivate->CompareByNetworkCapabilities(netCaps);
186     EXPECT_EQ(ret, false);
187 }
188 
189 HWTEST_F(NetActivateTest, CompareByNetworkNetType001, TestSize.Level1)
190 {
191     NetBearType bearerType = BEARER_WIFI;
192     bool ret = instance_->CompareByNetworkNetType(bearerType);
193     EXPECT_EQ(ret, true);
194 
195     instance_->netSpecifier_->SetType(BEARER_WIFI);
196     ret = instance_->CompareByNetworkNetType(bearerType);
197     EXPECT_EQ(ret, true);
198 
199     bearerType = BEARER_ETHERNET;
200     ret = instance_->CompareByNetworkNetType(bearerType);
201     EXPECT_EQ(ret, false);
202 
203     instance_->netSpecifier_->netCapabilities_.bearerTypes_.clear();
204     ret = instance_->CompareByNetworkNetType(bearerType);
205     EXPECT_EQ(ret, true);
206 
207     sptr<INetConnCallback> callback = new (std::nothrow) NetConnCallbackStubCb();
208     sptr<NetSpecifier> specifier = nullptr;
209     std::weak_ptr<INetActivateCallback> timeoutCallback = std::make_shared<NetActivateCallbackTest>();
210     std::shared_ptr<NetActivate> testNetActivate =
211         std::make_shared<NetActivate>(specifier, callback, timeoutCallback, TEST_TIMEOUT_MS, netActEventHandler_);
212     ret = testNetActivate->CompareByNetworkNetType(bearerType);
213     EXPECT_EQ(ret, false);
214 }
215 
216 HWTEST_F(NetActivateTest, CompareByNetworkBand001, TestSize.Level1)
217 {
218     uint32_t netLinkUpBand = 100;
219     uint32_t netLinkDownBand = 100;
220     instance_->netSpecifier_->netCapabilities_.linkUpBandwidthKbps_ = 100;
221     instance_->netSpecifier_->netCapabilities_.linkDownBandwidthKbps_ = 100;
222     bool ret = instance_->CompareByNetworkBand(netLinkUpBand, netLinkDownBand);
223     EXPECT_EQ(ret, true);
224 
225     netLinkUpBand = 50;
226     ret = instance_->CompareByNetworkBand(netLinkUpBand, netLinkDownBand);
227     EXPECT_EQ(ret, false);
228 }
229 
230 HWTEST_F(NetActivateTest, HaveCapability001, TestSize.Level1)
231 {
232     instance_->netSpecifier_->netCapabilities_.netCaps_.clear();
233     instance_->netSpecifier_->netCapabilities_.netCaps_.insert(NET_CAPABILITY_NOT_VPN);
234     NetCap netCap = NET_CAPABILITY_NOT_VPN;
235     bool ret = instance_->HaveCapability(netCap);
236     EXPECT_EQ(ret, true);
237 
238     netCap = NET_CAPABILITY_INTERNET;
239     ret = instance_->HaveCapability(netCap);
240     EXPECT_EQ(ret, false);
241 
242     sptr<INetConnCallback> callback = new (std::nothrow) NetConnCallbackStubCb();
243     sptr<NetSpecifier> specifier = nullptr;
244     std::weak_ptr<INetActivateCallback> timeoutCallback = std::make_shared<NetActivateCallbackTest>();
245     std::shared_ptr<NetActivate> testNetActivate =
246         std::make_shared<NetActivate>(specifier, callback, timeoutCallback, TEST_TIMEOUT_MS, netActEventHandler_);
247 
248     ret = testNetActivate->HaveCapability(netCap);
249     EXPECT_EQ(ret, false);
250 }
251 
252 HWTEST_F(NetActivateTest, HaveTypes001, TestSize.Level1)
253 {
254     std::set<NetBearType> bearerTypes;
255     bool ret = instance_->HaveTypes(bearerTypes);
256     EXPECT_EQ(ret, false);
257 
258     bearerTypes.insert(BEARER_WIFI);
259     instance_->netSpecifier_->netCapabilities_.bearerTypes_.insert(BEARER_VPN);
260     ret = instance_->HaveTypes(bearerTypes);
261     EXPECT_EQ(ret, false);
262 
263     instance_->netSpecifier_->netCapabilities_.bearerTypes_.insert(BEARER_WIFI);
264     ret = instance_->HaveTypes(bearerTypes);
265     EXPECT_EQ(ret, true);
266 
267     sptr<INetConnCallback> callback = new (std::nothrow) NetConnCallbackStubCb();
268     sptr<NetSpecifier> specifier = nullptr;
269     std::weak_ptr<INetActivateCallback> timeoutCallback = std::make_shared<NetActivateCallbackTest>();
270     std::shared_ptr<NetActivate> testNetActivate =
271         std::make_shared<NetActivate>(specifier, callback, timeoutCallback, TEST_TIMEOUT_MS, netActEventHandler_);
272 
273     ret = testNetActivate->HaveTypes(bearerTypes);
274     EXPECT_EQ(ret, false);
275 }
276 
277 HWTEST_F(NetActivateTest, NetActivateBranchTest001, TestSize.Level1)
278 {
279     instance_->netConnCallback_ = nullptr;
280     instance_->TimeOutNetAvailable();
281     EXPECT_TRUE(instance_->GetNetCallback() == nullptr);
282 }
283 } // namespace NetManagerStandard
284 } // namespace OHOS