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 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 10 | #include <base/logging.h> |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 11 | #include <base/memory/weak_ptr.h> |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 12 | #include <base/stl_util.h> |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 13 | #include <gtest/gtest.h> |
| 14 | #include <gmock/gmock.h> |
| 15 | |
| 16 | #include "shill/byte_string.h" |
| 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) && |
| 178 | !arg->HasAttribute(RTA_SRC) && |
| 179 | arg->HasAttribute(RTA_GATEWAY) && |
| 180 | IPAddress(arg->family(), |
| 181 | arg->GetAttribute(RTA_GATEWAY)).Equals(entry.gateway) && |
| 182 | arg->GetAttribute(RTA_OIF).ConvertToCPUUInt32(&oif) && |
| 183 | oif == index && |
| 184 | arg->GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&priority) && |
| 185 | priority == entry.metric; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 188 | } // namespace |
| 189 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 190 | void RoutingTableTest::SendRouteEntry(RTNLMessage::Mode mode, |
| 191 | uint32 interface_index, |
| 192 | const RoutingTableEntry &entry) { |
| 193 | SendRouteEntryWithSeqAndProto(mode, interface_index, entry, 0, RTPROT_BOOT); |
| 194 | } |
| 195 | |
| 196 | void RoutingTableTest::SendRouteEntryWithSeqAndProto( |
| 197 | RTNLMessage::Mode mode, |
| 198 | uint32 interface_index, |
| 199 | const RoutingTableEntry &entry, |
| 200 | uint32 seq, |
| 201 | unsigned char proto) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 202 | RTNLMessage msg( |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 203 | RTNLMessage::kTypeRoute, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 204 | mode, |
| 205 | 0, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 206 | seq, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 207 | 0, |
| 208 | 0, |
| 209 | entry.dst.family()); |
| 210 | |
| 211 | msg.set_route_status(RTNLMessage::RouteStatus( |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 212 | entry.dst.prefix(), |
| 213 | entry.src.prefix(), |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 214 | RT_TABLE_MAIN, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 215 | proto, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 216 | entry.scope, |
| 217 | RTN_UNICAST, |
| 218 | 0)); |
| 219 | |
| 220 | msg.SetAttribute(RTA_DST, entry.dst.address()); |
| 221 | if (!entry.src.IsDefault()) { |
| 222 | msg.SetAttribute(RTA_SRC, entry.src.address()); |
| 223 | } |
| 224 | if (!entry.gateway.IsDefault()) { |
| 225 | msg.SetAttribute(RTA_GATEWAY, entry.gateway.address()); |
| 226 | } |
| 227 | msg.SetAttribute(RTA_PRIORITY, ByteString::CreateFromCPUUInt32(entry.metric)); |
| 228 | msg.SetAttribute(RTA_OIF, ByteString::CreateFromCPUUInt32(interface_index)); |
| 229 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 230 | routing_table_->RouteMsgHandler(msg); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 233 | void RoutingTableTest::SendRouteMessage(const RTNLMessage &msg) { |
| 234 | routing_table_->RouteMsgHandler(msg); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 237 | TEST_F(RoutingTableTest, Start) { |
| 238 | EXPECT_CALL(rtnl_handler_, RequestDump(RTNLHandler::kRequestRoute)); |
| 239 | routing_table_->Start(); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | TEST_F(RoutingTableTest, RouteAddDelete) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 243 | // Expect the tables to be empty by default. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 244 | EXPECT_EQ(0, GetRoutingTables()->size()); |
| 245 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 246 | IPAddress default_address(IPAddress::kFamilyIPv4); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 247 | default_address.SetAddressToDefault(); |
| 248 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 249 | IPAddress gateway_address0(IPAddress::kFamilyIPv4); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 250 | gateway_address0.SetAddressFromString(kTestNetAddress0); |
| 251 | |
| 252 | int metric = 10; |
| 253 | |
| 254 | RoutingTableEntry entry0(default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 255 | default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 256 | gateway_address0, |
| 257 | metric, |
| 258 | RT_SCOPE_UNIVERSE, |
| 259 | true); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 260 | // Add a single entry. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 261 | SendRouteEntry(RTNLMessage::kModeAdd, |
| 262 | kTestDeviceIndex0, |
| 263 | entry0); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 264 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 265 | hash_map<int, vector<RoutingTableEntry> > *tables = |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 266 | GetRoutingTables(); |
| 267 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 268 | // 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] | 269 | EXPECT_EQ(1, tables->size()); |
| 270 | EXPECT_TRUE(ContainsKey(*tables, kTestDeviceIndex0)); |
| 271 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 272 | |
| 273 | RoutingTableEntry test_entry = (*tables)[kTestDeviceIndex0][0]; |
| 274 | EXPECT_TRUE(entry0.Equals(test_entry)); |
| 275 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 276 | // Add a second entry for a different interface. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 277 | SendRouteEntry(RTNLMessage::kModeAdd, |
| 278 | kTestDeviceIndex1, |
| 279 | entry0); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 280 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 281 | // We should have two tables, which should have a single entry each. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 282 | EXPECT_EQ(2, tables->size()); |
| 283 | EXPECT_TRUE(ContainsKey(*tables, kTestDeviceIndex1)); |
| 284 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 285 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex1].size()); |
| 286 | |
| 287 | test_entry = (*tables)[kTestDeviceIndex1][0]; |
| 288 | EXPECT_TRUE(entry0.Equals(test_entry)); |
| 289 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 290 | IPAddress gateway_address1(IPAddress::kFamilyIPv4); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 291 | gateway_address1.SetAddressFromString(kTestNetAddress1); |
| 292 | |
| 293 | RoutingTableEntry entry1(default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 294 | default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 295 | gateway_address1, |
| 296 | metric, |
| 297 | RT_SCOPE_UNIVERSE, |
| 298 | true); |
| 299 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 300 | // Add a second gateway route to the second interface. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 301 | SendRouteEntry(RTNLMessage::kModeAdd, |
| 302 | kTestDeviceIndex1, |
| 303 | entry1); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 304 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 305 | // We should have two tables, one of which has a single entry, the other has |
| 306 | // two. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 307 | EXPECT_EQ(2, tables->size()); |
| 308 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 309 | EXPECT_EQ(2, (*tables)[kTestDeviceIndex1].size()); |
| 310 | |
| 311 | test_entry = (*tables)[kTestDeviceIndex1][1]; |
| 312 | EXPECT_TRUE(entry1.Equals(test_entry)); |
| 313 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 314 | // Remove the first gateway route from the second interface. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 315 | SendRouteEntry(RTNLMessage::kModeDelete, |
| 316 | kTestDeviceIndex1, |
| 317 | entry0); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 318 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 319 | // We should be back to having one route per table. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 320 | EXPECT_EQ(2, tables->size()); |
| 321 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 322 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex1].size()); |
| 323 | |
| 324 | test_entry = (*tables)[kTestDeviceIndex1][0]; |
| 325 | EXPECT_TRUE(entry1.Equals(test_entry)); |
| 326 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 327 | // Send a duplicate of the second gateway route message, changing the metric. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 328 | RoutingTableEntry entry2(entry1); |
| 329 | entry2.metric++; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 330 | SendRouteEntry(RTNLMessage::kModeAdd, |
| 331 | kTestDeviceIndex1, |
| 332 | entry2); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 333 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 334 | // Routing table size shouldn't change, but the new metric should match. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 335 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex1].size()); |
| 336 | test_entry = (*tables)[kTestDeviceIndex1][0]; |
| 337 | EXPECT_TRUE(entry2.Equals(test_entry)); |
| 338 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 339 | // Find a matching entry. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 340 | EXPECT_TRUE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 341 | IPAddress::kFamilyIPv4, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 342 | &test_entry)); |
| 343 | EXPECT_TRUE(entry2.Equals(test_entry)); |
| 344 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 345 | // Test that a search for a non-matching family fails. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 346 | EXPECT_FALSE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 347 | IPAddress::kFamilyIPv6, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 348 | &test_entry)); |
| 349 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 350 | // 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] | 351 | SendRouteEntry(RTNLMessage::kModeDelete, |
| 352 | kTestDeviceIndex1, |
| 353 | entry2); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 354 | |
| 355 | EXPECT_FALSE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 356 | IPAddress::kFamilyIPv4, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 357 | &test_entry)); |
| 358 | |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 359 | // Add a route to a gatway address. |
| 360 | IPAddress gateway_address(IPAddress::kFamilyIPv4); |
| 361 | EXPECT_TRUE(gateway_address.SetAddressFromString(kTestNetAddress0)); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 362 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 363 | EXPECT_CALL(rtnl_handler_, |
| 364 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 365 | kTestDeviceIndex1, |
| 366 | entry0, |
| 367 | NLM_F_CREATE | NLM_F_EXCL))); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 368 | EXPECT_TRUE(routing_table_->SetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 369 | gateway_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 370 | metric)); |
| 371 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 372 | // The table entry should look much like entry0, except with |
| 373 | // from_rtnl = false. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 374 | RoutingTableEntry entry3(entry0); |
| 375 | entry3.from_rtnl = false; |
| 376 | EXPECT_TRUE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 377 | IPAddress::kFamilyIPv4, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 378 | &test_entry)); |
| 379 | EXPECT_TRUE(entry3.Equals(test_entry)); |
| 380 | |
| 381 | // Setting the same route on the interface with a different metric should |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 382 | // push the route with different flags to indicate we are replacing it, |
| 383 | // then it should delete the old entry. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 384 | RoutingTableEntry entry4(entry3); |
| 385 | entry4.metric += 10; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 386 | EXPECT_CALL(rtnl_handler_, |
| 387 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 388 | kTestDeviceIndex1, |
| 389 | entry4, |
| 390 | NLM_F_CREATE | NLM_F_REPLACE))); |
| 391 | EXPECT_CALL(rtnl_handler_, |
| 392 | SendMessage(IsRoutingPacket(RTNLMessage::kModeDelete, |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 393 | kTestDeviceIndex1, |
| 394 | entry3, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 395 | 0))); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 396 | EXPECT_TRUE(routing_table_->SetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 397 | gateway_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 398 | entry4.metric)); |
| 399 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 400 | // Test that removing the table causes the route to disappear. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 401 | routing_table_->ResetTable(kTestDeviceIndex1); |
| 402 | EXPECT_FALSE(ContainsKey(*tables, kTestDeviceIndex1)); |
| 403 | EXPECT_FALSE(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_EQ(1, GetRoutingTables()->size()); |
| 407 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 408 | // When we set the metric on an existing route, a new add and delete |
| 409 | // operation should occur. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 410 | RoutingTableEntry entry5(entry4); |
| 411 | entry5.metric += 10; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 412 | EXPECT_CALL(rtnl_handler_, |
| 413 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 414 | kTestDeviceIndex0, |
| 415 | entry5, |
| 416 | NLM_F_CREATE | NLM_F_REPLACE))); |
| 417 | EXPECT_CALL(rtnl_handler_, |
| 418 | SendMessage(IsRoutingPacket(RTNLMessage::kModeDelete, |
| 419 | kTestDeviceIndex0, |
| 420 | entry0, |
| 421 | 0))); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 422 | routing_table_->SetDefaultMetric(kTestDeviceIndex0, entry5.metric); |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 423 | // Furthermore, the routing table should reflect the change in the metric |
| 424 | // for the default route for the interface. |
| 425 | RoutingTableEntry default_route; |
| 426 | EXPECT_TRUE(routing_table_->GetDefaultRoute(kTestDeviceIndex0, |
| 427 | IPAddress::kFamilyIPv4, |
| 428 | &default_route)); |
| 429 | EXPECT_EQ(entry5.metric, default_route.metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 430 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 431 | // Ask to flush table0. We should see a delete message sent. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 432 | EXPECT_CALL(rtnl_handler_, |
| 433 | SendMessage(IsRoutingPacket(RTNLMessage::kModeDelete, |
| 434 | kTestDeviceIndex0, |
| 435 | entry5, |
| 436 | 0))); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 437 | routing_table_->FlushRoutes(kTestDeviceIndex0); |
| 438 | EXPECT_EQ(0, (*tables)[kTestDeviceIndex0].size()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 439 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 440 | // Test that the routing table size returns to zero. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 441 | SendRouteEntry(RTNLMessage::kModeAdd, |
| 442 | kTestDeviceIndex0, |
| 443 | entry5); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 444 | EXPECT_EQ(1, GetRoutingTables()->size()); |
| 445 | routing_table_->ResetTable(kTestDeviceIndex0); |
| 446 | EXPECT_EQ(0, GetRoutingTables()->size()); |
| 447 | |
| 448 | routing_table_->Stop(); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 451 | TEST_F(RoutingTableTest, ConfigureRoutes) { |
| 452 | MockControl control; |
| 453 | IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0)); |
| 454 | IPConfig::Properties properties; |
| 455 | properties.address_family = IPAddress::kFamilyIPv4; |
| 456 | vector<IPConfig::Route> &routes = properties.routes; |
| 457 | ipconfig->UpdateProperties(properties, true); |
| 458 | |
| 459 | const int kMetric = 10; |
| 460 | EXPECT_TRUE(routing_table_->ConfigureRoutes(kTestDeviceIndex0, |
| 461 | ipconfig, |
| 462 | kMetric)); |
| 463 | |
| 464 | IPConfig::Route route; |
| 465 | route.host = kTestRemoteNetwork4; |
| 466 | route.netmask = kTestRemoteNetmask4; |
| 467 | route.gateway = kTestGatewayAddress4; |
| 468 | routes.push_back(route); |
| 469 | ipconfig->UpdateProperties(properties, true); |
| 470 | |
| 471 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 472 | IPAddress source_address(IPAddress::kFamilyIPv4); |
| 473 | IPAddress gateway_address(IPAddress::kFamilyIPv4); |
| 474 | ASSERT_TRUE(destination_address.SetAddressFromString(kTestRemoteNetwork4)); |
| 475 | destination_address.set_prefix(kTestRemotePrefix4); |
| 476 | ASSERT_TRUE(gateway_address.SetAddressFromString(kTestGatewayAddress4)); |
| 477 | |
| 478 | RoutingTableEntry entry(destination_address, |
| 479 | source_address, |
| 480 | gateway_address, |
| 481 | kMetric, |
| 482 | RT_SCOPE_UNIVERSE, |
| 483 | false); |
| 484 | |
| 485 | EXPECT_CALL(rtnl_handler_, |
| 486 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 487 | kTestDeviceIndex0, |
| 488 | entry, |
| 489 | NLM_F_CREATE | NLM_F_EXCL))); |
| 490 | EXPECT_TRUE(routing_table_->ConfigureRoutes(kTestDeviceIndex0, |
| 491 | ipconfig, |
| 492 | kMetric)); |
| 493 | |
| 494 | routes.clear(); |
| 495 | route.gateway = "xxx"; // Invalid gateway entry -- should be skipped |
| 496 | routes.push_back(route); |
| 497 | route.host = "xxx"; // Invalid host entry -- should be skipped |
| 498 | route.gateway = kTestGatewayAddress4; |
| 499 | routes.push_back(route); |
| 500 | route.host = kTestRemoteNetwork4; |
| 501 | routes.push_back(route); |
| 502 | ipconfig->UpdateProperties(properties, true); |
| 503 | |
| 504 | EXPECT_CALL(rtnl_handler_, |
| 505 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 506 | kTestDeviceIndex0, |
| 507 | entry, |
| 508 | NLM_F_CREATE | NLM_F_EXCL))) |
| 509 | .Times(1); |
| 510 | EXPECT_FALSE(routing_table_->ConfigureRoutes(kTestDeviceIndex0, |
| 511 | ipconfig, |
| 512 | kMetric)); |
| 513 | } |
| 514 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 515 | MATCHER_P2(IsRoutingQuery, destination, index, "") { |
| 516 | const RTNLMessage::RouteStatus &status = arg->route_status(); |
| 517 | |
| 518 | uint32 oif; |
| 519 | |
| 520 | return |
| 521 | arg->type() == RTNLMessage::kTypeRoute && |
| 522 | arg->family() == destination.family() && |
| 523 | arg->flags() == NLM_F_REQUEST && |
| 524 | status.table == 0 && |
| 525 | status.protocol == 0 && |
| 526 | status.scope == 0 && |
| 527 | status.type == 0 && |
| 528 | arg->HasAttribute(RTA_DST) && |
| 529 | IPAddress(arg->family(), |
| 530 | arg->GetAttribute(RTA_DST), |
| 531 | status.dst_prefix).Equals(destination) && |
| 532 | !arg->HasAttribute(RTA_SRC) && |
| 533 | !arg->HasAttribute(RTA_GATEWAY) && |
| 534 | arg->GetAttribute(RTA_OIF).ConvertToCPUUInt32(&oif) && |
| 535 | oif == index && |
| 536 | !arg->HasAttribute(RTA_PRIORITY); |
| 537 | |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | TEST_F(RoutingTableTest, RequestHostRoute) { |
| 542 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 543 | destination_address.SetAddressFromString(kTestRemoteAddress4); |
| 544 | destination_address.set_prefix(24); |
| 545 | |
| 546 | EXPECT_CALL(rtnl_handler_, |
| 547 | SendMessage(IsRoutingQuery(destination_address, |
| 548 | kTestDeviceIndex0))) |
| 549 | .WillOnce(Invoke(this, &RoutingTableTest::SetSequenceForMessage)); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 550 | EXPECT_TRUE( |
| 551 | routing_table_->RequestRouteToHost(destination_address, |
| 552 | kTestDeviceIndex0, |
| 553 | kTestRouteTag, |
| 554 | RoutingTable::Query::Callback())); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 555 | |
| 556 | IPAddress gateway_address(IPAddress::kFamilyIPv4); |
| 557 | gateway_address.SetAddressFromString(kTestGatewayAddress4); |
| 558 | |
| 559 | IPAddress local_address(IPAddress::kFamilyIPv4); |
| 560 | local_address.SetAddressFromString(kTestDeviceNetAddress4); |
| 561 | |
| 562 | const int kMetric = 10; |
| 563 | RoutingTableEntry entry(destination_address, |
| 564 | local_address, |
| 565 | gateway_address, |
| 566 | kMetric, |
| 567 | RT_SCOPE_UNIVERSE, |
| 568 | true); |
| 569 | |
| 570 | EXPECT_CALL(rtnl_handler_, |
| 571 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 572 | kTestDeviceIndex0, |
| 573 | entry, |
| 574 | NLM_F_CREATE | NLM_F_EXCL))); |
| 575 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 576 | kTestDeviceIndex0, |
| 577 | entry, |
| 578 | kTestRequestSeq, |
| 579 | RTPROT_UNSPEC); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 580 | |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 581 | hash_map<int, vector<RoutingTableEntry> > *tables = GetRoutingTables(); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 582 | |
| 583 | // We should have a single table, which should in turn have a single entry. |
| 584 | EXPECT_EQ(1, tables->size()); |
| 585 | EXPECT_TRUE(ContainsKey(*tables, kTestDeviceIndex0)); |
| 586 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 587 | |
| 588 | // This entry's tag should match the tag we requested. |
| 589 | EXPECT_EQ(kTestRouteTag, (*tables)[kTestDeviceIndex0][0].tag); |
| 590 | |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 591 | EXPECT_TRUE(GetQueries()->empty()); |
| 592 | |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 593 | // Ask to flush routes with our tag. We should see a delete message sent. |
| 594 | EXPECT_CALL(rtnl_handler_, |
| 595 | SendMessage(IsRoutingPacket(RTNLMessage::kModeDelete, |
| 596 | kTestDeviceIndex0, |
| 597 | entry, |
| 598 | 0))); |
| 599 | |
| 600 | routing_table_->FlushRoutesWithTag(kTestRouteTag); |
| 601 | |
| 602 | // After flushing routes for this tag, we should end up with no routes. |
| 603 | EXPECT_EQ(0, (*tables)[kTestDeviceIndex0].size()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Paul Stewart | bbed76d | 2012-04-27 20:02:13 -0700 | [diff] [blame] | 606 | TEST_F(RoutingTableTest, RequestHostRouteWithoutGateway) { |
| 607 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 608 | destination_address.SetAddressFromString(kTestRemoteAddress4); |
| 609 | destination_address.set_prefix(24); |
| 610 | |
| 611 | EXPECT_CALL(rtnl_handler_, |
| 612 | SendMessage(IsRoutingQuery(destination_address, |
| 613 | kTestDeviceIndex0))) |
| 614 | .WillOnce(Invoke(this, &RoutingTableTest::SetSequenceForMessage)); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 615 | EXPECT_TRUE( |
| 616 | routing_table_->RequestRouteToHost(destination_address, |
| 617 | kTestDeviceIndex0, |
| 618 | kTestRouteTag, |
| 619 | RoutingTable::Query::Callback())); |
Paul Stewart | bbed76d | 2012-04-27 20:02:13 -0700 | [diff] [blame] | 620 | |
| 621 | // Don't specify a gateway address. |
| 622 | IPAddress gateway_address(IPAddress::kFamilyIPv4); |
| 623 | |
| 624 | IPAddress local_address(IPAddress::kFamilyIPv4); |
| 625 | local_address.SetAddressFromString(kTestDeviceNetAddress4); |
| 626 | |
| 627 | const int kMetric = 10; |
| 628 | RoutingTableEntry entry(destination_address, |
| 629 | local_address, |
| 630 | gateway_address, |
| 631 | kMetric, |
| 632 | RT_SCOPE_UNIVERSE, |
| 633 | true); |
| 634 | |
| 635 | // Ensure that without a gateway entry, we don't create a route. |
| 636 | EXPECT_CALL(rtnl_handler_, SendMessage(_)).Times(0); |
| 637 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 638 | kTestDeviceIndex0, |
| 639 | entry, |
| 640 | kTestRequestSeq, |
| 641 | RTPROT_UNSPEC); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 642 | EXPECT_TRUE(GetQueries()->empty()); |
Paul Stewart | bbed76d | 2012-04-27 20:02:13 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 645 | TEST_F(RoutingTableTest, RequestHostRouteBadSequence) { |
| 646 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 647 | destination_address.SetAddressFromString(kTestRemoteAddress4); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 648 | QueryCallbackTarget target; |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 649 | EXPECT_CALL(target, MockedTarget(_, _)).Times(0); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 650 | EXPECT_CALL(rtnl_handler_, SendMessage(_)) |
| 651 | .WillOnce(Invoke(this, &RoutingTableTest::SetSequenceForMessage)); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 652 | EXPECT_TRUE( |
| 653 | routing_table_->RequestRouteToHost(destination_address, |
| 654 | kTestDeviceIndex0, |
| 655 | kTestRouteTag, |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 656 | target.mocked_callback())); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 657 | EXPECT_FALSE(GetQueries()->empty()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 658 | |
| 659 | RoutingTableEntry entry(destination_address, |
| 660 | destination_address, |
| 661 | destination_address, |
| 662 | 0, |
| 663 | RT_SCOPE_UNIVERSE, |
| 664 | true); |
| 665 | |
| 666 | // Try a sequence arriving before the one RoutingTable is looking for. |
| 667 | // This should be a no-op. |
| 668 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 669 | kTestDeviceIndex0, |
| 670 | entry, |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 671 | kTestRequestSeq - 1, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 672 | RTPROT_UNSPEC); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 673 | EXPECT_FALSE(GetQueries()->empty()); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 674 | |
| 675 | // Try a sequence arriving after the one RoutingTable is looking for. |
| 676 | // This should cause the request to be purged. |
| 677 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 678 | kTestDeviceIndex0, |
| 679 | entry, |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 680 | kTestRequestSeq + 1, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 681 | RTPROT_UNSPEC); |
Paul Stewart | e93b038 | 2012-04-24 13:11:28 -0700 | [diff] [blame] | 682 | EXPECT_TRUE(GetQueries()->empty()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 685 | TEST_F(RoutingTableTest, RequestHostRouteWithCallback) { |
| 686 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 687 | |
| 688 | EXPECT_CALL(rtnl_handler_, SendMessage(_)) |
| 689 | .WillOnce(Invoke(this, &RoutingTableTest::SetSequenceForMessage)); |
| 690 | QueryCallbackTarget target; |
| 691 | EXPECT_TRUE( |
| 692 | routing_table_->RequestRouteToHost( |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 693 | destination_address, -1, kTestRouteTag, target.mocked_callback())); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 694 | |
| 695 | IPAddress gateway_address(IPAddress::kFamilyIPv4); |
| 696 | gateway_address.SetAddressFromString(kTestGatewayAddress4); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 697 | |
| 698 | const int kMetric = 10; |
| 699 | RoutingTableEntry entry(destination_address, |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 700 | IPAddress(IPAddress::kFamilyIPv4), |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 701 | gateway_address, |
| 702 | kMetric, |
| 703 | RT_SCOPE_UNIVERSE, |
| 704 | true); |
| 705 | |
| 706 | EXPECT_CALL(rtnl_handler_, SendMessage(_)); |
| 707 | EXPECT_CALL(target, |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 708 | MockedTarget(kTestDeviceIndex0, |
| 709 | Field(&RoutingTableEntry::tag, kTestRouteTag))); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 710 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 711 | kTestDeviceIndex0, |
| 712 | entry, |
| 713 | kTestRequestSeq, |
| 714 | RTPROT_UNSPEC); |
| 715 | } |
| 716 | |
| 717 | TEST_F(RoutingTableTest, RequestHostRouteWithoutGatewayWithCallback) { |
| 718 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 719 | |
| 720 | EXPECT_CALL(rtnl_handler_, SendMessage(_)) |
| 721 | .WillOnce(Invoke(this, &RoutingTableTest::SetSequenceForMessage)); |
| 722 | QueryCallbackTarget target; |
| 723 | EXPECT_TRUE( |
| 724 | routing_table_->RequestRouteToHost( |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 725 | destination_address, -1, kTestRouteTag, target.mocked_callback())); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 726 | |
| 727 | const int kMetric = 10; |
| 728 | RoutingTableEntry entry(destination_address, |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 729 | IPAddress(IPAddress::kFamilyIPv4), |
| 730 | IPAddress(IPAddress::kFamilyIPv4), |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 731 | kMetric, |
| 732 | RT_SCOPE_UNIVERSE, |
| 733 | true); |
| 734 | |
| 735 | EXPECT_CALL(target, |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 736 | MockedTarget(kTestDeviceIndex0, |
| 737 | Field(&RoutingTableEntry::tag, kTestRouteTag))); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 738 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 739 | kTestDeviceIndex0, |
| 740 | entry, |
| 741 | kTestRequestSeq, |
| 742 | RTPROT_UNSPEC); |
| 743 | } |
| 744 | |
| 745 | TEST_F(RoutingTableTest, CancelQueryCallback) { |
| 746 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 747 | destination_address.SetAddressFromString(kTestRemoteAddress4); |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 748 | scoped_ptr<QueryCallbackTarget> target(new QueryCallbackTarget()); |
| 749 | EXPECT_CALL(rtnl_handler_, SendMessage(_)) |
| 750 | .WillOnce(Invoke(this, &RoutingTableTest::SetSequenceForMessage)); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 751 | EXPECT_TRUE( |
| 752 | routing_table_->RequestRouteToHost(destination_address, |
| 753 | kTestDeviceIndex0, |
| 754 | kTestRouteTag, |
Darin Petkov | 13e6d55 | 2012-05-09 14:22:23 +0200 | [diff] [blame] | 755 | target->unreached_callback())); |
| 756 | ASSERT_EQ(1, GetQueries()->size()); |
| 757 | // Cancels the callback by destroying the owner object. |
| 758 | target.reset(); |
| 759 | const int kMetric = 10; |
| 760 | RoutingTableEntry entry(IPAddress(IPAddress::kFamilyIPv4), |
| 761 | IPAddress(IPAddress::kFamilyIPv4), |
| 762 | IPAddress(IPAddress::kFamilyIPv4), |
| 763 | kMetric, |
| 764 | RT_SCOPE_UNIVERSE, |
| 765 | true); |
| 766 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 767 | kTestDeviceIndex0, |
| 768 | entry, |
| 769 | kTestRequestSeq, |
| 770 | RTPROT_UNSPEC); |
Darin Petkov | abf6d28 | 2012-05-08 15:49:05 +0200 | [diff] [blame] | 771 | } |
| 772 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 773 | } // namespace shill |