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 "webrtc/p2p/base/stunrequest.h"
29 #include "webrtc/base/gunit.h"
30 #include "webrtc/base/helpers.h"
31 #include "webrtc/base/logging.h"
32 #include "webrtc/base/ssladapter.h"
33 #include "webrtc/base/timeutils.h"
35 using namespace cricket;
37 class StunRequestTest : public testing::Test,
38 public sigslot::has_slots<> {
41 : manager_(rtc::Thread::Current()),
42 request_count_(0), response_(NULL),
43 success_(false), failure_(false), timeout_(false) {
44 manager_.SignalSendPacket.connect(this, &StunRequestTest::OnSendPacket);
47 void OnSendPacket(const void* data, size_t size, StunRequest* req) {
51 void OnResponse(StunMessage* res) {
55 void OnErrorResponse(StunMessage* res) {
64 static StunMessage* CreateStunMessage(StunMessageType type,
66 StunMessage* msg = new StunMessage();
69 msg->SetTransactionID(req->transaction_id());
73 static int TotalDelay(int sends) {
75 for (int i = 0; i < sends; i++) {
84 StunRequestManager manager_;
86 StunMessage* response_;
92 // Forwards results to the test class.
93 class StunRequestThunker : public StunRequest {
95 StunRequestThunker(StunMessage* msg, StunRequestTest* test)
96 : StunRequest(msg), test_(test) {}
97 explicit StunRequestThunker(StunRequestTest* test) : test_(test) {}
99 virtual void OnResponse(StunMessage* res) {
100 test_->OnResponse(res);
102 virtual void OnErrorResponse(StunMessage* res) {
103 test_->OnErrorResponse(res);
105 virtual void OnTimeout() {
109 virtual void Prepare(StunMessage* request) {
110 request->SetType(STUN_BINDING_REQUEST);
113 StunRequestTest* test_;
116 // Test handling of a normal binding response.
117 TEST_F(StunRequestTest, TestSuccess) {
118 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
120 manager_.Send(new StunRequestThunker(req, this));
121 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req);
122 EXPECT_TRUE(manager_.CheckResponse(res));
124 EXPECT_TRUE(response_ == res);
125 EXPECT_TRUE(success_);
126 EXPECT_FALSE(failure_);
127 EXPECT_FALSE(timeout_);
131 // Test handling of an error binding response.
132 TEST_F(StunRequestTest, TestError) {
133 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
135 manager_.Send(new StunRequestThunker(req, this));
136 StunMessage* res = CreateStunMessage(STUN_BINDING_ERROR_RESPONSE, req);
137 EXPECT_TRUE(manager_.CheckResponse(res));
139 EXPECT_TRUE(response_ == res);
140 EXPECT_FALSE(success_);
141 EXPECT_TRUE(failure_);
142 EXPECT_FALSE(timeout_);
146 // Test handling of a binding response with the wrong transaction id.
147 TEST_F(StunRequestTest, TestUnexpected) {
148 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
150 manager_.Send(new StunRequestThunker(req, this));
151 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, NULL);
152 EXPECT_FALSE(manager_.CheckResponse(res));
154 EXPECT_TRUE(response_ == NULL);
155 EXPECT_FALSE(success_);
156 EXPECT_FALSE(failure_);
157 EXPECT_FALSE(timeout_);
161 // Test that requests are sent at the right times, and that the 9th request
162 // (sent at 7900 ms) can be properly replied to.
163 TEST_F(StunRequestTest, TestBackoff) {
164 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
166 uint32 start = rtc::Time();
167 manager_.Send(new StunRequestThunker(req, this));
168 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req);
169 for (int i = 0; i < 9; ++i) {
170 while (request_count_ == i)
171 rtc::Thread::Current()->ProcessMessages(1);
172 int32 elapsed = rtc::TimeSince(start);
173 LOG(LS_INFO) << "STUN request #" << (i + 1)
174 << " sent at " << elapsed << " ms";
175 EXPECT_GE(TotalDelay(i + 1), elapsed);
177 EXPECT_TRUE(manager_.CheckResponse(res));
179 EXPECT_TRUE(response_ == res);
180 EXPECT_TRUE(success_);
181 EXPECT_FALSE(failure_);
182 EXPECT_FALSE(timeout_);
186 // Test that we timeout properly if no response is received in 9500 ms.
187 TEST_F(StunRequestTest, TestTimeout) {
188 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
189 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req);
191 manager_.Send(new StunRequestThunker(req, this));
192 rtc::Thread::Current()->ProcessMessages(10000); // > STUN timeout
193 EXPECT_FALSE(manager_.CheckResponse(res));
195 EXPECT_TRUE(response_ == NULL);
196 EXPECT_FALSE(success_);
197 EXPECT_FALSE(failure_);
198 EXPECT_TRUE(timeout_);
202 // Regression test for specific crash where we receive a response with the
203 // same id as a request that doesn't have an underlying StunMessage yet.
204 TEST_F(StunRequestTest, TestNoEmptyRequest) {
205 StunRequestThunker* request = new StunRequestThunker(this);
207 manager_.SendDelayed(request, 100);
209 StunMessage dummy_req;
210 dummy_req.SetTransactionID(request->id());
211 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, &dummy_req);
213 EXPECT_TRUE(manager_.CheckResponse(res));
215 EXPECT_TRUE(response_ == res);
216 EXPECT_TRUE(success_);
217 EXPECT_FALSE(failure_);
218 EXPECT_FALSE(timeout_);