| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/p2p/base/stunrequest.h" | 11 #include "webrtc/p2p/base/stunrequest.h" |
| 12 #include "webrtc/base/fakeclock.h" |
| 12 #include "webrtc/base/gunit.h" | 13 #include "webrtc/base/gunit.h" |
| 13 #include "webrtc/base/helpers.h" | 14 #include "webrtc/base/helpers.h" |
| 14 #include "webrtc/base/logging.h" | 15 #include "webrtc/base/logging.h" |
| 15 #include "webrtc/base/ssladapter.h" | 16 #include "webrtc/base/ssladapter.h" |
| 16 #include "webrtc/base/timeutils.h" | 17 #include "webrtc/base/timeutils.h" |
| 17 | 18 |
| 18 using namespace cricket; | 19 using namespace cricket; |
| 19 | 20 |
| 20 // STUN timeout (with all retries) is 9500ms. | |
| 21 // Add some margin of error for slow bots. | |
| 22 // TODO(deadbeef): Use simulated clock instead of just increasing timeouts to | |
| 23 // fix flaky tests. | |
| 24 static const int kTimeoutMs = 15000; | |
| 25 | |
| 26 class StunRequestTest : public testing::Test, | 21 class StunRequestTest : public testing::Test, |
| 27 public sigslot::has_slots<> { | 22 public sigslot::has_slots<> { |
| 28 public: | 23 public: |
| 29 StunRequestTest() | 24 StunRequestTest() |
| 30 : manager_(rtc::Thread::Current()), | 25 : manager_(rtc::Thread::Current()), |
| 31 request_count_(0), response_(NULL), | 26 request_count_(0), response_(NULL), |
| 32 success_(false), failure_(false), timeout_(false) { | 27 success_(false), failure_(false), timeout_(false) { |
| 33 manager_.SignalSendPacket.connect(this, &StunRequestTest::OnSendPacket); | 28 manager_.SignalSendPacket.connect(this, &StunRequestTest::OnSendPacket); |
| 34 } | 29 } |
| 35 | 30 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 EXPECT_TRUE(response_ == NULL); | 138 EXPECT_TRUE(response_ == NULL); |
| 144 EXPECT_FALSE(success_); | 139 EXPECT_FALSE(success_); |
| 145 EXPECT_FALSE(failure_); | 140 EXPECT_FALSE(failure_); |
| 146 EXPECT_FALSE(timeout_); | 141 EXPECT_FALSE(timeout_); |
| 147 delete res; | 142 delete res; |
| 148 } | 143 } |
| 149 | 144 |
| 150 // Test that requests are sent at the right times, and that the 9th request | 145 // Test that requests are sent at the right times, and that the 9th request |
| 151 // (sent at 7900 ms) can be properly replied to. | 146 // (sent at 7900 ms) can be properly replied to. |
| 152 TEST_F(StunRequestTest, TestBackoff) { | 147 TEST_F(StunRequestTest, TestBackoff) { |
| 148 const int MAX_TIMEOUT_MS = 10000; |
| 149 rtc::ScopedFakeClock fake_clock; |
| 153 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL); | 150 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL); |
| 154 | 151 |
| 155 int64_t start = rtc::TimeMillis(); | 152 int64_t start = rtc::TimeMillis(); |
| 156 manager_.Send(new StunRequestThunker(req, this)); | 153 manager_.Send(new StunRequestThunker(req, this)); |
| 157 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req); | 154 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req); |
| 158 for (int i = 0; i < 9; ++i) { | 155 for (int i = 0; i < 9; ++i) { |
| 159 while (request_count_ == i) | 156 EXPECT_TRUE_SIMULATED_WAIT(request_count_ != i, MAX_TIMEOUT_MS, fake_clock); |
| 160 rtc::Thread::Current()->ProcessMessages(1); | |
| 161 int64_t elapsed = rtc::TimeMillis() - start; | 157 int64_t elapsed = rtc::TimeMillis() - start; |
| 162 LOG(LS_INFO) << "STUN request #" << (i + 1) | 158 LOG(LS_INFO) << "STUN request #" << (i + 1) |
| 163 << " sent at " << elapsed << " ms"; | 159 << " sent at " << elapsed << " ms"; |
| 164 EXPECT_GE(TotalDelay(i + 1), elapsed); | 160 EXPECT_EQ(TotalDelay(i), elapsed); |
| 165 } | 161 } |
| 166 EXPECT_TRUE(manager_.CheckResponse(res)); | 162 EXPECT_TRUE(manager_.CheckResponse(res)); |
| 167 | 163 |
| 168 EXPECT_TRUE(response_ == res); | 164 EXPECT_TRUE(response_ == res); |
| 169 EXPECT_TRUE(success_); | 165 EXPECT_TRUE(success_); |
| 170 EXPECT_FALSE(failure_); | 166 EXPECT_FALSE(failure_); |
| 171 EXPECT_FALSE(timeout_); | 167 EXPECT_FALSE(timeout_); |
| 172 delete res; | 168 delete res; |
| 173 } | 169 } |
| 174 | 170 |
| 175 // Test that we timeout properly if no response is received in 9500 ms. | 171 // Test that we timeout properly if no response is received in 9500 ms. |
| 176 TEST_F(StunRequestTest, TestTimeout) { | 172 TEST_F(StunRequestTest, TestTimeout) { |
| 173 rtc::ScopedFakeClock fake_clock; |
| 177 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL); | 174 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL); |
| 178 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req); | 175 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req); |
| 179 | 176 |
| 180 manager_.Send(new StunRequestThunker(req, this)); | 177 manager_.Send(new StunRequestThunker(req, this)); |
| 181 rtc::Thread::Current()->ProcessMessages(kTimeoutMs); | 178 // Simulate the 9500 ms STUN timeout |
| 179 SIMULATED_WAIT(false, 9500, fake_clock); |
| 180 |
| 182 EXPECT_FALSE(manager_.CheckResponse(res)); | 181 EXPECT_FALSE(manager_.CheckResponse(res)); |
| 183 | |
| 184 EXPECT_TRUE(response_ == NULL); | 182 EXPECT_TRUE(response_ == NULL); |
| 185 EXPECT_FALSE(success_); | 183 EXPECT_FALSE(success_); |
| 186 EXPECT_FALSE(failure_); | 184 EXPECT_FALSE(failure_); |
| 187 EXPECT_TRUE(timeout_); | 185 EXPECT_TRUE(timeout_); |
| 188 delete res; | 186 delete res; |
| 189 } | 187 } |
| 190 | 188 |
| 191 // Regression test for specific crash where we receive a response with the | 189 // Regression test for specific crash where we receive a response with the |
| 192 // same id as a request that doesn't have an underlying StunMessage yet. | 190 // same id as a request that doesn't have an underlying StunMessage yet. |
| 193 TEST_F(StunRequestTest, TestNoEmptyRequest) { | 191 TEST_F(StunRequestTest, TestNoEmptyRequest) { |
| 194 StunRequestThunker* request = new StunRequestThunker(this); | 192 StunRequestThunker* request = new StunRequestThunker(this); |
| 195 | 193 |
| 196 manager_.SendDelayed(request, 100); | 194 manager_.SendDelayed(request, 100); |
| 197 | 195 |
| 198 StunMessage dummy_req; | 196 StunMessage dummy_req; |
| 199 dummy_req.SetTransactionID(request->id()); | 197 dummy_req.SetTransactionID(request->id()); |
| 200 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, &dummy_req); | 198 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, &dummy_req); |
| 201 | 199 |
| 202 EXPECT_TRUE(manager_.CheckResponse(res)); | 200 EXPECT_TRUE(manager_.CheckResponse(res)); |
| 203 | 201 |
| 204 EXPECT_TRUE(response_ == res); | 202 EXPECT_TRUE(response_ == res); |
| 205 EXPECT_TRUE(success_); | 203 EXPECT_TRUE(success_); |
| 206 EXPECT_FALSE(failure_); | 204 EXPECT_FALSE(failure_); |
| 207 EXPECT_FALSE(timeout_); | 205 EXPECT_FALSE(timeout_); |
| 208 delete res; | 206 delete res; |
| 209 } | 207 } |
| OLD | NEW |