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 | |
Benedict Wong | b9baf26 | 2017-12-03 15:43:08 -0800 | [diff] [blame^] | 21 | #include "Fwmark.h" |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 22 | #include "IptablesBaseTest.h" |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 23 | #include "NetlinkCommands.h" |
| 24 | #include "RouteController.h" |
| 25 | |
Benedict Wong | b9baf26 | 2017-12-03 15:43:08 -0800 | [diff] [blame^] | 26 | #include <android-base/stringprintf.h> |
| 27 | |
| 28 | using android::base::StringPrintf; |
| 29 | |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 30 | namespace android { |
| 31 | namespace net { |
| 32 | |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 33 | class RouteControllerTest : public IptablesBaseTest { |
| 34 | public: |
| 35 | RouteControllerTest() { |
| 36 | RouteController::iptablesRestoreCommandFunction = fakeExecIptablesRestoreCommand; |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | TEST_F(RouteControllerTest, TestGetRulePriority) { |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 41 | // 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 Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 69 | TEST_F(RouteControllerTest, TestRouteFlush) { |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 70 | // 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 Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 92 | TEST_F(RouteControllerTest, TestModifyIncomingPacketMark) { |
Benedict Wong | b9baf26 | 2017-12-03 15:43:08 -0800 | [diff] [blame^] | 93 | uint32_t mask = ~Fwmark::getUidBillingMask(); |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 94 | |
Benedict Wong | b9baf26 | 2017-12-03 15:43:08 -0800 | [diff] [blame^] | 95 | 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 Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 109 | } |
| 110 | |
Lorenzo Colitti | 60367db | 2017-02-13 16:31:45 +0900 | [diff] [blame] | 111 | } // namespace net |
| 112 | } // namespace android |