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 15000, // RULE_PRIORITY_LEGACY_SYSTEM
50 16000, // RULE_PRIORITY_LEGACY_NETWORK
51 32000, // 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, table1, "lo", "192.0.2.2/32", nullptr));
83 EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, table1, "lo", "192.0.2.3/32", nullptr));
84 EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, table2, "lo", "192.0.2.4/32", nullptr));
85
86 EXPECT_EQ(0, flushRoutes(table1));
87
88 EXPECT_EQ(-ESRCH,
89 modifyIpRoute(RTM_DELROUTE, table1, "lo", "192.0.2.2/32", nullptr));
90 EXPECT_EQ(-ESRCH,
91 modifyIpRoute(RTM_DELROUTE, table1, "lo", "192.0.2.3/32", nullptr));
92 EXPECT_EQ(0,
93 modifyIpRoute(RTM_DELROUTE, table2, "lo", "192.0.2.4/32", nullptr));
94 }
95
TEST_F(RouteControllerTest,TestModifyIncomingPacketMark)96 TEST_F(RouteControllerTest, TestModifyIncomingPacketMark) {
97 uint32_t mask = ~Fwmark::getUidBillingMask();
98
99 static constexpr int TEST_NETID = 30;
100 EXPECT_EQ(0, modifyIncomingPacketMark(TEST_NETID, "netdtest0",
101 PERMISSION_NONE, true));
102 expectIptablesRestoreCommands({StringPrintf(
103 "-t mangle -A routectrl_mangle_INPUT -i netdtest0 -j MARK --set-mark "
104 "0x3001e/0x%x",
105 mask)});
106
107 EXPECT_EQ(0, modifyIncomingPacketMark(TEST_NETID, "netdtest0",
108 PERMISSION_NONE, false));
109 expectIptablesRestoreCommands({StringPrintf(
110 "-t mangle -D routectrl_mangle_INPUT -i netdtest0 -j MARK --set-mark "
111 "0x3001e/0x%x",
112 mask)});
113 }
114
115 } // namespace net
116 } // namespace android
117