RouteControllerTest.cpp 4.0 KB

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