Upstream version 9.38.198.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 "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"
34
35 using namespace cricket;
36
37 class StunRequestTest : public testing::Test,
38                         public sigslot::has_slots<> {
39  public:
40   static void SetUpTestCase() {
41     rtc::InitializeSSL();
42   }
43
44   static void TearDownTestCase() {
45     rtc::CleanupSSL();
46   }
47
48   StunRequestTest()
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);
53   }
54
55   void OnSendPacket(const void* data, size_t size, StunRequest* req) {
56     request_count_++;
57   }
58
59   void OnResponse(StunMessage* res) {
60     response_ = res;
61     success_ = true;
62   }
63   void OnErrorResponse(StunMessage* res) {
64     response_ = res;
65     failure_ = true;
66   }
67   void OnTimeout() {
68     timeout_ = true;
69   }
70
71  protected:
72   static StunMessage* CreateStunMessage(StunMessageType type,
73                                         StunMessage* req) {
74     StunMessage* msg = new StunMessage();
75     msg->SetType(type);
76     if (req) {
77       msg->SetTransactionID(req->transaction_id());
78     }
79     return msg;
80   }
81   static int TotalDelay(int sends) {
82     int total = 0;
83     for (int i = 0; i < sends; i++) {
84       if (i < 4)
85         total += 100 << i;
86       else
87         total += 1600;
88     }
89     return total;
90   }
91
92   StunRequestManager manager_;
93   int request_count_;
94   StunMessage* response_;
95   bool success_;
96   bool failure_;
97   bool timeout_;
98 };
99
100 // Forwards results to the test class.
101 class StunRequestThunker : public StunRequest {
102  public:
103   StunRequestThunker(StunMessage* msg, StunRequestTest* test)
104       : StunRequest(msg), test_(test) {}
105   explicit StunRequestThunker(StunRequestTest* test) : test_(test) {}
106  private:
107   virtual void OnResponse(StunMessage* res) {
108     test_->OnResponse(res);
109   }
110   virtual void OnErrorResponse(StunMessage* res) {
111     test_->OnErrorResponse(res);
112   }
113   virtual void OnTimeout() {
114     test_->OnTimeout();
115   }
116
117   virtual void Prepare(StunMessage* request) {
118     request->SetType(STUN_BINDING_REQUEST);
119   }
120
121   StunRequestTest* test_;
122 };
123
124 // Test handling of a normal binding response.
125 TEST_F(StunRequestTest, TestSuccess) {
126   StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
127
128   manager_.Send(new StunRequestThunker(req, this));
129   StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, req);
130   EXPECT_TRUE(manager_.CheckResponse(res));
131
132   EXPECT_TRUE(response_ == res);
133   EXPECT_TRUE(success_);
134   EXPECT_FALSE(failure_);
135   EXPECT_FALSE(timeout_);
136   delete res;
137 }
138
139 // Test handling of an error binding response.
140 TEST_F(StunRequestTest, TestError) {
141   StunMessage* req = CreateStunMessage(STUN_BINDING_REQUEST, NULL);
142
143   manager_.Send(new StunRequestThunker(req, this));
144   StunMessage* res = CreateStunMessage(STUN_BINDING_ERROR_RESPONSE, req);
145   EXPECT_TRUE(manager_.CheckResponse(res));
146
147   EXPECT_TRUE(response_ == res);
148   EXPECT_FALSE(success_);
149   EXPECT_TRUE(failure_);
150   EXPECT_FALSE(timeout_);
151   delete res;
152 }
153
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);
157
158   manager_.Send(new StunRequestThunker(req, this));
159   StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, NULL);
160   EXPECT_FALSE(manager_.CheckResponse(res));
161
162   EXPECT_TRUE(response_ == NULL);
163   EXPECT_FALSE(success_);
164   EXPECT_FALSE(failure_);
165   EXPECT_FALSE(timeout_);
166   delete res;
167 }
168
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);
173
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);
184   }
185   EXPECT_TRUE(manager_.CheckResponse(res));
186
187   EXPECT_TRUE(response_ == res);
188   EXPECT_TRUE(success_);
189   EXPECT_FALSE(failure_);
190   EXPECT_FALSE(timeout_);
191   delete res;
192 }
193
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);
198
199   manager_.Send(new StunRequestThunker(req, this));
200   rtc::Thread::Current()->ProcessMessages(10000);  // > STUN timeout
201   EXPECT_FALSE(manager_.CheckResponse(res));
202
203   EXPECT_TRUE(response_ == NULL);
204   EXPECT_FALSE(success_);
205   EXPECT_FALSE(failure_);
206   EXPECT_TRUE(timeout_);
207   delete res;
208 }
209
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);
214
215   manager_.SendDelayed(request, 100);
216
217   StunMessage dummy_req;
218   dummy_req.SetTransactionID(request->id());
219   StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, &dummy_req);
220
221   EXPECT_TRUE(manager_.CheckResponse(res));
222
223   EXPECT_TRUE(response_ == res);
224   EXPECT_TRUE(success_);
225   EXPECT_FALSE(failure_);
226   EXPECT_FALSE(timeout_);
227   delete res;
228 }