3
* Copyright 2004 Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#include "talk/base/gunit.h"
29
#include "talk/base/helpers.h"
30
#include "talk/base/logging.h"
31
#include "talk/base/timeutils.h"
32
#include "talk/p2p/base/stunrequest.h"
34
using namespace cricket;
36
class StunRequestTest : public testing::Test,
37
public sigslot::has_slots<> {
39
static void SetUpTestCase() {
40
talk_base::InitRandom(NULL, 0);
43
: manager_(talk_base::Thread::Current()),
44
request_count_(0), response_(NULL),
45
success_(false), failure_(false), timeout_(false) {
46
manager_.SignalSendPacket.connect(this, &StunRequestTest::OnSendPacket);
49
void OnSendPacket(const void* data, size_t size, StunRequest* req) {
53
void OnResponse(StunMessage* res) {
57
void OnErrorResponse(StunMessage* res) {
66
static StunMessage* CreateStunMessage(StunMessageType type,
68
StunMessage* msg = new StunMessage();
71
msg->SetTransactionID(req->transaction_id());
73
msg->SetTransactionID(
74
talk_base::CreateRandomString(kStunTransactionIdLength));
78
static int TotalDelay(int sends) {
80
for (int i = 0; i < sends; i++) {
89
StunRequestManager manager_;
91
StunMessage* response_;
97
// Forwards results to the test class.
98
class StunRequestThunker : public StunRequest {
100
StunRequestThunker(StunMessage* msg, StunRequestTest* test)
101
: StunRequest(msg), test_(test) {}
102
explicit StunRequestThunker(StunRequestTest* test) : test_(test) {}
104
virtual void OnResponse(StunMessage* res) {
105
test_->OnResponse(res);
107
virtual void OnErrorResponse(StunMessage* res) {
108
test_->OnErrorResponse(res);
110
virtual void OnTimeout() {
114
virtual void Prepare(StunMessage* request) {
115
request->SetType(STUN_BINDING_REQUEST);
118
StunRequestTest* test_;
121
// Test handling of a normal binding response.
122
TEST_F(StunRequestTest, TestSuccess) {
123
StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
124
StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req);
126
manager_.Send(new StunRequestThunker(req, this));
127
EXPECT_TRUE(manager_.CheckResponse(res));
129
EXPECT_TRUE(response_ == res);
130
EXPECT_TRUE(success_);
131
EXPECT_FALSE(failure_);
132
EXPECT_FALSE(timeout_);
136
// Test handling of an error binding response.
137
TEST_F(StunRequestTest, TestError) {
138
StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
139
StunMessage* res = CreateStunMessage(STUN_BINDING_ERROR_RESPONSE, req);
141
manager_.Send(new StunRequestThunker(req, this));
142
EXPECT_TRUE(manager_.CheckResponse(res));
144
EXPECT_TRUE(response_ == res);
145
EXPECT_FALSE(success_);
146
EXPECT_TRUE(failure_);
147
EXPECT_FALSE(timeout_);
151
// Test handling of a binding response with the wrong transaction id.
152
TEST_F(StunRequestTest, TestUnexpected) {
153
StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
154
StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, NULL);
156
manager_.Send(new StunRequestThunker(req, this));
157
EXPECT_FALSE(manager_.CheckResponse(res));
159
EXPECT_TRUE(response_ == NULL);
160
EXPECT_FALSE(success_);
161
EXPECT_FALSE(failure_);
162
EXPECT_FALSE(timeout_);
166
// Test that requests are sent at the right times, and that the 9th request
167
// (sent at 7900 ms) can be properly replied to.
168
TEST_F(StunRequestTest, TestBackoff) {
169
StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
170
StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req);
172
uint32 start = talk_base::Time();
173
manager_.Send(new StunRequestThunker(req, this));
174
for (int i = 0; i < 9; ++i) {
175
while (request_count_ == i)
176
talk_base::Thread::Current()->ProcessMessages(1);
177
int32 elapsed = talk_base::TimeSince(start);
178
LOG(LS_INFO) << "STUN request #" << (i + 1)
179
<< " sent at " << elapsed << " ms";
180
EXPECT_GE(TotalDelay(i + 1), elapsed);
182
EXPECT_TRUE(manager_.CheckResponse(res));
184
EXPECT_TRUE(response_ == res);
185
EXPECT_TRUE(success_);
186
EXPECT_FALSE(failure_);
187
EXPECT_FALSE(timeout_);
191
// Test that we timeout properly if no response is received in 9500 ms.
192
TEST_F(StunRequestTest, TestTimeout) {
193
StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
194
StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req);
196
manager_.Send(new StunRequestThunker(req, this));
197
talk_base::Thread::Current()->ProcessMessages(10000); // > STUN timeout
198
EXPECT_FALSE(manager_.CheckResponse(res));
200
EXPECT_TRUE(response_ == NULL);
201
EXPECT_FALSE(success_);
202
EXPECT_FALSE(failure_);
203
EXPECT_TRUE(timeout_);
207
// Regression test for specific crash where we receive a response with the
208
// same id as a request that doesn't have an underlying StunMessage yet.
209
TEST_F(StunRequestTest, TestNoEmptyRequest) {
210
StunRequestThunker* request = new StunRequestThunker(this);
212
manager_.SendDelayed(request, 100);
214
StunMessage dummy_req;
215
dummy_req.SetTransactionID(request->id());
216
StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, &dummy_req);
218
EXPECT_TRUE(manager_.CheckResponse(res));
220
EXPECT_TRUE(response_ == res);
221
EXPECT_TRUE(success_);
222
EXPECT_FALSE(failure_);
223
EXPECT_FALSE(timeout_);