blob: 6bcb231e75a95b2d4e645cd3349ad391fd3c6656 [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
21#include "NetlinkCommands.h"
22#include "RouteController.h"
23
24namespace android {
25namespace net {
26
27TEST(RouteControllerTest, TestGetRulePriority) {
28 // Expect a rule dump for these two families to contain at least the following priorities.
29 for (int family : {AF_INET, AF_INET6 }) {
30 std::set<uint32_t> expectedPriorities = {
31 0,
32 15000, // RULE_PRIORITY_LEGACY_SYSTEM
33 16000, // RULE_PRIORITY_LEGACY_NETWORK
34 32000, // RULE_PRIORITY_UNREACHABLE
35 };
36
37 NetlinkDumpCallback callback = [&expectedPriorities] (const nlmsghdr *nlh) {
38 expectedPriorities.erase(getRulePriority(nlh));
39 };
40
41 rtmsg rtm = { .rtm_family = static_cast<uint8_t>(family) };
42 iovec iov[] = {
43 { nullptr, 0 },
44 { &rtm, sizeof(rtm) },
45 };
46
47 ASSERT_EQ(0, sendNetlinkRequest(RTM_GETRULE, NETLINK_DUMP_FLAGS,
48 iov, ARRAY_SIZE(iov), &callback));
49
50 EXPECT_TRUE(expectedPriorities.empty()) <<
51 "Did not see rule with priority " << *expectedPriorities.begin() <<
52 " in dump for address family " << family;
53 }
54}
55
56TEST(RouteControllerTest, TestRouteFlush) {
57 // Pick a table number that's not used by the system.
58 const uint32_t table1 = 500;
59 const uint32_t table2 = 600;
60 static_assert(table1 < RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX,
61 "Test table1 number too large");
62 static_assert(table2 < RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX,
63 "Test table2 number too large");
64
65 EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, table1, "lo", "192.0.2.2/32", NULL));
66 EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, table1, "lo", "192.0.2.3/32", NULL));
67 EXPECT_EQ(0, modifyIpRoute(RTM_NEWROUTE, table2, "lo", "192.0.2.4/32", NULL));
68
69 EXPECT_EQ(0, flushRoutes(table1));
70
71 EXPECT_EQ(-ESRCH,
72 modifyIpRoute(RTM_DELROUTE, table1, "lo", "192.0.2.2/32", NULL));
73 EXPECT_EQ(-ESRCH,
74 modifyIpRoute(RTM_DELROUTE, table1, "lo", "192.0.2.3/32", NULL));
75 EXPECT_EQ(0,
76 modifyIpRoute(RTM_DELROUTE, table2, "lo", "192.0.2.4/32", NULL));
77}
78
79} // namespace net
80} // namespace android