blob: cb577cc631e2388a894b38e463b4face6909b7dc [file] [log] [blame]
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventodb4da5e2018-06-15 11:37:52 -04002/*
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -05003 * Copyright (c) 2013-2020 Regents of the University of California.
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/transport/tcp-transport.hpp"
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070025
26namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080027namespace tests {
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070028
Davide Pesaventoeee3e822016-11-26 19:19:34 +010029BOOST_AUTO_TEST_SUITE(Transport)
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050030BOOST_AUTO_TEST_SUITE(TestTcpTransport)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010031
32using ndn::Transport;
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070033
34BOOST_AUTO_TEST_CASE(GetDefaultSocketNameOk)
35{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070036 const auto got = TcpTransport::getSocketHostAndPortFromUri("tcp://127.0.0.1:6000");
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070037
38 BOOST_CHECK_EQUAL(got.first, "127.0.0.1");
39 BOOST_CHECK_EQUAL(got.second, "6000");
40}
41
42BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortBadMissingHost)
43{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070044 BOOST_CHECK_EXCEPTION(TcpTransport::getSocketHostAndPortFromUri("tcp://:6000"),
45 Transport::Error,
46 [] (const Transport::Error& error) {
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040047 return error.what() == "Malformed URI: tcp://:6000"s;
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070048 });
49}
50
51BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortOkOmittedPort)
52{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070053 const auto got = TcpTransport::getSocketHostAndPortFromUri("tcp://127.0.0.1");
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070054
55 BOOST_CHECK_EQUAL(got.first, "127.0.0.1");
56 BOOST_CHECK_EQUAL(got.second, "6363");
57}
58
59BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortNameOkOmittedHostOmittedPort)
60{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070061 const auto got = TcpTransport::getSocketHostAndPortFromUri("tcp://");
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070062
63 BOOST_CHECK_EQUAL(got.first, "localhost");
64 BOOST_CHECK_EQUAL(got.second, "6363");
65}
66
67BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortBadWrongTransport)
68{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070069 BOOST_CHECK_EXCEPTION(TcpTransport::getSocketHostAndPortFromUri("unix://"),
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070070 Transport::Error,
71 [] (const Transport::Error& error) {
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040072 return error.what() == "Cannot create TcpTransport from \"unix\" URI"s;
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070073 });
74}
75
76BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortBadMalformedUri)
77{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070078 BOOST_CHECK_EXCEPTION(TcpTransport::getSocketHostAndPortFromUri("tcp"),
79 Transport::Error,
80 [] (const Transport::Error& error) {
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040081 return error.what() == "Malformed URI: tcp"s;
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070082 });
83}
84
Davide Pesaventoeee3e822016-11-26 19:19:34 +010085BOOST_AUTO_TEST_SUITE_END() // TestTcpTransport
86BOOST_AUTO_TEST_SUITE_END() // Transport
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070087
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080088} // namespace tests
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070089} // namespace ndn