1 /*
2 * Copyright (c) 2022-2024 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 "netlink_define.h"
17 #include "securec.h"
18 #include <arpa/inet.h>
19 #include <gtest/gtest.h>
20 #include <ifaddrs.h>
21 #include <linux/genetlink.h>
22 #include <linux/rtnetlink.h>
23 #include <memory>
24 #include <net/if.h>
25 #include <netdb.h>
26
27 #ifdef GTEST_API_
28 #define private public
29 #define protected public
30 #endif
31
32 #include "wrapper_decoder.h"
33 #include <cstring>
34
35 namespace OHOS {
36 namespace nmd {
37 namespace {
38 using namespace testing::ext;
39 constexpr int16_t LOCAL_QLOG_NL_EVENT = 112;
40 constexpr const char TEST_ASCII_MESSAGE[] = {
41 "action@msg\0ACTION=add\0ACTION=remove\0ACTION=change\0SEQNUM=111\0SEQNUM=\0SUBSYSTEM=net\0SUBSYSTEM="
42 "\0SUBSYSTEM=test\0dfdfcc=ttt\0"};
43 } // namespace
44
45 class WrapperDecoderTest : public testing::Test {
46 public:
47 static void SetUpTestCase();
48 static void TearDownTestCase();
49 void SetUp();
50 void TearDown();
51 };
52
SetUpTestCase()53 void WrapperDecoderTest::SetUpTestCase() {}
54
TearDownTestCase()55 void WrapperDecoderTest::TearDownTestCase() {}
56
SetUp()57 void WrapperDecoderTest::SetUp() {}
58
TearDown()59 void WrapperDecoderTest::TearDown() {}
60
61 HWTEST_F(WrapperDecoderTest, DecodeAsciiTest001, TestSize.Level1)
62 {
63 auto msg = std::make_shared<NetsysEventMessage>();
64 std::unique_ptr<WrapperDecoder> decoder = std::make_unique<WrapperDecoder>(msg);
65 std::string buffer = "testMsg@@";
66 auto ret = decoder->DecodeAscii(buffer.data(), buffer.length());
67 EXPECT_FALSE(ret);
68 }
69
70 HWTEST_F(WrapperDecoderTest, DecodeAsciiTest002, TestSize.Level1)
71 {
72 auto msg = std::make_shared<NetsysEventMessage>();
73 std::unique_ptr<WrapperDecoder> decoder = std::make_unique<WrapperDecoder>(msg);
74 std::string buffer = "@testMsg";
75 auto ret = decoder->DecodeAscii(buffer.data(), 0);
76 EXPECT_FALSE(ret);
77 }
78
79 HWTEST_F(WrapperDecoderTest, DecodeAsciiTest003, TestSize.Level1)
80 {
81 auto msg = std::make_shared<NetsysEventMessage>();
82 std::unique_ptr<WrapperDecoder> decoder = std::make_unique<WrapperDecoder>(msg);
83 std::string buffer = "@testMsg";
84 auto ret = decoder->DecodeAscii(buffer.data(), buffer.length());
85 EXPECT_FALSE(ret);
86 }
87
88 HWTEST_F(WrapperDecoderTest, DecodeAsciiTest004, TestSize.Level1)
89 {
90 auto msg = std::make_shared<NetsysEventMessage>();
91 std::unique_ptr<WrapperDecoder> decoder = std::make_unique<WrapperDecoder>(msg);
92 auto ret = decoder->DecodeAscii(TEST_ASCII_MESSAGE, sizeof(TEST_ASCII_MESSAGE));
93 EXPECT_TRUE(ret);
94 }
95
96 HWTEST_F(WrapperDecoderTest, DecodeBinaryTest001, TestSize.Level1)
97 {
98 auto msg = std::make_shared<NetsysEventMessage>();
99 std::unique_ptr<WrapperDecoder> decoder = std::make_unique<WrapperDecoder>(msg);
100 char binarydata[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(sizeof(struct ifinfomsg)) +
101 RTA_ALIGN(sizeof(struct rtattr)) + IFNAMSIZ];
102 ASSERT_EQ(memset_s(&binarydata, sizeof(binarydata), 0, sizeof(binarydata)), EOK);
103 nlmsghdr *pmsghdr = reinterpret_cast<struct nlmsghdr *>(&binarydata);
104 ASSERT_NE(pmsghdr, nullptr);
105 ifinfomsg *pifInfomsg = reinterpret_cast<struct ifinfomsg *>(NLMSG_DATA(&binarydata));
106 ASSERT_NE(pifInfomsg, nullptr);
107 pmsghdr->nlmsg_len = sizeof(binarydata);
108 pmsghdr->nlmsg_type = RTM_MAX;
109 rtattr *prtattr = IFLA_RTA(pifInfomsg);
110 ASSERT_NE(prtattr, nullptr);
111
112 pifInfomsg->ifi_flags = 0;
113 auto ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), pmsghdr->nlmsg_len);
114 EXPECT_FALSE(ret);
115
116 pmsghdr->nlmsg_type = RTM_NEWLINK;
117 ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
118 EXPECT_FALSE(ret);
119
120 pifInfomsg->ifi_flags = IFF_LOOPBACK;
121 ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
122 EXPECT_FALSE(ret);
123
124 prtattr->rta_type = IFLA_IFNAME;
125 prtattr->rta_len = sizeof(struct rtattr) + IFNAMSIZ;
126 ASSERT_EQ(strcpy_s(&binarydata[sizeof(binarydata) - IFNAMSIZ], IFNAMSIZ, "ifacename"), 0);
127 pifInfomsg->ifi_flags = IFF_LOWER_UP;
128 ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
129 EXPECT_TRUE(ret);
130 }
131
132 HWTEST_F(WrapperDecoderTest, DecodeBinaryTest002, TestSize.Level1)
133 {
134 auto msg = std::make_shared<NetsysEventMessage>();
135 std::unique_ptr<WrapperDecoder> decoder = std::make_unique<WrapperDecoder>(msg);
136 char binarydata[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(192)];
137 ASSERT_EQ(memset_s(&binarydata, sizeof(binarydata), 0, sizeof(binarydata)), EOK);
138 nlmsghdr *pmsghdr = reinterpret_cast<struct nlmsghdr *>(&binarydata);
139 ASSERT_NE(pmsghdr, nullptr);
140
141 pmsghdr->nlmsg_len = NLMSG_ALIGN(sizeof(struct nlmsghdr));
142 pmsghdr->nlmsg_type = LOCAL_QLOG_NL_EVENT;
143
144 ASSERT_EQ(strcpy_s(&binarydata[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + 28], IFNAMSIZ, "testDevName"), 0);
145 auto ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
146 EXPECT_FALSE(ret);
147
148 pmsghdr->nlmsg_len = sizeof(binarydata);
149 ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
150 EXPECT_TRUE(ret);
151 }
152
153 HWTEST_F(WrapperDecoderTest, InterpreteAddressMsgTest001, TestSize.Level1)
154 {
155 auto msg = std::make_shared<NetsysEventMessage>();
156 std::unique_ptr<WrapperDecoder> decoder = std::make_unique<WrapperDecoder>(msg);
157 char binarydata[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
158 RTA_ALIGN(sizeof(struct rtattr)) + RTA_ALIGN(sizeof(struct in_addr)) +
159 RTA_ALIGN(sizeof(struct rtattr)) + NLMSG_ALIGN(sizeof(struct ifa_cacheinfo)) +
160 RTA_ALIGN(sizeof(struct rtattr)) + NLMSG_ALIGN(sizeof(uint32_t))];
161 ASSERT_EQ(memset_s(&binarydata, sizeof(binarydata), 0, sizeof(binarydata)), EOK);
162 nlmsghdr *pmsghdr = reinterpret_cast<struct nlmsghdr *>(&binarydata);
163 ASSERT_NE(pmsghdr, nullptr);
164 ifaddrmsg *pifaddrmsg = reinterpret_cast<struct ifaddrmsg *>(NLMSG_DATA(&binarydata));
165 ASSERT_NE(pifaddrmsg, nullptr);
166 rtattr *prtattr = IFA_RTA(pifaddrmsg);
167 ASSERT_NE(prtattr, nullptr);
168 in_addr *ipv4Addr = reinterpret_cast<struct in_addr *>(RTA_DATA(prtattr));
169 ASSERT_NE(ipv4Addr, nullptr);
170
171 pmsghdr->nlmsg_len = NLMSG_ALIGN(sizeof(struct nlmsghdr));
172 pmsghdr->nlmsg_type = RTM_NEWADDR;
173 EXPECT_FALSE(decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata)));
174
175 pmsghdr->nlmsg_len = sizeof(binarydata);
176 EXPECT_FALSE(decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata)));
177
178 prtattr->rta_type = IFLA_IFNAME;
179 EXPECT_FALSE(decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata)));
180
181 prtattr->rta_type = IFA_ADDRESS;
182 prtattr->rta_len = RTA_ALIGN(sizeof(struct rtattr));
183 EXPECT_FALSE(decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata)));
184
185 pifaddrmsg->ifa_family = AF_INET;
186 EXPECT_FALSE(decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata)));
187
188 ipv4Addr->s_addr = inet_addr("127.0.0.1");
189 prtattr->rta_len = RTA_ALIGN(sizeof(struct rtattr)) + RTA_ALIGN(sizeof(struct in_addr));
190 rtattr *prtattr1 = reinterpret_cast<struct rtattr *>((reinterpret_cast<char *>(prtattr)) + prtattr->rta_len);
191 ASSERT_NE(prtattr1, nullptr);
192 prtattr1->rta_type = IFA_CACHEINFO;
193 prtattr1->rta_len = RTA_ALIGN(sizeof(struct rtattr));
194 EXPECT_TRUE(decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata)));
195
196 prtattr1->rta_len = RTA_ALIGN(sizeof(struct rtattr)) + RTA_ALIGN(sizeof(struct ifa_cacheinfo));
197 rtattr *prtattr2 = reinterpret_cast<struct rtattr *>((reinterpret_cast<char *>(prtattr1)) + prtattr1->rta_len);
198 ASSERT_NE(prtattr2, nullptr);
199 prtattr2->rta_type = IFA_FLAGS;
200 prtattr2->rta_len = RTA_ALIGN(sizeof(struct rtattr)) + RTA_ALIGN(sizeof(uint32_t));
201 EXPECT_TRUE(decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata)));
202 }
203
204 HWTEST_F(WrapperDecoderTest, InterpreteAddressMsgTest002, TestSize.Level1)
205 {
206 auto msg = std::make_shared<NetsysEventMessage>();
207 std::unique_ptr<WrapperDecoder> decoder = std::make_unique<WrapperDecoder>(msg);
208 char binarydata[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
209 RTA_ALIGN(sizeof(struct rtattr)) + RTA_ALIGN(sizeof(struct in6_addr)) +
210 RTA_ALIGN(sizeof(struct rtattr)) + NLMSG_ALIGN(sizeof(struct ifa_cacheinfo)) +
211 RTA_ALIGN(sizeof(struct rtattr)) + NLMSG_ALIGN(sizeof(uint32_t))];
212 ASSERT_EQ(memset_s(&binarydata, sizeof(binarydata), 0, sizeof(binarydata)), EOK);
213 nlmsghdr *pmsghdr = reinterpret_cast<struct nlmsghdr *>(&binarydata);
214 ASSERT_NE(pmsghdr, nullptr);
215 ifaddrmsg *pifaddrmsg = reinterpret_cast<struct ifaddrmsg *>(NLMSG_DATA(&binarydata));
216 ASSERT_NE(pifaddrmsg, nullptr);
217 rtattr *prtattr = IFA_RTA(pifaddrmsg);
218 ASSERT_NE(prtattr, nullptr);
219 in6_addr *ipv6Addr = reinterpret_cast<struct in6_addr *>(RTA_DATA(prtattr));
220 ASSERT_NE(ipv6Addr, nullptr);
221
222 pmsghdr->nlmsg_len = NLMSG_ALIGN(sizeof(struct nlmsghdr));
223 pmsghdr->nlmsg_type = RTM_NEWADDR;
224
225 auto ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
226 EXPECT_FALSE(ret);
227
228 pmsghdr->nlmsg_len = sizeof(binarydata);
229 ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
230 EXPECT_FALSE(ret);
231
232 prtattr->rta_type = IFLA_IFNAME;
233 ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
234 EXPECT_FALSE(ret);
235
236 prtattr->rta_type = IFA_ADDRESS;
237 prtattr->rta_len = RTA_ALIGN(sizeof(struct rtattr));
238 ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
239 EXPECT_FALSE(ret);
240
241 pifaddrmsg->ifa_family = AF_INET6;
242 ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
243 EXPECT_FALSE(ret);
244
245 prtattr->rta_len = RTA_ALIGN(sizeof(struct rtattr)) + RTA_ALIGN(sizeof(struct in6_addr));
246 rtattr *prtattr1 = reinterpret_cast<struct rtattr *>((reinterpret_cast<char *>(prtattr)) + prtattr->rta_len);
247 ASSERT_NE(prtattr1, nullptr);
248 prtattr1->rta_type = IFA_CACHEINFO;
249 prtattr1->rta_len = RTA_ALIGN(sizeof(struct rtattr)) + RTA_ALIGN(sizeof(struct ifa_cacheinfo));
250 rtattr *prtattr2 = reinterpret_cast<struct rtattr *>((reinterpret_cast<char *>(prtattr1)) + prtattr1->rta_len);
251 ASSERT_NE(prtattr2, nullptr);
252 prtattr2->rta_type = IFA_FLAGS;
253 prtattr2->rta_len = RTA_ALIGN(sizeof(struct rtattr)) + RTA_ALIGN(sizeof(uint32_t));
254
255 ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
256 EXPECT_TRUE(ret);
257 }
258
InterpreteRtMsgTest001ParmaCheck(rtmsg * prtmsg,rtattr * prtattr,rtattr ** prtattr1,rtattr ** prtattr2,in_addr * ipv4Addr)259 void InterpreteRtMsgTest001ParmaCheck(rtmsg *prtmsg, rtattr *prtattr, rtattr **prtattr1, rtattr **prtattr2,
260 in_addr *ipv4Addr)
261 {
262 prtmsg->rtm_protocol = RTPROT_KERNEL;
263 prtmsg->rtm_family = AF_INET;
264 prtmsg->rtm_scope = RT_SCOPE_UNIVERSE;
265 prtmsg->rtm_type = RTN_UNICAST;
266 prtattr->rta_type = RTA_GATEWAY;
267 prtattr->rta_len = RTA_ALIGN(sizeof(struct rtattr)) + RTA_ALIGN(sizeof(struct in_addr));
268 ipv4Addr->s_addr = inet_addr("0.0.0.0");
269 *prtattr1 = reinterpret_cast<struct rtattr *>((reinterpret_cast<char *>(prtattr)) + prtattr->rta_len);
270 ASSERT_NE(prtattr1, nullptr);
271 (*prtattr1)->rta_type = RTA_DST;
272 (*prtattr1)->rta_len = RTA_ALIGN(sizeof(struct rtattr)) + RTA_ALIGN(sizeof(struct in_addr));
273 ipv4Addr = reinterpret_cast<struct in_addr *>(RTA_DATA(*prtattr1));
274 ASSERT_NE(ipv4Addr, nullptr);
275 ipv4Addr->s_addr = inet_addr("127.0.0.1");
276 (*prtattr2) = reinterpret_cast<struct rtattr *>((reinterpret_cast<char *>(*prtattr1)) + (*prtattr1)->rta_len);
277 ASSERT_NE(*prtattr2, nullptr);
278 (*prtattr2)->rta_type = RTA_OIF;
279 (*prtattr2)->rta_len = RTA_ALIGN(sizeof(struct rtattr)) + RTA_ALIGN(sizeof(uint32_t));
280 prtmsg->rtm_dst_len = 0;
281 }
282
283 HWTEST_F(WrapperDecoderTest, InterpreteRtMsgTest001, TestSize.Level1)
284 {
285 auto msg = std::make_shared<NetsysEventMessage>();
286 std::unique_ptr<WrapperDecoder> decoder = std::make_unique<WrapperDecoder>(msg);
287 char binarydata[NLMSG_ALIGN(sizeof(struct nlmsghdr)) + NLMSG_ALIGN(sizeof(struct rtmsg)) +
288 RTA_ALIGN(sizeof(struct rtattr)) + RTA_ALIGN(sizeof(struct in_addr)) +
289 RTA_ALIGN(sizeof(struct rtattr)) + NLMSG_ALIGN(sizeof(struct in_addr)) +
290 RTA_ALIGN(sizeof(struct rtattr)) + NLMSG_ALIGN(sizeof(uint32_t))];
291 ASSERT_EQ(memset_s(&binarydata, sizeof(binarydata), 0, sizeof(binarydata)), EOK);
292 nlmsghdr *pmsghdr = reinterpret_cast<struct nlmsghdr *>(&binarydata);
293 ASSERT_NE(pmsghdr, nullptr);
294 rtmsg *prtmsg = reinterpret_cast<struct rtmsg *>(NLMSG_DATA(&binarydata));
295 ASSERT_NE(prtmsg, nullptr);
296 rtattr *prtattr = RTM_RTA(prtmsg);
297 ASSERT_NE(prtattr, nullptr);
298 in_addr *ipv4Addr = reinterpret_cast<struct in_addr *>(RTA_DATA(prtattr));
299 ASSERT_NE(ipv4Addr, nullptr);
300 pmsghdr->nlmsg_type = RTM_NEWROUTE;
301 pmsghdr->nlmsg_len = NLMSG_ALIGN(sizeof(struct nlmsghdr));
302 auto ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
303 EXPECT_FALSE(ret);
304 pmsghdr->nlmsg_len = sizeof(binarydata);
305 ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
306 EXPECT_FALSE(ret);
307
308 rtattr *prtattr1 = nullptr;
309 rtattr *prtattr2 = nullptr;
310
311 InterpreteRtMsgTest001ParmaCheck(prtmsg, prtattr, &prtattr1, &prtattr2, ipv4Addr);
312
313 int32_t *pdeviceindex = reinterpret_cast<int32_t *>(RTA_DATA(prtattr2));
314 *pdeviceindex = -1;
315 ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
316 EXPECT_FALSE(ret);
317 uint32_t index = if_nametoindex("wlan0");
318 if (index == 0) {
319 index = if_nametoindex("eth0");
320 }
321 *pdeviceindex = index;
322 ret = decoder->DecodeBinary(reinterpret_cast<char *>(&binarydata), sizeof(binarydata));
323 if (index > 0) {
324 EXPECT_TRUE(ret);
325 } else {
326 EXPECT_FALSE(ret);
327 }
328 }
329
330 HWTEST_F(WrapperDecoderTest, PushAsciiMessageTest001, TestSize.Level1)
331 {
332 const char *start = TEST_ASCII_MESSAGE;
333 const char *end = start + sizeof(TEST_ASCII_MESSAGE);
334 std::vector<std::string> recvmsg;
335 start += strlen(start) + 1;
336 while (start < end) {
337 if (start != nullptr) {
338 recvmsg.emplace_back(start);
339 }
340 start += strlen(start) + 1;
341 }
342
343 auto msg = std::make_shared<NetsysEventMessage>();
344 std::unique_ptr<WrapperDecoder> decoder = std::make_unique<WrapperDecoder>(msg);
345 auto ret = decoder->PushAsciiMessage(recvmsg);
346 EXPECT_TRUE(ret);
347 }
348
349 HWTEST_F(WrapperDecoderTest, WrapperDecoderBranchTest001, TestSize.Level1)
350 {
351 auto msg = std::make_shared<NetsysEventMessage>();
352 std::unique_ptr<WrapperDecoder> decoder = std::make_unique<WrapperDecoder>(msg);
353
354 nlmsghdr *hdrMsg = nullptr;
355 auto ret = decoder->InterpreteInfoMsg(hdrMsg);
356 EXPECT_FALSE(ret);
357
358 ifaddrmsg *addrMsg = nullptr;
359 std::string testString = "";
360 ifa_cacheinfo *cacheInfo = nullptr;
361 ret = decoder->SaveAddressMsg(testString, addrMsg, testString, cacheInfo, testString);
362 EXPECT_FALSE(ret);
363
364 uint8_t type = RTM_NEWNEIGH;
365 auto result = decoder->CheckRtParam(hdrMsg, type);
366 EXPECT_TRUE(result == nullptr);
367
368 int32_t length = 0;
369 int32_t family = AF_INET6;
370 ret = decoder->SaveRtMsg(testString, testString, testString, length, family);
371 EXPECT_FALSE(ret);
372
373 length = 1;
374 testString = "test";
375 ret = decoder->SaveRtMsg(testString, testString, testString, length, family);
376 EXPECT_TRUE(ret);
377 }
378 } // namespace nmd
379 } // namespace OHOS
380