Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "nfdc/command-parser.hpp" |
| 27 | |
| 28 | #include "tests/test-common.hpp" |
| 29 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 30 | namespace nfd::tools::nfdc::tests { |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 31 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 32 | BOOST_AUTO_TEST_SUITE(Nfdc) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(TestCommandParser) |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 34 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 35 | BOOST_AUTO_TEST_CASE(PrintAvailableIn) |
| 36 | { |
| 37 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_NONE), "hidden"); |
| 38 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_ONE_SHOT), "one-shot|hidden"); |
| 39 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_BATCH), "batch|hidden"); |
| 40 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_HELP), "none"); |
| 41 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_ALL), "one-shot|batch"); |
| 42 | } |
| 43 | |
| 44 | BOOST_AUTO_TEST_CASE(PrintParseMode) |
| 45 | { |
| 46 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ParseMode::ONE_SHOT), "one-shot"); |
| 47 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ParseMode::BATCH), "batch"); |
| 48 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ParseMode>(0xFF)), "255"); |
| 49 | } |
| 50 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 51 | BOOST_AUTO_TEST_CASE(Basic) |
| 52 | { |
| 53 | CommandParser parser; |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 54 | ExecuteCommand dummyExecute = [] (ExecuteContext&) { BOOST_ERROR("should not be called"); }; |
| 55 | |
| 56 | BOOST_CHECK(parser.listCommands("", ParseMode::ONE_SHOT).empty()); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 57 | |
Davide Pesavento | d214744 | 2018-02-19 23:58:17 -0500 | [diff] [blame] | 58 | CommandDefinition defFoo("foo", ""); |
| 59 | parser.addCommand(defFoo, dummyExecute, AVAILABLE_IN_ONE_SHOT | AVAILABLE_IN_HELP); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 60 | |
| 61 | CommandDefinition defStatusShow("status", "show"); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 62 | parser.addCommand(defStatusShow, dummyExecute); |
Davide Pesavento | d214744 | 2018-02-19 23:58:17 -0500 | [diff] [blame] | 63 | parser.addAlias("status", "show", ""); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 64 | parser.addAlias("status", "show", "list"); |
| 65 | BOOST_CHECK_THROW(parser.addAlias("status", "show2", "list"), std::out_of_range); |
| 66 | |
| 67 | CommandDefinition defRouteList("route", "list"); |
| 68 | defRouteList |
| 69 | .addArg("nexthop", ArgValueType::FACE_ID_OR_URI, Required::NO, Positional::YES); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 70 | parser.addCommand(defRouteList, dummyExecute); |
Davide Pesavento | d214744 | 2018-02-19 23:58:17 -0500 | [diff] [blame] | 71 | parser.addAlias("route", "list", ""); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 72 | |
| 73 | CommandDefinition defRouteAdd("route", "add"); |
| 74 | defRouteAdd |
| 75 | .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES) |
| 76 | .addArg("nexthop", ArgValueType::FACE_ID_OR_URI, Required::YES, Positional::YES); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 77 | parser.addCommand(defRouteAdd, dummyExecute); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 78 | parser.addAlias("route", "add", "add2"); |
| 79 | |
Davide Pesavento | 2a58815 | 2018-02-19 18:10:03 -0500 | [diff] [blame] | 80 | CommandDefinition defHidden("hidden", ""); |
| 81 | parser.addCommand(defHidden, dummyExecute, AVAILABLE_IN_BATCH); |
| 82 | |
| 83 | BOOST_CHECK_EQUAL(parser.listCommands("", ParseMode::ONE_SHOT).size(), 4); |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(parser.listCommands("", ParseMode::BATCH).size(), 3); |
| 85 | BOOST_CHECK_EQUAL(parser.listCommands("route", ParseMode::ONE_SHOT).size(), 2); |
| 86 | BOOST_CHECK_EQUAL(parser.listCommands("unknown", ParseMode::ONE_SHOT).size(), 0); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 87 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 88 | std::string noun, verb; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 89 | CommandArguments ca; |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 90 | ExecuteCommand execute; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 91 | |
Davide Pesavento | d214744 | 2018-02-19 23:58:17 -0500 | [diff] [blame] | 92 | std::tie(noun, verb, ca, execute) = parser.parse({"foo"}, ParseMode::ONE_SHOT); |
| 93 | BOOST_CHECK_EQUAL(noun, "foo"); |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 94 | BOOST_CHECK_EQUAL(verb, ""); |
| 95 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 96 | std::tie(noun, verb, ca, execute) = parser.parse({"status"}, ParseMode::ONE_SHOT); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 97 | BOOST_CHECK_EQUAL(noun, "status"); |
| 98 | BOOST_CHECK_EQUAL(verb, "show"); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 99 | |
Davide Pesavento | d214744 | 2018-02-19 23:58:17 -0500 | [diff] [blame] | 100 | std::tie(noun, verb, ca, execute) = parser.parse({"status", "list"}, ParseMode::ONE_SHOT); |
| 101 | BOOST_CHECK_EQUAL(noun, "status"); |
| 102 | BOOST_CHECK_EQUAL(verb, "show"); |
| 103 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 104 | std::tie(noun, verb, ca, execute) = parser.parse({"route", "add", "/n", "300"}, ParseMode::ONE_SHOT); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 105 | BOOST_CHECK_EQUAL(noun, "route"); |
| 106 | BOOST_CHECK_EQUAL(verb, "add"); |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 107 | BOOST_CHECK_EQUAL(std::any_cast<Name>(ca.at("prefix")), "/n"); |
| 108 | BOOST_CHECK_EQUAL(std::any_cast<uint64_t>(ca.at("nexthop")), 300); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 109 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 110 | std::tie(noun, verb, ca, execute) = parser.parse({"route", "add2", "/n", "300"}, ParseMode::ONE_SHOT); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 111 | BOOST_CHECK_EQUAL(noun, "route"); |
| 112 | BOOST_CHECK_EQUAL(verb, "add"); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 113 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 114 | std::tie(noun, verb, ca, execute) = parser.parse({"route", "list", "400"}, ParseMode::ONE_SHOT); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(noun, "route"); |
| 116 | BOOST_CHECK_EQUAL(verb, "list"); |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(std::any_cast<uint64_t>(ca.at("nexthop")), 400); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 118 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 119 | BOOST_CHECK_THROW(parser.parse({}, ParseMode::ONE_SHOT), |
| 120 | CommandParser::NoSuchCommandError); |
Davide Pesavento | d214744 | 2018-02-19 23:58:17 -0500 | [diff] [blame] | 121 | BOOST_CHECK_THROW(parser.parse({"bar"}, ParseMode::ONE_SHOT), |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 122 | CommandParser::NoSuchCommandError); |
| 123 | BOOST_CHECK_THROW(parser.parse({"status", "hide"}, ParseMode::ONE_SHOT), |
| 124 | CommandParser::NoSuchCommandError); |
Davide Pesavento | d214744 | 2018-02-19 23:58:17 -0500 | [diff] [blame] | 125 | BOOST_CHECK_THROW(parser.parse({"status", "show", "something"}, ParseMode::ONE_SHOT), |
| 126 | CommandDefinition::Error); |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 127 | BOOST_CHECK_THROW(parser.parse({"route", "66"}, ParseMode::ONE_SHOT), |
| 128 | CommandParser::NoSuchCommandError); |
| 129 | BOOST_CHECK_THROW(parser.parse({"route", "add"}, ParseMode::ONE_SHOT), |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 130 | CommandDefinition::Error); |
Davide Pesavento | 2a58815 | 2018-02-19 18:10:03 -0500 | [diff] [blame] | 131 | BOOST_CHECK_THROW(parser.parse({"hidden"}, ParseMode::ONE_SHOT), |
| 132 | CommandParser::NoSuchCommandError); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 135 | BOOST_AUTO_TEST_SUITE_END() // TestCommandParser |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 136 | BOOST_AUTO_TEST_SUITE_END() // Nfdc |
| 137 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 138 | } // namespace nfd::tools::nfdc::tests |