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

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/p2p/base/stunserver_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 <string>
 
29
 
 
30
#include "talk/base/gunit.h"
 
31
#include "talk/base/logging.h"
 
32
#include "talk/base/physicalsocketserver.h"
 
33
#include "talk/base/virtualsocketserver.h"
 
34
#include "talk/base/testclient.h"
 
35
#include "talk/base/thread.h"
 
36
#include "talk/p2p/base/stunserver.h"
 
37
 
 
38
using namespace cricket;
 
39
 
 
40
static const talk_base::SocketAddress server_addr("99.99.99.1", 3478);
 
41
static const talk_base::SocketAddress client_addr("1.2.3.4", 1234);
 
42
 
 
43
class StunServerTest : public testing::Test {
 
44
 public:
 
45
  StunServerTest()
 
46
    : pss_(new talk_base::PhysicalSocketServer),
 
47
      ss_(new talk_base::VirtualSocketServer(pss_.get())),
 
48
      worker_(ss_.get()) {
 
49
  }
 
50
  virtual void SetUp() {
 
51
    server_.reset(new StunServer(
 
52
        talk_base::AsyncUDPSocket::Create(ss_.get(), server_addr)));
 
53
    client_.reset(new talk_base::TestClient(
 
54
        talk_base::AsyncUDPSocket::Create(ss_.get(), client_addr)));
 
55
 
 
56
    worker_.Start();
 
57
  }
 
58
  void Send(const StunMessage& msg) {
 
59
    talk_base::ByteBuffer buf;
 
60
    msg.Write(&buf);
 
61
    Send(buf.Data(), buf.Length());
 
62
  }
 
63
  void Send(const char* buf, int len) {
 
64
    client_->SendTo(buf, len, server_addr);
 
65
  }
 
66
  StunMessage* Receive() {
 
67
    StunMessage* msg = NULL;
 
68
    talk_base::TestClient::Packet* packet = client_->NextPacket();
 
69
    if (packet) {
 
70
      talk_base::ByteBuffer buf(packet->buf, packet->size);
 
71
      msg = new StunMessage();
 
72
      msg->Read(&buf);
 
73
      delete packet;
 
74
    }
 
75
    return msg;
 
76
  }
 
77
 private:
 
78
  talk_base::scoped_ptr<talk_base::PhysicalSocketServer> pss_;
 
79
  talk_base::scoped_ptr<talk_base::VirtualSocketServer> ss_;
 
80
  talk_base::Thread worker_;
 
81
  talk_base::scoped_ptr<StunServer> server_;
 
82
  talk_base::scoped_ptr<talk_base::TestClient> client_;
 
83
};
 
84
 
 
85
TEST_F(StunServerTest, TestGood) {
 
86
  StunMessage req;
 
87
  std::string transaction_id = "0123456789ab";
 
88
  req.SetType(STUN_BINDING_REQUEST);
 
89
  req.SetTransactionID(transaction_id);
 
90
  Send(req);
 
91
 
 
92
  StunMessage* msg = Receive();
 
93
  ASSERT_TRUE(msg != NULL);
 
94
  EXPECT_EQ(STUN_BINDING_RESPONSE, msg->type());
 
95
  EXPECT_EQ(req.transaction_id(), msg->transaction_id());
 
96
 
 
97
  const StunAddressAttribute* mapped_addr =
 
98
      msg->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
 
99
  EXPECT_TRUE(mapped_addr != NULL);
 
100
  EXPECT_EQ(1, mapped_addr->family());
 
101
  EXPECT_EQ(client_addr.port(), mapped_addr->port());
 
102
  if (mapped_addr->ipaddr() != client_addr.ipaddr()) {
 
103
    LOG(LS_WARNING) << "Warning: mapped IP ("
 
104
                    << mapped_addr->ipaddr()
 
105
                    << ") != local IP (" << client_addr.ipaddr()
 
106
                    << ")";
 
107
  }
 
108
 
 
109
  delete msg;
 
110
}
 
111
 
 
112
TEST_F(StunServerTest, TestBad) {
 
113
  const char* bad = "this is a completely nonsensical message whose only "
 
114
                    "purpose is to make the parser go 'ack'.  it doesn't "
 
115
                    "look anything like a normal stun message";
 
116
  Send(bad, std::strlen(bad));
 
117
 
 
118
  StunMessage* msg = Receive();
 
119
  ASSERT_TRUE(msg != NULL);
 
120
  EXPECT_EQ(STUN_BINDING_ERROR_RESPONSE, msg->type());
 
121
 
 
122
  const StunErrorCodeAttribute* err = msg->GetErrorCode();
 
123
  EXPECT_TRUE(err != NULL);
 
124
  EXPECT_EQ(4, err->error_class());
 
125
  EXPECT_EQ(0, err->number());
 
126
  EXPECT_EQ("Bad Request", err->reason());
 
127
 
 
128
  delete msg;
 
129
}