1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "p2p/base/stun_request.h"
#include <cstddef>
#include <cstdint>
#include <memory>
#include <vector>
#include "api/test/rtc_error_matchers.h"
#include "api/transport/stun.h"
#include "api/units/time_delta.h"
#include "rtc_base/fake_clock.h"
#include "rtc_base/gunit.h"
#include "rtc_base/logging.h"
#include "rtc_base/thread.h"
#include "rtc_base/time_utils.h"
#include "test/gmock.h"
#include "test/gtest.h"
#include "test/wait_until.h"
namespace webrtc {
namespace {
using ::testing::Ne;
std::unique_ptr<StunMessage> CreateStunMessage(
StunMessageType type,
const StunMessage* req = nullptr) {
std::unique_ptr<StunMessage> msg = std::make_unique<StunMessage>(
type, req ? req->transaction_id() : StunMessage::GenerateTransactionId());
return msg;
}
int TotalDelay(int sends) {
std::vector<int> delays = {0, 250, 750, 1750, 3750,
7750, 15750, 23750, 31750, 39750};
return delays[sends];
}
} // namespace
class StunRequestTest : public ::testing::Test {
public:
StunRequestTest()
: manager_(Thread::Current(),
[this](const void* data, size_t size, StunRequest* request) {
OnSendPacket(data, size, request);
}),
request_count_(0),
response_(nullptr),
success_(false),
failure_(false),
timeout_(false) {}
void OnSendPacket(const void* data, size_t size, StunRequest* req) {
request_count_++;
}
virtual void OnResponse(StunMessage* res) {
response_ = res;
success_ = true;
}
virtual void OnErrorResponse(StunMessage* res) {
response_ = res;
failure_ = true;
}
virtual void OnTimeout() { timeout_ = true; }
protected:
AutoThread main_thread_;
StunRequestManager manager_;
int request_count_;
StunMessage* response_;
bool success_;
bool failure_;
bool timeout_;
};
// Forwards results to the test class.
class StunRequestThunker : public StunRequest {
public:
StunRequestThunker(StunRequestManager& manager, StunRequestTest* test)
: StunRequest(manager, CreateStunMessage(STUN_BINDING_REQUEST)),
test_(test) {
SetAuthenticationRequired(false);
}
std::unique_ptr<StunMessage> CreateResponseMessage(StunMessageType type) {
return CreateStunMessage(type, msg());
}
private:
virtual void OnResponse(StunMessage* res) { test_->OnResponse(res); }
virtual void OnErrorResponse(StunMessage* res) {
test_->OnErrorResponse(res);
}
virtual void OnTimeout() { test_->OnTimeout(); }
StunRequestTest* test_;
};
// Test handling of a normal binding response.
TEST_F(StunRequestTest, TestSuccess) {
auto* request = new StunRequestThunker(manager_, this);
std::unique_ptr<StunMessage> res =
request->CreateResponseMessage(STUN_BINDING_RESPONSE);
manager_.Send(request);
EXPECT_TRUE(manager_.CheckResponse(res.get()));
EXPECT_TRUE(response_ == res.get());
EXPECT_TRUE(success_);
EXPECT_FALSE(failure_);
EXPECT_FALSE(timeout_);
}
// Test handling of an error binding response.
TEST_F(StunRequestTest, TestError) {
auto* request = new StunRequestThunker(manager_, this);
std::unique_ptr<StunMessage> res =
request->CreateResponseMessage(STUN_BINDING_ERROR_RESPONSE);
manager_.Send(request);
EXPECT_TRUE(manager_.CheckResponse(res.get()));
EXPECT_TRUE(response_ == res.get());
EXPECT_FALSE(success_);
EXPECT_TRUE(failure_);
EXPECT_FALSE(timeout_);
}
// Test handling of a binding response with the wrong transaction id.
TEST_F(StunRequestTest, TestUnexpected) {
auto* request = new StunRequestThunker(manager_, this);
std::unique_ptr<StunMessage> res = CreateStunMessage(STUN_BINDING_RESPONSE);
manager_.Send(request);
EXPECT_FALSE(manager_.CheckResponse(res.get()));
EXPECT_TRUE(response_ == nullptr);
EXPECT_FALSE(success_);
EXPECT_FALSE(failure_);
EXPECT_FALSE(timeout_);
}
// Test that requests are sent at the right times.
TEST_F(StunRequestTest, TestBackoff) {
ScopedFakeClock fake_clock;
auto* request = new StunRequestThunker(manager_, this);
std::unique_ptr<StunMessage> res =
request->CreateResponseMessage(STUN_BINDING_RESPONSE);
int64_t start = TimeMillis();
manager_.Send(request);
for (int i = 0; i < 9; ++i) {
EXPECT_THAT(WaitUntil([&] { return request_count_; }, Ne(i),
{.timeout = TimeDelta::Millis(STUN_TOTAL_TIMEOUT),
.clock = &fake_clock}),
IsRtcOk());
int64_t elapsed = TimeMillis() - start;
RTC_DLOG(LS_INFO) << "STUN request #" << (i + 1) << " sent at " << elapsed
<< " ms";
EXPECT_EQ(TotalDelay(i), elapsed);
}
EXPECT_TRUE(manager_.CheckResponse(res.get()));
EXPECT_TRUE(response_ == res.get());
EXPECT_TRUE(success_);
EXPECT_FALSE(failure_);
EXPECT_FALSE(timeout_);
}
// Test that we timeout properly if no response is received.
TEST_F(StunRequestTest, TestTimeout) {
ScopedFakeClock fake_clock;
auto* request = new StunRequestThunker(manager_, this);
std::unique_ptr<StunMessage> res =
request->CreateResponseMessage(STUN_BINDING_RESPONSE);
manager_.Send(request);
SIMULATED_WAIT(false, STUN_TOTAL_TIMEOUT, fake_clock);
EXPECT_FALSE(manager_.CheckResponse(res.get()));
EXPECT_TRUE(response_ == nullptr);
EXPECT_FALSE(success_);
EXPECT_FALSE(failure_);
EXPECT_TRUE(timeout_);
}
// Regression test for specific crash where we receive a response with the
// same id as a request that doesn't have an underlying StunMessage yet.
TEST_F(StunRequestTest, TestNoEmptyRequest) {
StunRequestThunker* request = new StunRequestThunker(manager_, this);
manager_.SendDelayed(request, 100);
StunMessage dummy_req(0, request->id());
std::unique_ptr<StunMessage> res =
CreateStunMessage(STUN_BINDING_RESPONSE, &dummy_req);
EXPECT_TRUE(manager_.CheckResponse(res.get()));
EXPECT_TRUE(response_ == res.get());
EXPECT_TRUE(success_);
EXPECT_FALSE(failure_);
EXPECT_FALSE(timeout_);
}
// If the response contains an attribute in the "comprehension required" range
// which is not recognized, the transaction should be considered a failure and
// the response should be ignored.
TEST_F(StunRequestTest, TestUnrecognizedComprehensionRequiredAttribute) {
auto* request = new StunRequestThunker(manager_, this);
std::unique_ptr<StunMessage> res =
request->CreateResponseMessage(STUN_BINDING_ERROR_RESPONSE);
manager_.Send(request);
res->AddAttribute(StunAttribute::CreateUInt32(0x7777));
EXPECT_FALSE(manager_.CheckResponse(res.get()));
EXPECT_EQ(nullptr, response_);
EXPECT_FALSE(success_);
EXPECT_FALSE(failure_);
EXPECT_FALSE(timeout_);
}
class StunRequestReentranceTest : public StunRequestTest {
public:
void OnResponse(StunMessage* res) override {
manager_.Clear();
StunRequestTest::OnResponse(res);
}
void OnErrorResponse(StunMessage* res) override {
manager_.Clear();
StunRequestTest::OnErrorResponse(res);
}
};
TEST_F(StunRequestReentranceTest, TestSuccess) {
auto* request = new StunRequestThunker(manager_, this);
std::unique_ptr<StunMessage> res =
request->CreateResponseMessage(STUN_BINDING_RESPONSE);
manager_.Send(request);
EXPECT_TRUE(manager_.CheckResponse(res.get()));
EXPECT_TRUE(response_ == res.get());
EXPECT_TRUE(success_);
EXPECT_FALSE(failure_);
EXPECT_FALSE(timeout_);
}
TEST_F(StunRequestReentranceTest, TestError) {
auto* request = new StunRequestThunker(manager_, this);
std::unique_ptr<StunMessage> res =
request->CreateResponseMessage(STUN_BINDING_ERROR_RESPONSE);
manager_.Send(request);
EXPECT_TRUE(manager_.CheckResponse(res.get()));
EXPECT_TRUE(response_ == res.get());
EXPECT_FALSE(success_);
EXPECT_TRUE(failure_);
EXPECT_FALSE(timeout_);
}
} // namespace webrtc
|