diff --git a/test/system/tools/fds/CliDiscoveryManagerTests.cpp b/test/system/tools/fds/CliDiscoveryManagerTests.cpp index c6e8ac0749..f767766bbb 100644 --- a/test/system/tools/fds/CliDiscoveryManagerTests.cpp +++ b/test/system/tools/fds/CliDiscoveryManagerTests.cpp @@ -409,6 +409,64 @@ class CliDiscoveryManagerTest : public ::testing::Test Parser parse; }; +TEST_F(CliDiscoveryManagerTest, InitialOptionsFail) +{ + // Domain with Locator list should fail if check_nonOpts is true + const char* argv[] = {"-d", "4", "127.0.0.1:0;192.168.1.42:1"}; + createOptionsAndParser(3, argv); + bool result = manager.initial_options_fail(options, parse, true); + EXPECT_TRUE(result); + // Domain with Locator list should fail if check_nonOpts is false + result = manager.initial_options_fail(options, parse, false); + EXPECT_FALSE(result); + + // Locator list with domain should fail if check_nonOpts is true + const char* argv2[] = {"127.0.0.1:0;192.168.1.42:1", "-d", "4"}; + createOptionsAndParser(3, argv2); + result = manager.initial_options_fail(options, parse, true); + EXPECT_TRUE(result); + + // Correct options + const char* argv3[] = {"-d", "4"}; + createOptionsAndParser(2, argv3); + result = manager.initial_options_fail(options, parse, true); + EXPECT_FALSE(result); + + // Unknown option + const char* argv4[] = {"-z", "4"}; + createOptionsAndParser(2, argv4); + result = manager.initial_options_fail(options, parse, true); + EXPECT_TRUE(result); +} + +TEST_F(CliDiscoveryManagerTest, GetDiscoveryServerPortFromCLI) +{ + // Port directly received from CLI + const char* argv_udp[] = {"-p", "11811"}; + createOptionsAndParser(2, argv_udp); + Option* udp_port_opt = getOption(UDP_PORT); + uint16_t udp_port = manager.getDiscoveryServerPort(udp_port_opt); + EXPECT_EQ(udp_port, 11811); + const char* argv_tcp[] = {"-q", "42100"}; + createOptionsAndParser(2, argv_tcp); + Option* tcp_port_opt = getOption(TCP_PORT); + uint16_t tcp_port = manager.getDiscoveryServerPort(tcp_port_opt); + EXPECT_EQ(tcp_port, 42100); +} + +#ifndef _WIN32 +TEST_F(CliDiscoveryManagerTest, GetDiscoveryServerPortFromDomainId) +{ + PortParameters port_params; + uint16_t port_1 = manager.getDiscoveryServerPort(1); + EXPECT_EQ(port_1, port_params.getDiscoveryServerPort(1)); + uint16_t port_232 = manager.getDiscoveryServerPort(232); + EXPECT_EQ(port_232, port_params.getDiscoveryServerPort(232)); + + uint16_t port_fail = manager.getDiscoveryServerPort(233); + EXPECT_EQ(port_fail, 0); +} + TEST_F(CliDiscoveryManagerTest, GetDomainIdFromCLI) { const char* argv[] = {"-d", "4"}; @@ -499,64 +557,6 @@ TEST_F(CliDiscoveryManagerTest, GetRemoteServersWithDomainParam) EXPECT_TRUE(expected_ports.empty()); } -TEST_F(CliDiscoveryManagerTest, InitialOptionsFail) -{ - // Domain with Locator list should fail if check_nonOpts is true - const char* argv[] = {"-d", "4", "127.0.0.1:0;192.168.1.42:1"}; - createOptionsAndParser(3, argv); - bool result = manager.initial_options_fail(options, parse, true); - EXPECT_TRUE(result); - // Domain with Locator list should fail if check_nonOpts is false - result = manager.initial_options_fail(options, parse, false); - EXPECT_FALSE(result); - - // Locator list with domain should fail if check_nonOpts is true - const char* argv2[] = {"127.0.0.1:0;192.168.1.42:1", "-d", "4"}; - createOptionsAndParser(3, argv2); - result = manager.initial_options_fail(options, parse, true); - EXPECT_TRUE(result); - - // Correct options - const char* argv3[] = {"-d", "4"}; - createOptionsAndParser(2, argv3); - result = manager.initial_options_fail(options, parse, true); - EXPECT_FALSE(result); - - // Unknown option - const char* argv4[] = {"-z", "4"}; - createOptionsAndParser(2, argv4); - result = manager.initial_options_fail(options, parse, true); - EXPECT_TRUE(result); -} - -TEST_F(CliDiscoveryManagerTest, GetDiscoveryServerPortFromCLI) -{ - // Port directly received from CLI - const char* argv_udp[] = {"-p", "11811"}; - createOptionsAndParser(2, argv_udp); - Option* udp_port_opt = getOption(UDP_PORT); - uint16_t udp_port = manager.getDiscoveryServerPort(udp_port_opt); - EXPECT_EQ(udp_port, 11811); - const char* argv_tcp[] = {"-q", "42100"}; - createOptionsAndParser(2, argv_tcp); - Option* tcp_port_opt = getOption(TCP_PORT); - uint16_t tcp_port = manager.getDiscoveryServerPort(tcp_port_opt); - EXPECT_EQ(tcp_port, 42100); -} - -TEST_F(CliDiscoveryManagerTest, GetDiscoveryServerPortFromDomainId) -{ - PortParameters port_params; - uint16_t port_1 = manager.getDiscoveryServerPort(1); - EXPECT_EQ(port_1, port_params.getDiscoveryServerPort(1)); - uint16_t port_232 = manager.getDiscoveryServerPort(232); - EXPECT_EQ(port_232, port_params.getDiscoveryServerPort(232)); - - uint16_t port_fail = manager.getDiscoveryServerPort(233); - EXPECT_EQ(port_fail, 0); -} - -#ifndef _WIN32 TEST_F(CliDiscoveryManagerTest, ExecCommand) { std::string result = manager.execCommand("echo \"Hello CLI Tool\""); diff --git a/tools/fds/CliDiscoveryManager.cpp b/tools/fds/CliDiscoveryManager.cpp index 4ade9eb50e..a91a193f63 100644 --- a/tools/fds/CliDiscoveryManager.cpp +++ b/tools/fds/CliDiscoveryManager.cpp @@ -122,6 +122,80 @@ std::string CliDiscoveryManager::get_default_shared_dir() return shm_path; } +bool CliDiscoveryManager::initial_options_fail( + const std::vector& options, + option::Parser& parse, + bool check_nonOpts) +{ + // Check the command line options + if (parse.error()) + { + option::printUsage(std::cout, usage); + return true; + } + + if (options[UNKNOWN]) + { + EPROSIMA_LOG_ERROR(CLI, "Unknown option: " << options[UNKNOWN].name); + option::printUsage(std::cout, usage); + return true; + } + + // No arguments beyond options + if (check_nonOpts) + { + int noopts = parse.nonOptionsCount(); + if (noopts) + { + std::string sep(noopts == 1 ? "argument: " : "arguments: "); + std::cout << "Unknown "; + + while (noopts--) + { + std::cout << sep << parse.nonOption(noopts); + sep = ", "; + } + + std::cout << std::endl; + return true; + } + } + return false; +} + +uint16_t CliDiscoveryManager::getDiscoveryServerPort( + const eprosima::option::Option* portArg) +{ + uint16_t port = 0; + if (nullptr != portArg) + { + std::stringstream port_stream; + + port_stream << portArg->arg; + port_stream >> port; + if (!port_stream.eof()) + { + EPROSIMA_LOG_WARNING(CLI, "Invalid listening locator port specified:" << port); + return 0; + } + } + return port; +} + +#ifndef _WIN32 +uint16_t CliDiscoveryManager::getDiscoveryServerPort( + const uint32_t& domainId) +{ + if (domainId > 232) + { + EPROSIMA_LOG_WARNING(CLI, "Domain ID " << domainId << " is too high and cannot run in an unreachable port."); + return 0; + } + uint16_t port = port_params_.getDiscoveryServerPort(domainId); + + return port; +} + DomainId_t CliDiscoveryManager::get_domain_id( const eprosima::option::Option* domain_id) { @@ -199,80 +273,6 @@ std::string CliDiscoveryManager::getRemoteServers( return servers; } -bool CliDiscoveryManager::initial_options_fail( - const std::vector& options, - option::Parser& parse, - bool check_nonOpts) -{ - // Check the command line options - if (parse.error()) - { - option::printUsage(std::cout, usage); - return true; - } - - if (options[UNKNOWN]) - { - EPROSIMA_LOG_ERROR(CLI, "Unknown option: " << options[UNKNOWN].name); - option::printUsage(std::cout, usage); - return true; - } - - // No arguments beyond options - if (check_nonOpts) - { - int noopts = parse.nonOptionsCount(); - if (noopts) - { - std::string sep(noopts == 1 ? "argument: " : "arguments: "); - std::cout << "Unknown "; - - while (noopts--) - { - std::cout << sep << parse.nonOption(noopts); - sep = ", "; - } - - std::cout << std::endl; - return true; - } - } - return false; -} - -uint16_t CliDiscoveryManager::getDiscoveryServerPort( - const eprosima::option::Option* portArg) -{ - uint16_t port = 0; - if (nullptr != portArg) - { - std::stringstream port_stream; - - port_stream << portArg->arg; - port_stream >> port; - if (!port_stream.eof()) - { - EPROSIMA_LOG_WARNING(CLI, "Invalid listening locator port specified:" << port); - return 0; - } - } - return port; -} - -uint16_t CliDiscoveryManager::getDiscoveryServerPort( - const uint32_t& domainId) -{ - if (domainId > 232) - { - EPROSIMA_LOG_WARNING(CLI, "Domain ID " << domainId << " is too high and cannot run in an unreachable port."); - return 0; - } - uint16_t port = port_params_.getDiscoveryServerPort(domainId); - - return port; -} - -#ifndef _WIN32 std::string CliDiscoveryManager::execCommand( const std::string& command) { diff --git a/tools/fds/CliDiscoveryManager.hpp b/tools/fds/CliDiscoveryManager.hpp index 02e0eaec4b..cc58f307f6 100644 --- a/tools/fds/CliDiscoveryManager.hpp +++ b/tools/fds/CliDiscoveryManager.hpp @@ -24,10 +24,6 @@ #include #include -#ifdef WIN32 -#define pid_t int -#endif // ifdef WIN32 - constexpr const char* domain_env_var = "ROS_DOMAIN_ID"; constexpr const char* remote_servers_env_var = "ROS_STATIC_PEERS"; constexpr const char* default_ip = "0.0.0.0"; @@ -103,33 +99,6 @@ class CliDiscoveryManager */ std::string get_default_shared_dir(); - /** - * @brief Get the domain id from the environment variable or the CLI argument. If none of the two is - * provided, the default domain id is 0. - * @param domain_id The domain id argument - * @return The domain id - */ - DomainId_t get_domain_id( - const eprosima::option::Option* domain_id); - - /** - * @brief Set the remote servers list from the environment variable. - * Previously set servers are not cleared. - * @param target_list The list to be set - */ - void addRemoteServersFromEnv( - rtps::LocatorList_t& target_list); - - /** - * @brief Load the remote servers information from the CLI or the environment variable. - * @param parse The parser object to be used - * @param numServs Number of nonOpts, which are meant to be servers - * @return The servers locators in string format - */ - std::string getRemoteServers( - option::Parser& parse, - int numServs); - /** * @brief Check if the options received by the CLI are free of errors. * @param options The options received from the CLI @@ -150,6 +119,7 @@ class CliDiscoveryManager uint16_t getDiscoveryServerPort( const eprosima::option::Option* portArg); +#ifndef _WIN32 /** * @brief Get the port of the Discovery Server running in the specified domain. * @param domainId The domain id of the Discovery Server @@ -158,7 +128,33 @@ class CliDiscoveryManager uint16_t getDiscoveryServerPort( const uint32_t& domainId); -#ifndef _WIN32 + /** + * @brief Get the domain id from the environment variable or the CLI argument. If none of the two is + * provided, the default domain id is 0. + * @param domain_id The domain id argument + * @return The domain id + */ + DomainId_t get_domain_id( + const eprosima::option::Option* domain_id); + + /** + * @brief Set the remote servers list from the environment variable. + * Previously set servers are not cleared. + * @param target_list The list to be set + */ + void addRemoteServersFromEnv( + rtps::LocatorList_t& target_list); + + /** + * @brief Load the remote servers information from the CLI or the environment variable. + * @param parse The parser object to be used + * @param numServs Number of nonOpts, which are meant to be servers + * @return The servers locators in string format + */ + std::string getRemoteServers( + option::Parser& parse, + int numServs); + /** * @brief Execute the command provided by the user. * @param command The command to be executed @@ -278,6 +274,7 @@ class CliDiscoveryManager const std::vector& options, option::Parser& parse); +#ifndef _WIN32 /** * @brief Launch the AUTO mode of the CLI. It checks if a new Discovery Server exists in the * specified domain. It it does not exist, it creates a new one. If it exists, it does nothing. @@ -288,7 +285,6 @@ class CliDiscoveryManager const std::vector& options, option::Parser& parse); -#ifndef _WIN32 /** * @brief Starts a new Discovery Server in the specified domain if there is not an active server * already running. It does nothing if there is an active server.