1 /*
2 * Copyright (c) 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 "common_notify_callback_test.h"
19 #include "notify_callback_stub.h"
20
21 namespace OHOS {
22 namespace NetsysNative {
23 namespace {
24 using namespace testing::ext;
25 } // namespace
26
27 class NotifyCallbackStubTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 void SetUp();
32 void TearDown();
33
34 static inline std::shared_ptr<NotifyCallbackTest> notifyStub_ = nullptr;
35 };
36
SetUpTestCase()37 void NotifyCallbackStubTest::SetUpTestCase()
38 {
39 notifyStub_ = std::make_shared<NotifyCallbackTest>();
40 }
41
TearDownTestCase()42 void NotifyCallbackStubTest::TearDownTestCase() {}
43
SetUp()44 void NotifyCallbackStubTest::SetUp() {}
45
TearDown()46 void NotifyCallbackStubTest::TearDown() {}
47
48 HWTEST_F(NotifyCallbackStubTest, OnInterfaceAddressUpdated001, TestSize.Level1)
49 {
50 std::string addr = "192.161.0.5";
51 std::string ifName = "test0";
52 int32_t flags = 2;
53 int32_t scope = 0;
54 MessageParcel data;
55 if (!data.WriteInterfaceToken(NotifyCallbackStub::GetDescriptor())) {
56 return;
57 }
58 if (!data.WriteString(addr)) {
59 return;
60 }
61 if (!data.WriteString(ifName)) {
62 return;
63 }
64 if (!data.WriteUint32(flags)) {
65 return;
66 }
67 if (!data.WriteUint32(scope)) {
68 return;
69 }
70 MessageParcel reply;
71 MessageOption option;
72 int32_t ret = notifyStub_->OnRemoteRequest(100, data, reply, option);
73 EXPECT_NE(ret, 0);
74
75 ret = notifyStub_->OnRemoteRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_ADDRESS_UPDATED),
76 data, reply, option);
77 EXPECT_NE(ret, 0);
78
79 MessageParcel dataOk;
80 if (!dataOk.WriteInterfaceToken(NotifyCallbackStub::GetDescriptor())) {
81 return;
82 }
83 if (!dataOk.WriteString(addr)) {
84 return;
85 }
86 if (!dataOk.WriteString(ifName)) {
87 return;
88 }
89 if (!dataOk.WriteUint32(flags)) {
90 return;
91 }
92 if (!dataOk.WriteUint32(scope)) {
93 return;
94 }
95 ret = notifyStub_->OnRemoteRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_ADDRESS_UPDATED),
96 dataOk, reply, option);
97 EXPECT_EQ(ret, 0);
98 }
99
100 HWTEST_F(NotifyCallbackStubTest, OnInterfaceAddressRemoved001, TestSize.Level1)
101 {
102 std::string addr = "192.161.0.5";
103 std::string ifName = "test0";
104 int32_t flags = 2;
105 int32_t scope = 0;
106 MessageParcel data;
107 if (!data.WriteInterfaceToken(NotifyCallbackStub::GetDescriptor())) {
108 return;
109 }
110 if (!data.WriteString(addr)) {
111 return;
112 }
113 if (!data.WriteString(ifName)) {
114 return;
115 }
116 if (!data.WriteUint32(flags)) {
117 return;
118 }
119 if (!data.WriteUint32(scope)) {
120 return;
121 }
122 MessageParcel reply;
123 MessageOption option;
124 int32_t ret = notifyStub_->OnRemoteRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_ADDRESS_REMOVED),
125 data, reply, option);
126 EXPECT_EQ(ret, 0);
127 }
128
129 HWTEST_F(NotifyCallbackStubTest, OnInterfaceAdded001, TestSize.Level1)
130 {
131 std::string ifName = "test0";
132 MessageParcel data;
133 if (!data.WriteInterfaceToken(NotifyCallbackStub::GetDescriptor())) {
134 return;
135 }
136 if (!data.WriteString(ifName)) {
137 return;
138 }
139 MessageParcel reply;
140 MessageOption option;
141 int32_t ret = notifyStub_->OnRemoteRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_ADDED),
142 data, reply, option);
143 EXPECT_EQ(ret, 0);
144 }
145
146 HWTEST_F(NotifyCallbackStubTest, OnInterfaceRemoved001, TestSize.Level1)
147 {
148 std::string ifName = "test0";
149 MessageParcel data;
150 if (!data.WriteInterfaceToken(NotifyCallbackStub::GetDescriptor())) {
151 return;
152 }
153 if (!data.WriteString(ifName)) {
154 return;
155 }
156 MessageParcel reply;
157 MessageOption option;
158 int32_t ret = notifyStub_->OnRemoteRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_REMOVED),
159 data, reply, option);
160 EXPECT_EQ(ret, 0);
161 }
162
163 HWTEST_F(NotifyCallbackStubTest, OnInterfaceChanged001, TestSize.Level1)
164 {
165 std::string ifName = "test0";
166 bool isUp = false;
167 MessageParcel data;
168 if (!data.WriteInterfaceToken(NotifyCallbackStub::GetDescriptor())) {
169 return;
170 }
171 if (!data.WriteString(ifName)) {
172 return;
173 }
174 if (!data.WriteBool(isUp)) {
175 return;
176 }
177 MessageParcel reply;
178 MessageOption option;
179 int32_t ret = notifyStub_->OnRemoteRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_CHANGED),
180 data, reply, option);
181 EXPECT_EQ(ret, 0);
182 }
183
184 HWTEST_F(NotifyCallbackStubTest, OnInterfaceLinkStateChanged001, TestSize.Level1)
185 {
186 std::string ifName = "test0";
187 bool isUp = false;
188 MessageParcel data;
189 if (!data.WriteInterfaceToken(NotifyCallbackStub::GetDescriptor())) {
190 return;
191 }
192 if (!data.WriteString(ifName)) {
193 return;
194 }
195 if (!data.WriteBool(isUp)) {
196 return;
197 }
198 MessageParcel reply;
199 MessageOption option;
200 int32_t ret = notifyStub_->OnRemoteRequest(
201 static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_LINK_STATE_CHANGED), data, reply, option);
202 EXPECT_EQ(ret, 0);
203 }
204
205 HWTEST_F(NotifyCallbackStubTest, OnRouteChanged001, TestSize.Level1)
206 {
207 bool updated = false;
208 std::string route = "192.168.0.1";
209 std::string gateway = "192.168.0.1";
210 std::string ifName = "test0";
211 MessageParcel data;
212 if (!data.WriteInterfaceToken(NotifyCallbackStub::GetDescriptor())) {
213 return;
214 }
215 if (!data.WriteBool(updated)) {
216 return;
217 }
218 if (!data.WriteString(route)) {
219 return;
220 }
221 if (!data.WriteString(gateway)) {
222 return;
223 }
224 if (!data.WriteString(ifName)) {
225 return;
226 }
227 MessageParcel reply;
228 MessageOption option;
229 int32_t ret = notifyStub_->OnRemoteRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_ROUTE_CHANGED),
230 data, reply, option);
231 EXPECT_EQ(ret, 0);
232 }
233
234 HWTEST_F(NotifyCallbackStubTest, OnDhcpSuccess001, TestSize.Level1)
235 {
236 MessageParcel data;
237 if (!data.WriteInterfaceToken(NotifyCallbackStub::GetDescriptor())) {
238 return;
239 }
240 sptr<DhcpResultParcel> dhcpResult = new (std::nothrow) DhcpResultParcel;
241 dhcpResult->iface_ = "test0";
242 dhcpResult->ipAddr_ = "192.168.11.55";
243 dhcpResult->gateWay_ = "192.168.10.1";
244 dhcpResult->subNet_ = "255.255.255.0";
245 dhcpResult->Marshalling(data);
246
247 MessageParcel reply;
248 MessageOption option;
249 int32_t ret = notifyStub_->OnRemoteRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_DHCP_SUCCESS),
250 data, reply, option);
251 EXPECT_EQ(ret, 0);
252 }
253
254 HWTEST_F(NotifyCallbackStubTest, OnBandwidthReachedLimit001, TestSize.Level1)
255 {
256 std::string limitName = "limit";
257 std::string iface = "test0";
258 MessageParcel data;
259 if (!data.WriteInterfaceToken(NotifyCallbackStub::GetDescriptor())) {
260 return;
261 }
262 if (!data.WriteString(limitName)) {
263 return;
264 }
265 if (!data.WriteString(iface)) {
266 return;
267 }
268 MessageParcel reply;
269 MessageOption option;
270 int32_t ret = notifyStub_->OnRemoteRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_BANDWIDTH_REACHED_LIMIT),
271 data, reply, option);
272 EXPECT_EQ(ret, 0);
273 }
274 } // namespace nmd
275 } // namespace OHOS