Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 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 "shill/link_monitor.h" |
| 6 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 7 | #include <base/bind.h> |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 8 | #include <gtest/gtest.h> |
| 9 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 10 | #include "shill/arp_packet.h" |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 11 | #include "shill/byte_string.h" |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 12 | #include "shill/ip_address.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 13 | #include "shill/logging.h" |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 14 | #include "shill/mock_arp_client.h" |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 15 | #include "shill/mock_control.h" |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 16 | #include "shill/mock_connection.h" |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 17 | #include "shill/mock_device_info.h" |
| 18 | #include "shill/mock_event_dispatcher.h" |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 19 | #include "shill/mock_log.h" |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 20 | #include "shill/mock_metrics.h" |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 21 | #include "shill/mock_sockets.h" |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 22 | #include "shill/mock_time.h" |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 23 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 24 | using base::Bind; |
| 25 | using base::Unretained; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 26 | using std::string; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 27 | using testing::_; |
| 28 | using testing::AnyNumber; |
| 29 | using testing::HasSubstr; |
| 30 | using testing::Invoke; |
| 31 | using testing::Mock; |
| 32 | using testing::NiceMock; |
| 33 | using testing::Return; |
| 34 | using testing::ReturnRef; |
| 35 | using testing::SetArgumentPointee; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 36 | using testing::StrictMock; |
| 37 | using testing::Test; |
| 38 | |
| 39 | namespace shill { |
| 40 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 41 | namespace { |
| 42 | const int kInterfaceIndex = 123; |
| 43 | const char kLocalIPAddress[] = "10.0.1.1"; |
| 44 | const uint8 kLocalMACAddress[] = { 0, 1, 2, 3, 4, 5 }; |
| 45 | const char kRemoteIPAddress[] = "10.0.1.2"; |
| 46 | const uint8 kRemoteMACAddress[] = { 6, 7, 8, 9, 10, 11 }; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 47 | } // namespace |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 48 | |
| 49 | |
| 50 | class LinkMonitorForTest : public LinkMonitor { |
| 51 | public: |
| 52 | LinkMonitorForTest(const ConnectionRefPtr &connection, |
| 53 | EventDispatcher *dispatcher, |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 54 | Metrics *metrics, |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 55 | DeviceInfo *device_info) |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 56 | : LinkMonitor(connection, dispatcher, metrics, device_info, |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 57 | Bind(&LinkMonitorForTest::FailureCallbackHandler, |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 58 | Unretained(this))) {} |
| 59 | |
| 60 | virtual ~LinkMonitorForTest() {} |
| 61 | |
| 62 | MOCK_METHOD0(CreateClient, bool()); |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 63 | |
| 64 | // This does not override any methods in LinkMonitor, but is used here |
| 65 | // in unit tests to support expectations when the LinkMonitor notifies |
| 66 | // its client of a failure. |
| 67 | MOCK_METHOD0(FailureCallbackHandler, void()); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | MATCHER_P4(IsArpRequest, local_ip, remote_ip, local_mac, remote_mac, "") { |
| 71 | return |
| 72 | local_ip.Equals(arg.local_ip_address()) && |
| 73 | remote_ip.Equals(arg.remote_ip_address()) && |
| 74 | local_mac.Equals(arg.local_mac_address()) && |
| 75 | remote_mac.Equals(arg.remote_mac_address()); |
| 76 | } |
| 77 | |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 78 | class LinkMonitorTest : public Test { |
| 79 | public: |
| 80 | LinkMonitorTest() |
Thieu Le | 6c1e3bb | 2013-02-06 15:20:35 -0800 | [diff] [blame] | 81 | : metrics_(&dispatcher_), |
| 82 | device_info_( |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 83 | &control_, |
| 84 | reinterpret_cast<EventDispatcher*>(NULL), |
| 85 | reinterpret_cast<Metrics*>(NULL), |
| 86 | reinterpret_cast<Manager*>(NULL)), |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 87 | connection_(new StrictMock<MockConnection>(&device_info_)), |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 88 | monitor_(connection_, &dispatcher_, &metrics_, &device_info_), |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 89 | client_(NULL), |
| 90 | next_client_(new StrictMock<MockArpClient>()), |
| 91 | gateway_ip_(IPAddress::kFamilyIPv4), |
| 92 | local_ip_(IPAddress::kFamilyIPv4), |
| 93 | gateway_mac_(kRemoteMACAddress, arraysize(kRemoteMACAddress)), |
| 94 | local_mac_(kLocalMACAddress, arraysize(kLocalMACAddress)), |
| 95 | zero_mac_(arraysize(kLocalMACAddress)), |
| 96 | link_scope_logging_was_enabled_(false) {} |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 97 | virtual ~LinkMonitorTest() {} |
| 98 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 99 | virtual void SetUp() { |
| 100 | link_scope_logging_was_enabled_ = SLOG_IS_ON(Link, 0); |
| 101 | if (!link_scope_logging_was_enabled_) { |
| 102 | ScopeLogger::GetInstance()->EnableScopesByName("link"); |
| 103 | ScopeLogger::GetInstance()->set_verbose_level(4); |
| 104 | } |
| 105 | monitor_.time_ = &time_; |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 106 | time_val_.tv_sec = 0; |
| 107 | time_val_.tv_usec = 0; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 108 | EXPECT_CALL(time_, GetTimeMonotonic(_)) |
| 109 | .WillRepeatedly(DoAll(SetArgumentPointee<0>(time_val_), Return(0))); |
| 110 | EXPECT_TRUE(local_ip_.SetAddressFromString(kLocalIPAddress)); |
| 111 | EXPECT_CALL(*connection_, local()).WillRepeatedly(ReturnRef(local_ip_)); |
| 112 | EXPECT_TRUE(gateway_ip_.SetAddressFromString(kRemoteIPAddress)); |
| 113 | EXPECT_CALL(*connection_, gateway()).WillRepeatedly(ReturnRef(gateway_ip_)); |
Alex Deymo | fddc09a | 2013-07-03 18:41:31 -0700 | [diff] [blame] | 114 | EXPECT_CALL(*connection_, technology()) |
| 115 | .WillRepeatedly(Return(Technology::kEthernet)); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | virtual void TearDown() { |
| 119 | if (!link_scope_logging_was_enabled_) { |
| 120 | ScopeLogger::GetInstance()->EnableScopesByName("-link"); |
| 121 | ScopeLogger::GetInstance()->set_verbose_level(0); |
| 122 | } |
| 123 | } |
| 124 | |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 125 | void AdvanceTime(int time_ms) { |
Han Shen | fc34925 | 2012-08-30 11:36:04 -0700 | [diff] [blame] | 126 | struct timeval adv_time = { |
| 127 | static_cast<time_t>(time_ms/1000), |
| 128 | static_cast<time_t>((time_ms % 1000) * 1000) }; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 129 | timeradd(&time_val_, &adv_time, &time_val_); |
| 130 | EXPECT_CALL(time_, GetTimeMonotonic(_)) |
| 131 | .WillRepeatedly(DoAll(SetArgumentPointee<0>(time_val_), Return(0))); |
| 132 | } |
| 133 | |
| 134 | bool CreateMockClient() { |
| 135 | EXPECT_FALSE(monitor_.arp_client_.get()); |
| 136 | if (client_) { |
| 137 | Mock::VerifyAndClearExpectations(client_); |
| 138 | } |
| 139 | client_ = next_client_; |
| 140 | next_client_ = new StrictMock<MockArpClient>(); |
| 141 | monitor_.arp_client_.reset(client_); |
| 142 | return true; |
| 143 | } |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 144 | |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 145 | string HardwareAddressToString(const ByteString &address) { |
| 146 | return LinkMonitor::HardwareAddressToString(address); |
| 147 | } |
| 148 | |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 149 | protected: |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 150 | void ExpectReset() { |
| 151 | EXPECT_FALSE(monitor_.GetResponseTimeMilliseconds()); |
| 152 | EXPECT_FALSE(GetArpClient()); |
| 153 | EXPECT_TRUE(GetSendRequestCallback().IsCancelled()); |
| 154 | EXPECT_EQ(0, GetBroadcastFailureCount()); |
| 155 | EXPECT_EQ(0, GetUnicastFailureCount()); |
| 156 | EXPECT_FALSE(IsUnicast()); |
| 157 | } |
| 158 | const ArpClient *GetArpClient() { return monitor_.arp_client_.get(); } |
| 159 | void TriggerRequestTimer() { |
| 160 | GetSendRequestCallback().callback().Run(); |
| 161 | } |
| 162 | const base::CancelableClosure &GetSendRequestCallback() { |
| 163 | return monitor_.send_request_callback_; |
| 164 | } |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 165 | int GetBroadcastFailureCount() { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 166 | return monitor_.broadcast_failure_count_; |
| 167 | } |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 168 | int GetUnicastFailureCount() { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 169 | return monitor_.unicast_failure_count_; |
| 170 | } |
| 171 | bool IsUnicast() { return monitor_.is_unicast_; } |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 172 | int GetTestPeriodMilliseconds() { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 173 | return LinkMonitor::kTestPeriodMilliseconds; |
| 174 | } |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 175 | int GetFailureThreshold() { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 176 | return LinkMonitor::kFailureThreshold; |
| 177 | } |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 178 | int GetMaxResponseSampleFilterDepth() { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 179 | return LinkMonitor::kMaxResponseSampleFilterDepth; |
| 180 | } |
| 181 | void ExpectTransmit(bool is_unicast) { |
| 182 | const ByteString &destination_mac = is_unicast ? gateway_mac_ : zero_mac_; |
| 183 | if (monitor_.arp_client_.get()) { |
| 184 | EXPECT_EQ(client_, monitor_.arp_client_.get()); |
| 185 | EXPECT_CALL(*client_, TransmitRequest( |
| 186 | IsArpRequest(local_ip_, gateway_ip_, local_mac_, destination_mac))) |
| 187 | .WillOnce(Return(true)); |
| 188 | } else { |
| 189 | EXPECT_CALL(monitor_, CreateClient()) |
| 190 | .WillOnce(Invoke(this, &LinkMonitorTest::CreateMockClient)); |
| 191 | EXPECT_CALL(*next_client_, TransmitRequest( |
| 192 | IsArpRequest(local_ip_, gateway_ip_, local_mac_, destination_mac))) |
| 193 | .WillOnce(Return(true)); |
| 194 | } |
| 195 | EXPECT_CALL(dispatcher_, PostDelayedTask(_, GetTestPeriodMilliseconds())); |
| 196 | } |
| 197 | void SendNextRequest() { |
| 198 | EXPECT_CALL(monitor_, CreateClient()) |
| 199 | .WillOnce(Invoke(this, &LinkMonitorTest::CreateMockClient)); |
| 200 | EXPECT_CALL(*next_client_, TransmitRequest(_)).WillOnce(Return(true)); |
| 201 | EXPECT_CALL(dispatcher_, PostDelayedTask(_, GetTestPeriodMilliseconds())); |
| 202 | TriggerRequestTimer(); |
| 203 | } |
| 204 | void ExpectNoTransmit() { |
| 205 | MockArpClient *client = |
| 206 | monitor_.arp_client_.get() ? client_ : next_client_; |
| 207 | EXPECT_CALL(*client, TransmitRequest(_)).Times(0); |
| 208 | } |
| 209 | void StartMonitor() { |
| 210 | EXPECT_CALL(device_info_, GetMACAddress(0, _)) |
| 211 | .WillOnce(DoAll(SetArgumentPointee<1>(local_mac_), Return(true))); |
| 212 | ExpectTransmit(false); |
| 213 | EXPECT_TRUE(monitor_.Start()); |
| 214 | EXPECT_TRUE(GetArpClient()); |
| 215 | EXPECT_FALSE(IsUnicast()); |
| 216 | EXPECT_FALSE(GetSendRequestCallback().IsCancelled()); |
| 217 | } |
| 218 | bool SimulateReceiveReply(ArpPacket *packet, ByteString *sender) { |
| 219 | packet->set_local_ip_address(rx_packet_.local_ip_address()); |
| 220 | packet->set_remote_ip_address(rx_packet_.remote_ip_address()); |
| 221 | packet->set_local_mac_address(rx_packet_.local_mac_address()); |
| 222 | packet->set_remote_mac_address(rx_packet_.remote_mac_address()); |
| 223 | return true; |
| 224 | } |
| 225 | void ReceiveResponse(const IPAddress &local_ip, |
| 226 | const ByteString &local_mac, |
| 227 | const IPAddress &remote_ip, |
| 228 | const ByteString &remote_mac) { |
| 229 | rx_packet_.set_local_ip_address(local_ip); |
| 230 | rx_packet_.set_local_mac_address(local_mac); |
| 231 | rx_packet_.set_remote_ip_address(remote_ip); |
| 232 | rx_packet_.set_remote_mac_address(remote_mac); |
| 233 | |
| 234 | EXPECT_CALL(*client_, ReceiveReply(_, _)) |
| 235 | .WillOnce(Invoke(this, &LinkMonitorTest::SimulateReceiveReply)); |
| 236 | monitor_.ReceiveResponse(0); |
| 237 | } |
| 238 | void ReceiveCorrectResponse() { |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 239 | ReceiveResponse(gateway_ip_, gateway_mac_, local_ip_, local_mac_); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | MockEventDispatcher dispatcher_; |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 243 | StrictMock<MockMetrics> metrics_; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 244 | MockControl control_; |
| 245 | NiceMock<MockDeviceInfo> device_info_; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 246 | scoped_refptr<MockConnection> connection_; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 247 | StrictMock<LinkMonitorForTest> monitor_; |
| 248 | MockTime time_; |
| 249 | struct timeval time_val_; |
| 250 | // This is owned by the LinkMonitor, and only tracked here for EXPECT*(). |
| 251 | MockArpClient *client_; |
| 252 | // This is owned by the test until it is handed to the LinkMonitorForTest |
| 253 | // and placed // in client_ above. |
| 254 | MockArpClient *next_client_; |
| 255 | IPAddress gateway_ip_; |
| 256 | IPAddress local_ip_; |
| 257 | ByteString gateway_mac_; |
| 258 | ByteString local_mac_; |
| 259 | ByteString zero_mac_; |
| 260 | ArpPacket rx_packet_; |
| 261 | bool link_scope_logging_was_enabled_; |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 262 | }; |
| 263 | |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 264 | |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 265 | TEST_F(LinkMonitorTest, Constructor) { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 266 | ExpectReset(); |
| 267 | } |
| 268 | |
| 269 | TEST_F(LinkMonitorTest, StartFailedGetMACAddress) { |
| 270 | ScopedMockLog log; |
| 271 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 272 | EXPECT_CALL(log, |
| 273 | Log(logging::LOG_ERROR, _, |
| 274 | HasSubstr("Could not get local MAC address"))).Times(1); |
| 275 | EXPECT_CALL(device_info_, GetMACAddress(0, _)).WillOnce(Return(false)); |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 276 | EXPECT_CALL(metrics_, SendEnumToUMA( |
| 277 | HasSubstr("LinkMonitorFailure"), Metrics::kLinkMonitorMacAddressNotFound, |
| 278 | _)); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 279 | EXPECT_CALL(monitor_, CreateClient()).Times(0); |
| 280 | EXPECT_FALSE(monitor_.Start()); |
| 281 | ExpectReset(); |
| 282 | } |
| 283 | |
| 284 | TEST_F(LinkMonitorTest, StartFailedCreateClient) { |
| 285 | ScopedMockLog log; |
| 286 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 287 | EXPECT_CALL(log, |
| 288 | Log(logging::LOG_ERROR, _, |
| 289 | HasSubstr("Failed to start ARP client"))).Times(1); |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 290 | EXPECT_CALL(metrics_, SendEnumToUMA( |
| 291 | HasSubstr("LinkMonitorFailure"), Metrics::kLinkMonitorClientStartFailure, |
| 292 | _)); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 293 | EXPECT_CALL(device_info_, GetMACAddress(0, _)).WillOnce(Return(true)); |
| 294 | EXPECT_CALL(monitor_, CreateClient()).WillOnce(Return(false)); |
| 295 | EXPECT_FALSE(monitor_.Start()); |
| 296 | ExpectReset(); |
| 297 | } |
| 298 | |
| 299 | TEST_F(LinkMonitorTest, StartFailedTransmitRequest) { |
| 300 | ScopedMockLog log; |
| 301 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 302 | EXPECT_CALL(log, |
| 303 | Log(logging::LOG_ERROR, _, |
| 304 | HasSubstr("Failed to send ARP"))).Times(1); |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 305 | EXPECT_CALL(metrics_, SendEnumToUMA( |
| 306 | HasSubstr("LinkMonitorFailure"), Metrics::kLinkMonitorTransmitFailure, |
| 307 | _)); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 308 | EXPECT_CALL(device_info_, GetMACAddress(0, _)).WillOnce(Return(true)); |
| 309 | EXPECT_CALL(monitor_, CreateClient()) |
| 310 | .WillOnce(Invoke(this, &LinkMonitorTest::CreateMockClient)); |
| 311 | EXPECT_CALL(*next_client_, TransmitRequest(_)).WillOnce(Return(false)); |
| 312 | EXPECT_FALSE(monitor_.Start()); |
| 313 | ExpectReset(); |
| 314 | } |
| 315 | |
| 316 | TEST_F(LinkMonitorTest, StartSuccess) { |
| 317 | StartMonitor(); |
| 318 | } |
| 319 | |
| 320 | TEST_F(LinkMonitorTest, Stop) { |
| 321 | StartMonitor(); |
| 322 | monitor_.Stop(); |
| 323 | ExpectReset(); |
| 324 | } |
| 325 | |
| 326 | TEST_F(LinkMonitorTest, ReplyReception) { |
| 327 | StartMonitor(); |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 328 | const int kResponseTime = 1234; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 329 | AdvanceTime(kResponseTime); |
| 330 | ScopedMockLog log; |
| 331 | |
| 332 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 333 | EXPECT_CALL(log, Log(_, _, HasSubstr("not for our IP"))).Times(1); |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 334 | ReceiveResponse(gateway_ip_, gateway_mac_, gateway_ip_, local_mac_); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 335 | Mock::VerifyAndClearExpectations(&log); |
| 336 | |
| 337 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 338 | EXPECT_CALL(log, Log(_, _, HasSubstr("not for our MAC"))).Times(1); |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 339 | ReceiveResponse(gateway_ip_, gateway_mac_, local_ip_, gateway_mac_); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 340 | Mock::VerifyAndClearExpectations(&log); |
| 341 | |
| 342 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 343 | EXPECT_CALL(log, Log(_, _, HasSubstr("not from the gateway"))).Times(1); |
Paul Stewart | 9f7823e | 2012-08-09 10:58:26 -0700 | [diff] [blame] | 344 | ReceiveResponse(local_ip_, gateway_mac_, local_ip_, local_mac_); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 345 | Mock::VerifyAndClearExpectations(&log); |
| 346 | |
| 347 | EXPECT_TRUE(GetArpClient()); |
| 348 | EXPECT_FALSE(monitor_.GetResponseTimeMilliseconds()); |
| 349 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 350 | EXPECT_CALL(log, Log(_, _, HasSubstr("Found gateway"))).Times(1); |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 351 | EXPECT_CALL(metrics_, SendToUMA( |
| 352 | HasSubstr("LinkMonitorResponseTimeSample"), kResponseTime, |
| 353 | _, _, _)).Times(1); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 354 | ReceiveCorrectResponse(); |
| 355 | EXPECT_FALSE(GetArpClient()); |
| 356 | EXPECT_EQ(kResponseTime, monitor_.GetResponseTimeMilliseconds()); |
| 357 | EXPECT_TRUE(IsUnicast()); |
| 358 | } |
| 359 | |
| 360 | TEST_F(LinkMonitorTest, TimeoutBroadcast) { |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 361 | EXPECT_CALL(metrics_, SendToUMA( |
| 362 | HasSubstr("LinkMonitorResponseTimeSample"), GetTestPeriodMilliseconds(), |
| 363 | _, _, _)).Times(GetFailureThreshold()); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 364 | StartMonitor(); |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 365 | // This value doesn't match real life (the timer in this scenario should |
| 366 | // advance by LinkMonitor::kTestPeriodMilliseconds), but this demonstrates |
| 367 | // the LinkMonitorSecondsToFailure independent from the response-time |
| 368 | // figures. |
| 369 | const int kTimeIncrement = 1000; |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 370 | for (int i = 1; i < GetFailureThreshold(); ++i) { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 371 | ExpectTransmit(false); |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 372 | AdvanceTime(kTimeIncrement); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 373 | TriggerRequestTimer(); |
| 374 | EXPECT_FALSE(IsUnicast()); |
| 375 | EXPECT_EQ(i, GetBroadcastFailureCount()); |
| 376 | EXPECT_EQ(0, GetUnicastFailureCount()); |
| 377 | EXPECT_EQ(GetTestPeriodMilliseconds(), |
| 378 | monitor_.GetResponseTimeMilliseconds()); |
| 379 | } |
| 380 | ScopedMockLog log; |
| 381 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 382 | EXPECT_CALL(log, |
| 383 | Log(logging::LOG_ERROR, _, |
| 384 | HasSubstr("monitor has reached the failure threshold"))).Times(1); |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 385 | EXPECT_CALL(metrics_, SendEnumToUMA( |
| 386 | HasSubstr("LinkMonitorFailure"), |
| 387 | Metrics::kLinkMonitorFailureThresholdReached, _)); |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 388 | EXPECT_CALL(metrics_, SendToUMA( |
| 389 | HasSubstr("LinkMonitorSecondsToFailure"), |
| 390 | kTimeIncrement * GetFailureThreshold() / 1000, _, _, _)); |
| 391 | EXPECT_CALL(metrics_, SendToUMA( |
| 392 | HasSubstr("BroadcastErrorsAtFailure"), GetFailureThreshold(), _, _, _)); |
| 393 | EXPECT_CALL(metrics_, SendToUMA( |
| 394 | HasSubstr("UnicastErrorsAtFailure"), 0, _, _, _)); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 395 | EXPECT_FALSE(GetSendRequestCallback().IsCancelled()); |
| 396 | ExpectNoTransmit(); |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 397 | EXPECT_CALL(monitor_, FailureCallbackHandler()); |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 398 | AdvanceTime(kTimeIncrement); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 399 | TriggerRequestTimer(); |
| 400 | ExpectReset(); |
| 401 | } |
| 402 | |
| 403 | TEST_F(LinkMonitorTest, TimeoutUnicast) { |
| 404 | StartMonitor(); |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 405 | // Successful broadcast receptions. |
| 406 | EXPECT_CALL(metrics_, SendToUMA( |
| 407 | HasSubstr("LinkMonitorResponseTimeSample"), 0, _, _, _)) |
| 408 | .Times(GetFailureThreshold()); |
| 409 | // Unsuccessful unicast receptions. |
| 410 | EXPECT_CALL(metrics_, SendToUMA( |
| 411 | HasSubstr("LinkMonitorResponseTimeSample"), GetTestPeriodMilliseconds(), |
| 412 | _, _, _)).Times(GetFailureThreshold()); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 413 | ReceiveCorrectResponse(); |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 414 | for (int i = 1; i < GetFailureThreshold(); ++i) { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 415 | // Failed unicast ARP. |
| 416 | ExpectTransmit(true); |
| 417 | TriggerRequestTimer(); |
| 418 | |
| 419 | // Successful broadcast ARP. |
| 420 | ExpectTransmit(false); |
| 421 | TriggerRequestTimer(); |
| 422 | ReceiveCorrectResponse(); |
| 423 | |
| 424 | EXPECT_EQ(0, GetBroadcastFailureCount()); |
| 425 | EXPECT_EQ(i, GetUnicastFailureCount()); |
| 426 | } |
| 427 | // Last unicast ARP transmission. |
| 428 | ExpectTransmit(true); |
| 429 | TriggerRequestTimer(); |
| 430 | |
| 431 | ScopedMockLog log; |
| 432 | EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber()); |
| 433 | EXPECT_CALL(log, |
| 434 | Log(logging::LOG_ERROR, _, |
| 435 | HasSubstr("monitor has reached the failure threshold"))).Times(1); |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 436 | EXPECT_CALL(metrics_, SendEnumToUMA( |
| 437 | HasSubstr("LinkMonitorFailure"), |
| 438 | Metrics::kLinkMonitorFailureThresholdReached, _)); |
Paul Stewart | 0443aa5 | 2012-08-09 10:43:50 -0700 | [diff] [blame] | 439 | EXPECT_CALL(metrics_, SendToUMA( |
| 440 | HasSubstr("LinkMonitorSecondsToFailure"), 0, _, _, _)); |
| 441 | EXPECT_CALL(metrics_, SendToUMA( |
| 442 | HasSubstr("BroadcastErrorsAtFailure"), 0, _, _, _)); |
| 443 | EXPECT_CALL(metrics_, SendToUMA( |
| 444 | HasSubstr("UnicastErrorsAtFailure"), GetFailureThreshold(), _, _, _)); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 445 | EXPECT_FALSE(GetSendRequestCallback().IsCancelled()); |
| 446 | ExpectNoTransmit(); |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 447 | EXPECT_CALL(monitor_, FailureCallbackHandler()); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 448 | TriggerRequestTimer(); |
| 449 | ExpectReset(); |
| 450 | } |
| 451 | |
| 452 | TEST_F(LinkMonitorTest, Average) { |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 453 | const int kSamples[] = { 200, 950, 1200, 4096, 5000, |
| 454 | 86, 120, 3060, 842, 750 }; |
| 455 | const size_t filter_depth = GetMaxResponseSampleFilterDepth(); |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 456 | EXPECT_CALL(metrics_, SendToUMA( |
| 457 | HasSubstr("LinkMonitorResponseTimeSample"), _, _, _, _)) |
| 458 | .Times(arraysize(kSamples)); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 459 | ASSERT_GT(arraysize(kSamples), filter_depth); |
| 460 | StartMonitor(); |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 461 | size_t i = 0; |
| 462 | int sum = 0; |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 463 | for (; i < filter_depth; ++i) { |
| 464 | AdvanceTime(kSamples[i]); |
| 465 | ReceiveCorrectResponse(); |
| 466 | sum += kSamples[i]; |
| 467 | EXPECT_EQ(sum / (i + 1), monitor_.GetResponseTimeMilliseconds()); |
| 468 | SendNextRequest(); |
| 469 | } |
| 470 | for (; i < arraysize(kSamples); ++i) { |
| 471 | AdvanceTime(kSamples[i]); |
| 472 | ReceiveCorrectResponse(); |
| 473 | sum = (sum + kSamples[i]) * filter_depth / (filter_depth + 1); |
| 474 | EXPECT_EQ(sum / filter_depth, monitor_.GetResponseTimeMilliseconds()); |
| 475 | SendNextRequest(); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | TEST_F(LinkMonitorTest, ImpulseResponse) { |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 480 | const int kNormalValue = 50; |
| 481 | const int kExceptionalValue = 5000; |
| 482 | const int filter_depth = GetMaxResponseSampleFilterDepth(); |
Paul Stewart | ff845fc | 2012-08-07 07:28:44 -0700 | [diff] [blame] | 483 | EXPECT_CALL(metrics_, SendToUMA( |
| 484 | HasSubstr("LinkMonitorResponseTimeSample"), _, _, _, _)) |
| 485 | .Times(AnyNumber()); |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 486 | StartMonitor(); |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 487 | for (int i = 0; i < filter_depth * 2; ++i) { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 488 | AdvanceTime(kNormalValue); |
| 489 | ReceiveCorrectResponse(); |
| 490 | EXPECT_EQ(kNormalValue, monitor_.GetResponseTimeMilliseconds()); |
| 491 | SendNextRequest(); |
| 492 | } |
| 493 | AdvanceTime(kExceptionalValue); |
| 494 | ReceiveCorrectResponse(); |
| 495 | // Our expectation is that an impulse input will be a |
| 496 | // impulse_height / (filter_depth + 1) increase to the running average. |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 497 | int expected_impulse_response = |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 498 | kNormalValue + (kExceptionalValue - kNormalValue) / (filter_depth + 1); |
| 499 | EXPECT_EQ(expected_impulse_response, monitor_.GetResponseTimeMilliseconds()); |
| 500 | SendNextRequest(); |
| 501 | |
| 502 | // From here, if we end up continuing to receive normal values, our |
| 503 | // running average should decay backwards to the normal value. |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 504 | const int failsafe = 100; |
| 505 | int last_value = monitor_.GetResponseTimeMilliseconds(); |
| 506 | for (int i = 0; i < failsafe && last_value != kNormalValue; ++i) { |
Paul Stewart | 6c72c97 | 2012-07-27 11:29:20 -0700 | [diff] [blame] | 507 | AdvanceTime(kNormalValue); |
| 508 | ReceiveCorrectResponse(); |
| 509 | // We should advance monotonically (but not necessarily linearly) |
| 510 | // back towards the normal value. |
| 511 | EXPECT_GE(last_value, monitor_.GetResponseTimeMilliseconds()); |
| 512 | SendNextRequest(); |
| 513 | last_value = monitor_.GetResponseTimeMilliseconds(); |
| 514 | } |
| 515 | EXPECT_EQ(kNormalValue, last_value); |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Paul Stewart | f1961f8 | 2012-09-11 20:45:39 -0700 | [diff] [blame] | 518 | TEST_F(LinkMonitorTest, HardwareAddressToString) { |
| 519 | const uint8 address0[] = { 0, 1, 2, 3, 4, 5 }; |
| 520 | EXPECT_EQ("00:01:02:03:04:05", |
| 521 | HardwareAddressToString(ByteString(address0, arraysize(address0)))); |
| 522 | const uint8 address1[] = { 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd }; |
| 523 | EXPECT_EQ("88:99:aa:bb:cc:dd", |
| 524 | HardwareAddressToString(ByteString(address1, arraysize(address1)))); |
| 525 | } |
| 526 | |
Paul Stewart | 3f43f43 | 2012-07-16 12:12:45 -0700 | [diff] [blame] | 527 | } // namespace shill |