1 /*
2 * Copyright (C) 2021-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 #include <gtest/gtest.h>
16
17 #include <memory>
18 #define private public
19 #include "net_conn_callback_stub.h"
20 #undef private
21 #include "net_conn_callback_test.h"
22 #include "net_conn_constants.h"
23 #include "net_manager_constants.h"
24
25 #include "net_mgr_log_wrapper.h"
26
27 namespace OHOS {
28 namespace NetManagerStandard {
NetConnCallbackTest()29 NetConnCallbackTest::NetConnCallbackTest() {}
30
~NetConnCallbackTest()31 NetConnCallbackTest::~NetConnCallbackTest() {}
32
NotifyAll()33 void NetConnCallbackTest::NotifyAll()
34 {
35 std::unique_lock<std::mutex> callbackLock(callbackMutex_);
36 cv_.notify_all();
37 }
38
WaitFor(int timeoutSecond)39 void NetConnCallbackTest::WaitFor(int timeoutSecond)
40 {
41 std::unique_lock<std::mutex> callbackLock(callbackMutex_);
42 cv_.wait_for(callbackLock, std::chrono::seconds(timeoutSecond));
43 }
44
NetAvailable(sptr<NetHandle> & netHandle)45 int32_t NetConnCallbackTest::NetAvailable(sptr<NetHandle> &netHandle)
46 {
47 if (netHandle != nullptr) {
48 return 0;
49 }
50
51 NETMGR_LOG_D("NetAvailable: netId = %{public}d", netHandle->GetNetId());
52 NotifyAll();
53 return 0;
54 }
55
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)56 int32_t NetConnCallbackTest::NetCapabilitiesChange(
57 sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap)
58 {
59 if (netHandle == nullptr || netAllCap == nullptr) {
60 return 0;
61 }
62
63 NETMGR_LOG_D("NetCapabilitiesChange: netId = [%{public}d]", netHandle->GetNetId());
64 NETMGR_LOG_D("[%{public}s]", netAllCap->ToString("|").c_str());
65 NotifyAll();
66 return 0;
67 }
68
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)69 int32_t NetConnCallbackTest::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle,
70 const sptr<NetLinkInfo> &info)
71 {
72 if (netHandle == nullptr || info == nullptr) {
73 return 0;
74 }
75 NETMGR_LOG_D("NetConnectionPropertiesChange: netId = %{public}d info = %{public}s", netHandle->GetNetId(),
76 info->ToString(" ").c_str());
77 NotifyAll();
78 return 0;
79 }
80
NetLost(sptr<NetHandle> & netHandle)81 int32_t NetConnCallbackTest::NetLost(sptr<NetHandle> &netHandle)
82 {
83 if (netHandle == nullptr) {
84 return 0;
85 }
86 NETMGR_LOG_D("NetLost: netId = %{public}d", netHandle->GetNetId());
87 NotifyAll();
88 return 0;
89 }
90
NetUnavailable()91 int32_t NetConnCallbackTest::NetUnavailable()
92 {
93 NETMGR_LOG_D("NetUnavailable");
94 NotifyAll();
95 return 0;
96 }
97
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)98 int32_t NetConnCallbackTest::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
99 {
100 NETMGR_LOG_D("NetConnCallbackTest::NetLost: netId = %{public}d bolcked = %{public}s",
101 netHandle->GetNetId(), blocked ? "true" : "false");
102 NotifyAll();
103 return 0;
104 }
105
106 using namespace testing::ext;
107 class NetConnCallbackStubTest : public testing::Test {
108 public:
109 static void SetUpTestCase();
110 static void TearDownTestCase();
111 void SetUp();
112 void TearDown();
113 static inline std::shared_ptr<NetConnCallbackStub> instance_ = std::make_shared<NetConnCallbackStub>();
114 };
115
SetUpTestCase()116 void NetConnCallbackStubTest::SetUpTestCase() {}
117
TearDownTestCase()118 void NetConnCallbackStubTest::TearDownTestCase() {}
119
SetUp()120 void NetConnCallbackStubTest::SetUp() {}
121
TearDown()122 void NetConnCallbackStubTest::TearDown() {}
123
124 HWTEST_F(NetConnCallbackStubTest, OnNetLost001, TestSize.Level1)
125 {
126 MessageParcel data;
127 MessageParcel reply;
128 int32_t ret = instance_->OnNetLost(data, reply);
129 EXPECT_NE(ret, NETMANAGER_SUCCESS);
130 }
131
132 HWTEST_F(NetConnCallbackStubTest, OnRemoteRequest001, TestSize.Level1)
133 {
134 MessageParcel data;
135 MessageParcel reply;
136 MessageOption option;
137
138 int32_t ret = instance_->OnRemoteRequest(0, data, reply, option);
139 EXPECT_NE(ret, NETMANAGER_SUCCESS);
140 }
141
142 HWTEST_F(NetConnCallbackStubTest, OnRemoteRequest002, TestSize.Level1)
143 {
144 MessageParcel data;
145 MessageParcel reply;
146 MessageOption option;
147
148 int32_t ret = instance_->OnRemoteRequest(100, data, reply, option);
149 EXPECT_NE(ret, NETMANAGER_SUCCESS);
150 }
151
152 HWTEST_F(NetConnCallbackStubTest, OnNetBlockStatusChange001, TestSize.Level1)
153 {
154 MessageParcel data;
155 MessageParcel reply;
156
157 int32_t ret = instance_->OnNetBlockStatusChange(data, reply);
158 EXPECT_NE(ret, NETMANAGER_SUCCESS);
159 }
160
161 HWTEST_F(NetConnCallbackStubTest, NetAvailable001, TestSize.Level1)
162 {
163 sptr<NetHandle> handle = nullptr;
164 int32_t ret = instance_->NetAvailable(handle);
165 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
166 }
167
168 HWTEST_F(NetConnCallbackStubTest, NetCapabilitiesChange001, TestSize.Level1)
169 {
170 sptr<NetHandle> handle = nullptr;
171 sptr<NetAllCapabilities> allCapabilities = nullptr;
172 int32_t ret = instance_->NetCapabilitiesChange(handle, allCapabilities);
173 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
174 }
175
176 HWTEST_F(NetConnCallbackStubTest, NetConnectionPropertiesChange001, TestSize.Level1)
177 {
178 sptr<NetHandle> handle = nullptr;
179 sptr<NetLinkInfo> info = nullptr;
180 int32_t ret = instance_->NetConnectionPropertiesChange(handle, info);
181 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
182 }
183
184 HWTEST_F(NetConnCallbackStubTest, NetLost001, TestSize.Level1)
185 {
186 sptr<NetHandle> handle = nullptr;
187 int32_t ret = instance_->NetLost(handle);
188 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
189 }
190
191 HWTEST_F(NetConnCallbackStubTest, NetUnavailable001, TestSize.Level1)
192 {
193 int32_t ret = instance_->NetUnavailable();
194 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
195 }
196
197 HWTEST_F(NetConnCallbackStubTest, NetBlockStatusChange001, TestSize.Level1)
198 {
199 sptr<NetHandle> handle = nullptr;
200 int32_t ret = instance_->NetBlockStatusChange(handle, false);
201 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
202 }
203 } // namespace NetManagerStandard
204 } // namespace OHOS
205