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