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 #include <memory>
18
19 #include "net_activate.h"
20 #include "net_conn_callback_stub.h"
21 #include "net_manager_constants.h"
22
23 namespace OHOS {
24 namespace NetManagerStandard {
25 namespace {
26 using namespace testing::ext;
27 constexpr uint32_t TEST_TIMEOUT_MS = 1000;
28 constexpr uint32_t TEST_REQUEST_ID = 54656;
29 constexpr const char *TEST_IDENT = "testIdent";
30 class ConnCallbackTest : public NetConnCallbackStub {
NetAvailable(sptr<NetHandle> & netHandle)31 inline int32_t NetAvailable(sptr<NetHandle> &netHandle) override
32 {
33 return NETMANAGER_SUCCESS;
34 }
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)35 inline int32_t NetCapabilitiesChange(sptr<NetHandle> &netHandle,
36 const sptr<NetAllCapabilities> &netAllCap) override
37 {
38 return NETMANAGER_SUCCESS;
39 }
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)40 inline int32_t NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info) override
41 {
42 return NETMANAGER_SUCCESS;
43 }
NetLost(sptr<NetHandle> & netHandle)44 inline int32_t NetLost(sptr<NetHandle> &netHandle) override
45 {
46 return NETMANAGER_SUCCESS;
47 }
NetUnavailable()48 inline int32_t NetUnavailable() override
49 {
50 return NETMANAGER_SUCCESS;
51 }
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)52 inline int32_t NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked) override
53 {
54 return NETMANAGER_SUCCESS;
55 }
56 };
57
58 class NetActivateCallbackTest : public INetActivateCallback {
OnNetActivateTimeOut(uint32_t reqId)59 void OnNetActivateTimeOut(uint32_t reqId) override
60 {
61 std::cout << "Activate network request " << reqId << " timeout." << std::endl;
62 }
63 };
64 } // namespace
65
66 class NetActivateTest : public testing::Test {
67 public:
68 static void SetUpTestCase();
69 static void TearDownTestCase();
70 void SetUp();
71 void TearDown();
72
73 static inline std::unique_ptr<NetActivate> instance_ = nullptr;
74 static inline sptr<INetConnCallback> callback_ = nullptr;
75 static inline sptr<NetSpecifier> specifier_ = nullptr;
76 static inline std::shared_ptr<INetActivateCallback> timeoutCallback_ = nullptr;
77 };
78
SetUpTestCase()79 void NetActivateTest::SetUpTestCase()
80 {
81 callback_ = new (std::nothrow) ConnCallbackTest();
82 specifier_ = new (std::nothrow) NetSpecifier();
83 timeoutCallback_ = std::make_shared<NetActivateCallbackTest>();
84 instance_ = std::make_unique<NetActivate>(specifier_, callback_, timeoutCallback_, TEST_TIMEOUT_MS);
85 }
86
TearDownTestCase()87 void NetActivateTest::TearDownTestCase() {}
88
SetUp()89 void NetActivateTest::SetUp() {}
90
TearDown()91 void NetActivateTest::TearDown() {}
92
93 HWTEST_F(NetActivateTest, MatchRequestAndNetworkTest001, TestSize.Level1)
94 {
95 std::set<NetCap> netCaps;
96 sptr<NetSupplier> supplier = new (std::nothrow) NetSupplier(NetBearType::BEARER_ETHERNET, TEST_IDENT, netCaps);
97 bool ret = instance_->MatchRequestAndNetwork(supplier);
98 EXPECT_TRUE(ret);
99 }
100
101 HWTEST_F(NetActivateTest, SetGetRequestIdTest001, TestSize.Level1)
102 {
103 instance_->SetRequestId(TEST_REQUEST_ID);
104 uint32_t requestId = instance_->GetRequestId();
105 EXPECT_EQ(requestId, TEST_REQUEST_ID);
106 }
107
108 HWTEST_F(NetActivateTest, SetGetServiceSupplyTest001, TestSize.Level1)
109 {
110 std::set<NetCap> netCaps;
111 sptr<NetSupplier> supplier = new (std::nothrow) NetSupplier(NetBearType::BEARER_ETHERNET, TEST_IDENT, netCaps);
112 instance_->SetServiceSupply(supplier);
113 auto result = instance_->GetServiceSupply();
114 EXPECT_EQ(result, supplier);
115 }
116
117 HWTEST_F(NetActivateTest, GetNetCallbackTest001, TestSize.Level1)
118 {
119 auto result = instance_->GetNetCallback();
120 EXPECT_EQ(result, callback_);
121 }
122
123 HWTEST_F(NetActivateTest, GetNetSpecifierTest001, TestSize.Level1)
124 {
125 auto result = instance_->GetNetSpecifier();
126 EXPECT_EQ(result, specifier_);
127 }
128 } // namespace NetManagerStandard
129 } // namespace OHOS