blob: a2cecc94802c8de3dd68d58d269d162ed44e6780 [file] [log] [blame]
Junxiao Shi64567bb2016-09-04 16:00:27 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -05002/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shi64567bb2016-09-04 16:00:27 +00004 * 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 Pesaventoa9b09b62022-06-04 14:07:25 -040030#include <boost/lexical_cast.hpp>
31
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::tools::nfdc::tests {
Junxiao Shi64567bb2016-09-04 16:00:27 +000033
Junxiao Shi64567bb2016-09-04 16:00:27 +000034BOOST_AUTO_TEST_SUITE(Nfdc)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040035BOOST_AUTO_TEST_SUITE(TestCommandParser)
Junxiao Shi64567bb2016-09-04 16:00:27 +000036
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050037BOOST_AUTO_TEST_CASE(PrintAvailableIn)
38{
39 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_NONE), "hidden");
40 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_ONE_SHOT), "one-shot|hidden");
41 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_BATCH), "batch|hidden");
42 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_HELP), "none");
43 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(AVAILABLE_IN_ALL), "one-shot|batch");
44}
45
46BOOST_AUTO_TEST_CASE(PrintParseMode)
47{
48 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ParseMode::ONE_SHOT), "one-shot");
49 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ParseMode::BATCH), "batch");
50 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ParseMode>(0xFF)), "255");
51}
52
Junxiao Shi64567bb2016-09-04 16:00:27 +000053BOOST_AUTO_TEST_CASE(Basic)
54{
55 CommandParser parser;
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050056 ExecuteCommand dummyExecute = [] (ExecuteContext&) { BOOST_ERROR("should not be called"); };
57
58 BOOST_CHECK(parser.listCommands("", ParseMode::ONE_SHOT).empty());
Junxiao Shi64567bb2016-09-04 16:00:27 +000059
Davide Pesaventod2147442018-02-19 23:58:17 -050060 CommandDefinition defFoo("foo", "");
61 parser.addCommand(defFoo, dummyExecute, AVAILABLE_IN_ONE_SHOT | AVAILABLE_IN_HELP);
Junxiao Shi64567bb2016-09-04 16:00:27 +000062
63 CommandDefinition defStatusShow("status", "show");
Junxiao Shi737c43c2016-09-14 02:51:44 +000064 parser.addCommand(defStatusShow, dummyExecute);
Davide Pesaventod2147442018-02-19 23:58:17 -050065 parser.addAlias("status", "show", "");
Junxiao Shi64567bb2016-09-04 16:00:27 +000066 parser.addAlias("status", "show", "list");
67 BOOST_CHECK_THROW(parser.addAlias("status", "show2", "list"), std::out_of_range);
68
69 CommandDefinition defRouteList("route", "list");
70 defRouteList
71 .addArg("nexthop", ArgValueType::FACE_ID_OR_URI, Required::NO, Positional::YES);
Junxiao Shi737c43c2016-09-14 02:51:44 +000072 parser.addCommand(defRouteList, dummyExecute);
Davide Pesaventod2147442018-02-19 23:58:17 -050073 parser.addAlias("route", "list", "");
Junxiao Shi64567bb2016-09-04 16:00:27 +000074
75 CommandDefinition defRouteAdd("route", "add");
76 defRouteAdd
77 .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES)
78 .addArg("nexthop", ArgValueType::FACE_ID_OR_URI, Required::YES, Positional::YES);
Junxiao Shi737c43c2016-09-14 02:51:44 +000079 parser.addCommand(defRouteAdd, dummyExecute);
Junxiao Shi64567bb2016-09-04 16:00:27 +000080 parser.addAlias("route", "add", "add2");
81
Davide Pesavento2a588152018-02-19 18:10:03 -050082 CommandDefinition defHidden("hidden", "");
83 parser.addCommand(defHidden, dummyExecute, AVAILABLE_IN_BATCH);
84
85 BOOST_CHECK_EQUAL(parser.listCommands("", ParseMode::ONE_SHOT).size(), 4);
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050086 BOOST_CHECK_EQUAL(parser.listCommands("", ParseMode::BATCH).size(), 3);
87 BOOST_CHECK_EQUAL(parser.listCommands("route", ParseMode::ONE_SHOT).size(), 2);
88 BOOST_CHECK_EQUAL(parser.listCommands("unknown", ParseMode::ONE_SHOT).size(), 0);
Junxiao Shi64567bb2016-09-04 16:00:27 +000089
Junxiao Shi737c43c2016-09-14 02:51:44 +000090 std::string noun, verb;
Junxiao Shi64567bb2016-09-04 16:00:27 +000091 CommandArguments ca;
Junxiao Shi737c43c2016-09-14 02:51:44 +000092 ExecuteCommand execute;
Junxiao Shi64567bb2016-09-04 16:00:27 +000093
Davide Pesaventod2147442018-02-19 23:58:17 -050094 std::tie(noun, verb, ca, execute) = parser.parse({"foo"}, ParseMode::ONE_SHOT);
95 BOOST_CHECK_EQUAL(noun, "foo");
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050096 BOOST_CHECK_EQUAL(verb, "");
97
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050098 std::tie(noun, verb, ca, execute) = parser.parse({"status"}, ParseMode::ONE_SHOT);
Junxiao Shi737c43c2016-09-14 02:51:44 +000099 BOOST_CHECK_EQUAL(noun, "status");
100 BOOST_CHECK_EQUAL(verb, "show");
Junxiao Shi64567bb2016-09-04 16:00:27 +0000101
Davide Pesaventod2147442018-02-19 23:58:17 -0500102 std::tie(noun, verb, ca, execute) = parser.parse({"status", "list"}, ParseMode::ONE_SHOT);
103 BOOST_CHECK_EQUAL(noun, "status");
104 BOOST_CHECK_EQUAL(verb, "show");
105
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500106 std::tie(noun, verb, ca, execute) = parser.parse({"route", "add", "/n", "300"}, ParseMode::ONE_SHOT);
Junxiao Shi737c43c2016-09-14 02:51:44 +0000107 BOOST_CHECK_EQUAL(noun, "route");
108 BOOST_CHECK_EQUAL(verb, "add");
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400109 BOOST_CHECK_EQUAL(std::any_cast<Name>(ca.at("prefix")), "/n");
110 BOOST_CHECK_EQUAL(std::any_cast<uint64_t>(ca.at("nexthop")), 300);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000111
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500112 std::tie(noun, verb, ca, execute) = parser.parse({"route", "add2", "/n", "300"}, ParseMode::ONE_SHOT);
Junxiao Shi737c43c2016-09-14 02:51:44 +0000113 BOOST_CHECK_EQUAL(noun, "route");
114 BOOST_CHECK_EQUAL(verb, "add");
Junxiao Shi64567bb2016-09-04 16:00:27 +0000115
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500116 std::tie(noun, verb, ca, execute) = parser.parse({"route", "list", "400"}, ParseMode::ONE_SHOT);
Junxiao Shi737c43c2016-09-14 02:51:44 +0000117 BOOST_CHECK_EQUAL(noun, "route");
118 BOOST_CHECK_EQUAL(verb, "list");
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400119 BOOST_CHECK_EQUAL(std::any_cast<uint64_t>(ca.at("nexthop")), 400);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000120
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500121 BOOST_CHECK_THROW(parser.parse({}, ParseMode::ONE_SHOT),
122 CommandParser::NoSuchCommandError);
Davide Pesaventod2147442018-02-19 23:58:17 -0500123 BOOST_CHECK_THROW(parser.parse({"bar"}, ParseMode::ONE_SHOT),
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500124 CommandParser::NoSuchCommandError);
125 BOOST_CHECK_THROW(parser.parse({"status", "hide"}, ParseMode::ONE_SHOT),
126 CommandParser::NoSuchCommandError);
Davide Pesaventod2147442018-02-19 23:58:17 -0500127 BOOST_CHECK_THROW(parser.parse({"status", "show", "something"}, ParseMode::ONE_SHOT),
128 CommandDefinition::Error);
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500129 BOOST_CHECK_THROW(parser.parse({"route", "66"}, ParseMode::ONE_SHOT),
130 CommandParser::NoSuchCommandError);
131 BOOST_CHECK_THROW(parser.parse({"route", "add"}, ParseMode::ONE_SHOT),
Junxiao Shi64567bb2016-09-04 16:00:27 +0000132 CommandDefinition::Error);
Davide Pesavento2a588152018-02-19 18:10:03 -0500133 BOOST_CHECK_THROW(parser.parse({"hidden"}, ParseMode::ONE_SHOT),
134 CommandParser::NoSuchCommandError);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000135}
136
Junxiao Shi737c43c2016-09-14 02:51:44 +0000137BOOST_AUTO_TEST_SUITE_END() // TestCommandParser
Junxiao Shi64567bb2016-09-04 16:00:27 +0000138BOOST_AUTO_TEST_SUITE_END() // Nfdc
139
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400140} // namespace nfd::tools::nfdc::tests