blob: cd24cd2fdcd25cdd42b2646a946eed693cd4d49e [file] [log] [blame]
Samuel Tanf66080e2015-06-18 15:53:00 -07001// Copyright 2015 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/icmp_session.h"
6
7#include <base/test/simple_test_tick_clock.h>
8#include <gtest/gtest.h>
9
10#include "shill/mock_event_dispatcher.h"
11#include "shill/mock_icmp.h"
12#include "shill/net/ip_address.h"
13
14using base::Bind;
15using base::Unretained;
16using testing::_;
17using testing::NiceMock;
18using testing::Return;
19using testing::StrictMock;
20using testing::Test;
21
22namespace shill {
23
24namespace {
25
26// ICMP echo replies with 0 bytes of data and and echo ID 0. Sequence numbers
27// are 0x8, 0x9, and 0xa respectively to simulate replies to a sequence of sent
28// echo requests.
29const uint8_t kIcmpEchoReply1[] = {0x00, 0x00, 0xf7, 0xff,
30 0x00, 0x00, 0x08, 0x00};
31const uint16_t kIcmpEchoReply1_SeqNum = 0x08;
32const uint8_t kIcmpEchoReply2[] = {0x00, 0x00, 0xf6, 0xff,
33 0x00, 0x00, 0x09, 0x00};
34const uint16_t kIcmpEchoReply2_SeqNum = 0x09;
35const uint8_t kIcmpEchoReply3[] = {0x00, 0x00, 0xf5, 0xff,
36 0x00, 0x00, 0x0a, 0x00};
37const uint16_t kIcmpEchoReply3_SeqNum = 0x0a;
38
39// This ICMP echo reply has an echo ID of 0xe, which is different from the
40// echo ID used in the unit tests (0).
41const uint8_t kIcmpEchoReplyDifferentEchoID[] = {0x00, 0x00, 0xea, 0xff,
42 0x0e, 0x00, 0x0b, 0x00};
43
44} // namespace
45
46MATCHER_P(IsIPAddress, address, "") {
47 // IPAddress objects don't support the "==" operator as per style, so we need
48 // a custom matcher.
49 return address.Equals(arg);
50}
51
52class IcmpSessionTest : public Test {
53 public:
54 IcmpSessionTest() : icmp_session_(&dispatcher_) {}
55 virtual ~IcmpSessionTest() {}
56
57 virtual void SetUp() {
58 icmp_session_.tick_clock_ = &testing_clock_;
59 icmp_ = new NiceMock<MockIcmp>();
60 // Passes ownership.
61 icmp_session_.icmp_.reset(icmp_);
62 ON_CALL(*icmp_, IsStarted()).WillByDefault(Return(false));
63 }
64
65 virtual void TearDown() {
66 EXPECT_CALL(*icmp_, IsStarted());
67 IcmpSession::kNextUniqueEchoId = 0;
68 }
69
70 MOCK_METHOD1(ResultCallback, void(const IcmpSession::IcmpSessionResult&));
71
72 protected:
73 static const char kIPAddress[];
74
75 void StartAndVerify(const IPAddress& destination) {
76 EXPECT_CALL(*icmp_, IsStarted());
77 EXPECT_CALL(*icmp_, Start()).WillOnce(Return(true));
78 EXPECT_CALL(dispatcher_, CreateInputHandler(icmp_->socket(), _, _));
79 EXPECT_CALL(dispatcher_, PostDelayedTask(_, GetTimeoutSeconds() * 1000));
80 EXPECT_CALL(dispatcher_, PostTask(_));
81 EXPECT_TRUE(Start(destination));
82 EXPECT_TRUE(GetSeqNumToSentRecvTime()->empty());
83 EXPECT_TRUE(GetReceivedEchoReplySeqNumbers()->empty());
84 EXPECT_CALL(*icmp_, IsStarted()).WillRepeatedly(Return(true));
85 }
86
87 bool Start(const IPAddress& destination) {
88 return icmp_session_.Start(
89 destination, Bind(&IcmpSessionTest::ResultCallback, Unretained(this)));
90 }
91
92 void Stop() {
93 icmp_session_.Stop();
94 }
95
96 bool SeqNumToSentRecvTimeContains(uint16_t seq_num) {
97 return icmp_session_.seq_num_to_sent_recv_time_.find(seq_num) !=
98 icmp_session_.seq_num_to_sent_recv_time_.end();
99 }
100
101 bool ReceivedEchoReplySeqNumbersContains(uint16_t seq_num) {
102 return icmp_session_.received_echo_reply_seq_numbers_.find(seq_num) !=
103 icmp_session_.received_echo_reply_seq_numbers_.end();
104 }
105
106 void TransmitEchoRequestTask(const IPAddress& destination,
107 bool transmit_request_success) {
108 EXPECT_CALL(*icmp_, TransmitEchoRequest(IsIPAddress(destination),
109 icmp_session_.echo_id_,
110 GetCurrentSequenceNumber()))
111 .WillOnce(Return(transmit_request_success));
112 icmp_session_.TransmitEchoRequestTask(destination);
113 }
114
115 void VerifyIcmpSessionStopped() {
116 EXPECT_TRUE(icmp_session_.timeout_callback_.IsCancelled());
117 EXPECT_TRUE(icmp_session_.result_callback_.is_null());
118 EXPECT_FALSE(icmp_session_.echo_reply_handler_);
119 }
120
121 void OnEchoReplyReceived(InputData* data) {
122 icmp_session_.OnEchoReplyReceived(data);
123 }
124
125 IcmpSession::IcmpSessionResult GenerateIcmpResult() {
126 return icmp_session_.GenerateIcmpResult();
127 }
128
129 std::map<uint16_t, IcmpSession::SentRecvTimePair>* GetSeqNumToSentRecvTime() {
130 return &icmp_session_.seq_num_to_sent_recv_time_;
131 }
132 std::set<uint16_t>* GetReceivedEchoReplySeqNumbers() {
133 return &icmp_session_.received_echo_reply_seq_numbers_;
134 }
135 uint16_t GetNextUniqueEchoId() const {
136 return IcmpSession::kNextUniqueEchoId;
137 }
138 int GetTotalNumEchoRequests() const {
139 return IcmpSession::kTotalNumEchoRequests;
140 }
141 int GetCurrentSequenceNumber() const {
142 return icmp_session_.current_sequence_number_;
143 }
144 void SetCurrentSequenceNumber(uint16_t val) {
145 icmp_session_.current_sequence_number_ = val;
146 }
147 size_t GetTimeoutSeconds() const { return IcmpSession::kTimeoutSeconds; }
148 int GetEchoRequestIntervalSeconds() const {
149 return IcmpSession::kEchoRequestIntervalSeconds;
150 }
151
152 MockIcmp* icmp_;
153 StrictMock<MockEventDispatcher> dispatcher_;
154 IcmpSession icmp_session_;
155 base::SimpleTestTickClock testing_clock_;
156};
157
158const char IcmpSessionTest::kIPAddress[] = "10.0.1.1";
159
160TEST_F(IcmpSessionTest, Constructor) {
161 // |icmp_session_| should have been assigned the value of |kNextUniqueEchoId|
162 // on construction, and caused the value of this static variable to be
163 // incremented.
164 uint16_t saved_echo_id = GetNextUniqueEchoId();
165 EXPECT_EQ(saved_echo_id - 1, icmp_session_.echo_id_);
166
167 // The next IcmpSession object constructed, |session| should get the next
168 // unique value of |kNextUniqueEchoId|, and further increment this variable.
169 IcmpSession session(&dispatcher_);
170 EXPECT_EQ(saved_echo_id, session.echo_id_);
171 EXPECT_EQ(saved_echo_id + 1, GetNextUniqueEchoId());
172}
173
174TEST_F(IcmpSessionTest, StartWhileAlreadyStarted) {
175 IPAddress ipv4_destination(IPAddress::kFamilyIPv4);
176 EXPECT_TRUE(ipv4_destination.SetAddressFromString(kIPAddress));
177 StartAndVerify(ipv4_destination);
178
179 // Since an ICMP session is already started, we should fail to start it again.
180 EXPECT_CALL(*icmp_, Start()).Times(0);
181 EXPECT_CALL(dispatcher_, CreateInputHandler(_, _, _)).Times(0);
182 EXPECT_CALL(dispatcher_, PostDelayedTask(_, _)).Times(0);
183 EXPECT_CALL(dispatcher_, PostTask(_)).Times(0);
184 EXPECT_FALSE(Start(ipv4_destination));
185}
186
187TEST_F(IcmpSessionTest, StopWhileNotStarted) {
188 // Attempting to stop the ICMP session while it is not started should do
189 // nothing.
190 EXPECT_CALL(*icmp_, IsStarted()).WillOnce(Return(false));
191 EXPECT_CALL(*this, ResultCallback(_)).Times(0);
192 EXPECT_CALL(*icmp_, Stop()).Times(0);
193 Stop();
194}
195
196TEST_F(IcmpSessionTest, SessionSuccess) {
197 // Test a successful ICMP session where the sending of requests and receiving
198 // of replies are interleaved. Moreover, test the case where transmitting an
199 // echo request fails.
200
201 base::TimeTicks now = testing_clock_.NowTicks();
202 base::TimeTicks kSentTime1 = base::TimeTicks::FromInternalValue(10);
203 base::TimeTicks kRecvTime1 = base::TimeTicks::FromInternalValue(20);
204 base::TimeTicks kSentTime2 = base::TimeTicks::FromInternalValue(30);
205 base::TimeTicks kSentTime3 = base::TimeTicks::FromInternalValue(40);
206 base::TimeTicks kRecvTime2 = base::TimeTicks::FromInternalValue(50);
207 base::TimeTicks kWrongEchoIDRecvTime = base::TimeTicks::FromInternalValue(60);
208 base::TimeTicks kRecvTime3 = base::TimeTicks::FromInternalValue(70);
209
210 IcmpSession::IcmpSessionResult expected_result;
211 expected_result.push_back(kRecvTime1 - kSentTime1);
212 expected_result.push_back(kRecvTime2 - kSentTime2);
213 expected_result.push_back(kRecvTime3 - kSentTime3);
214
215 // Initiate session.
216 IPAddress ipv4_destination(IPAddress::kFamilyIPv4);
217 EXPECT_TRUE(ipv4_destination.SetAddressFromString(kIPAddress));
218 StartAndVerify(ipv4_destination);
219
220 // Send the first echo request.
221 testing_clock_.Advance(kSentTime1 - now);
222 now = testing_clock_.NowTicks();
223 SetCurrentSequenceNumber(kIcmpEchoReply1_SeqNum);
224 EXPECT_CALL(dispatcher_,
225 PostDelayedTask(_, GetEchoRequestIntervalSeconds() * 1000));
226 TransmitEchoRequestTask(ipv4_destination, true);
227 EXPECT_TRUE(GetReceivedEchoReplySeqNumbers()->empty());
228 EXPECT_EQ(1, GetSeqNumToSentRecvTime()->size());
229 EXPECT_TRUE(SeqNumToSentRecvTimeContains(kIcmpEchoReply1_SeqNum));
230 EXPECT_EQ(now, GetSeqNumToSentRecvTime()->at(kIcmpEchoReply1_SeqNum).first);
231 EXPECT_EQ(kIcmpEchoReply2_SeqNum, GetCurrentSequenceNumber());
232
233 // Receive first reply.
234 testing_clock_.Advance(kRecvTime1 - now);
235 now = testing_clock_.NowTicks();
236 uint8_t buffer_1[sizeof(kIcmpEchoReply1)];
237 memcpy(buffer_1, kIcmpEchoReply1, sizeof(kIcmpEchoReply1));
238 InputData data_1(reinterpret_cast<unsigned char*>(buffer_1),
239 sizeof(buffer_1));
240 EXPECT_CALL(*this, ResultCallback(_)).Times(0);
241 OnEchoReplyReceived(&data_1);
242 EXPECT_EQ(1, GetReceivedEchoReplySeqNumbers()->size());
243 EXPECT_TRUE(ReceivedEchoReplySeqNumbersContains(kIcmpEchoReply1_SeqNum));
244
245 // Send the second echo request.
246 testing_clock_.Advance(kSentTime2 - now);
247 now = testing_clock_.NowTicks();
248 EXPECT_CALL(dispatcher_,
249 PostDelayedTask(_, GetEchoRequestIntervalSeconds() * 1000));
250 TransmitEchoRequestTask(ipv4_destination, true);
251 EXPECT_EQ(1, GetReceivedEchoReplySeqNumbers()->size());
252 EXPECT_EQ(2, GetSeqNumToSentRecvTime()->size());
253 EXPECT_TRUE(SeqNumToSentRecvTimeContains(kIcmpEchoReply2_SeqNum));
254 EXPECT_EQ(now, GetSeqNumToSentRecvTime()->at(kIcmpEchoReply2_SeqNum).first);
255 EXPECT_EQ(kIcmpEchoReply3_SeqNum, GetCurrentSequenceNumber());
256
257 // Sending final request.
258 testing_clock_.Advance(kSentTime3 - now);
259 now = testing_clock_.NowTicks();
260 EXPECT_CALL(dispatcher_, PostDelayedTask(_, _)).Times(0);
261 EXPECT_CALL(*icmp_, Stop()).Times(0);
262 TransmitEchoRequestTask(ipv4_destination, true);
263 EXPECT_EQ(1, GetReceivedEchoReplySeqNumbers()->size());
264 EXPECT_EQ(3, GetSeqNumToSentRecvTime()->size());
265 EXPECT_TRUE(SeqNumToSentRecvTimeContains(kIcmpEchoReply3_SeqNum));
266 EXPECT_EQ(now, GetSeqNumToSentRecvTime()->at(kIcmpEchoReply3_SeqNum).first);
267 EXPECT_EQ(kIcmpEchoReply3_SeqNum + 1, GetCurrentSequenceNumber());
268
269 // Receive second reply.
270 testing_clock_.Advance(kRecvTime2 - now);
271 now = testing_clock_.NowTicks();
272 uint8_t buffer_2[sizeof(kIcmpEchoReply2)];
273 memcpy(buffer_2, kIcmpEchoReply2, sizeof(kIcmpEchoReply2));
274 InputData data_2(reinterpret_cast<unsigned char*>(buffer_2),
275 sizeof(buffer_2));
276 EXPECT_CALL(*this, ResultCallback(_)).Times(0);
277 EXPECT_CALL(*icmp_, Stop()).Times(0);
278 OnEchoReplyReceived(&data_2);
279 EXPECT_EQ(3, GetSeqNumToSentRecvTime()->size());
280 EXPECT_EQ(2, GetReceivedEchoReplySeqNumbers()->size());
281 EXPECT_TRUE(ReceivedEchoReplySeqNumbersContains(kIcmpEchoReply2_SeqNum));
282
283 // Receive a reply that has an echo ID that does not match that of this
284 // ICMP session. This reply will not be processed.
285 testing_clock_.Advance(kWrongEchoIDRecvTime - now);
286 now = testing_clock_.NowTicks();
287 uint8_t buffer_3[sizeof(kIcmpEchoReplyDifferentEchoID)];
288 memcpy(buffer_3, kIcmpEchoReplyDifferentEchoID,
289 sizeof(kIcmpEchoReplyDifferentEchoID));
290 InputData data_3(reinterpret_cast<unsigned char*>(buffer_3),
291 sizeof(buffer_3));
292 EXPECT_CALL(*this, ResultCallback(_)).Times(0);
293 EXPECT_CALL(*icmp_, Stop()).Times(0);
294 OnEchoReplyReceived(&data_3);
295 EXPECT_EQ(3, GetSeqNumToSentRecvTime()->size());
296 EXPECT_EQ(2, GetReceivedEchoReplySeqNumbers()->size());
297
298 // Receive third reply, which concludes the ICMP session.
299 testing_clock_.Advance(kRecvTime3 - now);
300 now = testing_clock_.NowTicks();
301 uint8_t buffer_4[sizeof(kIcmpEchoReply3)];
302 memcpy(buffer_4, kIcmpEchoReply3, sizeof(kIcmpEchoReply3));
303 InputData data_4(reinterpret_cast<unsigned char*>(buffer_4),
304 sizeof(buffer_4));
305 EXPECT_CALL(*this, ResultCallback(expected_result));
306 EXPECT_CALL(*icmp_, Stop());
307 OnEchoReplyReceived(&data_4);
308 EXPECT_EQ(3, GetSeqNumToSentRecvTime()->size());
309 EXPECT_EQ(3, GetReceivedEchoReplySeqNumbers()->size());
310 EXPECT_TRUE(ReceivedEchoReplySeqNumbersContains(kIcmpEchoReply3_SeqNum));
311
312 VerifyIcmpSessionStopped();
313}
314
315TEST_F(IcmpSessionTest, SessionTimeoutOrInterrupted) {
316 // Test a failed ICMP session where we neither send out all echo requests nor
317 // receive all echo replies before stopping the ICMP session (because of a
318 // timeout or a manually-triggered stop). Moreover, test that echo requests
319 // that are sent unsuccessfully are sent again.
320
321 base::TimeTicks now = testing_clock_.NowTicks();
322 base::TimeTicks kSentTime1 = base::TimeTicks::FromInternalValue(10);
323 base::TimeTicks kSentTime2 = base::TimeTicks::FromInternalValue(20);
324 base::TimeTicks kRecvTime1 = base::TimeTicks::FromInternalValue(30);
325 base::TimeTicks kResendTime1 = base::TimeTicks::FromInternalValue(40);
326
327 IcmpSession::IcmpSessionResult expected_partial_result;
328 expected_partial_result.push_back(kRecvTime1 - kSentTime1);
329 expected_partial_result.push_back(base::TimeDelta());
330
331 // Initiate session.
332 IPAddress ipv4_destination(IPAddress::kFamilyIPv4);
333 EXPECT_TRUE(ipv4_destination.SetAddressFromString(kIPAddress));
334 StartAndVerify(ipv4_destination);
335
336 // Send the first echo request successfully.
337 testing_clock_.Advance(kSentTime1 - now);
338 now = testing_clock_.NowTicks();
339 SetCurrentSequenceNumber(kIcmpEchoReply1_SeqNum);
340 EXPECT_CALL(dispatcher_,
341 PostDelayedTask(_, GetEchoRequestIntervalSeconds() * 1000));
342 TransmitEchoRequestTask(ipv4_destination, true);
343 EXPECT_TRUE(GetReceivedEchoReplySeqNumbers()->empty());
344 EXPECT_EQ(1, GetSeqNumToSentRecvTime()->size());
345 EXPECT_TRUE(SeqNumToSentRecvTimeContains(kIcmpEchoReply1_SeqNum));
346 EXPECT_EQ(now, GetSeqNumToSentRecvTime()->at(kIcmpEchoReply1_SeqNum).first);
347 EXPECT_EQ(kIcmpEchoReply2_SeqNum, GetCurrentSequenceNumber());
348
349 // Send the second echo request unsuccessfully.
350 testing_clock_.Advance(kSentTime2 - now);
351 now = testing_clock_.NowTicks();
352 EXPECT_CALL(dispatcher_,
353 PostDelayedTask(_, GetEchoRequestIntervalSeconds() * 1000));
354 TransmitEchoRequestTask(ipv4_destination, false);
355 EXPECT_TRUE(GetReceivedEchoReplySeqNumbers()->empty());
356 EXPECT_EQ(1, GetSeqNumToSentRecvTime()->size());
357 EXPECT_FALSE(SeqNumToSentRecvTimeContains(kIcmpEchoReply2_SeqNum));
358 // The sequence number should still be incremented when we fail to transmit an
359 // echo request.
360 EXPECT_EQ(kIcmpEchoReply3_SeqNum, GetCurrentSequenceNumber());
361
362 // Receive first reply.
363 testing_clock_.Advance(kRecvTime1 - now);
364 now = testing_clock_.NowTicks();
365 uint8_t buffer_1[sizeof(kIcmpEchoReply1)];
366 memcpy(buffer_1, kIcmpEchoReply1, sizeof(kIcmpEchoReply1));
367 InputData data_1(reinterpret_cast<unsigned char*>(buffer_1),
368 sizeof(buffer_1));
369 EXPECT_CALL(*this, ResultCallback(_)).Times(0);
370 OnEchoReplyReceived(&data_1);
371 EXPECT_EQ(1, GetReceivedEchoReplySeqNumbers()->size());
372 EXPECT_TRUE(ReceivedEchoReplySeqNumbersContains(kIcmpEchoReply1_SeqNum));
373
374 // Resend second echo request successfully.
375 testing_clock_.Advance(kResendTime1 - now);
376 now = testing_clock_.NowTicks();
377 EXPECT_CALL(dispatcher_,
378 PostDelayedTask(_, GetEchoRequestIntervalSeconds() * 1000));
379 TransmitEchoRequestTask(ipv4_destination, true);
380 EXPECT_EQ(1, GetReceivedEchoReplySeqNumbers()->size());
381 EXPECT_EQ(2, GetSeqNumToSentRecvTime()->size());
382 EXPECT_TRUE(SeqNumToSentRecvTimeContains(kIcmpEchoReply3_SeqNum));
383 EXPECT_EQ(now, GetSeqNumToSentRecvTime()->at(kIcmpEchoReply3_SeqNum).first);
384 EXPECT_EQ(kIcmpEchoReply3_SeqNum + 1, GetCurrentSequenceNumber());
385
386 // Timeout triggered or session manually stopped, so we report partial
387 // results.
388 EXPECT_CALL(*this, ResultCallback(expected_partial_result));
389 EXPECT_CALL(*icmp_, Stop());
390 Stop();
391 EXPECT_EQ(2, GetSeqNumToSentRecvTime()->size());
392 EXPECT_EQ(1, GetReceivedEchoReplySeqNumbers()->size());
393
394 VerifyIcmpSessionStopped();
395}
396
Samuel Tan83375982015-06-30 14:35:20 -0700397TEST_F(IcmpSessionTest, AnyRepliesReceived) {
398 IcmpSession::IcmpSessionResult none_sent;
399 EXPECT_FALSE(IcmpSession::AnyRepliesReceived(none_sent));
400
401 IcmpSession::IcmpSessionResult two_sent_none_received;
402 two_sent_none_received.push_back(base::TimeDelta());
403 two_sent_none_received.push_back(base::TimeDelta());
404 EXPECT_FALSE(IcmpSession::AnyRepliesReceived(two_sent_none_received));
405
406 IcmpSession::IcmpSessionResult one_sent_one_received;
407 one_sent_one_received.push_back(base::TimeDelta::FromSeconds(10));
408 EXPECT_TRUE(IcmpSession::AnyRepliesReceived(one_sent_one_received));
409
410 IcmpSession::IcmpSessionResult two_sent_one_received;
411 two_sent_one_received.push_back(base::TimeDelta::FromSeconds(20));
412 two_sent_one_received.push_back(base::TimeDelta());
413 EXPECT_TRUE(IcmpSession::AnyRepliesReceived(two_sent_one_received));
414}
415
Samuel Tanf66080e2015-06-18 15:53:00 -0700416} // namespace shill