Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 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 | |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 21 | #include "IptablesBaseTest.h" |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 22 | #include "NetlinkCommands.h" |
| 23 | #include "RouteController.h" |
| 24 | |
| 25 | namespace android { |
| 26 | namespace net { |
| 27 | |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 28 | class RouteControllerTest : public IptablesBaseTest { |
| 29 | public: |
| 30 | RouteControllerTest() { |
| 31 | RouteController::iptablesRestoreCommandFunction = fakeExecIptablesRestoreCommand; |
| 32 | } |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame^] | 33 | |
| 34 | int flushRoutes(uint32_t a) { |
| 35 | return RouteController::flushRoutes(a); |
| 36 | } |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | TEST_F(RouteControllerTest, TestGetRulePriority) { |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 40 | // 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 Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 68 | TEST_F(RouteControllerTest, TestRouteFlush) { |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 69 | // 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 Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 91 | TEST_F(RouteControllerTest, TestModifyIncomingPacketMark) { |
| 92 | static constexpr int TEST_NETID = 30; |
| 93 | EXPECT_EQ(0, modifyIncomingPacketMark(TEST_NETID, "netdtest0", PERMISSION_NONE, true)); |
Lorenzo Colitti | d78843e | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 94 | expectIptablesRestoreCommands({ |
| 95 | "-t mangle -A routectrl_mangle_INPUT -i netdtest0 -j MARK --set-mark 0x3001e" }); |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 96 | |
| 97 | EXPECT_EQ(0, modifyIncomingPacketMark(TEST_NETID, "netdtest0", PERMISSION_NONE, false)); |
Lorenzo Colitti | d78843e | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 98 | expectIptablesRestoreCommands({ |
| 99 | "-t mangle -D routectrl_mangle_INPUT -i netdtest0 -j MARK --set-mark 0x3001e" }); |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 100 | } |
| 101 | |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 102 | } // namespace net |
| 103 | } // namespace android |