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 | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 285 | // Send a duplicate of the second gateway 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 | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 317 | // Add a route to a gatway address. |
| 318 | IPAddress gateway_address(IPAddress::kFamilyIPv4); |
| 319 | EXPECT_TRUE(gateway_address.SetAddressFromString(kTestNetAddress0)); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 320 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 321 | EXPECT_CALL(rtnl_handler_, |
| 322 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 323 | kTestDeviceIndex1, |
| 324 | entry0, |
| 325 | NLM_F_CREATE | NLM_F_EXCL))); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 326 | EXPECT_TRUE(routing_table_->SetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 327 | gateway_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 328 | metric)); |
| 329 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 330 | // The table entry should look much like entry0, except with |
| 331 | // from_rtnl = false. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 332 | RoutingTableEntry entry3(entry0); |
| 333 | entry3.from_rtnl = false; |
| 334 | EXPECT_TRUE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 335 | IPAddress::kFamilyIPv4, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 336 | &test_entry)); |
| 337 | EXPECT_TRUE(entry3.Equals(test_entry)); |
| 338 | |
| 339 | // Setting the same route on the interface with a different metric should |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 340 | // push the route with different flags to indicate we are replacing it, |
| 341 | // then it should delete the old entry. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 342 | RoutingTableEntry entry4(entry3); |
| 343 | entry4.metric += 10; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 344 | EXPECT_CALL(rtnl_handler_, |
| 345 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 346 | kTestDeviceIndex1, |
| 347 | entry4, |
| 348 | NLM_F_CREATE | NLM_F_REPLACE))); |
| 349 | EXPECT_CALL(rtnl_handler_, |
| 350 | SendMessage(IsRoutingPacket(RTNLMessage::kModeDelete, |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 351 | kTestDeviceIndex1, |
| 352 | entry3, |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 353 | 0))); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 354 | EXPECT_TRUE(routing_table_->SetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 5b7ba8c | 2012-04-18 09:08:00 -0700 | [diff] [blame] | 355 | gateway_address, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 356 | entry4.metric)); |
| 357 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 358 | // Test that removing the table causes the route to disappear. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 359 | routing_table_->ResetTable(kTestDeviceIndex1); |
| 360 | EXPECT_FALSE(ContainsKey(*tables, kTestDeviceIndex1)); |
| 361 | EXPECT_FALSE(routing_table_->GetDefaultRoute(kTestDeviceIndex1, |
Paul Stewart | 7355ce1 | 2011-09-02 10:47:01 -0700 | [diff] [blame] | 362 | IPAddress::kFamilyIPv4, |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 363 | &test_entry)); |
| 364 | EXPECT_EQ(1, GetRoutingTables()->size()); |
| 365 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 366 | // When we set the metric on an existing route, a new add and delete |
| 367 | // operation should occur. |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 368 | RoutingTableEntry entry5(entry4); |
| 369 | entry5.metric += 10; |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 370 | EXPECT_CALL(rtnl_handler_, |
| 371 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 372 | kTestDeviceIndex0, |
| 373 | entry5, |
| 374 | NLM_F_CREATE | NLM_F_REPLACE))); |
| 375 | EXPECT_CALL(rtnl_handler_, |
| 376 | SendMessage(IsRoutingPacket(RTNLMessage::kModeDelete, |
| 377 | kTestDeviceIndex0, |
| 378 | entry0, |
| 379 | 0))); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 380 | routing_table_->SetDefaultMetric(kTestDeviceIndex0, entry5.metric); |
mukesh agrawal | d4ef677 | 2012-02-21 16:28:04 -0800 | [diff] [blame] | 381 | // Furthermore, the routing table should reflect the change in the metric |
| 382 | // for the default route for the interface. |
| 383 | RoutingTableEntry default_route; |
| 384 | EXPECT_TRUE(routing_table_->GetDefaultRoute(kTestDeviceIndex0, |
| 385 | IPAddress::kFamilyIPv4, |
| 386 | &default_route)); |
| 387 | EXPECT_EQ(entry5.metric, default_route.metric); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 388 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 389 | // Ask to flush table0. We should see a delete message sent. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 390 | EXPECT_CALL(rtnl_handler_, |
| 391 | SendMessage(IsRoutingPacket(RTNLMessage::kModeDelete, |
| 392 | kTestDeviceIndex0, |
| 393 | entry5, |
| 394 | 0))); |
Thieu Le | fb46caf | 2012-03-08 11:57:15 -0800 | [diff] [blame] | 395 | routing_table_->FlushRoutes(kTestDeviceIndex0); |
| 396 | EXPECT_EQ(0, (*tables)[kTestDeviceIndex0].size()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 397 | |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 398 | // Test that the routing table size returns to zero. |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 399 | SendRouteEntry(RTNLMessage::kModeAdd, |
| 400 | kTestDeviceIndex0, |
| 401 | entry5); |
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(); |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Paul Stewart | 3f68bb1 | 2012-03-15 13:33:10 -0700 | [diff] [blame] | 409 | TEST_F(RoutingTableTest, ConfigureRoutes) { |
| 410 | MockControl control; |
| 411 | IPConfigRefPtr ipconfig(new IPConfig(&control, kTestDeviceName0)); |
| 412 | IPConfig::Properties properties; |
| 413 | properties.address_family = IPAddress::kFamilyIPv4; |
| 414 | vector<IPConfig::Route> &routes = properties.routes; |
| 415 | ipconfig->UpdateProperties(properties, true); |
| 416 | |
| 417 | const int kMetric = 10; |
| 418 | EXPECT_TRUE(routing_table_->ConfigureRoutes(kTestDeviceIndex0, |
| 419 | ipconfig, |
| 420 | kMetric)); |
| 421 | |
| 422 | IPConfig::Route route; |
| 423 | route.host = kTestRemoteNetwork4; |
| 424 | route.netmask = kTestRemoteNetmask4; |
| 425 | route.gateway = kTestGatewayAddress4; |
| 426 | routes.push_back(route); |
| 427 | ipconfig->UpdateProperties(properties, true); |
| 428 | |
| 429 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 430 | IPAddress source_address(IPAddress::kFamilyIPv4); |
| 431 | IPAddress gateway_address(IPAddress::kFamilyIPv4); |
| 432 | ASSERT_TRUE(destination_address.SetAddressFromString(kTestRemoteNetwork4)); |
| 433 | destination_address.set_prefix(kTestRemotePrefix4); |
| 434 | ASSERT_TRUE(gateway_address.SetAddressFromString(kTestGatewayAddress4)); |
| 435 | |
| 436 | RoutingTableEntry entry(destination_address, |
| 437 | source_address, |
| 438 | gateway_address, |
| 439 | kMetric, |
| 440 | RT_SCOPE_UNIVERSE, |
| 441 | false); |
| 442 | |
| 443 | EXPECT_CALL(rtnl_handler_, |
| 444 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 445 | kTestDeviceIndex0, |
| 446 | entry, |
| 447 | NLM_F_CREATE | NLM_F_EXCL))); |
| 448 | EXPECT_TRUE(routing_table_->ConfigureRoutes(kTestDeviceIndex0, |
| 449 | ipconfig, |
| 450 | kMetric)); |
| 451 | |
| 452 | routes.clear(); |
| 453 | route.gateway = "xxx"; // Invalid gateway entry -- should be skipped |
| 454 | routes.push_back(route); |
| 455 | route.host = "xxx"; // Invalid host entry -- should be skipped |
| 456 | route.gateway = kTestGatewayAddress4; |
| 457 | routes.push_back(route); |
| 458 | route.host = kTestRemoteNetwork4; |
| 459 | routes.push_back(route); |
| 460 | ipconfig->UpdateProperties(properties, true); |
| 461 | |
| 462 | EXPECT_CALL(rtnl_handler_, |
| 463 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 464 | kTestDeviceIndex0, |
| 465 | entry, |
| 466 | NLM_F_CREATE | NLM_F_EXCL))) |
| 467 | .Times(1); |
| 468 | EXPECT_FALSE(routing_table_->ConfigureRoutes(kTestDeviceIndex0, |
| 469 | ipconfig, |
| 470 | kMetric)); |
| 471 | } |
| 472 | |
Paul Stewart | f748a36 | 2012-03-07 12:01:20 -0800 | [diff] [blame] | 473 | MATCHER_P2(IsRoutingQuery, destination, index, "") { |
| 474 | const RTNLMessage::RouteStatus &status = arg->route_status(); |
| 475 | |
| 476 | uint32 oif; |
| 477 | |
| 478 | return |
| 479 | arg->type() == RTNLMessage::kTypeRoute && |
| 480 | arg->family() == destination.family() && |
| 481 | arg->flags() == NLM_F_REQUEST && |
| 482 | status.table == 0 && |
| 483 | status.protocol == 0 && |
| 484 | status.scope == 0 && |
| 485 | status.type == 0 && |
| 486 | arg->HasAttribute(RTA_DST) && |
| 487 | IPAddress(arg->family(), |
| 488 | arg->GetAttribute(RTA_DST), |
| 489 | status.dst_prefix).Equals(destination) && |
| 490 | !arg->HasAttribute(RTA_SRC) && |
| 491 | !arg->HasAttribute(RTA_GATEWAY) && |
| 492 | arg->GetAttribute(RTA_OIF).ConvertToCPUUInt32(&oif) && |
| 493 | oif == index && |
| 494 | !arg->HasAttribute(RTA_PRIORITY); |
| 495 | |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | TEST_F(RoutingTableTest, RequestHostRoute) { |
| 500 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 501 | destination_address.SetAddressFromString(kTestRemoteAddress4); |
| 502 | destination_address.set_prefix(24); |
| 503 | |
| 504 | EXPECT_CALL(rtnl_handler_, |
| 505 | SendMessage(IsRoutingQuery(destination_address, |
| 506 | kTestDeviceIndex0))) |
| 507 | .WillOnce(Invoke(this, &RoutingTableTest::SetSequenceForMessage)); |
| 508 | EXPECT_TRUE(routing_table_->RequestRouteToHost(destination_address, |
| 509 | kTestDeviceIndex0)); |
| 510 | |
| 511 | IPAddress gateway_address(IPAddress::kFamilyIPv4); |
| 512 | gateway_address.SetAddressFromString(kTestGatewayAddress4); |
| 513 | |
| 514 | IPAddress local_address(IPAddress::kFamilyIPv4); |
| 515 | local_address.SetAddressFromString(kTestDeviceNetAddress4); |
| 516 | |
| 517 | const int kMetric = 10; |
| 518 | RoutingTableEntry entry(destination_address, |
| 519 | local_address, |
| 520 | gateway_address, |
| 521 | kMetric, |
| 522 | RT_SCOPE_UNIVERSE, |
| 523 | true); |
| 524 | |
| 525 | EXPECT_CALL(rtnl_handler_, |
| 526 | SendMessage(IsRoutingPacket(RTNLMessage::kModeAdd, |
| 527 | kTestDeviceIndex0, |
| 528 | entry, |
| 529 | NLM_F_CREATE | NLM_F_EXCL))); |
| 530 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 531 | kTestDeviceIndex0, |
| 532 | entry, |
| 533 | kTestRequestSeq, |
| 534 | RTPROT_UNSPEC); |
| 535 | } |
| 536 | |
| 537 | TEST_F(RoutingTableTest, RequestHostRouteBadSequence) { |
| 538 | IPAddress destination_address(IPAddress::kFamilyIPv4); |
| 539 | destination_address.SetAddressFromString(kTestRemoteAddress4); |
| 540 | EXPECT_CALL(rtnl_handler_, SendMessage(_)) |
| 541 | .WillOnce(Invoke(this, &RoutingTableTest::SetSequenceForMessage)); |
| 542 | EXPECT_TRUE(routing_table_->RequestRouteToHost(destination_address, |
| 543 | kTestDeviceIndex0)); |
| 544 | EXPECT_FALSE(GetQuerySequences()->empty()); |
| 545 | |
| 546 | RoutingTableEntry entry(destination_address, |
| 547 | destination_address, |
| 548 | destination_address, |
| 549 | 0, |
| 550 | RT_SCOPE_UNIVERSE, |
| 551 | true); |
| 552 | |
| 553 | // Try a sequence arriving before the one RoutingTable is looking for. |
| 554 | // This should be a no-op. |
| 555 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 556 | kTestDeviceIndex0, |
| 557 | entry, |
| 558 | kTestRequestSeq-1, |
| 559 | RTPROT_UNSPEC); |
| 560 | EXPECT_FALSE(GetQuerySequences()->empty()); |
| 561 | |
| 562 | // Try a sequence arriving after the one RoutingTable is looking for. |
| 563 | // This should cause the request to be purged. |
| 564 | SendRouteEntryWithSeqAndProto(RTNLMessage::kModeAdd, |
| 565 | kTestDeviceIndex0, |
| 566 | entry, |
| 567 | kTestRequestSeq+1, |
| 568 | RTPROT_UNSPEC); |
| 569 | EXPECT_TRUE(GetQuerySequences()->empty()); |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Paul Stewart | 75e89d2 | 2011-08-01 10:00:02 -0700 | [diff] [blame] | 572 | } // namespace shill |