blob: eee7c2625ad3ea545865f019b5fbc5779db53c45 [file] [log] [blame]
Roger Meier01144552015-04-04 16:14:08 +02001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <boost/test/auto_unit_test.hpp>
21#include <thrift/transport/TSocket.h>
22#include <thrift/transport/TServerSocket.h>
23#include "TestPortFixture.h"
Roger Meierefd14e72015-04-14 21:06:14 +020024#include "TTransportCheckThrow.h"
Roger Meier01144552015-04-04 16:14:08 +020025
26using apache::thrift::transport::TServerSocket;
27using apache::thrift::transport::TSocket;
28using apache::thrift::transport::TTransport;
29using apache::thrift::transport::TTransportException;
30
31BOOST_FIXTURE_TEST_SUITE ( TServerSocketTest, TestPortFixture )
32
33class TestTServerSocket : public TServerSocket
34{
35 public:
36 TestTServerSocket(const std::string& address, int port) : TServerSocket(address, port) { }
37 using TServerSocket::acceptImpl;
38};
39
40BOOST_AUTO_TEST_CASE( test_bind_to_address )
41{
42 TestTServerSocket sock1("localhost", m_serverPort);
43 sock1.listen();
44 TSocket clientSock("localhost", m_serverPort);
45 clientSock.open();
46 boost::shared_ptr<TTransport> accepted = sock1.acceptImpl();
47 accepted->close();
48 sock1.close();
49
50 TServerSocket sock2("this.is.truly.an.unrecognizable.address.", m_serverPort);
51 BOOST_CHECK_THROW(sock2.listen(), TTransportException);
52 sock2.close();
53}
54
Roger Meierefd14e72015-04-14 21:06:14 +020055BOOST_AUTO_TEST_CASE( test_listen_valid_port )
56{
57 TServerSocket sock1(-1);
58 TTRANSPORT_CHECK_THROW(sock1.listen(), TTransportException::BAD_ARGS);
59
60 TServerSocket sock2(65536);
61 TTRANSPORT_CHECK_THROW(sock2.listen(), TTransportException::BAD_ARGS);
62}
63
Roger Meier01144552015-04-04 16:14:08 +020064BOOST_AUTO_TEST_CASE( test_close_before_listen )
65{
66 TServerSocket sock1("localhost", m_serverPort);
67 sock1.close();
68}
69
70BOOST_AUTO_TEST_CASE( test_get_port )
71{
72 TServerSocket sock1("localHost", 888);
73 BOOST_CHECK_EQUAL(888, sock1.getPort());
74}
75
76BOOST_AUTO_TEST_SUITE_END()
77