Roger Meier | 0114455 | 2015-04-04 16:14:08 +0200 | [diff] [blame] | 1 | /* |
| 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> |
Roger Meier | efd14e7 | 2015-04-14 21:06:14 +0200 | [diff] [blame] | 23 | #include "TTransportCheckThrow.h" |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 24 | #include <iostream> |
Roger Meier | 0114455 | 2015-04-04 16:14:08 +0200 | [diff] [blame] | 25 | |
| 26 | using apache::thrift::transport::TServerSocket; |
| 27 | using apache::thrift::transport::TSocket; |
| 28 | using apache::thrift::transport::TTransport; |
| 29 | using apache::thrift::transport::TTransportException; |
| 30 | |
John Sirois | cab5600 | 2016-02-12 17:58:14 -0700 | [diff] [blame^] | 31 | BOOST_AUTO_TEST_SUITE(TServerSocketTest) |
Roger Meier | 0114455 | 2015-04-04 16:14:08 +0200 | [diff] [blame] | 32 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 33 | BOOST_AUTO_TEST_CASE(test_bind_to_address) { |
John Sirois | cab5600 | 2016-02-12 17:58:14 -0700 | [diff] [blame^] | 34 | TServerSocket sock1("localhost", 0); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 35 | sock1.listen(); |
John Sirois | cab5600 | 2016-02-12 17:58:14 -0700 | [diff] [blame^] | 36 | int port = sock1.getPort(); |
| 37 | TSocket clientSock("localhost", port); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 38 | clientSock.open(); |
| 39 | boost::shared_ptr<TTransport> accepted = sock1.accept(); |
| 40 | accepted->close(); |
| 41 | sock1.close(); |
Roger Meier | 0114455 | 2015-04-04 16:14:08 +0200 | [diff] [blame] | 42 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 43 | std::cout << "An error message from getaddrinfo on the console is expected:" << std::endl; |
John Sirois | cab5600 | 2016-02-12 17:58:14 -0700 | [diff] [blame^] | 44 | TServerSocket sock2("257.258.259.260", 0); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 45 | BOOST_CHECK_THROW(sock2.listen(), TTransportException); |
| 46 | sock2.close(); |
Roger Meier | 0114455 | 2015-04-04 16:14:08 +0200 | [diff] [blame] | 47 | } |
| 48 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 49 | BOOST_AUTO_TEST_CASE(test_listen_valid_port) { |
| 50 | TServerSocket sock1(-1); |
| 51 | TTRANSPORT_CHECK_THROW(sock1.listen(), TTransportException::BAD_ARGS); |
Roger Meier | efd14e7 | 2015-04-14 21:06:14 +0200 | [diff] [blame] | 52 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 53 | TServerSocket sock2(65536); |
| 54 | TTRANSPORT_CHECK_THROW(sock2.listen(), TTransportException::BAD_ARGS); |
Roger Meier | efd14e7 | 2015-04-14 21:06:14 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 57 | BOOST_AUTO_TEST_CASE(test_close_before_listen) { |
John Sirois | cab5600 | 2016-02-12 17:58:14 -0700 | [diff] [blame^] | 58 | TServerSocket sock1("localhost", 0); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 59 | sock1.close(); |
Roger Meier | 0114455 | 2015-04-04 16:14:08 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 62 | BOOST_AUTO_TEST_CASE(test_get_port) { |
| 63 | TServerSocket sock1("localHost", 888); |
| 64 | BOOST_CHECK_EQUAL(888, sock1.getPort()); |
Roger Meier | 0114455 | 2015-04-04 16:14:08 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | BOOST_AUTO_TEST_SUITE_END() |