~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/googletalk/libjingle/talk/p2p/base/stunrequest_unittest.cc

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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/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"
33
 
 
34
 
using namespace cricket;
35
 
 
36
 
class StunRequestTest : public testing::Test,
37
 
                        public sigslot::has_slots<> {
38
 
 public:
39
 
  static void SetUpTestCase() {
40
 
    talk_base::InitRandom(NULL, 0);
41
 
  }
42
 
  StunRequestTest()
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);
47
 
  }
48
 
 
49
 
  void OnSendPacket(const void* data, size_t size, StunRequest* req) {
50
 
    request_count_++;
51
 
  }
52
 
 
53
 
  void OnResponse(StunMessage* res) {
54
 
    response_ = res;
55
 
    success_ = true;
56
 
  }
57
 
  void OnErrorResponse(StunMessage* res) {
58
 
    response_ = res;
59
 
    failure_ = true;
60
 
  }
61
 
  void OnTimeout() {
62
 
    timeout_ = true;
63
 
  }
64
 
 
65
 
 protected:
66
 
  static StunMessage* CreateStunMessage(StunMessageType type,
67
 
                                        StunMessage* req) {
68
 
    StunMessage* msg = new StunMessage();
69
 
    msg->SetType(type);
70
 
    if (req) {
71
 
      msg->SetTransactionID(req->transaction_id());
72
 
    } else {
73
 
      msg->SetTransactionID(
74
 
          talk_base::CreateRandomString(kStunTransactionIdLength));
75
 
    }
76
 
    return msg;
77
 
  }
78
 
  static int TotalDelay(int sends) {
79
 
    int total = 0;
80
 
    for (int i = 0; i < sends; i++) {
81
 
      if (i < 4)
82
 
        total += 100 << i;
83
 
      else
84
 
        total += 1600;
85
 
    }
86
 
    return total;
87
 
  }
88
 
 
89
 
  StunRequestManager manager_;
90
 
  int request_count_;
91
 
  StunMessage* response_;
92
 
  bool success_;
93
 
  bool failure_;
94
 
  bool timeout_;
95
 
};
96
 
 
97
 
// Forwards results to the test class.
98
 
class StunRequestThunker : public StunRequest {
99
 
 public:
100
 
  StunRequestThunker(StunMessage* msg, StunRequestTest* test)
101
 
      : StunRequest(msg), test_(test) {}
102
 
  explicit StunRequestThunker(StunRequestTest* test) : test_(test) {}
103
 
 private:
104
 
  virtual void OnResponse(StunMessage* res) {
105
 
    test_->OnResponse(res);
106
 
  }
107
 
  virtual void OnErrorResponse(StunMessage* res) {
108
 
    test_->OnErrorResponse(res);
109
 
  }
110
 
  virtual void OnTimeout() {
111
 
    test_->OnTimeout();
112
 
  }
113
 
 
114
 
  virtual void Prepare(StunMessage* request) {
115
 
    request->SetType(STUN_BINDING_REQUEST);
116
 
  }
117
 
 
118
 
  StunRequestTest* test_;
119
 
};
120
 
 
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);
125
 
 
126
 
  manager_.Send(new StunRequestThunker(req, this));
127
 
  EXPECT_TRUE(manager_.CheckResponse(res));
128
 
 
129
 
  EXPECT_TRUE(response_ == res);
130
 
  EXPECT_TRUE(success_);
131
 
  EXPECT_FALSE(failure_);
132
 
  EXPECT_FALSE(timeout_);
133
 
  delete res;
134
 
}
135
 
 
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);
140
 
 
141
 
  manager_.Send(new StunRequestThunker(req, this));
142
 
  EXPECT_TRUE(manager_.CheckResponse(res));
143
 
 
144
 
  EXPECT_TRUE(response_ == res);
145
 
  EXPECT_FALSE(success_);
146
 
  EXPECT_TRUE(failure_);
147
 
  EXPECT_FALSE(timeout_);
148
 
  delete res;
149
 
}
150
 
 
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);
155
 
 
156
 
  manager_.Send(new StunRequestThunker(req, this));
157
 
  EXPECT_FALSE(manager_.CheckResponse(res));
158
 
 
159
 
  EXPECT_TRUE(response_ == NULL);
160
 
  EXPECT_FALSE(success_);
161
 
  EXPECT_FALSE(failure_);
162
 
  EXPECT_FALSE(timeout_);
163
 
  delete res;
164
 
}
165
 
 
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);
171
 
 
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);
181
 
  }
182
 
  EXPECT_TRUE(manager_.CheckResponse(res));
183
 
 
184
 
  EXPECT_TRUE(response_ == res);
185
 
  EXPECT_TRUE(success_);
186
 
  EXPECT_FALSE(failure_);
187
 
  EXPECT_FALSE(timeout_);
188
 
  delete res;
189
 
}
190
 
 
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);
195
 
 
196
 
  manager_.Send(new StunRequestThunker(req, this));
197
 
  talk_base::Thread::Current()->ProcessMessages(10000);  // > STUN timeout
198
 
  EXPECT_FALSE(manager_.CheckResponse(res));
199
 
 
200
 
  EXPECT_TRUE(response_ == NULL);
201
 
  EXPECT_FALSE(success_);
202
 
  EXPECT_FALSE(failure_);
203
 
  EXPECT_TRUE(timeout_);
204
 
  delete res;
205
 
}
206
 
 
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);
211
 
 
212
 
  manager_.SendDelayed(request, 100);
213
 
 
214
 
  StunMessage dummy_req;
215
 
  dummy_req.SetTransactionID(request->id());
216
 
  StunMessage* res = CreateStunMessage(STUN_BINDING_RESPONSE, &dummy_req);
217
 
 
218
 
  EXPECT_TRUE(manager_.CheckResponse(res));
219
 
 
220
 
  EXPECT_TRUE(response_ == res);
221
 
  EXPECT_TRUE(success_);
222
 
  EXPECT_FALSE(failure_);
223
 
  EXPECT_FALSE(timeout_);
224
 
  delete res;
225
 
}