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     size_t dataLength100 = 100;
68     size_t dataLength10 = 10;
69     int32_t ret = netLinkMsg.AddAttr(action, nullptr, dataLength100);
70     EXPECT_EQ(ret, -1);
71     char temp[10] = "123456789";
72     char* data = temp;
73     ret = netLinkMsg.AddAttr(action, data, dataLength100);
74     EXPECT_EQ(ret, -1);
75     ret = netLinkMsg.AddAttr(action, data, dataLength10);
76     EXPECT_EQ(ret, 0);
77 }
78 } // namespace NetsysNative
79 } // namespace OHOS
80