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