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