blob: 5d86baa2d4a6f357b05a2657b6bea60a5aca1a19 [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 Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040026namespace ndn::tests {
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070027
Davide Pesaventoeee3e822016-11-26 19:19:34 +010028BOOST_AUTO_TEST_SUITE(Transport)
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050029BOOST_AUTO_TEST_SUITE(TestTcpTransport)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010030
31using ndn::Transport;
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070032
33BOOST_AUTO_TEST_CASE(GetDefaultSocketNameOk)
34{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070035 const auto got = TcpTransport::getSocketHostAndPortFromUri("tcp://127.0.0.1:6000");
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070036
37 BOOST_CHECK_EQUAL(got.first, "127.0.0.1");
38 BOOST_CHECK_EQUAL(got.second, "6000");
39}
40
41BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortBadMissingHost)
42{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070043 BOOST_CHECK_EXCEPTION(TcpTransport::getSocketHostAndPortFromUri("tcp://:6000"),
44 Transport::Error,
45 [] (const Transport::Error& error) {
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040046 return error.what() == "Malformed URI: tcp://:6000"s;
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070047 });
48}
49
50BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortOkOmittedPort)
51{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070052 const auto got = TcpTransport::getSocketHostAndPortFromUri("tcp://127.0.0.1");
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070053
54 BOOST_CHECK_EQUAL(got.first, "127.0.0.1");
55 BOOST_CHECK_EQUAL(got.second, "6363");
56}
57
58BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortNameOkOmittedHostOmittedPort)
59{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070060 const auto got = TcpTransport::getSocketHostAndPortFromUri("tcp://");
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070061
62 BOOST_CHECK_EQUAL(got.first, "localhost");
63 BOOST_CHECK_EQUAL(got.second, "6363");
64}
65
66BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortBadWrongTransport)
67{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070068 BOOST_CHECK_EXCEPTION(TcpTransport::getSocketHostAndPortFromUri("unix://"),
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070069 Transport::Error,
70 [] (const Transport::Error& error) {
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040071 return error.what() == "Cannot create TcpTransport from \"unix\" URI"s;
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070072 });
73}
74
75BOOST_AUTO_TEST_CASE(GetDefaultSocketHostAndPortBadMalformedUri)
76{
Alexander Afanasyev57e00362016-06-23 13:22:54 -070077 BOOST_CHECK_EXCEPTION(TcpTransport::getSocketHostAndPortFromUri("tcp"),
78 Transport::Error,
79 [] (const Transport::Error& error) {
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040080 return error.what() == "Malformed URI: tcp"s;
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070081 });
82}
83
Davide Pesaventoeee3e822016-11-26 19:19:34 +010084BOOST_AUTO_TEST_SUITE_END() // TestTcpTransport
85BOOST_AUTO_TEST_SUITE_END() // Transport
Steve DiBenedettoa8659ff2014-12-04 14:50:28 -070086
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040087} // namespace ndn::tests