blob: 3eef29dcda990853037c40b95cf6a9dbcb221cd1 [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/*
3 * Copyright (c) 2014-2018, 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
30namespace nfd {
31namespace tools {
32namespace nfdc {
33namespace tests {
34
35using namespace nfd::tests;
36
37BOOST_AUTO_TEST_SUITE(Nfdc)
38BOOST_FIXTURE_TEST_SUITE(TestCommandParser, BaseFixture)
39
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050040BOOST_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
49BOOST_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 Shi64567bb2016-09-04 16:00:27 +000056BOOST_AUTO_TEST_CASE(Basic)
57{
58 CommandParser parser;
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050059 ExecuteCommand dummyExecute = [] (ExecuteContext&) { BOOST_ERROR("should not be called"); };
60
61 BOOST_CHECK(parser.listCommands("", ParseMode::ONE_SHOT).empty());
Junxiao Shi64567bb2016-09-04 16:00:27 +000062
Davide Pesaventod2147442018-02-19 23:58:17 -050063 CommandDefinition defFoo("foo", "");
64 parser.addCommand(defFoo, dummyExecute, AVAILABLE_IN_ONE_SHOT | AVAILABLE_IN_HELP);
Junxiao Shi64567bb2016-09-04 16:00:27 +000065
66 CommandDefinition defStatusShow("status", "show");
Junxiao Shi737c43c2016-09-14 02:51:44 +000067 parser.addCommand(defStatusShow, dummyExecute);
Davide Pesaventod2147442018-02-19 23:58:17 -050068 parser.addAlias("status", "show", "");
Junxiao Shi64567bb2016-09-04 16:00:27 +000069 parser.addAlias("status", "show", "list");
70 BOOST_CHECK_THROW(parser.addAlias("status", "show2", "list"), std::out_of_range);
71
72 CommandDefinition defRouteList("route", "list");
73 defRouteList
74 .addArg("nexthop", ArgValueType::FACE_ID_OR_URI, Required::NO, Positional::YES);
Junxiao Shi737c43c2016-09-14 02:51:44 +000075 parser.addCommand(defRouteList, dummyExecute);
Davide Pesaventod2147442018-02-19 23:58:17 -050076 parser.addAlias("route", "list", "");
Junxiao Shi64567bb2016-09-04 16:00:27 +000077
78 CommandDefinition defRouteAdd("route", "add");
79 defRouteAdd
80 .addArg("prefix", ArgValueType::NAME, Required::YES, Positional::YES)
81 .addArg("nexthop", ArgValueType::FACE_ID_OR_URI, Required::YES, Positional::YES);
Junxiao Shi737c43c2016-09-14 02:51:44 +000082 parser.addCommand(defRouteAdd, dummyExecute);
Junxiao Shi64567bb2016-09-04 16:00:27 +000083 parser.addAlias("route", "add", "add2");
84
Davide Pesavento2a588152018-02-19 18:10:03 -050085 CommandDefinition defHidden("hidden", "");
86 parser.addCommand(defHidden, dummyExecute, AVAILABLE_IN_BATCH);
87
88 BOOST_CHECK_EQUAL(parser.listCommands("", ParseMode::ONE_SHOT).size(), 4);
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050089 BOOST_CHECK_EQUAL(parser.listCommands("", ParseMode::BATCH).size(), 3);
90 BOOST_CHECK_EQUAL(parser.listCommands("route", ParseMode::ONE_SHOT).size(), 2);
91 BOOST_CHECK_EQUAL(parser.listCommands("unknown", ParseMode::ONE_SHOT).size(), 0);
Junxiao Shi64567bb2016-09-04 16:00:27 +000092
Junxiao Shi737c43c2016-09-14 02:51:44 +000093 std::string noun, verb;
Junxiao Shi64567bb2016-09-04 16:00:27 +000094 CommandArguments ca;
Junxiao Shi737c43c2016-09-14 02:51:44 +000095 ExecuteCommand execute;
Junxiao Shi64567bb2016-09-04 16:00:27 +000096
Davide Pesaventod2147442018-02-19 23:58:17 -050097 std::tie(noun, verb, ca, execute) = parser.parse({"foo"}, ParseMode::ONE_SHOT);
98 BOOST_CHECK_EQUAL(noun, "foo");
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -050099 BOOST_CHECK_EQUAL(verb, "");
100
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500101 std::tie(noun, verb, ca, execute) = parser.parse({"status"}, ParseMode::ONE_SHOT);
Junxiao Shi737c43c2016-09-14 02:51:44 +0000102 BOOST_CHECK_EQUAL(noun, "status");
103 BOOST_CHECK_EQUAL(verb, "show");
Junxiao Shi64567bb2016-09-04 16:00:27 +0000104
Davide Pesaventod2147442018-02-19 23:58:17 -0500105 std::tie(noun, verb, ca, execute) = parser.parse({"status", "list"}, ParseMode::ONE_SHOT);
106 BOOST_CHECK_EQUAL(noun, "status");
107 BOOST_CHECK_EQUAL(verb, "show");
108
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500109 std::tie(noun, verb, ca, execute) = parser.parse({"route", "add", "/n", "300"}, ParseMode::ONE_SHOT);
Junxiao Shi737c43c2016-09-14 02:51:44 +0000110 BOOST_CHECK_EQUAL(noun, "route");
111 BOOST_CHECK_EQUAL(verb, "add");
Davide Pesavento8b663a92018-11-21 22:57:20 -0500112 BOOST_CHECK_EQUAL(ndn::any_cast<Name>(ca.at("prefix")), "/n");
113 BOOST_CHECK_EQUAL(ndn::any_cast<uint64_t>(ca.at("nexthop")), 300);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000114
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500115 std::tie(noun, verb, ca, execute) = parser.parse({"route", "add2", "/n", "300"}, ParseMode::ONE_SHOT);
Junxiao Shi737c43c2016-09-14 02:51:44 +0000116 BOOST_CHECK_EQUAL(noun, "route");
117 BOOST_CHECK_EQUAL(verb, "add");
Junxiao Shi64567bb2016-09-04 16:00:27 +0000118
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500119 std::tie(noun, verb, ca, execute) = parser.parse({"route", "list", "400"}, ParseMode::ONE_SHOT);
Junxiao Shi737c43c2016-09-14 02:51:44 +0000120 BOOST_CHECK_EQUAL(noun, "route");
121 BOOST_CHECK_EQUAL(verb, "list");
Davide Pesavento8b663a92018-11-21 22:57:20 -0500122 BOOST_CHECK_EQUAL(ndn::any_cast<uint64_t>(ca.at("nexthop")), 400);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000123
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500124 BOOST_CHECK_THROW(parser.parse({}, ParseMode::ONE_SHOT),
125 CommandParser::NoSuchCommandError);
Davide Pesaventod2147442018-02-19 23:58:17 -0500126 BOOST_CHECK_THROW(parser.parse({"bar"}, ParseMode::ONE_SHOT),
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500127 CommandParser::NoSuchCommandError);
128 BOOST_CHECK_THROW(parser.parse({"status", "hide"}, ParseMode::ONE_SHOT),
129 CommandParser::NoSuchCommandError);
Davide Pesaventod2147442018-02-19 23:58:17 -0500130 BOOST_CHECK_THROW(parser.parse({"status", "show", "something"}, ParseMode::ONE_SHOT),
131 CommandDefinition::Error);
Davide Pesaventoe0bae0f2018-02-17 22:07:52 -0500132 BOOST_CHECK_THROW(parser.parse({"route", "66"}, ParseMode::ONE_SHOT),
133 CommandParser::NoSuchCommandError);
134 BOOST_CHECK_THROW(parser.parse({"route", "add"}, ParseMode::ONE_SHOT),
Junxiao Shi64567bb2016-09-04 16:00:27 +0000135 CommandDefinition::Error);
Davide Pesavento2a588152018-02-19 18:10:03 -0500136 BOOST_CHECK_THROW(parser.parse({"hidden"}, ParseMode::ONE_SHOT),
137 CommandParser::NoSuchCommandError);
Junxiao Shi64567bb2016-09-04 16:00:27 +0000138}
139
Junxiao Shi737c43c2016-09-14 02:51:44 +0000140BOOST_AUTO_TEST_SUITE_END() // TestCommandParser
Junxiao Shi64567bb2016-09-04 16:00:27 +0000141BOOST_AUTO_TEST_SUITE_END() // Nfdc
142
143} // namespace tests
144} // namespace nfdc
145} // namespace tools
146} // namespace nfd