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