| 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 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 static StunMessage* CreateStunMessage(StunMessageType type, | 48 static StunMessage* CreateStunMessage(StunMessageType type, |
| 49 StunMessage* req) { | 49 StunMessage* req) { |
| 50 StunMessage* msg = new StunMessage(); | 50 StunMessage* msg = new StunMessage(); |
| 51 msg->SetType(type); | 51 msg->SetType(type); |
| 52 if (req) { | 52 if (req) { |
| 53 msg->SetTransactionID(req->transaction_id()); | 53 msg->SetTransactionID(req->transaction_id()); |
| 54 } | 54 } |
| 55 return msg; | 55 return msg; |
| 56 } | 56 } |
| 57 static int TotalDelay(int sends) { | 57 static int TotalDelay(int sends) { |
| 58 int total = 0; | 58 std::vector<int> delays = {0, 250, 750, 1750, 3750, |
| 59 for (int i = 0; i < sends; i++) { | 59 7750, 15750, 23750, 31750, 39750}; |
| 60 if (i < 4) | 60 return delays[sends]; |
| 61 total += 100 << i; | |
| 62 else | |
| 63 total += 1600; | |
| 64 } | |
| 65 return total; | |
| 66 } | 61 } |
| 67 | 62 |
| 68 StunRequestManager manager_; | 63 StunRequestManager manager_; |
| 69 int request_count_; | 64 int request_count_; |
| 70 StunMessage* response_; | 65 StunMessage* response_; |
| 71 bool success_; | 66 bool success_; |
| 72 bool failure_; | 67 bool failure_; |
| 73 bool timeout_; | 68 bool timeout_; |
| 74 }; | 69 }; |
| 75 | 70 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, NULL); | 130 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, NULL); |
| 136 EXPECT_FALSE(manager_.CheckResponse(res)); | 131 EXPECT_FALSE(manager_.CheckResponse(res)); |
| 137 | 132 |
| 138 EXPECT_TRUE(response_ == NULL); | 133 EXPECT_TRUE(response_ == NULL); |
| 139 EXPECT_FALSE(success_); | 134 EXPECT_FALSE(success_); |
| 140 EXPECT_FALSE(failure_); | 135 EXPECT_FALSE(failure_); |
| 141 EXPECT_FALSE(timeout_); | 136 EXPECT_FALSE(timeout_); |
| 142 delete res; | 137 delete res; |
| 143 } | 138 } |
| 144 | 139 |
| 145 // Test that requests are sent at the right times, and that the 9th request | 140 // Test that requests are sent at the right times. |
| 146 // (sent at 7900 ms) can be properly replied to. | |
| 147 TEST_F(StunRequestTest, TestBackoff) { | 141 TEST_F(StunRequestTest, TestBackoff) { |
| 148 const int MAX_TIMEOUT_MS = 10000; | |
| 149 rtc::ScopedFakeClock fake_clock; | 142 rtc::ScopedFakeClock fake_clock; |
| 150 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL); | 143 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL); |
| 151 | 144 |
| 152 int64_t start = rtc::TimeMillis(); | 145 int64_t start = rtc::TimeMillis(); |
| 153 manager_.Send(new StunRequestThunker(req, this)); | 146 manager_.Send(new StunRequestThunker(req, this)); |
| 154 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req); | 147 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req); |
| 155 for (int i = 0; i < 9; ++i) { | 148 for (int i = 0; i < 9; ++i) { |
| 156 EXPECT_TRUE_SIMULATED_WAIT(request_count_ != i, MAX_TIMEOUT_MS, fake_clock); | 149 EXPECT_TRUE_SIMULATED_WAIT(request_count_ != i, STUN_TOTAL_TIMEOUT, |
| 150 fake_clock); |
| 157 int64_t elapsed = rtc::TimeMillis() - start; | 151 int64_t elapsed = rtc::TimeMillis() - start; |
| 158 LOG(LS_INFO) << "STUN request #" << (i + 1) | 152 LOG(LS_INFO) << "STUN request #" << (i + 1) |
| 159 << " sent at " << elapsed << " ms"; | 153 << " sent at " << elapsed << " ms"; |
| 160 EXPECT_EQ(TotalDelay(i), elapsed); | 154 EXPECT_EQ(TotalDelay(i), elapsed); |
| 161 } | 155 } |
| 162 EXPECT_TRUE(manager_.CheckResponse(res)); | 156 EXPECT_TRUE(manager_.CheckResponse(res)); |
| 163 | 157 |
| 164 EXPECT_TRUE(response_ == res); | 158 EXPECT_TRUE(response_ == res); |
| 165 EXPECT_TRUE(success_); | 159 EXPECT_TRUE(success_); |
| 166 EXPECT_FALSE(failure_); | 160 EXPECT_FALSE(failure_); |
| 167 EXPECT_FALSE(timeout_); | 161 EXPECT_FALSE(timeout_); |
| 168 delete res; | 162 delete res; |
| 169 } | 163 } |
| 170 | 164 |
| 171 // Test that we timeout properly if no response is received in 9500 ms. | 165 // Test that we timeout properly if no response is received. |
| 172 TEST_F(StunRequestTest, TestTimeout) { | 166 TEST_F(StunRequestTest, TestTimeout) { |
| 173 rtc::ScopedFakeClock fake_clock; | 167 rtc::ScopedFakeClock fake_clock; |
| 174 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL); | 168 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL); |
| 175 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req); | 169 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req); |
| 176 | 170 |
| 177 manager_.Send(new StunRequestThunker(req, this)); | 171 manager_.Send(new StunRequestThunker(req, this)); |
| 178 // Simulate the 9500 ms STUN timeout | 172 SIMULATED_WAIT(false, cricket::STUN_TOTAL_TIMEOUT, fake_clock); |
| 179 SIMULATED_WAIT(false, 9500, fake_clock); | |
| 180 | 173 |
| 181 EXPECT_FALSE(manager_.CheckResponse(res)); | 174 EXPECT_FALSE(manager_.CheckResponse(res)); |
| 182 EXPECT_TRUE(response_ == NULL); | 175 EXPECT_TRUE(response_ == NULL); |
| 183 EXPECT_FALSE(success_); | 176 EXPECT_FALSE(success_); |
| 184 EXPECT_FALSE(failure_); | 177 EXPECT_FALSE(failure_); |
| 185 EXPECT_TRUE(timeout_); | 178 EXPECT_TRUE(timeout_); |
| 186 delete res; | 179 delete res; |
| 187 } | 180 } |
| 188 | 181 |
| 189 // Regression test for specific crash where we receive a response with the | 182 // Regression test for specific crash where we receive a response with the |
| 190 // same id as a request that doesn't have an underlying StunMessage yet. | 183 // same id as a request that doesn't have an underlying StunMessage yet. |
| 191 TEST_F(StunRequestTest, TestNoEmptyRequest) { | 184 TEST_F(StunRequestTest, TestNoEmptyRequest) { |
| 192 StunRequestThunker* request = new StunRequestThunker(this); | 185 StunRequestThunker* request = new StunRequestThunker(this); |
| 193 | 186 |
| 194 manager_.SendDelayed(request, 100); | 187 manager_.SendDelayed(request, 100); |
| 195 | 188 |
| 196 StunMessage dummy_req; | 189 StunMessage dummy_req; |
| 197 dummy_req.SetTransactionID(request->id()); | 190 dummy_req.SetTransactionID(request->id()); |
| 198 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, &dummy_req); | 191 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, &dummy_req); |
| 199 | 192 |
| 200 EXPECT_TRUE(manager_.CheckResponse(res)); | 193 EXPECT_TRUE(manager_.CheckResponse(res)); |
| 201 | 194 |
| 202 EXPECT_TRUE(response_ == res); | 195 EXPECT_TRUE(response_ == res); |
| 203 EXPECT_TRUE(success_); | 196 EXPECT_TRUE(success_); |
| 204 EXPECT_FALSE(failure_); | 197 EXPECT_FALSE(failure_); |
| 205 EXPECT_FALSE(timeout_); | 198 EXPECT_FALSE(timeout_); |
| 206 delete res; | 199 delete res; |
| 207 } | 200 } |
| OLD | NEW |