1 /*
2 * Copyright (c) 2021 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 <sys/socket.h>
17
18 #include <gtest/gtest.h>
19
20 #include "net_manager_constants.h"
21 #include "route_utils.h"
22 #include "net_conn_types.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
26 namespace {
27 using namespace testing::ext;
GetRoute()28 Route GetRoute()
29 {
30 std::string iface("eth0");
31 Route route;
32 route.iface_ = iface;
33 route.rtnType_ = RTN_UNICAST;
34 route.hasGateway_ = true;
35 route.isDefaultRoute_ = false;
36 route.destination_.type_ = INetAddr::IPV4;
37 route.destination_.family_ = AF_INET;
38 route.destination_.prefixlen_ = 0x18;
39 route.destination_.address_ = "192.168.2.10";
40 route.destination_.netMask_ = "255.255.255.0";
41 route.destination_.hostName_ = "netAddr";
42 route.gateway_.type_ = INetAddr::IPV4;
43 route.gateway_.family_ = AF_INET;
44 route.gateway_.prefixlen_ = 0x18;
45 route.gateway_.address_ = "192.168.2.1";
46 route.gateway_.netMask_ = "255.255.255.0";
47 route.gateway_.hostName_ = "netAddr";
48 return route;
49 }
50
51 constexpr uint32_t TEST_NETID = 110;
52 } // namespace
53
54 class RouteUtilsTest : public testing::Test {
55 public:
56 static void SetUpTestCase();
57 static void TearDownTestCase();
58 void SetUp();
59 void TearDown();
60 };
61
SetUpTestCase()62 void RouteUtilsTest::SetUpTestCase() {}
63
TearDownTestCase()64 void RouteUtilsTest::TearDownTestCase() {}
65
SetUp()66 void RouteUtilsTest::SetUp() {}
67
TearDown()68 void RouteUtilsTest::TearDown() {}
69
70 HWTEST_F(RouteUtilsTest, AddRouteToLocal01, TestSize.Level1)
71 {
72 std::list<Route> rList;
73 std::string iface("eth0");
74 rList.push_back(GetRoute());
75 RouteUtils::AddRoutesToLocal(iface, rList);
76 EXPECT_FALSE(rList.empty());
77 }
78
79 HWTEST_F(RouteUtilsTest, RemoveRouteFromLocal01, TestSize.Level1)
80 {
81 std::list<Route> rList;
82 rList.push_back(GetRoute());
83
84 EXPECT_EQ(0, RouteUtils::RemoveRoutesFromLocal(rList));
85 }
86
87 HWTEST_F(RouteUtilsTest, AddRoute01, TestSize.Level1)
88 {
89 EXPECT_GE(0, RouteUtils::AddRoute(TEST_NETID, GetRoute()));
90 }
91
92 HWTEST_F(RouteUtilsTest, RemoveRoute01, TestSize.Level1)
93 {
94 EXPECT_GE(0, RouteUtils::RemoveRoute(TEST_NETID, GetRoute()));
95 }
96
97 HWTEST_F(RouteUtilsTest, UpdateRoutes01, TestSize.Level1)
98 {
99 NetLinkInfo nlio;
100 NetLinkInfo nlin;
101 nlio.routeList_.push_back(GetRoute());
102
103 EXPECT_TRUE(RouteUtils::UpdateRoutes(TEST_NETID, nlin, nlio));
104 }
105 } // namespace NetManagerStandard
106 } // namespace OHOS