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

« back to all changes in this revision

Viewing changes to protocols/jabber/googletalk/libjingle/talk/p2p/base/stunport_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 2009 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/basicpacketsocketfactory.h"
29
 
#include "talk/base/gunit.h"
30
 
#include "talk/base/helpers.h"
31
 
#include "talk/base/socketaddress.h"
32
 
#include "talk/p2p/base/stunport.h"
33
 
#include "talk/p2p/base/teststunserver.h"
34
 
 
35
 
using talk_base::SocketAddress;
36
 
 
37
 
static const SocketAddress kLocalAddr("127.0.0.1", 0);
38
 
static const SocketAddress kStunAddr("127.0.0.1", 5000);
39
 
static const SocketAddress kBadAddr("0.0.0.1", 5000);
40
 
static const SocketAddress kStunHostnameAddr("localhost", 5000);
41
 
static const SocketAddress kBadHostnameAddr("not-a-real-hostname", 5000);
42
 
static const int kTimeoutMs = 10000;
43
 
 
44
 
// Tests connecting a StunPort to a fake STUN server (cricket::StunServer)
45
 
// TODO: Use a VirtualSocketServer here. We have to use a
46
 
// PhysicalSocketServer right now since DNS is not part of SocketServer yet.
47
 
class StunPortTest : public testing::Test,
48
 
                     public sigslot::has_slots<> {
49
 
 public:
50
 
  StunPortTest()
51
 
      : network_("unittest", "unittest", talk_base::IPAddress(INADDR_ANY), 32),
52
 
        socket_factory_(talk_base::Thread::Current()),
53
 
        stun_server_(new cricket::TestStunServer(
54
 
          talk_base::Thread::Current(), kStunAddr)),
55
 
        done_(false), error_(false) {
56
 
  }
57
 
 
58
 
  const cricket::Port* port() const { return stun_port_.get(); }
59
 
  bool done() const { return done_; }
60
 
  bool error() const { return error_; }
61
 
 
62
 
  void CreateStunPort(const talk_base::SocketAddress& server_addr) {
63
 
    stun_port_.reset(cricket::StunPort::Create(
64
 
        talk_base::Thread::Current(), &socket_factory_, &network_,
65
 
        kLocalAddr.ipaddr(), 0, 0, server_addr));
66
 
    stun_port_->SignalAddressReady.connect(this,
67
 
        &StunPortTest::OnAddressReady);
68
 
    stun_port_->SignalAddressError.connect(this,
69
 
        &StunPortTest::OnAddressError);
70
 
  }
71
 
 
72
 
  void PrepareAddress() {
73
 
    stun_port_->PrepareAddress();
74
 
  }
75
 
 
76
 
 protected:
77
 
  static void SetUpTestCase() {
78
 
    // Ensure the RNG is inited.
79
 
    talk_base::InitRandom(NULL, 0);
80
 
  }
81
 
 
82
 
  void OnAddressReady(cricket::Port* port) {
83
 
    done_ = true;
84
 
    error_ = false;
85
 
  }
86
 
  void OnAddressError(cricket::Port* port) {
87
 
    done_ = true;
88
 
    error_ = true;
89
 
  }
90
 
 
91
 
 private:
92
 
  talk_base::Network network_;
93
 
  talk_base::BasicPacketSocketFactory socket_factory_;
94
 
  talk_base::scoped_ptr<cricket::StunPort> stun_port_;
95
 
  talk_base::scoped_ptr<cricket::TestStunServer> stun_server_;
96
 
  bool done_;
97
 
  bool error_;
98
 
};
99
 
 
100
 
// Test that we can create a STUN port
101
 
TEST_F(StunPortTest, TestBasic) {
102
 
  CreateStunPort(kStunAddr);
103
 
  EXPECT_EQ("stun", port()->type());
104
 
  EXPECT_EQ(0U, port()->candidates().size());
105
 
}
106
 
 
107
 
// Test that we can get an address from a STUN server.
108
 
TEST_F(StunPortTest, TestPrepareAddress) {
109
 
  CreateStunPort(kStunAddr);
110
 
  PrepareAddress();
111
 
  EXPECT_TRUE_WAIT(done(), kTimeoutMs);
112
 
  ASSERT_EQ(1U, port()->candidates().size());
113
 
  EXPECT_TRUE(kLocalAddr.EqualIPs(port()->candidates()[0].address()));
114
 
 
115
 
  // TODO: Add IPv6 tests here, once either physicalsocketserver supports
116
 
  // IPv6, or this test is changed to use VirtualSocketServer.
117
 
}
118
 
 
119
 
// Test that we fail properly if we can't get an address.
120
 
TEST_F(StunPortTest, TestPrepareAddressFail) {
121
 
  CreateStunPort(kBadAddr);
122
 
  PrepareAddress();
123
 
  EXPECT_TRUE_WAIT(done(), kTimeoutMs);
124
 
  EXPECT_TRUE(error());
125
 
  EXPECT_EQ(0U, port()->candidates().size());
126
 
}
127
 
 
128
 
// Test that we can get an address from a STUN server specified by a hostname.
129
 
TEST_F(StunPortTest, TestPrepareAddressHostname) {
130
 
  CreateStunPort(kStunHostnameAddr);
131
 
  PrepareAddress();
132
 
  EXPECT_TRUE_WAIT(done(), kTimeoutMs);
133
 
  ASSERT_EQ(1U, port()->candidates().size());
134
 
  EXPECT_TRUE(kLocalAddr.EqualIPs(port()->candidates()[0].address()));
135
 
}
136
 
 
137
 
// Test that we handle hostname lookup failures properly.
138
 
TEST_F(StunPortTest, TestPrepareAddressHostnameFail) {
139
 
  CreateStunPort(kBadHostnameAddr);
140
 
  PrepareAddress();
141
 
  EXPECT_TRUE_WAIT(done(), kTimeoutMs);
142
 
  EXPECT_TRUE(error());
143
 
  EXPECT_EQ(0U, port()->candidates().size());
144
 
}