• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "netlink_msg.h"
19 #include "netnative_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace NetsysNative {
23 using namespace testing::ext;
24 using namespace OHOS::nmd;
25 class NetlinkMsgTest : public testing::Test {
26 public:
27     static void SetUpTestCase();
28     static void TearDownTestCase();
29     void SetUp();
30     void TearDown();
31 };
32 
SetUpTestCase()33 void NetlinkMsgTest::SetUpTestCase() {}
34 
TearDownTestCase()35 void NetlinkMsgTest::TearDownTestCase() {}
36 
SetUp()37 void NetlinkMsgTest::SetUp() {}
38 
TearDown()39 void NetlinkMsgTest::TearDown() {}
40 
41 HWTEST_F(NetlinkMsgTest, AddRouteTest001, TestSize.Level1)
42 {
43     NETNATIVE_LOGI("AddRouteTest001 enter");
44     uint16_t flags = 2;
45     size_t maxBufLen = 50;
46     int32_t pid = 513;
47     NetlinkMsg netLinkMsg(flags, maxBufLen, pid);
48     rtmsg rtmsg = {
49         .rtm_family = 0,
50         .rtm_dst_len = 10,
51         .rtm_src_len = 10,
52     };
53     uint16_t action = 2;
54     netLinkMsg.AddRoute(action, rtmsg);
55     fib_rule_hdr hdr = {
56         .family = 0,
57         .dst_len = 10,
58         .src_len = 10,
59     };
60     ifaddrmsg addrmsg = {
61         .ifa_family = 0,
62         .ifa_prefixlen = 10,
63         .ifa_flags = 1,
64     };
65     netLinkMsg.AddRule(action, hdr);
66     netLinkMsg.AddAddress(action, addrmsg);
67 
68     uint16_t action1 = 28;
69     uint32_t index = 1;
70     struct ndmsg ndm = {};
71     ndm.ndm_family = AF_INET6;
72     ndm.ndm_ifindex = index;
73     ndm.ndm_pad1 = 0;
74     ndm.ndm_pad2 = 0;
75     ndm.ndm_state = NUD_PERMANENT;
76     ndm.ndm_flags = 0;
77     ndm.ndm_type = RTN_UNICAST;
78     netLinkMsg.AddNeighbor(action1, ndm);
79     size_t dataLength100 = 100;
80     size_t dataLength10 = 10;
81     int32_t ret = netLinkMsg.AddAttr(action, nullptr, dataLength100);
82     EXPECT_EQ(ret, -1);
83     char temp[10] = "123456789";
84     char* data = temp;
85     ret = netLinkMsg.AddAttr(action, data, dataLength100);
86     EXPECT_EQ(ret, -1);
87     ret = netLinkMsg.AddAttr(action, data, dataLength10);
88     EXPECT_EQ(ret, 0);
89 }
90 } // namespace NetsysNative
91 } // namespace OHOS
92