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/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<> {
40 static void SetUpTestCase() {
44 static void TearDownTestCase() {
49 : manager_(rtc::Thread::Current()),
50 request_count_(0), response_(NULL),
51 success_(false), failure_(false), timeout_(false) {
52 manager_.SignalSendPacket.connect(this, &StunRequestTest::OnSendPacket);
55 void OnSendPacket(const void* data, size_t size, StunRequest* req) {
59 void OnResponse(StunMessage* res) {
63 void OnErrorResponse(StunMessage* res) {
72 static StunMessage* CreateStunMessage(StunMessageType type,
74 StunMessage* msg = new StunMessage();
77 msg->SetTransactionID(req->transaction_id());
81 static int TotalDelay(int sends) {
83 for (int i = 0; i < sends; i++) {
92 StunRequestManager manager_;
94 StunMessage* response_;
100 // Forwards results to the test class.
101 class StunRequestThunker : public StunRequest {
103 StunRequestThunker(StunMessage* msg, StunRequestTest* test)
104 : StunRequest(msg), test_(test) {}
105 explicit StunRequestThunker(StunRequestTest* test) : test_(test) {}
107 virtual void OnResponse(StunMessage* res) {
108 test_->OnResponse(res);
110 virtual void OnErrorResponse(StunMessage* res) {
111 test_->OnErrorResponse(res);
113 virtual void OnTimeout() {
117 virtual void Prepare(StunMessage* request) {
118 request->SetType(STUN_BINDING_REQUEST);
121 StunRequestTest* test_;
124 // Test handling of a normal binding response.
125 TEST_F(StunRequestTest, TestSuccess) {
126 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
128 manager_.Send(new StunRequestThunker(req, this));
129 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req);
130 EXPECT_TRUE(manager_.CheckResponse(res));
132 EXPECT_TRUE(response_ == res);
133 EXPECT_TRUE(success_);
134 EXPECT_FALSE(failure_);
135 EXPECT_FALSE(timeout_);
139 // Test handling of an error binding response.
140 TEST_F(StunRequestTest, TestError) {
141 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
143 manager_.Send(new StunRequestThunker(req, this));
144 StunMessage* res = CreateStunMessage(STUN_BINDING_ERROR_RESPONSE, req);
145 EXPECT_TRUE(manager_.CheckResponse(res));
147 EXPECT_TRUE(response_ == res);
148 EXPECT_FALSE(success_);
149 EXPECT_TRUE(failure_);
150 EXPECT_FALSE(timeout_);
154 // Test handling of a binding response with the wrong transaction id.
155 TEST_F(StunRequestTest, TestUnexpected) {
156 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
158 manager_.Send(new StunRequestThunker(req, this));
159 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, NULL);
160 EXPECT_FALSE(manager_.CheckResponse(res));
162 EXPECT_TRUE(response_ == NULL);
163 EXPECT_FALSE(success_);
164 EXPECT_FALSE(failure_);
165 EXPECT_FALSE(timeout_);
169 // Test that requests are sent at the right times, and that the 9th request
170 // (sent at 7900 ms) can be properly replied to.
171 TEST_F(StunRequestTest, TestBackoff) {
172 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
174 uint32 start = rtc::Time();
175 manager_.Send(new StunRequestThunker(req, this));
176 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req);
177 for (int i = 0; i < 9; ++i) {
178 while (request_count_ == i)
179 rtc::Thread::Current()->ProcessMessages(1);
180 int32 elapsed = rtc::TimeSince(start);
181 LOG(LS_INFO) << "STUN request #" << (i + 1)
182 << " sent at " << elapsed << " ms";
183 EXPECT_GE(TotalDelay(i + 1), elapsed);
185 EXPECT_TRUE(manager_.CheckResponse(res));
187 EXPECT_TRUE(response_ == res);
188 EXPECT_TRUE(success_);
189 EXPECT_FALSE(failure_);
190 EXPECT_FALSE(timeout_);
194 // Test that we timeout properly if no response is received in 9500 ms.
195 TEST_F(StunRequestTest, TestTimeout) {
196 StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
197 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req);
199 manager_.Send(new StunRequestThunker(req, this));
200 rtc::Thread::Current()->ProcessMessages(10000); // > STUN timeout
201 EXPECT_FALSE(manager_.CheckResponse(res));
203 EXPECT_TRUE(response_ == NULL);
204 EXPECT_FALSE(success_);
205 EXPECT_FALSE(failure_);
206 EXPECT_TRUE(timeout_);
210 // Regression test for specific crash where we receive a response with the
211 // same id as a request that doesn't have an underlying StunMessage yet.
212 TEST_F(StunRequestTest, TestNoEmptyRequest) {
213 StunRequestThunker* request = new StunRequestThunker(this);
215 manager_.SendDelayed(request, 100);
217 StunMessage dummy_req;
218 dummy_req.SetTransactionID(request->id());
219 StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, &dummy_req);
221 EXPECT_TRUE(manager_.CheckResponse(res));
223 EXPECT_TRUE(response_ == res);
224 EXPECT_TRUE(success_);
225 EXPECT_FALSE(failure_);
226 EXPECT_FALSE(timeout_);