blob: 5845652a9645c4e2ae156315677969009c94594a [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 }
38};
39
40TEST_F(RouteControllerTest, TestGetRulePriority) {
Lorenzo Colitti60367db2017-02-13 16:31:45 +090041 // Expect a rule dump for these two families to contain at least the following priorities.
42 for (int family : {AF_INET, AF_INET6 }) {
43 std::set<uint32_t> expectedPriorities = {
44 0,
45 15000, // RULE_PRIORITY_LEGACY_SYSTEM
46 16000, // RULE_PRIORITY_LEGACY_NETWORK
47 32000, // RULE_PRIORITY_UNREACHABLE
48 };
49
50 NetlinkDumpCallback callback = [&expectedPriorities] (const nlmsghdr *nlh) {
51 expectedPriorities.erase(getRulePriority(nlh));
52 };
53
54 rtmsg rtm = { .rtm_family = static_cast<uint8_t>(family) };
55 iovec iov[] = {
56 { nullptr, 0 },
57 { &rtm, sizeof(rtm) },
58 };
59
60 ASSERT_EQ(0, sendNetlinkRequest(RTM_GETRULE, NETLINK_DUMP_FLAGS,
61 iov, ARRAY_SIZE(iov), &callback));
62
63 EXPECT_TRUE(expectedPriorities.empty()) <<
64 "Did not see rule with priority " << *expectedPriorities.begin() <<
65 " in dump for address family " << family;
66 }
67}
68
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090069TEST_F(RouteControllerTest, TestRouteFlush) {
Lorenzo Colitti60367db2017-02-13 16:31:45 +090070 // Pick a table number that's not used by the system.
71 const uint32_t table1 = 500;
72 const uint32_t table2 = 600;
73 static_assert(table1 < RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX,
74 "Test table1 number too large");
75 static_assert(table2 < RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX,
76 "Test table2 number too large");
77
78 EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, table1, "lo", "192.0.2.2/32", NULL));
79 EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, table1, "lo", "192.0.2.3/32", NULL));
80 EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, table2, "lo", "192.0.2.4/32", NULL));
81
82 EXPECT_EQ(0, flushRoutes(table1));
83
84 EXPECT_EQ(-ESRCH,
85 modifyIpRoute(RTM_DELROUTE, table1, "lo", "192.0.2.2/32", NULL));
86 EXPECT_EQ(-ESRCH,
87 modifyIpRoute(RTM_DELROUTE, table1, "lo", "192.0.2.3/32", NULL));
88 EXPECT_EQ(0,
89 modifyIpRoute(RTM_DELROUTE, table2, "lo", "192.0.2.4/32", NULL));
90}
91
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090092TEST_F(RouteControllerTest, TestModifyIncomingPacketMark) {
Benedict Wongb9baf262017-12-03 15:43:08 -080093 uint32_t mask = ~Fwmark::getUidBillingMask();
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090094
Benedict Wongb9baf262017-12-03 15:43:08 -080095 static constexpr int TEST_NETID = 30;
96 EXPECT_EQ(0, modifyIncomingPacketMark(TEST_NETID, "netdtest0",
97 PERMISSION_NONE, true));
98 expectIptablesRestoreCommands({StringPrintf(
99 "-t mangle -A routectrl_mangle_INPUT -i netdtest0 -j MARK --set-mark "
100 "0x3001e/0x%x",
101 mask)});
102
103 EXPECT_EQ(0, modifyIncomingPacketMark(TEST_NETID, "netdtest0",
104 PERMISSION_NONE, false));
105 expectIptablesRestoreCommands({StringPrintf(
106 "-t mangle -D routectrl_mangle_INPUT -i netdtest0 -j MARK --set-mark "
107 "0x3001e/0x%x",
108 mask)});
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900109}
110
Lorenzo Colitti60367db2017-02-13 16:31:45 +0900111} // namespace net
112} // namespace android