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 | /* |
| 3 | * Copyright (c) 2014-2018, 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 | |
| 30 | namespace nfd { |
| 31 | namespace tools { |
| 32 | namespace nfdc { |
| 33 | namespace tests { |
| 34 | |
| 35 | using namespace nfd::tests; |
| 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(Nfdc) |
| 38 | BOOST_FIXTURE_TEST_SUITE(TestCommandParser, BaseFixture) |
| 39 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 40 | BOOST_AUTO_TEST_CASE(PrintAvailableIn) |
| 41 | { |
| 42 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_NONE), "hidden"); |
| 43 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_ONE_SHOT), "one-shot|hidden"); |
| 44 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_BATCH), "batch|hidden"); |
| 45 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_HELP), "none"); |
| 46 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_ALL), "one-shot|batch"); |
| 47 | } |
| 48 | |
| 49 | BOOST_AUTO_TEST_CASE(PrintParseMode) |
| 50 | { |
| 51 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ParseMode::ONE_SHOT), "one-shot"); |
| 52 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ParseMode::BATCH), "batch"); |
| 53 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ParseMode>(0xFF)), "255"); |
| 54 | } |
| 55 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 56 | BOOST_AUTO_TEST_CASE(Basic) |
| 57 | { |
| 58 | CommandParser parser; |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 59 | ExecuteCommand dummyExecute = [] (ExecuteContext&) { BOOST_ERROR("should not be called"); }; |
| 60 | |
| 61 | BOOST_CHECK(parser.listCommands("", ParseMode::ONE_SHOT).empty()); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 62 | |
| 63 | CommandDefinition defHelp("help", ""); |
| 64 | defHelp |
| 65 | .addArg("noun", ArgValueType::STRING, Required::NO, Positional::YES) |
| 66 | .addArg("verb", ArgValueType::STRING, Required::NO, Positional::YES); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 67 | parser.addCommand(defHelp, dummyExecute, AVAILABLE_IN_ONE_SHOT); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 68 | |
| 69 | CommandDefinition defStatusShow("status", "show"); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 70 | parser.addCommand(defStatusShow, dummyExecute); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 71 | parser.addAlias("status", "show", "list"); |
| 72 | BOOST_CHECK_THROW(parser.addAlias("status", "show2", "list"), std::out_of_range); |
| 73 | |
| 74 | CommandDefinition defRouteList("route", "list"); |
| 75 | defRouteList |
| 76 | .addArg("nexthop", ArgValueType::FACE_ID_OR_URI, Required::NO, Positional::YES); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 77 | parser.addCommand(defRouteList, dummyExecute); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 78 | |
| 79 | CommandDefinition defRouteAdd("route", "add"); |
| 80 | defRouteAdd |
| 81 | .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES) |
| 82 | .addArg("nexthop", ArgValueType::FACE_ID_OR_URI, Required::YES, Positional::YES); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 83 | parser.addCommand(defRouteAdd, dummyExecute); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 84 | parser.addAlias("route", "add", "add2"); |
| 85 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 86 | BOOST_CHECK_EQUAL(parser.listCommands("", ParseMode::ONE_SHOT).size(), 3); |
| 87 | BOOST_CHECK_EQUAL(parser.listCommands("", ParseMode::BATCH).size(), 3); |
| 88 | BOOST_CHECK_EQUAL(parser.listCommands("route", ParseMode::ONE_SHOT).size(), 2); |
| 89 | BOOST_CHECK_EQUAL(parser.listCommands("unknown", ParseMode::ONE_SHOT).size(), 0); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 90 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 91 | std::string noun, verb; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 92 | CommandArguments ca; |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 93 | ExecuteCommand execute; |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 94 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 95 | std::tie(noun, verb, ca, execute) = parser.parse({"help"}, ParseMode::ONE_SHOT); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(noun, "help"); |
| 97 | BOOST_CHECK_EQUAL(verb, ""); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 98 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 99 | std::tie(noun, verb, ca, execute) = parser.parse({"help", "foo"}, ParseMode::ONE_SHOT); |
| 100 | BOOST_CHECK_EQUAL(noun, "help"); |
| 101 | BOOST_CHECK_EQUAL(verb, ""); |
| 102 | |
| 103 | std::tie(noun, verb, ca, execute) = parser.parse({"foo", "help"}, ParseMode::ONE_SHOT); |
| 104 | BOOST_CHECK_EQUAL(noun, "help"); |
| 105 | BOOST_CHECK_EQUAL(verb, ""); |
| 106 | |
| 107 | std::tie(noun, verb, ca, execute) = parser.parse({"foo", "bar", "-h"}, ParseMode::ONE_SHOT); |
| 108 | BOOST_CHECK_EQUAL(noun, "help"); |
| 109 | BOOST_CHECK_EQUAL(verb, ""); |
| 110 | |
| 111 | std::tie(noun, verb, ca, execute) = parser.parse({"status"}, ParseMode::ONE_SHOT); |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 112 | BOOST_CHECK_EQUAL(noun, "status"); |
| 113 | BOOST_CHECK_EQUAL(verb, "show"); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 114 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 115 | 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] | 116 | BOOST_CHECK_EQUAL(noun, "route"); |
| 117 | BOOST_CHECK_EQUAL(verb, "add"); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(boost::any_cast<Name>(ca.at("prefix")), "/n"); |
| 119 | BOOST_CHECK_EQUAL(boost::any_cast<uint64_t>(ca.at("nexthop")), 300); |
| 120 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 121 | 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] | 122 | BOOST_CHECK_EQUAL(noun, "route"); |
| 123 | BOOST_CHECK_EQUAL(verb, "add"); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 124 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 125 | 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] | 126 | BOOST_CHECK_EQUAL(noun, "route"); |
| 127 | BOOST_CHECK_EQUAL(verb, "list"); |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 128 | BOOST_CHECK_EQUAL(boost::any_cast<uint64_t>(ca.at("nexthop")), 400); |
| 129 | |
Davide Pesavento | e0bae0f | 2018-02-17 22:07:52 -0500 | [diff] [blame] | 130 | BOOST_CHECK_THROW(parser.parse({}, ParseMode::ONE_SHOT), |
| 131 | CommandParser::NoSuchCommandError); |
| 132 | BOOST_CHECK_THROW(parser.parse({"cant-help"}, ParseMode::ONE_SHOT), |
| 133 | CommandParser::NoSuchCommandError); |
| 134 | BOOST_CHECK_THROW(parser.parse({"status", "hide"}, ParseMode::ONE_SHOT), |
| 135 | CommandParser::NoSuchCommandError); |
| 136 | BOOST_CHECK_THROW(parser.parse({"route", "66"}, ParseMode::ONE_SHOT), |
| 137 | CommandParser::NoSuchCommandError); |
| 138 | BOOST_CHECK_THROW(parser.parse({"route", "add"}, ParseMode::ONE_SHOT), |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 139 | CommandDefinition::Error); |
| 140 | } |
| 141 | |
Junxiao Shi | 737c43c | 2016-09-14 02:51:44 +0000 | [diff] [blame] | 142 | BOOST_AUTO_TEST_SUITE_END() // TestCommandParser |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 143 | BOOST_AUTO_TEST_SUITE_END() // Nfdc |
| 144 | |
| 145 | } // namespace tests |
| 146 | } // namespace nfdc |
| 147 | } // namespace tools |
| 148 | } // namespace nfd |