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