blob: fed15a3a5c913e07b595e9a751e7321464ea1a12 [file] [log] [blame]
Lorenzo Colitti60367db2017-02-13 16:31:45 +09001/*
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
Benedict Wongb9baf262017-12-03 15:43:08 -080021#include "Fwmark.h"
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090022#include "IptablesBaseTest.h"
Lorenzo Colitti60367db2017-02-13 16:31:45 +090023#include "NetlinkCommands.h"
24#include "RouteController.h"
25
Benedict Wongb9baf262017-12-03 15:43:08 -080026#include <android-base/stringprintf.h>
27
28using android::base::StringPrintf;
29
Lorenzo Colitti60367db2017-02-13 16:31:45 +090030namespace android {
31namespace net {
32
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090033class RouteControllerTest : public IptablesBaseTest {
34public:
35 RouteControllerTest() {
36 RouteController::iptablesRestoreCommandFunction = fakeExecIptablesRestoreCommand;
37 }
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +090038
39 int flushRoutes(uint32_t a) {
40 return RouteController::flushRoutes(a);
41 }
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090042};
43
44TEST_F(RouteControllerTest, TestGetRulePriority) {
Lorenzo Colitti60367db2017-02-13 16:31:45 +090045 // 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
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090073TEST_F(RouteControllerTest, TestRouteFlush) {
Lorenzo Colitti60367db2017-02-13 16:31:45 +090074 // 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
Tyler Wearfa94a272019-12-05 15:01:48 -080082 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 */));
Lorenzo Colitti60367db2017-02-13 16:31:45 +090088
89 EXPECT_EQ(0, flushRoutes(table1));
90
91 EXPECT_EQ(-ESRCH,
Tyler Wearfa94a272019-12-05 15:01:48 -080092 modifyIpRoute(RTM_DELROUTE, NETLINK_ROUTE_CREATE_FLAGS, table1, "lo", "192.0.2.2/32",
93 nullptr, 0 /* mtu */));
Lorenzo Colitti60367db2017-02-13 16:31:45 +090094 EXPECT_EQ(-ESRCH,
Tyler Wearfa94a272019-12-05 15:01:48 -080095 modifyIpRoute(RTM_DELROUTE, NETLINK_ROUTE_CREATE_FLAGS, table1, "lo", "192.0.2.3/32",
96 nullptr, 0 /* mtu */));
Lorenzo Colitti60367db2017-02-13 16:31:45 +090097 EXPECT_EQ(0,
Tyler Wearfa94a272019-12-05 15:01:48 -080098 modifyIpRoute(RTM_DELROUTE, NETLINK_ROUTE_CREATE_FLAGS, table2, "lo", "192.0.2.4/32",
99 nullptr, 0 /* mtu */));
Lorenzo Colitti60367db2017-02-13 16:31:45 +0900100}
101
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900102TEST_F(RouteControllerTest, TestModifyIncomingPacketMark) {
Benedict Wongb9baf262017-12-03 15:43:08 -0800103 uint32_t mask = ~Fwmark::getUidBillingMask();
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900104
Benedict Wongb9baf262017-12-03 15:43:08 -0800105 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)});
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900119}
120
Lorenzo Colitti60367db2017-02-13 16:31:45 +0900121} // namespace net
122} // namespace android