• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * RouteControllerTest.cpp - unit tests for RouteController.cpp
17  */
18 
19 #include <gtest/gtest.h>
20 
21 #include "Fwmark.h"
22 #include "IptablesBaseTest.h"
23 #include "NetlinkCommands.h"
24 #include "RouteController.h"
25 
26 #include <android-base/stringprintf.h>
27 
28 using android::base::StringPrintf;
29 
30 namespace android {
31 namespace net {
32 
33 class RouteControllerTest : public IptablesBaseTest {
34 public:
RouteControllerTest()35     RouteControllerTest() {
36         RouteController::iptablesRestoreCommandFunction = fakeExecIptablesRestoreCommand;
37     }
38 
flushRoutes(uint32_t a)39     int flushRoutes(uint32_t a) {
40         return RouteController::flushRoutes(a);
41     }
42 };
43 
TEST_F(RouteControllerTest,TestGetRulePriority)44 TEST_F(RouteControllerTest, TestGetRulePriority) {
45     // Expect a rule dump for these two families to contain at least the following priorities.
46     for (int family : {AF_INET, AF_INET6 }) {
47         std::set<uint32_t> expectedPriorities = {
48                 0,
49                 RULE_PRIORITY_LEGACY_SYSTEM,
50                 RULE_PRIORITY_LEGACY_NETWORK,
51                 RULE_PRIORITY_UNREACHABLE,
52         };
53 
54         NetlinkDumpCallback callback = [&expectedPriorities] (const nlmsghdr *nlh) {
55             expectedPriorities.erase(getRulePriority(nlh));
56         };
57 
58         rtmsg rtm = { .rtm_family = static_cast<uint8_t>(family) };
59         iovec iov[] = {
60             { nullptr, 0           },
61             { &rtm,    sizeof(rtm) },
62         };
63 
64         ASSERT_EQ(0, sendNetlinkRequest(RTM_GETRULE, NETLINK_DUMP_FLAGS,
65                                         iov, ARRAY_SIZE(iov), &callback));
66 
67         EXPECT_TRUE(expectedPriorities.empty()) <<
68             "Did not see rule with priority " << *expectedPriorities.begin() <<
69             " in dump for address family " << family;
70     }
71 }
72 
TEST_F(RouteControllerTest,TestRouteFlush)73 TEST_F(RouteControllerTest, TestRouteFlush) {
74     // Pick a table number that's not used by the system.
75     const uint32_t table1 = 500;
76     const uint32_t table2 = 600;
77     static_assert(table1 < RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX,
78                   "Test table1 number too large");
79     static_assert(table2 < RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX,
80                   "Test table2 number too large");
81 
82     EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table1, "lo",
83               "192.0.2.2/32", nullptr, 0 /* mtu */));
84     EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table1, "lo",
85               "192.0.2.3/32", nullptr, 0 /* mtu */));
86     EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table2, "lo",
87               "192.0.2.4/32", nullptr, 0 /* mtu */));
88 
89     EXPECT_EQ(0, flushRoutes(table1));
90 
91     EXPECT_EQ(-ESRCH,
92               modifyIpRoute(RTM_DELROUTE, NETLINK_ROUTE_CREATE_FLAGS, table1, "lo", "192.0.2.2/32",
93                             nullptr, 0 /* mtu */));
94     EXPECT_EQ(-ESRCH,
95               modifyIpRoute(RTM_DELROUTE, NETLINK_ROUTE_CREATE_FLAGS, table1, "lo", "192.0.2.3/32",
96                             nullptr, 0 /* mtu */));
97     EXPECT_EQ(0,
98               modifyIpRoute(RTM_DELROUTE, NETLINK_ROUTE_CREATE_FLAGS, table2, "lo", "192.0.2.4/32",
99                             nullptr, 0 /* mtu */));
100 }
101 
TEST_F(RouteControllerTest,TestModifyIncomingPacketMark)102 TEST_F(RouteControllerTest, TestModifyIncomingPacketMark) {
103   uint32_t mask = ~Fwmark::getUidBillingMask();
104 
105   static constexpr int TEST_NETID = 30;
106   EXPECT_EQ(0, modifyIncomingPacketMark(TEST_NETID, "netdtest0",
107                                         PERMISSION_NONE, true));
108   expectIptablesRestoreCommands({StringPrintf(
109       "-t mangle -A routectrl_mangle_INPUT -i netdtest0 -j MARK --set-mark "
110       "0x3001e/0x%x",
111       mask)});
112 
113   EXPECT_EQ(0, modifyIncomingPacketMark(TEST_NETID, "netdtest0",
114                                         PERMISSION_NONE, false));
115   expectIptablesRestoreCommands({StringPrintf(
116       "-t mangle -D routectrl_mangle_INPUT -i netdtest0 -j MARK --set-mark "
117       "0x3001e/0x%x",
118       mask)});
119 }
120 
121 }  // namespace net
122 }  // namespace android
123