blob: 929defa0a89c2caf093b261ce95c97e6bf074a90 [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
zeshuai0076b1cb302020-05-27 12:08:01 +080020#include <boost/test/unit_test.hpp>
Roger Meier01144552015-04-04 16:14:08 +020021#include <thrift/transport/TSocket.h>
22#include <thrift/transport/TServerSocket.h>
cyy316723a2019-01-05 16:35:14 +080023#include <memory>
Roger Meierefd14e72015-04-14 21:06:14 +020024#include "TTransportCheckThrow.h"
Ben Craig1684c422015-04-24 08:52:44 -050025#include <iostream>
Roger Meier01144552015-04-04 16:14:08 +020026
27using apache::thrift::transport::TServerSocket;
28using apache::thrift::transport::TSocket;
29using apache::thrift::transport::TTransport;
30using apache::thrift::transport::TTransportException;
cyy316723a2019-01-05 16:35:14 +080031using std::shared_ptr;
Roger Meier01144552015-04-04 16:14:08 +020032
John Siroiscab56002016-02-12 17:58:14 -070033BOOST_AUTO_TEST_SUITE(TServerSocketTest)
Roger Meier01144552015-04-04 16:14:08 +020034
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020035BOOST_AUTO_TEST_CASE(test_bind_to_address) {
John Siroiscab56002016-02-12 17:58:14 -070036 TServerSocket sock1("localhost", 0);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020037 sock1.listen();
Mario Emmenlauered0bad32020-04-24 08:50:43 +020038 BOOST_CHECK(sock1.isOpen());
John Siroiscab56002016-02-12 17:58:14 -070039 int port = sock1.getPort();
40 TSocket clientSock("localhost", port);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020041 clientSock.open();
James E. King, III82ae9572017-08-05 12:23:54 -040042 shared_ptr<TTransport> accepted = sock1.accept();
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020043 accepted->close();
44 sock1.close();
Roger Meier01144552015-04-04 16:14:08 +020045
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020046 std::cout << "An error message from getaddrinfo on the console is expected:" << std::endl;
John Siroiscab56002016-02-12 17:58:14 -070047 TServerSocket sock2("257.258.259.260", 0);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020048 BOOST_CHECK_THROW(sock2.listen(), TTransportException);
49 sock2.close();
Roger Meier01144552015-04-04 16:14:08 +020050}
51
Mario Emmenlauered0bad32020-04-24 08:50:43 +020052BOOST_AUTO_TEST_CASE(test_listen_invalid_port) {
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020053 TServerSocket sock1(-1);
54 TTRANSPORT_CHECK_THROW(sock1.listen(), TTransportException::BAD_ARGS);
Mario Emmenlauered0bad32020-04-24 08:50:43 +020055 BOOST_CHECK(!sock1.isOpen());
Roger Meierefd14e72015-04-14 21:06:14 +020056
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020057 TServerSocket sock2(65536);
58 TTRANSPORT_CHECK_THROW(sock2.listen(), TTransportException::BAD_ARGS);
Mario Emmenlauered0bad32020-04-24 08:50:43 +020059 BOOST_CHECK(!sock1.isOpen());
Roger Meierefd14e72015-04-14 21:06:14 +020060}
61
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020062BOOST_AUTO_TEST_CASE(test_close_before_listen) {
John Siroiscab56002016-02-12 17:58:14 -070063 TServerSocket sock1("localhost", 0);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020064 sock1.close();
Mario Emmenlauered0bad32020-04-24 08:50:43 +020065 BOOST_CHECK(!sock1.isOpen());
Roger Meier01144552015-04-04 16:14:08 +020066}
67
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020068BOOST_AUTO_TEST_CASE(test_get_port) {
69 TServerSocket sock1("localHost", 888);
70 BOOST_CHECK_EQUAL(888, sock1.getPort());
Roger Meier01144552015-04-04 16:14:08 +020071}
72
73BOOST_AUTO_TEST_SUITE_END()