blob: 62540b9302d90c8f8ee66fcd81fe9aab83757de8 [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
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090021#include "IptablesBaseTest.h"
Lorenzo Colitti60367db2017-02-13 16:31:45 +090022#include "NetlinkCommands.h"
23#include "RouteController.h"
24
25namespace android {
26namespace net {
27
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090028class RouteControllerTest : public IptablesBaseTest {
29public:
30 RouteControllerTest() {
31 RouteController::iptablesRestoreCommandFunction = fakeExecIptablesRestoreCommand;
32 }
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +090033
34 int flushRoutes(uint32_t a) {
35 return RouteController::flushRoutes(a);
36 }
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090037};
38
39TEST_F(RouteControllerTest, TestGetRulePriority) {
Lorenzo Colitti60367db2017-02-13 16:31:45 +090040 // Expect a rule dump for these two families to contain at least the following priorities.
41 for (int family : {AF_INET, AF_INET6 }) {
42 std::set<uint32_t> expectedPriorities = {
43 0,
44 15000, // RULE_PRIORITY_LEGACY_SYSTEM
45 16000, // RULE_PRIORITY_LEGACY_NETWORK
46 32000, // RULE_PRIORITY_UNREACHABLE
47 };
48
49 NetlinkDumpCallback callback = [&expectedPriorities] (const nlmsghdr *nlh) {
50 expectedPriorities.erase(getRulePriority(nlh));
51 };
52
53 rtmsg rtm = { .rtm_family = static_cast<uint8_t>(family) };
54 iovec iov[] = {
55 { nullptr, 0 },
56 { &rtm, sizeof(rtm) },
57 };
58
59 ASSERT_EQ(0, sendNetlinkRequest(RTM_GETRULE, NETLINK_DUMP_FLAGS,
60 iov, ARRAY_SIZE(iov), &callback));
61
62 EXPECT_TRUE(expectedPriorities.empty()) <<
63 "Did not see rule with priority " << *expectedPriorities.begin() <<
64 " in dump for address family " << family;
65 }
66}
67
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090068TEST_F(RouteControllerTest, TestRouteFlush) {
Lorenzo Colitti60367db2017-02-13 16:31:45 +090069 // Pick a table number that's not used by the system.
70 const uint32_t table1 = 500;
71 const uint32_t table2 = 600;
72 static_assert(table1 < RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX,
73 "Test table1 number too large");
74 static_assert(table2 < RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX,
75 "Test table2 number too large");
76
77 EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, table1, "lo", "192.0.2.2/32", NULL));
78 EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, table1, "lo", "192.0.2.3/32", NULL));
79 EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, table2, "lo", "192.0.2.4/32", NULL));
80
81 EXPECT_EQ(0, flushRoutes(table1));
82
83 EXPECT_EQ(-ESRCH,
84 modifyIpRoute(RTM_DELROUTE, table1, "lo", "192.0.2.2/32", NULL));
85 EXPECT_EQ(-ESRCH,
86 modifyIpRoute(RTM_DELROUTE, table1, "lo", "192.0.2.3/32", NULL));
87 EXPECT_EQ(0,
88 modifyIpRoute(RTM_DELROUTE, table2, "lo", "192.0.2.4/32", NULL));
89}
90
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090091TEST_F(RouteControllerTest, TestModifyIncomingPacketMark) {
92 static constexpr int TEST_NETID = 30;
93 EXPECT_EQ(0, modifyIncomingPacketMark(TEST_NETID, "netdtest0", PERMISSION_NONE, true));
Lorenzo Colittid78843e2017-03-27 05:52:31 +090094 expectIptablesRestoreCommands({
95 "-t mangle -A routectrl_mangle_INPUT -i netdtest0 -j MARK --set-mark 0x3001e" });
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090096
97 EXPECT_EQ(0, modifyIncomingPacketMark(TEST_NETID, "netdtest0", PERMISSION_NONE, false));
Lorenzo Colittid78843e2017-03-27 05:52:31 +090098 expectIptablesRestoreCommands({
99 "-t mangle -D routectrl_mangle_INPUT -i netdtest0 -j MARK --set-mark 0x3001e" });
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900100}
101
Lorenzo Colitti60367db2017-02-13 16:31:45 +0900102} // namespace net
103} // namespace android