Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / p2p / base / stunrequest_unittest.cc
1 /*
2  * libjingle
3  * Copyright 2004 Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
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.
15  *
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.
26  */
27
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"
34
35 using namespace cricket;
36
37 class StunRequestTest : public testing::Test,
38                         public sigslot::has_slots<> {
39  public:
40   StunRequestTest()
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);
45   }
46
47   void OnSendPacket(const void* data, size_t size, StunRequest* req) {
48     request_count_++;
49   }
50
51   void OnResponse(StunMessage* res) {
52     response_ = res;
53     success_ = true;
54   }
55   void OnErrorResponse(StunMessage* res) {
56     response_ = res;
57     failure_ = true;
58   }
59   void OnTimeout() {
60     timeout_ = true;
61   }
62
63  protected:
64   static StunMessage* CreateStunMessage(StunMessageType type,
65                                         StunMessage* req) {
66     StunMessage* msg = new StunMessage();
67     msg->SetType(type);
68     if (req) {
69       msg->SetTransactionID(req->transaction_id());
70     }
71     return msg;
72   }
73   static int TotalDelay(int sends) {
74     int total = 0;
75     for (int i = 0; i < sends; i++) {
76       if (i < 4)
77         total += 100 << i;
78       else
79         total += 1600;
80     }
81     return total;
82   }
83
84   StunRequestManager manager_;
85   int request_count_;
86   StunMessage* response_;
87   bool success_;
88   bool failure_;
89   bool timeout_;
90 };
91
92 // Forwards results to the test class.
93 class StunRequestThunker : public StunRequest {
94  public:
95   StunRequestThunker(StunMessage* msg, StunRequestTest* test)
96       : StunRequest(msg), test_(test) {}
97   explicit StunRequestThunker(StunRequestTest* test) : test_(test) {}
98  private:
99   virtual void OnResponse(StunMessage* res) {
100     test_->OnResponse(res);
101   }
102   virtual void OnErrorResponse(StunMessage* res) {
103     test_->OnErrorResponse(res);
104   }
105   virtual void OnTimeout() {
106     test_->OnTimeout();
107   }
108
109   virtual void Prepare(StunMessage* request) {
110     request->SetType(STUN_BINDING_REQUEST);
111   }
112
113   StunRequestTest* test_;
114 };
115
116 // Test handling of a normal binding response.
117 TEST_F(StunRequestTest, TestSuccess) {
118   StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
119
120   manager_.Send(new StunRequestThunker(req, this));
121   StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req);
122   EXPECT_TRUE(manager_.CheckResponse(res));
123
124   EXPECT_TRUE(response_ == res);
125   EXPECT_TRUE(success_);
126   EXPECT_FALSE(failure_);
127   EXPECT_FALSE(timeout_);
128   delete res;
129 }
130
131 // Test handling of an error binding response.
132 TEST_F(StunRequestTest, TestError) {
133   StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
134
135   manager_.Send(new StunRequestThunker(req, this));
136   StunMessage* res = CreateStunMessage(STUN_BINDING_ERROR_RESPONSE, req);
137   EXPECT_TRUE(manager_.CheckResponse(res));
138
139   EXPECT_TRUE(response_ == res);
140   EXPECT_FALSE(success_);
141   EXPECT_TRUE(failure_);
142   EXPECT_FALSE(timeout_);
143   delete res;
144 }
145
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);
149
150   manager_.Send(new StunRequestThunker(req, this));
151   StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, NULL);
152   EXPECT_FALSE(manager_.CheckResponse(res));
153
154   EXPECT_TRUE(response_ == NULL);
155   EXPECT_FALSE(success_);
156   EXPECT_FALSE(failure_);
157   EXPECT_FALSE(timeout_);
158   delete res;
159 }
160
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);
165
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);
176   }
177   EXPECT_TRUE(manager_.CheckResponse(res));
178
179   EXPECT_TRUE(response_ == res);
180   EXPECT_TRUE(success_);
181   EXPECT_FALSE(failure_);
182   EXPECT_FALSE(timeout_);
183   delete res;
184 }
185
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);
190
191   manager_.Send(new StunRequestThunker(req, this));
192   rtc::Thread::Current()->ProcessMessages(10000);  // > STUN timeout
193   EXPECT_FALSE(manager_.CheckResponse(res));
194
195   EXPECT_TRUE(response_ == NULL);
196   EXPECT_FALSE(success_);
197   EXPECT_FALSE(failure_);
198   EXPECT_TRUE(timeout_);
199   delete res;
200 }
201
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);
206
207   manager_.SendDelayed(request, 100);
208
209   StunMessage dummy_req;
210   dummy_req.SetTransactionID(request->id());
211   StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, &dummy_req);
212
213   EXPECT_TRUE(manager_.CheckResponse(res));
214
215   EXPECT_TRUE(response_ == res);
216   EXPECT_TRUE(success_);
217   EXPECT_FALSE(failure_);
218   EXPECT_FALSE(timeout_);
219   delete res;
220 }