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