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