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_proxy.h"
19
20 namespace OHOS {
21 namespace NetsysNative {
22 namespace {
23 using namespace testing::ext;
24
25 class MockNetIRemoteObject : public IRemoteObject {
26 public:
MockNetIRemoteObject()27 MockNetIRemoteObject() : IRemoteObject(u"mock_i_remote_object") {}
~MockNetIRemoteObject()28 ~MockNetIRemoteObject() {}
29
GetObjectRefCount()30 int32_t GetObjectRefCount() override
31 {
32 return 0;
33 }
34
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override
36 {
37 return 0;
38 }
39
IsProxyObject() const40 bool IsProxyObject() const override
41 {
42 return true;
43 }
44
CheckObjectLegality() const45 bool CheckObjectLegality() const override
46 {
47 return true;
48 }
49
AddDeathRecipient(const sptr<DeathRecipient> & recipient)50 bool AddDeathRecipient(const sptr<DeathRecipient> &recipient) override
51 {
52 return true;
53 }
54
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)55 bool RemoveDeathRecipient(const sptr<DeathRecipient> &recipient) override
56 {
57 return true;
58 }
59
Marshalling(Parcel & parcel) const60 bool Marshalling(Parcel &parcel) const override
61 {
62 return true;
63 }
64
AsInterface()65 sptr<IRemoteBroker> AsInterface() override
66 {
67 return nullptr;
68 }
69
Dump(int fd,const std::vector<std::u16string> & args)70 int Dump(int fd, const std::vector<std::u16string> &args) override
71 {
72 return 0;
73 }
74
GetObjectDescriptor() const75 std::u16string GetObjectDescriptor() const
76 {
77 std::u16string descriptor = std::u16string();
78 return descriptor;
79 }
80 };
81 } // namespace
82
83 class NotifyCallbackProxyTest : public testing::Test {
84 public:
85 static void SetUpTestCase();
86 static void TearDownTestCase();
87 void SetUp();
88 void TearDown();
89
90 static inline std::shared_ptr<NotifyCallbackProxy> notifyProxy = nullptr;
91 };
92
SetUpTestCase()93 void NotifyCallbackProxyTest::SetUpTestCase()
94 {
95 sptr<IRemoteObject> impl = new (std::nothrow) MockNetIRemoteObject();
96 notifyProxy = std::make_shared<NotifyCallbackProxy>(impl);
97 }
98
TearDownTestCase()99 void NotifyCallbackProxyTest::TearDownTestCase() {}
100
SetUp()101 void NotifyCallbackProxyTest::SetUp() {}
102
TearDown()103 void NotifyCallbackProxyTest::TearDown() {}
104
105 HWTEST_F(NotifyCallbackProxyTest, OnInterfaceAddressUpdated001, TestSize.Level1)
106 {
107 std::string addr = "192.161.0.5";
108 std::string ifName = "test0";
109 int flags = 1;
110 int scope = 2;
111 int32_t ret = notifyProxy->OnInterfaceAddressUpdated(addr, ifName, flags, scope);
112 EXPECT_EQ(ret, 0);
113 }
114
115 HWTEST_F(NotifyCallbackProxyTest, OnInterfaceAddressRemoved001, TestSize.Level1)
116 {
117 sptr<IRemoteObject> impl = new (std::nothrow) MockNetIRemoteObject();
118 sptr<NotifyCallbackProxy> notifyProxy = new (std::nothrow) NotifyCallbackProxy(impl);
119 std::string addr = "192.161.0.5";
120 std::string ifName = "test0";
121 int flags = 1;
122 int scope = 2;
123 int32_t ret = notifyProxy->OnInterfaceAddressRemoved(addr, ifName, flags, scope);
124 EXPECT_EQ(ret, 0);
125 }
126
127 HWTEST_F(NotifyCallbackProxyTest, OnInterfaceAdded001, TestSize.Level1)
128 {
129 std::string ifName = "test0";
130 int32_t ret = notifyProxy->OnInterfaceAdded(ifName);
131 EXPECT_EQ(ret, 0);
132 }
133
134 HWTEST_F(NotifyCallbackProxyTest, OnInterfaceRemoved001, TestSize.Level1)
135 {
136 std::string ifName = "test0";
137 int32_t ret = notifyProxy->OnInterfaceRemoved(ifName);
138 EXPECT_EQ(ret, 0);
139 }
140
141 HWTEST_F(NotifyCallbackProxyTest, OnInterfaceChanged001, TestSize.Level1)
142 {
143 std::string ifName = "test0";
144 bool isUp = false;
145 int32_t ret = notifyProxy->OnInterfaceChanged(ifName, isUp);
146 EXPECT_EQ(ret, 0);
147 }
148
149 HWTEST_F(NotifyCallbackProxyTest, OnInterfaceLinkStateChanged001, TestSize.Level1)
150 {
151 std::string ifName = "test0";
152 bool isUp = false;
153 int32_t ret = notifyProxy->OnInterfaceLinkStateChanged(ifName, isUp);
154 EXPECT_EQ(ret, 0);
155 }
156
157 HWTEST_F(NotifyCallbackProxyTest, OnRouteChanged001, TestSize.Level1)
158 {
159 bool updated = false;
160 std::string route = "192.168.0.1";
161 std::string gateway = "192.168.0.1";
162 std::string ifName = "test0";
163 int32_t ret = notifyProxy->OnRouteChanged(updated, route, gateway, ifName);
164 EXPECT_EQ(ret, 0);
165 }
166
167 HWTEST_F(NotifyCallbackProxyTest, OnDhcpSuccess001, TestSize.Level1)
168 {
169 sptr<DhcpResultParcel> dhcpResult = new (std::nothrow) DhcpResultParcel;
170 dhcpResult->iface_ = "test0";
171 dhcpResult->ipAddr_ = "192.168.11.55";
172 dhcpResult->gateWay_ = "192.168.10.1";
173 dhcpResult->subNet_ = "255.255.255.0";
174 int32_t ret = notifyProxy->OnDhcpSuccess(dhcpResult);
175 EXPECT_EQ(ret, 0);
176 }
177
178 HWTEST_F(NotifyCallbackProxyTest, OnBandwidthReachedLimit001, TestSize.Level1)
179 {
180 std::string limitName = "limit";
181 std::string iface = "test0";
182 int32_t ret = notifyProxy->OnBandwidthReachedLimit(limitName, iface);
183 EXPECT_EQ(ret, 0);
184 }
185
186 HWTEST_F(NotifyCallbackProxyTest, OnBandwidthReachedLimit002, TestSize.Level1)
187 {
188 std::string limitName1 = "limit";
189 std::string iface1;
190 int32_t ret = notifyProxy->OnBandwidthReachedLimit(limitName1, iface1);
191 EXPECT_EQ(ret, 0);
192 }
193
194 HWTEST_F(NotifyCallbackProxyTest, OnBandwidthReachedLimit003, TestSize.Level1)
195 {
196 std::string limitName1;
197 std::string iface1;
198 int32_t ret = notifyProxy->OnBandwidthReachedLimit(limitName1, iface1);
199 EXPECT_EQ(ret, 0);
200 }
201 } // namespace nmd
202 } // namespace OHOS