mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Alex Deymo | cddd2d0 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 5 | #include "shill/routing_table.h" |
| 6 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 7 | #include <linux/rtnetlink.h> |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 8 | #include <sys/socket.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 9 | |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 10 | #include <memory> |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 13 | #include <base/bind.h> |
| 14 | #include <base/callback.h> |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 15 | #include <base/memory/weak_ptr.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 16 | #include <base/stl_util.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 17 | #include <gtest/gtest.h> |
| 18 | #include <gmock/gmock.h> |
| 19 | |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 20 | #include "shill/event_dispatcher.h" |
| 21 | #include "shill/ipconfig.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 22 | #include "shill/logging.h" |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 23 | #include "shill/mock_control.h" |
Peter Qiu | 8d6b597 | 2014-10-28 15:33:34 -0700 | [diff] [blame] | 24 | #include "shill/net/byte_string.h" |
| 25 | #include "shill/net/mock_rtnl_handler.h" |
| 26 | #include "shill/net/rtnl_message.h" |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 27 | #include "shill/routing_table_entry.h" |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 28 | |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 29 | using base::Bind; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 30 | using base::Callback; |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 31 | using base::Unretained; |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 32 | using std::deque; |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 33 | using std::vector; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 34 | using testing::_; |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 35 | using testing::Field; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 36 | using testing::Invoke; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 37 | using testing::Return; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 38 | using testing::StrictMock; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 39 | using testing::Test; |
| 40 | |
| 41 | namespace shill { |
| 42 | |
| 43 | class TestEventDispatcher : public EventDispatcher { |
| 44 | public: |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 45 | virtual IOHandler* CreateInputHandler( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 46 | int /*fd*/, |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 47 | const IOHandler::InputCallback& /*input_callback*/, |
| 48 | const IOHandler::ErrorCallback& /*error_callback*/) { |
Ben Chan | cc225ef | 2014-09-30 13:26:51 -0700 | [diff] [blame] | 49 | return nullptr; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 50 | } |
| 51 | }; |
| 52 | |
| 53 | class RoutingTableTest : public Test { |
| 54 | public: |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 55 | static const uint8_t kTestTableId; |
| 56 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 57 | RoutingTableTest() : routing_table_(new RoutingTable()) {} |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 58 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 59 | virtual void SetUp() { |
| 60 | routing_table_->rtnl_handler_ = &rtnl_handler_; |
| 61 | ON_CALL(rtnl_handler_, SendMessage(_)).WillByDefault(Return(true)); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Paul Stewart | 65c40f5 | 2011-08-08 07:27:46 -0700 | [diff] [blame] | 64 | virtual void TearDown() { |
| 65 | RTNLHandler::GetInstance()->Stop(); |
| 66 | } |
| 67 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 68 | std::unordered_map<int, vector<RoutingTableEntry>>* GetRoutingTables() { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 69 | return &routing_table_->tables_; |
| 70 | } |
| 71 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 72 | deque<RoutingTable::Query>* GetQueries() { |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 73 | return &routing_table_->route_queries_; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void SendRouteEntry(RTNLMessage::Mode mode, |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 77 | uint32_t interface_index, |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 78 | const RoutingTableEntry& entry); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 79 | |
| 80 | void SendRouteEntryWithSeqAndProto(RTNLMessage::Mode mode, |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 81 | uint32_t interface_index, |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 82 | const RoutingTableEntry& entry, |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 83 | uint32_t seq, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 84 | unsigned char proto); |
| 85 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 86 | void SendRouteMessage(const RTNLMessage& msg); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 87 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 88 | bool SetSequenceForMessage(RTNLMessage* message) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 89 | message->set_seq(RoutingTableTest::kTestRequestSeq); |
| 90 | return true; |
| 91 | } |
| 92 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 93 | protected: |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 94 | static const uint32_t kTestDeviceIndex0; |
| 95 | static const uint32_t kTestDeviceIndex1; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 96 | static const char kTestDeviceName0[]; |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame] | 97 | static const char kTestDeviceNetAddress4[]; |
| 98 | static const char kTestForeignNetAddress4[]; |
| 99 | static const char kTestForeignNetGateway4[]; |
| 100 | static const char kTestForeignNetAddress6[]; |
| 101 | static const char kTestForeignNetGateway6[]; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 102 | static const char kTestGatewayAddress4[]; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 103 | static const char kTestNetAddress0[]; |
| 104 | static const char kTestNetAddress1[]; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 105 | static const char kTestRemoteAddress4[]; |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 106 | static const char kTestRemoteNetmask4[]; |
| 107 | static const char kTestRemoteNetwork4[]; |
| 108 | static const int kTestRemotePrefix4; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 109 | static const uint32_t kTestRequestSeq; |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 110 | static const int kTestRouteTag; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 111 | |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 112 | class QueryCallbackTarget { |
| 113 | public: |
| 114 | QueryCallbackTarget() |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 115 | : weak_ptr_factory_(this), |
| 116 | mocked_callback_( |
| 117 | Bind(&QueryCallbackTarget::MockedTarget, Unretained(this))), |
| 118 | unreached_callback_(Bind(&QueryCallbackTarget::UnreachedTarget, |
| 119 | weak_ptr_factory_.GetWeakPtr())) {} |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 120 | |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 121 | MOCK_METHOD2(MockedTarget, |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 122 | void(int interface_index, const RoutingTableEntry& entry)); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 123 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 124 | void UnreachedTarget(int interface_index, const RoutingTableEntry& entry) { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 125 | CHECK(false); |
| 126 | } |
| 127 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 128 | const RoutingTable::Query::Callback& mocked_callback() const { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 129 | return mocked_callback_; |
| 130 | } |
| 131 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 132 | const RoutingTable::Query::Callback& unreached_callback() const { |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 133 | return unreached_callback_; |
| 134 | } |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 135 | |
| 136 | private: |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 137 | base::WeakPtrFactory<QueryCallbackTarget> weak_ptr_factory_; |
| 138 | const RoutingTable::Query::Callback mocked_callback_; |
| 139 | const RoutingTable::Query::Callback unreached_callback_; |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 140 | }; |
| 141 | |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 142 | std::unique_ptr<RoutingTable> routing_table_; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 143 | TestEventDispatcher dispatcher_; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 144 | StrictMock<MockRTNLHandler> rtnl_handler_; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 147 | const uint32_t RoutingTableTest::kTestDeviceIndex0 = 12345; |
| 148 | const uint32_t RoutingTableTest::kTestDeviceIndex1 = 67890; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 149 | const char RoutingTableTest::kTestDeviceName0[] = "test-device0"; |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame] | 150 | const char RoutingTableTest::kTestDeviceNetAddress4[] = "192.168.2.0/24"; |
| 151 | const char RoutingTableTest::kTestForeignNetAddress4[] = "192.168.2.2"; |
| 152 | const char RoutingTableTest::kTestForeignNetGateway4[] = "192.168.2.1"; |
| 153 | const char RoutingTableTest::kTestForeignNetAddress6[] = "2000::/3"; |
| 154 | const char RoutingTableTest::kTestForeignNetGateway6[] = "fe80:::::1"; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 155 | const char RoutingTableTest::kTestGatewayAddress4[] = "192.168.2.254"; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 156 | const char RoutingTableTest::kTestNetAddress0[] = "192.168.1.1"; |
| 157 | const char RoutingTableTest::kTestNetAddress1[] = "192.168.1.2"; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 158 | const char RoutingTableTest::kTestRemoteAddress4[] = "192.168.2.254"; |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 159 | const char RoutingTableTest::kTestRemoteNetmask4[] = "255.255.255.0"; |
| 160 | const char RoutingTableTest::kTestRemoteNetwork4[] = "192.168.100.0"; |
| 161 | const int RoutingTableTest::kTestRemotePrefix4 = 24; |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 162 | const uint32_t RoutingTableTest::kTestRequestSeq = 456; |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 163 | const int RoutingTableTest::kTestRouteTag = 789; |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 164 | const uint8_t RoutingTableTest::kTestTableId = 0xa5; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 165 | |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 166 | namespace { |
| 167 | |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 168 | MATCHER_P3(IsBlackholeRoutingPacket, index, family, metric, "") { |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 169 | const RTNLMessage::RouteStatus& status = arg->route_status(); |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 170 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 171 | uint32_t oif; |
| 172 | uint32_t priority; |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 173 | |
| 174 | return |
| 175 | arg->type() == RTNLMessage::kTypeRoute && |
| 176 | arg->family() == family && |
| 177 | arg->flags() == (NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL) && |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 178 | status.table == RoutingTableTest::kTestTableId && |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 179 | status.protocol == RTPROT_BOOT && |
| 180 | status.scope == RT_SCOPE_UNIVERSE && |
| 181 | status.type == RTN_BLACKHOLE && |
| 182 | !arg->HasAttribute(RTA_DST) && |
| 183 | !arg->HasAttribute(RTA_SRC) && |
| 184 | !arg->HasAttribute(RTA_GATEWAY) && |
| 185 | arg->GetAttribute(RTA_OIF).ConvertToCPUUInt32(&oif) && |
| 186 | oif == index && |
| 187 | arg->GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&priority) && |
| 188 | priority == metric; |
| 189 | } |
| 190 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 191 | MATCHER_P4(IsRoutingPacket, mode, index, entry, flags, "") { |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 192 | const RTNLMessage::RouteStatus& status = arg->route_status(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 193 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 194 | uint32_t oif; |
| 195 | uint32_t priority; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 196 | |
| 197 | return |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 198 | arg->type() == RTNLMessage::kTypeRoute && |
| 199 | arg->family() == entry.gateway.family() && |
| 200 | arg->flags() == (NLM_F_REQUEST | flags) && |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 201 | status.table == RoutingTableTest::kTestTableId && |
| 202 | entry.table == RoutingTableTest::kTestTableId && |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 203 | status.protocol == RTPROT_BOOT && |
| 204 | status.scope == entry.scope && |
| 205 | status.type == RTN_UNICAST && |
| 206 | arg->HasAttribute(RTA_DST) && |
| 207 | IPAddress(arg->family(), |
| 208 | arg->GetAttribute(RTA_DST), |
| 209 | status.dst_prefix).Equals(entry.dst) && |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 210 | ((!arg->HasAttribute(RTA_SRC) && entry.src.IsDefault()) || |
| 211 | (arg->HasAttribute(RTA_SRC) && IPAddress(arg->family(), |
| 212 | arg->GetAttribute(RTA_SRC), |
| 213 | status.src_prefix).Equals(entry.src))) && |
| 214 | ((!arg->HasAttribute(RTA_GATEWAY) && entry.gateway.IsDefault()) || |
| 215 | (arg->HasAttribute(RTA_GATEWAY) && IPAddress(arg->family(), |
| 216 | arg->GetAttribute(RTA_GATEWAY)).Equals(entry.gateway))) && |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 217 | arg->GetAttribute(RTA_OIF).ConvertToCPUUInt32(&oif) && |
| 218 | oif == index && |
| 219 | arg->GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&priority) && |
| 220 | priority == entry.metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 223 | } // namespace |
| 224 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 225 | void RoutingTableTest::SendRouteEntry(RTNLMessage::Mode mode, |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 226 | uint32_t interface_index, |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 227 | const RoutingTableEntry& entry) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 228 | SendRouteEntryWithSeqAndProto(mode, interface_index, entry, 0, RTPROT_BOOT); |
| 229 | } |
| 230 | |
| 231 | void RoutingTableTest::SendRouteEntryWithSeqAndProto( |
| 232 | RTNLMessage::Mode mode, |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 233 | uint32_t interface_index, |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 234 | const RoutingTableEntry& entry, |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 235 | uint32_t seq, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 236 | unsigned char proto) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 237 | RTNLMessage msg( |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 238 | RTNLMessage::kTypeRoute, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 239 | mode, |
| 240 | 0, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 241 | seq, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 242 | 0, |
| 243 | 0, |
| 244 | entry.dst.family()); |
| 245 | |
| 246 | msg.set_route_status(RTNLMessage::RouteStatus( |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 247 | entry.dst.prefix(), |
| 248 | entry.src.prefix(), |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 249 | entry.table, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 250 | proto, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 251 | entry.scope, |
| 252 | RTN_UNICAST, |
| 253 | 0)); |
| 254 | |
| 255 | msg.SetAttribute(RTA_DST, entry.dst.address()); |
| 256 | if (!entry.src.IsDefault()) { |
| 257 | msg.SetAttribute(RTA_SRC, entry.src.address()); |
| 258 | } |
| 259 | if (!entry.gateway.IsDefault()) { |
| 260 | msg.SetAttribute(RTA_GATEWAY, entry.gateway.address()); |
| 261 | } |
| 262 | msg.SetAttribute(RTA_PRIORITY, ByteString::CreateFromCPUUInt32(entry.metric)); |
| 263 | msg.SetAttribute(RTA_OIF, ByteString::CreateFromCPUUInt32(interface_index)); |
| 264 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 265 | routing_table_->RouteMsgHandler(msg); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 268 | void RoutingTableTest::SendRouteMessage(const RTNLMessage& msg) { |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 269 | routing_table_->RouteMsgHandler(msg); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 272 | TEST_F(RoutingTableTest, Start) { |
| 273 | EXPECT_CALL(rtnl_handler_, RequestDump(RTNLHandler::kRequestRoute)); |
| 274 | routing_table_->Start(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | TEST_F(RoutingTableTest, RouteAddDelete) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 278 | // Expect the tables to be empty by default. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 279 | EXPECT_EQ(0, GetRoutingTables()->size()); |
| 280 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 281 | IPAddress default_address(IPAddress::kFamilyIPv4); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 282 | default_address.SetAddressToDefault(); |
| 283 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 284 | IPAddress gateway_address0(IPAddress::kFamilyIPv4); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 285 | gateway_address0.SetAddressFromString(kTestNetAddress0); |
| 286 | |
| 287 | int metric = 10; |
| 288 | |
| 289 | RoutingTableEntry entry0(default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 290 | default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 291 | gateway_address0, |
| 292 | metric, |
| 293 | RT_SCOPE_UNIVERSE, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 294 | true, |
| 295 | kTestTableId, |
| 296 | RoutingTableEntry::kDefaultTag); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 297 | // Add a single entry. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 298 | SendRouteEntry(RTNLMessage::kModeAdd, |
| 299 | kTestDeviceIndex0, |
| 300 | entry0); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 301 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 302 | std::unordered_map<int, vector<RoutingTableEntry>>* tables = |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 303 | GetRoutingTables(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 304 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 305 | // We should have a single table, which should in turn have a single entry. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 306 | EXPECT_EQ(1, tables->size()); |
| 307 | EXPECT_TRUE(ContainsKey(*tables, kTestDeviceIndex0)); |
| 308 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 309 | |
| 310 | RoutingTableEntry test_entry = (*tables)[kTestDeviceIndex0][0]; |
| 311 | EXPECT_TRUE(entry0.Equals(test_entry)); |
| 312 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 313 | // Add a second entry for a different interface. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 314 | SendRouteEntry(RTNLMessage::kModeAdd, |
| 315 | kTestDeviceIndex1, |
| 316 | entry0); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 317 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 318 | // We should have two tables, which should have a single entry each. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 319 | EXPECT_EQ(2, tables->size()); |
| 320 | EXPECT_TRUE(ContainsKey(*tables, kTestDeviceIndex1)); |
| 321 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 322 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex1].size()); |
| 323 | |
| 324 | test_entry = (*tables)[kTestDeviceIndex1][0]; |
| 325 | EXPECT_TRUE(entry0.Equals(test_entry)); |
| 326 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 327 | IPAddress gateway_address1(IPAddress::kFamilyIPv4); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 328 | gateway_address1.SetAddressFromString(kTestNetAddress1); |
| 329 | |
| 330 | RoutingTableEntry entry1(default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 331 | default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 332 | gateway_address1, |
| 333 | metric, |
| 334 | RT_SCOPE_UNIVERSE, |
| 335 | true); |
| 336 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 337 | // Add a second gateway route to the second interface. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 338 | SendRouteEntry(RTNLMessage::kModeAdd, |
| 339 | kTestDeviceIndex1, |
| 340 | entry1); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 341 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 342 | // We should have two tables, one of which has a single entry, the other has |
| 343 | // two. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 344 | EXPECT_EQ(2, tables->size()); |
| 345 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 346 | EXPECT_EQ(2, (*tables)[kTestDeviceIndex1].size()); |
| 347 | |
| 348 | test_entry = (*tables)[kTestDeviceIndex1][1]; |
| 349 | EXPECT_TRUE(entry1.Equals(test_entry)); |
| 350 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 351 | // Remove the first gateway route from the second interface. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 352 | SendRouteEntry(RTNLMessage::kModeDelete, |
| 353 | kTestDeviceIndex1, |
| 354 | entry0); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 355 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 356 | // We should be back to having one route per table. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 357 | EXPECT_EQ(2, tables->size()); |
| 358 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 359 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex1].size()); |
| 360 | |
| 361 | test_entry = (*tables)[kTestDeviceIndex1][0]; |
| 362 | EXPECT_TRUE(entry1.Equals(test_entry)); |
| 363 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 364 | // Send a duplicate of the second gateway route message, changing the metric. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 365 | RoutingTableEntry entry2(entry1); |
| 366 | entry2.metric++; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 367 | SendRouteEntry(RTNLMessage::kModeAdd, |
| 368 | kTestDeviceIndex1, |
| 369 | entry2); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 370 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 371 | // Routing table size shouldn't change, but the new metric should match. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 372 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex1].size()); |
| 373 | test_entry = (*tables)[kTestDeviceIndex1][0]; |
| 374 | EXPECT_TRUE(entry2.Equals(test_entry)); |
| 375 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 376 | // Find a matching entry. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 377 | EXPECT_TRUE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 378 | IPAddress::kFamilyIPv4, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 379 | &test_entry)); |
| 380 | EXPECT_TRUE(entry2.Equals(test_entry)); |
| 381 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 382 | // Test that a search for a non-matching family fails. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 383 | EXPECT_FALSE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 384 | IPAddress::kFamilyIPv6, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 385 | &test_entry)); |
| 386 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 387 | // Remove last entry from an existing interface and test that we now fail. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 388 | SendRouteEntry(RTNLMessage::kModeDelete, |
| 389 | kTestDeviceIndex1, |
| 390 | entry2); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 391 | |
| 392 | EXPECT_FALSE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 393 | IPAddress::kFamilyIPv4, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 394 | &test_entry)); |
| 395 | |
Ben Chan | a6bfe87 | 2012-09-26 09:48:34 -0700 | [diff] [blame] | 396 | // Add a route to a gateway address. |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 397 | IPAddress gateway_address(IPAddress::kFamilyIPv4); |
| 398 | EXPECT_TRUE(gateway_address.SetAddressFromString(kTestNetAddress0)); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 399 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 400 | EXPECT_CALL(rtnl_handler_, |
| 401 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 402 | kTestDeviceIndex1, |
| 403 | entry0, |
| 404 | NLM_F_CREATE | NLM_F_EXCL))); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 405 | EXPECT_TRUE(routing_table_->SetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 406 | gateway_address, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 407 | metric, |
| 408 | kTestTableId)); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 409 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 410 | // The table entry should look much like entry0, except with |
| 411 | // from_rtnl = false. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 412 | RoutingTableEntry entry3(entry0); |
| 413 | entry3.from_rtnl = false; |
| 414 | EXPECT_TRUE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 415 | IPAddress::kFamilyIPv4, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 416 | &test_entry)); |
| 417 | EXPECT_TRUE(entry3.Equals(test_entry)); |
| 418 | |
| 419 | // Setting the same route on the interface with a different metric should |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 420 | // push the route with different flags to indicate we are replacing it, |
| 421 | // then it should delete the old entry. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 422 | RoutingTableEntry entry4(entry3); |
| 423 | entry4.metric += 10; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 424 | EXPECT_CALL(rtnl_handler_, |
| 425 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 426 | kTestDeviceIndex1, |
| 427 | entry4, |
| 428 | NLM_F_CREATE | NLM_F_REPLACE))); |
| 429 | EXPECT_CALL(rtnl_handler_, |
| 430 | SendMessage(IsRoutingPacket(RTNLMessage::kModeDelete, |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 431 | kTestDeviceIndex1, |
| 432 | entry3, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 433 | 0))); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 434 | EXPECT_TRUE(routing_table_->SetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 435 | gateway_address, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 436 | entry4.metric, |
| 437 | kTestTableId)); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 438 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 439 | // Test that removing the table causes the route to disappear. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 440 | routing_table_->ResetTable(kTestDeviceIndex1); |
| 441 | EXPECT_FALSE(ContainsKey(*tables, kTestDeviceIndex1)); |
| 442 | EXPECT_FALSE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 443 | IPAddress::kFamilyIPv4, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 444 | &test_entry)); |
| 445 | EXPECT_EQ(1, GetRoutingTables()->size()); |
| 446 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 447 | // When we set the metric on an existing route, a new add and delete |
| 448 | // operation should occur. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 449 | RoutingTableEntry entry5(entry4); |
| 450 | entry5.metric += 10; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 451 | EXPECT_CALL(rtnl_handler_, |
| 452 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 453 | kTestDeviceIndex0, |
| 454 | entry5, |
| 455 | NLM_F_CREATE | NLM_F_REPLACE))); |
| 456 | EXPECT_CALL(rtnl_handler_, |
| 457 | SendMessage(IsRoutingPacket(RTNLMessage::kModeDelete, |
| 458 | kTestDeviceIndex0, |
| 459 | entry0, |
| 460 | 0))); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 461 | routing_table_->SetDefaultMetric(kTestDeviceIndex0, entry5.metric); |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 462 | // Furthermore, the routing table should reflect the change in the metric |
| 463 | // for the default route for the interface. |
| 464 | RoutingTableEntry default_route; |
| 465 | EXPECT_TRUE(routing_table_->GetDefaultRoute(kTestDeviceIndex0, |
| 466 | IPAddress::kFamilyIPv4, |
| 467 | &default_route)); |
| 468 | EXPECT_EQ(entry5.metric, default_route.metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 469 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 470 | // Ask to flush table0. We should see a delete message sent. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 471 | EXPECT_CALL(rtnl_handler_, |
| 472 | SendMessage(IsRoutingPacket(RTNLMessage::kModeDelete, |
| 473 | kTestDeviceIndex0, |
| 474 | entry5, |
| 475 | 0))); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 476 | routing_table_->FlushRoutes(kTestDeviceIndex0); |
| 477 | EXPECT_EQ(0, (*tables)[kTestDeviceIndex0].size()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 478 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 479 | // Test that the routing table size returns to zero. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 480 | SendRouteEntry(RTNLMessage::kModeAdd, |
| 481 | kTestDeviceIndex0, |
| 482 | entry5); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 483 | EXPECT_EQ(1, GetRoutingTables()->size()); |
| 484 | routing_table_->ResetTable(kTestDeviceIndex0); |
| 485 | EXPECT_EQ(0, GetRoutingTables()->size()); |
| 486 | |
| 487 | routing_table_->Stop(); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 488 | } |
| 489 | |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 490 | TEST_F(RoutingTableTest, ConfigureRoutes) { |
| 491 | MockControl control; |
| 492 | IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0)); |
| 493 | IPConfig::Properties properties; |
| 494 | properties.address_family = IPAddress::kFamilyIPv4; |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 495 | vector<IPConfig::Route>& routes = properties.routes; |
Samuel Tan | 3c3c36a | 2014-12-16 16:53:19 -0800 | [diff] [blame] | 496 | ipconfig->UpdateProperties(properties, true); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 497 | |
| 498 | const int kMetric = 10; |
| 499 | EXPECT_TRUE(routing_table_->ConfigureRoutes(kTestDeviceIndex0, |
| 500 | ipconfig, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 501 | kMetric, |
| 502 | kTestTableId)); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 503 | |
| 504 | IPConfig::Route route; |
| 505 | route.host = kTestRemoteNetwork4; |
| 506 | route.netmask = kTestRemoteNetmask4; |
| 507 | route.gateway = kTestGatewayAddress4; |
| 508 | routes.push_back(route); |
Samuel Tan | 3c3c36a | 2014-12-16 16:53:19 -0800 | [diff] [blame] | 509 | ipconfig->UpdateProperties(properties, true); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 510 | |
| 511 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 512 | IPAddress source_address(IPAddress::kFamilyIPv4); |
| 513 | IPAddress gateway_address(IPAddress::kFamilyIPv4); |
| 514 | ASSERT_TRUE(destination_address.SetAddressFromString(kTestRemoteNetwork4)); |
| 515 | destination_address.set_prefix(kTestRemotePrefix4); |
| 516 | ASSERT_TRUE(gateway_address.SetAddressFromString(kTestGatewayAddress4)); |
| 517 | |
| 518 | RoutingTableEntry entry(destination_address, |
| 519 | source_address, |
| 520 | gateway_address, |
| 521 | kMetric, |
| 522 | RT_SCOPE_UNIVERSE, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 523 | false, |
| 524 | kTestTableId, |
| 525 | RoutingTableEntry::kDefaultTag); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 526 | |
| 527 | EXPECT_CALL(rtnl_handler_, |
| 528 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 529 | kTestDeviceIndex0, |
| 530 | entry, |
| 531 | NLM_F_CREATE | NLM_F_EXCL))); |
| 532 | EXPECT_TRUE(routing_table_->ConfigureRoutes(kTestDeviceIndex0, |
| 533 | ipconfig, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 534 | kMetric, |
| 535 | kTestTableId)); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 536 | |
| 537 | routes.clear(); |
| 538 | route.gateway = "xxx"; // Invalid gateway entry -- should be skipped |
| 539 | routes.push_back(route); |
| 540 | route.host = "xxx"; // Invalid host entry -- should be skipped |
| 541 | route.gateway = kTestGatewayAddress4; |
| 542 | routes.push_back(route); |
| 543 | route.host = kTestRemoteNetwork4; |
| 544 | routes.push_back(route); |
Samuel Tan | 3c3c36a | 2014-12-16 16:53:19 -0800 | [diff] [blame] | 545 | ipconfig->UpdateProperties(properties, true); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 546 | |
| 547 | EXPECT_CALL(rtnl_handler_, |
| 548 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 549 | kTestDeviceIndex0, |
| 550 | entry, |
| 551 | NLM_F_CREATE | NLM_F_EXCL))) |
| 552 | .Times(1); |
| 553 | EXPECT_FALSE(routing_table_->ConfigureRoutes(kTestDeviceIndex0, |
| 554 | ipconfig, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 555 | kMetric, |
| 556 | kTestTableId)); |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 559 | MATCHER_P2(IsRoutingQuery, destination, index, "") { |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 560 | const RTNLMessage::RouteStatus& status = arg->route_status(); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 561 | |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 562 | uint32_t oif; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 563 | |
| 564 | return |
| 565 | arg->type() == RTNLMessage::kTypeRoute && |
| 566 | arg->family() == destination.family() && |
| 567 | arg->flags() == NLM_F_REQUEST && |
| 568 | status.table == 0 && |
| 569 | status.protocol == 0 && |
| 570 | status.scope == 0 && |
| 571 | status.type == 0 && |
| 572 | arg->HasAttribute(RTA_DST) && |
| 573 | IPAddress(arg->family(), |
| 574 | arg->GetAttribute(RTA_DST), |
| 575 | status.dst_prefix).Equals(destination) && |
| 576 | !arg->HasAttribute(RTA_SRC) && |
| 577 | !arg->HasAttribute(RTA_GATEWAY) && |
| 578 | arg->GetAttribute(RTA_OIF).ConvertToCPUUInt32(&oif) && |
| 579 | oif == index && |
| 580 | !arg->HasAttribute(RTA_PRIORITY); |
| 581 | |
| 582 | return false; |
| 583 | } |
| 584 | |
| 585 | TEST_F(RoutingTableTest, RequestHostRoute) { |
| 586 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 587 | destination_address.SetAddressFromString(kTestRemoteAddress4); |
| 588 | destination_address.set_prefix(24); |
| 589 | |
| 590 | EXPECT_CALL(rtnl_handler_, |
| 591 | SendMessage(IsRoutingQuery(destination_address, |
| 592 | kTestDeviceIndex0))) |
| 593 | .WillOnce(Invoke(this, &RoutingTableTest::SetSequenceForMessage)); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 594 | EXPECT_TRUE( |
| 595 | routing_table_->RequestRouteToHost(destination_address, |
| 596 | kTestDeviceIndex0, |
| 597 | kTestRouteTag, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 598 | RoutingTable::Query::Callback(), |
| 599 | kTestTableId)); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 600 | |
| 601 | IPAddress gateway_address(IPAddress::kFamilyIPv4); |
| 602 | gateway_address.SetAddressFromString(kTestGatewayAddress4); |
| 603 | |
| 604 | IPAddress local_address(IPAddress::kFamilyIPv4); |
| 605 | local_address.SetAddressFromString(kTestDeviceNetAddress4); |
| 606 | |
| 607 | const int kMetric = 10; |
| 608 | RoutingTableEntry entry(destination_address, |
| 609 | local_address, |
| 610 | gateway_address, |
| 611 | kMetric, |
| 612 | RT_SCOPE_UNIVERSE, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 613 | true, |
| 614 | kTestTableId, |
| 615 | RoutingTableEntry::kDefaultTag); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 616 | |
| 617 | EXPECT_CALL(rtnl_handler_, |
| 618 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 619 | kTestDeviceIndex0, |
| 620 | entry, |
| 621 | NLM_F_CREATE | NLM_F_EXCL))); |
| 622 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 623 | kTestDeviceIndex0, |
| 624 | entry, |
| 625 | kTestRequestSeq, |
| 626 | RTPROT_UNSPEC); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 627 | |
Paul Stewart | 3b30ca5 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 628 | std::unordered_map<int, vector<RoutingTableEntry>>* tables = |
Alex Vakulenko | 8a53229 | 2014-06-16 17:18:44 -0700 | [diff] [blame] | 629 | GetRoutingTables(); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 630 | |
| 631 | // We should have a single table, which should in turn have a single entry. |
| 632 | EXPECT_EQ(1, tables->size()); |
| 633 | EXPECT_TRUE(ContainsKey(*tables, kTestDeviceIndex0)); |
| 634 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 635 | |
| 636 | // This entry's tag should match the tag we requested. |
| 637 | EXPECT_EQ(kTestRouteTag, (*tables)[kTestDeviceIndex0][0].tag); |
| 638 | |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 639 | EXPECT_TRUE(GetQueries()->empty()); |
| 640 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 641 | // Ask to flush routes with our tag. We should see a delete message sent. |
| 642 | EXPECT_CALL(rtnl_handler_, |
| 643 | SendMessage(IsRoutingPacket(RTNLMessage::kModeDelete, |
| 644 | kTestDeviceIndex0, |
| 645 | entry, |
| 646 | 0))); |
| 647 | |
| 648 | routing_table_->FlushRoutesWithTag(kTestRouteTag); |
| 649 | |
| 650 | // After flushing routes for this tag, we should end up with no routes. |
| 651 | EXPECT_EQ(0, (*tables)[kTestDeviceIndex0].size()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 652 | } |
| 653 | |
Paul Stewart | bbed76d | 2012-04-27 20:02:13 -0700 | [diff] [blame] | 654 | TEST_F(RoutingTableTest, RequestHostRouteWithoutGateway) { |
| 655 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 656 | destination_address.SetAddressFromString(kTestRemoteAddress4); |
| 657 | destination_address.set_prefix(24); |
| 658 | |
| 659 | EXPECT_CALL(rtnl_handler_, |
| 660 | SendMessage(IsRoutingQuery(destination_address, |
| 661 | kTestDeviceIndex0))) |
| 662 | .WillOnce(Invoke(this, &RoutingTableTest::SetSequenceForMessage)); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 663 | EXPECT_TRUE( |
| 664 | routing_table_->RequestRouteToHost(destination_address, |
| 665 | kTestDeviceIndex0, |
| 666 | kTestRouteTag, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 667 | RoutingTable::Query::Callback(), |
| 668 | kTestTableId)); |
Paul Stewart | bbed76d | 2012-04-27 20:02:13 -0700 | [diff] [blame] | 669 | |
| 670 | // Don't specify a gateway address. |
| 671 | IPAddress gateway_address(IPAddress::kFamilyIPv4); |
| 672 | |
| 673 | IPAddress local_address(IPAddress::kFamilyIPv4); |
| 674 | local_address.SetAddressFromString(kTestDeviceNetAddress4); |
| 675 | |
| 676 | const int kMetric = 10; |
| 677 | RoutingTableEntry entry(destination_address, |
| 678 | local_address, |
| 679 | gateway_address, |
| 680 | kMetric, |
| 681 | RT_SCOPE_UNIVERSE, |
| 682 | true); |
| 683 | |
| 684 | // Ensure that without a gateway entry, we don't create a route. |
| 685 | EXPECT_CALL(rtnl_handler_, SendMessage(_)).Times(0); |
| 686 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 687 | kTestDeviceIndex0, |
| 688 | entry, |
| 689 | kTestRequestSeq, |
| 690 | RTPROT_UNSPEC); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 691 | EXPECT_TRUE(GetQueries()->empty()); |
Paul Stewart | bbed76d | 2012-04-27 20:02:13 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 694 | TEST_F(RoutingTableTest, RequestHostRouteBadSequence) { |
| 695 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 696 | destination_address.SetAddressFromString(kTestRemoteAddress4); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 697 | QueryCallbackTarget target; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 698 | EXPECT_CALL(target, MockedTarget(_, _)).Times(0); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 699 | EXPECT_CALL(rtnl_handler_, SendMessage(_)) |
| 700 | .WillOnce(Invoke(this, &RoutingTableTest::SetSequenceForMessage)); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 701 | EXPECT_TRUE( |
| 702 | routing_table_->RequestRouteToHost(destination_address, |
| 703 | kTestDeviceIndex0, |
| 704 | kTestRouteTag, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 705 | target.mocked_callback(), |
| 706 | kTestTableId)); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 707 | EXPECT_FALSE(GetQueries()->empty()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 708 | |
| 709 | RoutingTableEntry entry(destination_address, |
| 710 | destination_address, |
| 711 | destination_address, |
| 712 | 0, |
| 713 | RT_SCOPE_UNIVERSE, |
| 714 | true); |
| 715 | |
| 716 | // Try a sequence arriving before the one RoutingTable is looking for. |
| 717 | // This should be a no-op. |
| 718 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 719 | kTestDeviceIndex0, |
| 720 | entry, |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 721 | kTestRequestSeq - 1, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 722 | RTPROT_UNSPEC); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 723 | EXPECT_FALSE(GetQueries()->empty()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 724 | |
| 725 | // Try a sequence arriving after the one RoutingTable is looking for. |
| 726 | // This should cause the request to be purged. |
| 727 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 728 | kTestDeviceIndex0, |
| 729 | entry, |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 730 | kTestRequestSeq + 1, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 731 | RTPROT_UNSPEC); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 732 | EXPECT_TRUE(GetQueries()->empty()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 733 | } |
| 734 | |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 735 | TEST_F(RoutingTableTest, RequestHostRouteWithCallback) { |
| 736 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 737 | |
| 738 | EXPECT_CALL(rtnl_handler_, SendMessage(_)) |
| 739 | .WillOnce(Invoke(this, &RoutingTableTest::SetSequenceForMessage)); |
| 740 | QueryCallbackTarget target; |
| 741 | EXPECT_TRUE( |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 742 | routing_table_->RequestRouteToHost(destination_address, |
| 743 | -1, |
| 744 | kTestRouteTag, |
| 745 | target.mocked_callback(), |
| 746 | kTestTableId)); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 747 | |
| 748 | IPAddress gateway_address(IPAddress::kFamilyIPv4); |
| 749 | gateway_address.SetAddressFromString(kTestGatewayAddress4); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 750 | |
| 751 | const int kMetric = 10; |
| 752 | RoutingTableEntry entry(destination_address, |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 753 | IPAddress(IPAddress::kFamilyIPv4), |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 754 | gateway_address, |
| 755 | kMetric, |
| 756 | RT_SCOPE_UNIVERSE, |
| 757 | true); |
| 758 | |
| 759 | EXPECT_CALL(rtnl_handler_, SendMessage(_)); |
| 760 | EXPECT_CALL(target, |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 761 | MockedTarget(kTestDeviceIndex0, |
| 762 | Field(&RoutingTableEntry::tag, kTestRouteTag))); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 763 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 764 | kTestDeviceIndex0, |
| 765 | entry, |
| 766 | kTestRequestSeq, |
| 767 | RTPROT_UNSPEC); |
| 768 | } |
| 769 | |
| 770 | TEST_F(RoutingTableTest, RequestHostRouteWithoutGatewayWithCallback) { |
| 771 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 772 | |
| 773 | EXPECT_CALL(rtnl_handler_, SendMessage(_)) |
| 774 | .WillOnce(Invoke(this, &RoutingTableTest::SetSequenceForMessage)); |
| 775 | QueryCallbackTarget target; |
| 776 | EXPECT_TRUE( |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 777 | routing_table_->RequestRouteToHost(destination_address, |
| 778 | -1, |
| 779 | kTestRouteTag, |
| 780 | target.mocked_callback(), |
| 781 | kTestTableId)); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 782 | |
| 783 | const int kMetric = 10; |
| 784 | RoutingTableEntry entry(destination_address, |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 785 | IPAddress(IPAddress::kFamilyIPv4), |
| 786 | IPAddress(IPAddress::kFamilyIPv4), |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 787 | kMetric, |
| 788 | RT_SCOPE_UNIVERSE, |
| 789 | true); |
| 790 | |
| 791 | EXPECT_CALL(target, |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 792 | MockedTarget(kTestDeviceIndex0, |
| 793 | Field(&RoutingTableEntry::tag, kTestRouteTag))); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 794 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 795 | kTestDeviceIndex0, |
| 796 | entry, |
| 797 | kTestRequestSeq, |
| 798 | RTPROT_UNSPEC); |
| 799 | } |
| 800 | |
| 801 | TEST_F(RoutingTableTest, CancelQueryCallback) { |
| 802 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 803 | destination_address.SetAddressFromString(kTestRemoteAddress4); |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 804 | std::unique_ptr<QueryCallbackTarget> target(new QueryCallbackTarget()); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 805 | EXPECT_CALL(rtnl_handler_, SendMessage(_)) |
| 806 | .WillOnce(Invoke(this, &RoutingTableTest::SetSequenceForMessage)); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 807 | EXPECT_TRUE( |
| 808 | routing_table_->RequestRouteToHost(destination_address, |
| 809 | kTestDeviceIndex0, |
| 810 | kTestRouteTag, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 811 | target->unreached_callback(), |
| 812 | kTestTableId)); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 813 | ASSERT_EQ(1, GetQueries()->size()); |
| 814 | // Cancels the callback by destroying the owner object. |
| 815 | target.reset(); |
| 816 | const int kMetric = 10; |
| 817 | RoutingTableEntry entry(IPAddress(IPAddress::kFamilyIPv4), |
| 818 | IPAddress(IPAddress::kFamilyIPv4), |
| 819 | IPAddress(IPAddress::kFamilyIPv4), |
| 820 | kMetric, |
| 821 | RT_SCOPE_UNIVERSE, |
| 822 | true); |
| 823 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 824 | kTestDeviceIndex0, |
| 825 | entry, |
| 826 | kTestRequestSeq, |
| 827 | RTPROT_UNSPEC); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 828 | } |
| 829 | |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 830 | TEST_F(RoutingTableTest, CreateBlackholeRoute) { |
Ben Chan | 7fab897 | 2014-08-10 17:14:46 -0700 | [diff] [blame] | 831 | const uint32_t kMetric = 2; |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 832 | EXPECT_CALL(rtnl_handler_, |
| 833 | SendMessage(IsBlackholeRoutingPacket(kTestDeviceIndex0, |
| 834 | IPAddress::kFamilyIPv6, |
| 835 | kMetric))) |
| 836 | .Times(1); |
| 837 | EXPECT_TRUE(routing_table_->CreateBlackholeRoute(kTestDeviceIndex0, |
| 838 | IPAddress::kFamilyIPv6, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 839 | kMetric, |
| 840 | kTestTableId)); |
Ben Chan | a016312 | 2012-09-25 15:10:52 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 843 | TEST_F(RoutingTableTest, CreateLinkRoute) { |
| 844 | IPAddress local_address(IPAddress::kFamilyIPv4); |
| 845 | ASSERT_TRUE(local_address.SetAddressFromString(kTestNetAddress0)); |
| 846 | local_address.set_prefix(kTestRemotePrefix4); |
| 847 | IPAddress remote_address(IPAddress::kFamilyIPv4); |
| 848 | ASSERT_TRUE(remote_address.SetAddressFromString(kTestNetAddress1)); |
| 849 | IPAddress default_address(IPAddress::kFamilyIPv4); |
| 850 | IPAddress remote_address_with_prefix(remote_address); |
| 851 | remote_address_with_prefix.set_prefix( |
| 852 | IPAddress::GetMaxPrefixLength(remote_address_with_prefix.family())); |
| 853 | RoutingTableEntry entry(remote_address_with_prefix, |
| 854 | local_address, |
| 855 | default_address, |
| 856 | 0, |
| 857 | RT_SCOPE_LINK, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 858 | false, |
| 859 | kTestTableId, |
| 860 | RoutingTableEntry::kDefaultTag); |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 861 | EXPECT_CALL(rtnl_handler_, |
| 862 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 863 | kTestDeviceIndex0, |
| 864 | entry, |
| 865 | NLM_F_CREATE | NLM_F_EXCL))) |
| 866 | .Times(1); |
| 867 | EXPECT_TRUE(routing_table_->CreateLinkRoute(kTestDeviceIndex0, |
| 868 | local_address, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 869 | remote_address, |
| 870 | kTestTableId)); |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 871 | |
| 872 | ASSERT_TRUE(remote_address.SetAddressFromString(kTestRemoteAddress4)); |
| 873 | EXPECT_FALSE(routing_table_->CreateLinkRoute(kTestDeviceIndex0, |
| 874 | local_address, |
Prabhu Kaliamoorthi | 762bfb8 | 2015-02-06 13:17:08 +0100 | [diff] [blame] | 875 | remote_address, |
| 876 | kTestTableId)); |
Paul Stewart | 4a6748d | 2012-07-17 14:31:36 -0700 | [diff] [blame] | 877 | } |
| 878 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 879 | } // namespace shill |