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 | |
| 8 | #include <base/logging.h> |
| 9 | #include <base/stl_util-inl.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | #include <gmock/gmock.h> |
| 12 | |
| 13 | #include "shill/byte_string.h" |
| 14 | #include "shill/mock_control.h" |
| 15 | #include "shill/mock_sockets.h" |
| 16 | #include "shill/routing_table.h" |
| 17 | #include "shill/routing_table_entry.h" |
| 18 | #include "shill/rtnl_handler.h" |
| 19 | #include "shill/rtnl_message.h" |
| 20 | |
| 21 | using testing::_; |
| 22 | using testing::Return; |
| 23 | using testing::Test; |
| 24 | |
| 25 | namespace shill { |
| 26 | |
| 27 | class TestEventDispatcher : public EventDispatcher { |
| 28 | public: |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 29 | virtual IOHandler *CreateInputHandler( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 30 | int /*fd*/, |
| 31 | Callback1<InputData*>::Type */*callback*/) { |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 32 | return NULL; |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | class RoutingTableTest : public Test { |
| 37 | public: |
| 38 | RoutingTableTest() : routing_table_(RoutingTable::GetInstance()) {} |
| 39 | |
| 40 | base::hash_map<int, std::vector<RoutingTableEntry> > *GetRoutingTables() { |
| 41 | return &routing_table_->tables_; |
| 42 | } |
| 43 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 44 | void SendRouteMsg(RTNLMessage::Mode mode, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 45 | uint32 interface_index, |
| 46 | const RoutingTableEntry &entry); |
| 47 | |
Paul Stewart | 65c40f5 | 2011-08-08 07:27:46 -0700 | [diff] [blame] | 48 | virtual void TearDown() { |
| 49 | RTNLHandler::GetInstance()->Stop(); |
| 50 | } |
| 51 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 52 | protected: |
| 53 | static const int kTestSocket; |
| 54 | static const uint32 kTestDeviceIndex0; |
| 55 | static const uint32 kTestDeviceIndex1; |
| 56 | static const char kTestDeviceName0[]; |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame^] | 57 | static const char kTestDeviceNetAddress4[]; |
| 58 | static const char kTestForeignNetAddress4[]; |
| 59 | static const char kTestForeignNetGateway4[]; |
| 60 | static const char kTestForeignNetAddress6[]; |
| 61 | static const char kTestForeignNetGateway6[]; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 62 | static const char kTestNetAddress0[]; |
| 63 | static const char kTestNetAddress1[]; |
| 64 | |
| 65 | void StartRTNLHandler(); |
| 66 | void StopRTNLHandler(); |
| 67 | |
| 68 | MockSockets sockets_; |
| 69 | RoutingTable *routing_table_; |
| 70 | TestEventDispatcher dispatcher_; |
| 71 | }; |
| 72 | |
| 73 | const int RoutingTableTest::kTestSocket = 123; |
| 74 | const uint32 RoutingTableTest::kTestDeviceIndex0 = 12345; |
| 75 | const uint32 RoutingTableTest::kTestDeviceIndex1 = 67890; |
| 76 | const char RoutingTableTest::kTestDeviceName0[] = "test-device0"; |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame^] | 77 | const char RoutingTableTest::kTestDeviceNetAddress4[] = "192.168.2.0/24"; |
| 78 | const char RoutingTableTest::kTestForeignNetAddress4[] = "192.168.2.2"; |
| 79 | const char RoutingTableTest::kTestForeignNetGateway4[] = "192.168.2.1"; |
| 80 | const char RoutingTableTest::kTestForeignNetAddress6[] = "2000::/3"; |
| 81 | const char RoutingTableTest::kTestForeignNetGateway6[] = "fe80:::::1"; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 82 | const char RoutingTableTest::kTestNetAddress0[] = "192.168.1.1"; |
| 83 | const char RoutingTableTest::kTestNetAddress1[] = "192.168.1.2"; |
| 84 | |
| 85 | |
| 86 | MATCHER_P4(IsRoutingPacket, mode, index, entry, flags, "") { |
| 87 | // NB: Matchers don't get passed multiple arguments, so we can |
| 88 | // get the address of a Send(), its length, but not both. |
| 89 | // We have to punt and assume the length is correct -- which |
| 90 | // should already be tested in rtnl_message_unittest. |
| 91 | struct nlmsghdr hdr; |
| 92 | memcpy(&hdr, arg, sizeof(hdr)); |
| 93 | |
| 94 | RTNLMessage msg; |
| 95 | if (!msg.Decode(ByteString(reinterpret_cast<const unsigned char *>(arg), |
| 96 | hdr.nlmsg_len))) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | const RTNLMessage::RouteStatus &status = msg.route_status(); |
| 101 | |
| 102 | uint32 oif; |
| 103 | uint32 priority; |
| 104 | |
| 105 | return |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 106 | msg.type() == RTNLMessage::kTypeRoute && |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 107 | msg.family() == entry.gateway.family() && |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 108 | msg.flags() == (NLM_F_REQUEST | flags) && |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 109 | status.table == RT_TABLE_MAIN && |
| 110 | status.protocol == RTPROT_BOOT && |
| 111 | status.scope == entry.scope && |
| 112 | status.type == RTN_UNICAST && |
| 113 | msg.HasAttribute(RTA_DST) && |
| 114 | IPAddress(msg.family(), |
| 115 | msg.GetAttribute(RTA_DST)).Equals(entry.dst) && |
| 116 | !msg.HasAttribute(RTA_SRC) && |
| 117 | msg.HasAttribute(RTA_GATEWAY) && |
| 118 | IPAddress(msg.family(), |
| 119 | msg.GetAttribute(RTA_GATEWAY)).Equals(entry.gateway) && |
| 120 | msg.GetAttribute(RTA_OIF).ConvertToCPUUInt32(&oif) && |
| 121 | oif == index && |
| 122 | msg.GetAttribute(RTA_PRIORITY).ConvertToCPUUInt32(&priority) && |
| 123 | priority == entry.metric; |
| 124 | } |
| 125 | |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 126 | void RoutingTableTest::SendRouteMsg(RTNLMessage::Mode mode, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 127 | uint32 interface_index, |
| 128 | const RoutingTableEntry &entry) { |
| 129 | RTNLMessage msg( |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 130 | RTNLMessage::kTypeRoute, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 131 | mode, |
| 132 | 0, |
| 133 | 0, |
| 134 | 0, |
| 135 | 0, |
| 136 | entry.dst.family()); |
| 137 | |
| 138 | msg.set_route_status(RTNLMessage::RouteStatus( |
Paul Stewart | 9e3fcd7 | 2011-08-26 15:46:16 -0700 | [diff] [blame] | 139 | entry.dst.prefix(), |
| 140 | entry.src.prefix(), |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 141 | RT_TABLE_MAIN, |
| 142 | RTPROT_BOOT, |
| 143 | entry.scope, |
| 144 | RTN_UNICAST, |
| 145 | 0)); |
| 146 | |
| 147 | msg.SetAttribute(RTA_DST, entry.dst.address()); |
| 148 | if (!entry.src.IsDefault()) { |
| 149 | msg.SetAttribute(RTA_SRC, entry.src.address()); |
| 150 | } |
| 151 | if (!entry.gateway.IsDefault()) { |
| 152 | msg.SetAttribute(RTA_GATEWAY, entry.gateway.address()); |
| 153 | } |
| 154 | msg.SetAttribute(RTA_PRIORITY, ByteString::CreateFromCPUUInt32(entry.metric)); |
| 155 | msg.SetAttribute(RTA_OIF, ByteString::CreateFromCPUUInt32(interface_index)); |
| 156 | |
| 157 | ByteString msgdata = msg.Encode(); |
| 158 | EXPECT_NE(0, msgdata.GetLength()); |
| 159 | |
| 160 | InputData data(msgdata.GetData(), msgdata.GetLength()); |
| 161 | RTNLHandler::GetInstance()->ParseRTNL(&data); |
| 162 | } |
| 163 | |
| 164 | void RoutingTableTest::StartRTNLHandler() { |
| 165 | EXPECT_CALL(sockets_, Socket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) |
| 166 | .WillOnce(Return(kTestSocket)); |
| 167 | EXPECT_CALL(sockets_, Bind(kTestSocket, _, sizeof(sockaddr_nl))) |
| 168 | .WillOnce(Return(0)); |
| 169 | RTNLHandler::GetInstance()->Start(&dispatcher_, &sockets_); |
| 170 | } |
| 171 | |
| 172 | void RoutingTableTest::StopRTNLHandler() { |
| 173 | EXPECT_CALL(sockets_, Close(_)).WillOnce(Return(0)); |
| 174 | RTNLHandler::GetInstance()->Stop(); |
| 175 | } |
| 176 | |
| 177 | TEST_F(RoutingTableTest, RouteAddDelete) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 178 | EXPECT_CALL(sockets_, Send(kTestSocket, _, _, 0)); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 179 | StartRTNLHandler(); |
| 180 | routing_table_->Start(); |
| 181 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 182 | // Expect the tables to be empty by default. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 183 | EXPECT_EQ(0, GetRoutingTables()->size()); |
| 184 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 185 | IPAddress default_address(IPAddress::kFamilyIPv4); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 186 | default_address.SetAddressToDefault(); |
| 187 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 188 | IPAddress gateway_address0(IPAddress::kFamilyIPv4); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 189 | gateway_address0.SetAddressFromString(kTestNetAddress0); |
| 190 | |
| 191 | int metric = 10; |
| 192 | |
| 193 | RoutingTableEntry entry0(default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 194 | default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 195 | gateway_address0, |
| 196 | metric, |
| 197 | RT_SCOPE_UNIVERSE, |
| 198 | true); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 199 | // Add a single entry. |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 200 | SendRouteMsg(RTNLMessage::kModeAdd, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 201 | kTestDeviceIndex0, |
| 202 | entry0); |
| 203 | |
| 204 | base::hash_map<int, std::vector<RoutingTableEntry> > *tables = |
| 205 | GetRoutingTables(); |
| 206 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 207 | // 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] | 208 | EXPECT_EQ(1, tables->size()); |
| 209 | EXPECT_TRUE(ContainsKey(*tables, kTestDeviceIndex0)); |
| 210 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 211 | |
| 212 | RoutingTableEntry test_entry = (*tables)[kTestDeviceIndex0][0]; |
| 213 | EXPECT_TRUE(entry0.Equals(test_entry)); |
| 214 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 215 | // Add a second entry for a different interface. |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 216 | SendRouteMsg(RTNLMessage::kModeAdd, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 217 | kTestDeviceIndex1, |
| 218 | entry0); |
| 219 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 220 | // We should have two tables, which should have a single entry each. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 221 | EXPECT_EQ(2, tables->size()); |
| 222 | EXPECT_TRUE(ContainsKey(*tables, kTestDeviceIndex1)); |
| 223 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 224 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex1].size()); |
| 225 | |
| 226 | test_entry = (*tables)[kTestDeviceIndex1][0]; |
| 227 | EXPECT_TRUE(entry0.Equals(test_entry)); |
| 228 | |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 229 | IPAddress gateway_address1(IPAddress::kFamilyIPv4); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 230 | gateway_address1.SetAddressFromString(kTestNetAddress1); |
| 231 | |
| 232 | RoutingTableEntry entry1(default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 233 | default_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 234 | gateway_address1, |
| 235 | metric, |
| 236 | RT_SCOPE_UNIVERSE, |
| 237 | true); |
| 238 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 239 | // Add a second gateway route to the second interface. |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 240 | SendRouteMsg(RTNLMessage::kModeAdd, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 241 | kTestDeviceIndex1, |
| 242 | entry1); |
| 243 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 244 | // We should have two tables, one of which has a single entry, the other has |
| 245 | // two. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 246 | EXPECT_EQ(2, tables->size()); |
| 247 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 248 | EXPECT_EQ(2, (*tables)[kTestDeviceIndex1].size()); |
| 249 | |
| 250 | test_entry = (*tables)[kTestDeviceIndex1][1]; |
| 251 | EXPECT_TRUE(entry1.Equals(test_entry)); |
| 252 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 253 | // Remove the first gateway route from the second interface. |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 254 | SendRouteMsg(RTNLMessage::kModeDelete, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 255 | kTestDeviceIndex1, |
| 256 | entry0); |
| 257 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 258 | // We should be back to having one route per table. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 259 | EXPECT_EQ(2, tables->size()); |
| 260 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 261 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex1].size()); |
| 262 | |
| 263 | test_entry = (*tables)[kTestDeviceIndex1][0]; |
| 264 | EXPECT_TRUE(entry1.Equals(test_entry)); |
| 265 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 266 | // Send a duplicate of the second gatway route message, changing the metric. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 267 | RoutingTableEntry entry2(entry1); |
| 268 | entry2.metric++; |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 269 | SendRouteMsg(RTNLMessage::kModeAdd, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 270 | kTestDeviceIndex1, |
| 271 | entry2); |
| 272 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 273 | // Routing table size shouldn't change, but the new metric should match. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 274 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex1].size()); |
| 275 | test_entry = (*tables)[kTestDeviceIndex1][0]; |
| 276 | EXPECT_TRUE(entry2.Equals(test_entry)); |
| 277 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 278 | // Find a matching entry. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 279 | EXPECT_TRUE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 280 | IPAddress::kFamilyIPv4, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 281 | &test_entry)); |
| 282 | EXPECT_TRUE(entry2.Equals(test_entry)); |
| 283 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 284 | // Test that a search for a non-matching family fails. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 285 | EXPECT_FALSE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 286 | IPAddress::kFamilyIPv6, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 287 | &test_entry)); |
| 288 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 289 | // Remove last entry from an existing interface and test that we now fail. |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 290 | SendRouteMsg(RTNLMessage::kModeDelete, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 291 | kTestDeviceIndex1, |
| 292 | entry2); |
| 293 | |
| 294 | EXPECT_FALSE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 295 | IPAddress::kFamilyIPv4, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 296 | &test_entry)); |
| 297 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 298 | // Add a route from an IPConfig entry. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 299 | MockControl control; |
| 300 | IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0)); |
| 301 | IPConfig::Properties properties; |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 302 | properties.address_family = IPAddress::kFamilyIPv4; |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 303 | properties.gateway = kTestNetAddress0; |
| 304 | properties.address = kTestNetAddress1; |
| 305 | ipconfig->UpdateProperties(properties, true); |
| 306 | |
| 307 | EXPECT_CALL(sockets_, |
| 308 | Send(kTestSocket, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 309 | IsRoutingPacket(RTNLMessage::kModeAdd, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 310 | kTestDeviceIndex1, |
| 311 | entry0, |
| 312 | NLM_F_CREATE | NLM_F_EXCL), |
| 313 | _, |
| 314 | 0)); |
| 315 | EXPECT_TRUE(routing_table_->SetDefaultRoute(kTestDeviceIndex1, |
| 316 | ipconfig, |
| 317 | metric)); |
| 318 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 319 | // The table entry should look much like entry0, except with |
| 320 | // from_rtnl = false. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 321 | RoutingTableEntry entry3(entry0); |
| 322 | entry3.from_rtnl = false; |
| 323 | EXPECT_TRUE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 324 | IPAddress::kFamilyIPv4, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 325 | &test_entry)); |
| 326 | EXPECT_TRUE(entry3.Equals(test_entry)); |
| 327 | |
| 328 | // Setting the same route on the interface with a different metric should |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 329 | // push the route with different flags to indicate we are replacing it, |
| 330 | // then it should delete the old entry. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 331 | RoutingTableEntry entry4(entry3); |
| 332 | entry4.metric += 10; |
| 333 | EXPECT_CALL(sockets_, |
| 334 | Send(kTestSocket, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 335 | IsRoutingPacket(RTNLMessage::kModeAdd, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 336 | kTestDeviceIndex1, |
| 337 | entry4, |
| 338 | NLM_F_CREATE | NLM_F_REPLACE), |
| 339 | _, |
| 340 | 0)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 341 | EXPECT_CALL(sockets_, |
| 342 | Send(kTestSocket, |
| 343 | IsRoutingPacket(RTNLMessage::kModeDelete, |
| 344 | kTestDeviceIndex1, |
| 345 | entry3, |
| 346 | 0), |
| 347 | _, |
| 348 | 0)); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 349 | EXPECT_TRUE(routing_table_->SetDefaultRoute(kTestDeviceIndex1, |
| 350 | ipconfig, |
| 351 | entry4.metric)); |
| 352 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 353 | // Test that removing the table causes the route to disappear. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 354 | routing_table_->ResetTable(kTestDeviceIndex1); |
| 355 | EXPECT_FALSE(ContainsKey(*tables, kTestDeviceIndex1)); |
| 356 | EXPECT_FALSE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 357 | IPAddress::kFamilyIPv4, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 358 | &test_entry)); |
| 359 | EXPECT_EQ(1, GetRoutingTables()->size()); |
| 360 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 361 | // When we set the metric on an existing route, a new add and delete |
| 362 | // operation should occur. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 363 | RoutingTableEntry entry5(entry4); |
| 364 | entry5.metric += 10; |
| 365 | EXPECT_CALL(sockets_, |
| 366 | Send(kTestSocket, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 367 | IsRoutingPacket(RTNLMessage::kModeAdd, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 368 | kTestDeviceIndex0, |
| 369 | entry5, |
| 370 | NLM_F_CREATE | NLM_F_REPLACE), |
| 371 | _, |
| 372 | 0)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 373 | EXPECT_CALL(sockets_, |
| 374 | Send(kTestSocket, |
| 375 | IsRoutingPacket(RTNLMessage::kModeDelete, |
| 376 | kTestDeviceIndex0, |
| 377 | entry0, |
| 378 | 0), |
| 379 | _, |
| 380 | 0)); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 381 | routing_table_->SetDefaultMetric(kTestDeviceIndex0, entry5.metric); |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 382 | // Furthermore, the routing table should reflect the change in the metric |
| 383 | // for the default route for the interface. |
| 384 | RoutingTableEntry default_route; |
| 385 | EXPECT_TRUE(routing_table_->GetDefaultRoute(kTestDeviceIndex0, |
| 386 | IPAddress::kFamilyIPv4, |
| 387 | &default_route)); |
| 388 | EXPECT_EQ(entry5.metric, default_route.metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 389 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 390 | // Ask to flush table0. We should see a delete message sent. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 391 | EXPECT_CALL(sockets_, |
| 392 | Send(kTestSocket, |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 393 | IsRoutingPacket(RTNLMessage::kModeDelete, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 394 | kTestDeviceIndex0, |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 395 | entry5, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 396 | 0), |
| 397 | _, |
| 398 | 0)); |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame^] | 399 | routing_table_->FlushRoutes(kTestDeviceIndex0, true); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 400 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 401 | // Test that the routing table size returns to zero. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 402 | EXPECT_EQ(1, GetRoutingTables()->size()); |
| 403 | routing_table_->ResetTable(kTestDeviceIndex0); |
| 404 | EXPECT_EQ(0, GetRoutingTables()->size()); |
| 405 | |
| 406 | routing_table_->Stop(); |
| 407 | StopRTNLHandler(); |
| 408 | } |
| 409 | |
Thieu Le | caef893 | 2012-02-28 16:06:59 -0800 | [diff] [blame^] | 410 | TEST_F(RoutingTableTest, RouteDeleteForeign) { |
| 411 | EXPECT_CALL(sockets_, Send(kTestSocket, _, _, 0)); |
| 412 | StartRTNLHandler(); |
| 413 | routing_table_->Start(); |
| 414 | |
| 415 | // Expect the tables to be empty by default. |
| 416 | EXPECT_EQ(0, GetRoutingTables()->size()); |
| 417 | |
| 418 | // Add a foreign IPv4 entry. |
| 419 | IPAddress default_address4(IPAddress::kFamilyIPv4); |
| 420 | default_address4.SetAddressToDefault(); |
| 421 | |
| 422 | IPAddress foreign_address4(IPAddress::kFamilyIPv4); |
| 423 | foreign_address4.SetAddressFromString(kTestForeignNetAddress4); |
| 424 | |
| 425 | IPAddress foreign_gateway4(IPAddress::kFamilyIPv4); |
| 426 | foreign_gateway4.SetAddressFromString(kTestForeignNetGateway4); |
| 427 | |
| 428 | const int metric = 10; |
| 429 | |
| 430 | RoutingTableEntry foreign_entry4(foreign_address4, |
| 431 | default_address4, |
| 432 | foreign_gateway4, |
| 433 | metric, |
| 434 | RT_SCOPE_UNIVERSE, |
| 435 | true); |
| 436 | SendRouteMsg(RTNLMessage::kModeAdd, |
| 437 | kTestDeviceIndex0, |
| 438 | foreign_entry4); |
| 439 | |
| 440 | base::hash_map<int, std::vector<RoutingTableEntry> > *tables = |
| 441 | GetRoutingTables(); |
| 442 | EXPECT_EQ(1, tables->size()); |
| 443 | EXPECT_EQ(1, (*tables)[kTestDeviceIndex0].size()); |
| 444 | RoutingTableEntry test_entry = (*tables)[kTestDeviceIndex0][0]; |
| 445 | EXPECT_TRUE(test_entry.from_rtnl); |
| 446 | |
| 447 | // Add a foreign IPv6 entry. |
| 448 | // This entry should not be deleted. |
| 449 | IPAddress default_address6(IPAddress::kFamilyIPv6); |
| 450 | default_address6.SetAddressToDefault(); |
| 451 | |
| 452 | IPAddress foreign_address6(IPAddress::kFamilyIPv6); |
| 453 | foreign_address6.SetAddressFromString(kTestForeignNetAddress6); |
| 454 | |
| 455 | IPAddress foreign_gateway6(IPAddress::kFamilyIPv6); |
| 456 | foreign_gateway6.SetAddressFromString(kTestForeignNetGateway6); |
| 457 | |
| 458 | RoutingTableEntry foreign_entry6(foreign_address6, |
| 459 | default_address6, |
| 460 | foreign_gateway6, |
| 461 | metric, |
| 462 | RT_SCOPE_UNIVERSE, |
| 463 | true); |
| 464 | SendRouteMsg(RTNLMessage::kModeAdd, |
| 465 | kTestDeviceIndex0, |
| 466 | foreign_entry6); |
| 467 | |
| 468 | EXPECT_EQ(2, (*tables)[kTestDeviceIndex0].size()); |
| 469 | test_entry = (*tables)[kTestDeviceIndex0][1]; |
| 470 | EXPECT_TRUE(test_entry.from_rtnl); |
| 471 | |
| 472 | // Add device route. |
| 473 | IPAddress device_net_address4(IPAddress::kFamilyIPv4); |
| 474 | device_net_address4.SetAddressFromString(kTestDeviceNetAddress4); |
| 475 | RTNLMessage msg( |
| 476 | RTNLMessage::kTypeRoute, |
| 477 | RTNLMessage::kModeAdd, |
| 478 | 0, |
| 479 | 0, |
| 480 | 0, |
| 481 | 0, |
| 482 | device_net_address4.family()); |
| 483 | msg.set_route_status(RTNLMessage::RouteStatus( |
| 484 | device_net_address4.prefix(), |
| 485 | default_address4.prefix(), |
| 486 | RT_TABLE_MAIN, |
| 487 | RTPROT_KERNEL, |
| 488 | RT_SCOPE_UNIVERSE, |
| 489 | RTN_UNICAST, |
| 490 | 0)); |
| 491 | |
| 492 | msg.SetAttribute(RTA_DST, device_net_address4.address()); |
| 493 | msg.SetAttribute(RTA_SRC, default_address4.address()); |
| 494 | msg.SetAttribute(RTA_GATEWAY, foreign_gateway4.address()); |
| 495 | msg.SetAttribute(RTA_PRIORITY, ByteString::CreateFromCPUUInt32(metric)); |
| 496 | msg.SetAttribute(RTA_OIF, ByteString::CreateFromCPUUInt32(kTestDeviceIndex0)); |
| 497 | |
| 498 | ByteString msgdata = msg.Encode(); |
| 499 | EXPECT_NE(0, msgdata.GetLength()); |
| 500 | |
| 501 | InputData data(msgdata.GetData(), msgdata.GetLength()); |
| 502 | RTNLHandler::GetInstance()->ParseRTNL(&data); |
| 503 | |
| 504 | // The device route should not make it into our routing table. |
| 505 | EXPECT_EQ(2, (*tables)[kTestDeviceIndex0].size()); |
| 506 | |
| 507 | // Add an entry from IPConfig. |
| 508 | MockControl control; |
| 509 | IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0)); |
| 510 | IPConfig::Properties properties; |
| 511 | properties.address_family = IPAddress::kFamilyIPv4; |
| 512 | properties.gateway = kTestNetAddress0; |
| 513 | properties.address = kTestNetAddress1; |
| 514 | ipconfig->UpdateProperties(properties, true); |
| 515 | |
| 516 | IPAddress gateway_address4(IPAddress::kFamilyIPv4); |
| 517 | gateway_address4.SetAddressFromString(kTestNetAddress0); |
| 518 | RoutingTableEntry entry(default_address4, |
| 519 | default_address4, |
| 520 | gateway_address4, |
| 521 | metric, |
| 522 | RT_SCOPE_UNIVERSE, |
| 523 | false); |
| 524 | |
| 525 | EXPECT_CALL(sockets_, |
| 526 | Send(kTestSocket, |
| 527 | IsRoutingPacket(RTNLMessage::kModeDelete, |
| 528 | kTestDeviceIndex0, |
| 529 | foreign_entry4, |
| 530 | 0), |
| 531 | _, |
| 532 | 0)); |
| 533 | EXPECT_CALL(sockets_, |
| 534 | Send(kTestSocket, |
| 535 | IsRoutingPacket(RTNLMessage::kModeDelete, |
| 536 | kTestDeviceIndex0, |
| 537 | foreign_entry6, |
| 538 | 0), |
| 539 | _, |
| 540 | 0)).Times(0); |
| 541 | EXPECT_CALL(sockets_, |
| 542 | Send(kTestSocket, |
| 543 | IsRoutingPacket(RTNLMessage::kModeAdd, |
| 544 | kTestDeviceIndex0, |
| 545 | entry, |
| 546 | NLM_F_CREATE | NLM_F_EXCL), |
| 547 | _, |
| 548 | 0)); |
| 549 | EXPECT_TRUE(routing_table_->SetDefaultRoute(kTestDeviceIndex0, |
| 550 | ipconfig, |
| 551 | metric)); |
| 552 | |
| 553 | routing_table_->Stop(); |
| 554 | StopRTNLHandler(); |
| 555 | } |
| 556 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 557 | } // namespace shill |