Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 215fd0b | 2017-01-28 19:01:04 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, 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-definition.hpp" |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 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 | |
Junxiao Shi | 64567bb | 2016-09-04 16:00:27 +0000 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Nfdc) |
| 38 | BOOST_FIXTURE_TEST_SUITE(TestCommandDefinition, BaseFixture) |
| 39 | |
| 40 | BOOST_AUTO_TEST_SUITE(Arguments) |
| 41 | |
| 42 | BOOST_AUTO_TEST_CASE(NoArg) |
| 43 | { |
| 44 | CommandDefinition cs("noun", "verb"); |
| 45 | |
| 46 | CommandArguments ca; |
| 47 | |
| 48 | ca = cs.parse(std::vector<std::string>{}); |
| 49 | BOOST_CHECK_EQUAL(ca.size(), 0); |
| 50 | |
| 51 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"x"}), CommandDefinition::Error); |
| 52 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"x", "y"}), CommandDefinition::Error); |
| 53 | } |
| 54 | |
| 55 | BOOST_AUTO_TEST_CASE(NamedArgs) |
| 56 | { |
| 57 | CommandDefinition cs("noun", "verb"); |
| 58 | cs.addArg("a", ArgValueType::UNSIGNED, Required::NO, Positional::NO, "int") |
| 59 | .addArg("b", ArgValueType::NAME, Required::NO, Positional::NO, "name"); |
| 60 | |
| 61 | CommandArguments ca; |
| 62 | |
| 63 | ca = cs.parse(std::vector<std::string>{}); |
| 64 | BOOST_CHECK_EQUAL(ca.size(), 0); |
| 65 | |
| 66 | ca = cs.parse(std::vector<std::string>{"a", "1"}); |
| 67 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 68 | BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 1); |
| 69 | |
| 70 | ca = cs.parse(std::vector<std::string>{"a", "1", "b", "/n"}); |
| 71 | BOOST_CHECK_EQUAL(ca.size(), 2); |
| 72 | BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 1); |
| 73 | BOOST_CHECK_EQUAL(ca.get<Name>("b"), "/n"); |
| 74 | |
| 75 | ca = cs.parse(std::vector<std::string>{"b", "/n", "a", "1"}); |
| 76 | BOOST_CHECK_EQUAL(ca.size(), 2); |
| 77 | BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 1); |
| 78 | BOOST_CHECK_EQUAL(ca.get<Name>("b"), "/n"); |
| 79 | |
| 80 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"1"}), CommandDefinition::Error); |
| 81 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"c", "1"}), CommandDefinition::Error); |
| 82 | } |
| 83 | |
| 84 | BOOST_AUTO_TEST_CASE(PositionalArgs) |
| 85 | { |
| 86 | CommandDefinition cs("face", "create"); |
| 87 | cs.addArg("remote", ArgValueType::FACE_URI, Required::YES, Positional::YES) |
| 88 | .addArg("persistency", ArgValueType::FACE_PERSISTENCY, Required::NO, Positional::YES); |
| 89 | |
| 90 | CommandArguments ca; |
| 91 | |
| 92 | ca = cs.parse(std::vector<std::string>{"udp4://router.example.com", "persistent"}); |
| 93 | BOOST_CHECK_EQUAL(ca.size(), 2); |
| 94 | BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"), |
| 95 | FaceUri("udp4://router.example.com")); |
| 96 | BOOST_CHECK_EQUAL(ca.get<FacePersistency>("persistency"), |
| 97 | FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
| 98 | |
| 99 | ca = cs.parse(std::vector<std::string>{"udp4://router.example.com"}); |
| 100 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 101 | BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"), |
| 102 | FaceUri("udp4://router.example.com")); |
| 103 | |
| 104 | ca = cs.parse(std::vector<std::string>{"remote", "udp4://router.example.com"}); |
| 105 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 106 | BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"), |
| 107 | FaceUri("udp4://router.example.com")); |
| 108 | |
| 109 | ca = cs.parse(std::vector<std::string>{ |
| 110 | "udp4://router.example.com", "persistency", "persistent"}); |
| 111 | BOOST_CHECK_EQUAL(ca.size(), 2); |
| 112 | BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"), |
| 113 | FaceUri("udp4://router.example.com")); |
| 114 | BOOST_CHECK_EQUAL(ca.get<FacePersistency>("persistency"), |
| 115 | FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
| 116 | |
| 117 | ca = cs.parse(std::vector<std::string>{ |
| 118 | "remote", "udp4://router.example.com", "persistency", "persistent"}); |
| 119 | BOOST_CHECK_EQUAL(ca.size(), 2); |
| 120 | BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"), |
| 121 | FaceUri("udp4://router.example.com")); |
| 122 | BOOST_CHECK_EQUAL(ca.get<FacePersistency>("persistency"), |
| 123 | FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
| 124 | |
| 125 | ca = cs.parse(std::vector<std::string>{ |
| 126 | "persistency", "persistent", "remote", "udp4://router.example.com"}); |
| 127 | BOOST_CHECK_EQUAL(ca.size(), 2); |
| 128 | BOOST_CHECK_EQUAL(ca.get<FaceUri>("remote"), |
| 129 | FaceUri("udp4://router.example.com")); |
| 130 | BOOST_CHECK_EQUAL(ca.get<FacePersistency>("persistency"), |
| 131 | FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
| 132 | |
| 133 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{ |
| 134 | "persistent", "udp4://router.example.com"}), CommandDefinition::Error); |
| 135 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{ |
| 136 | "persistency", "persistent", "udp4://router.example.com"}), CommandDefinition::Error); |
| 137 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{ |
| 138 | "remote", "udp4://router.example.com", "persistent"}), CommandDefinition::Error); |
| 139 | } |
| 140 | |
| 141 | BOOST_AUTO_TEST_SUITE_END() // Arguments |
| 142 | |
| 143 | BOOST_AUTO_TEST_SUITE(ParseValue) |
| 144 | |
| 145 | BOOST_AUTO_TEST_CASE(NoneType) |
| 146 | { |
| 147 | CommandDefinition cs("noun", "verb"); |
| 148 | cs.addArg("a", ArgValueType::NONE, Required::YES, Positional::NO); |
| 149 | |
| 150 | CommandArguments ca; |
| 151 | |
| 152 | ca = cs.parse(std::vector<std::string>{"a"}); |
| 153 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 154 | BOOST_CHECK(ca.at("a").type() == typeid(bool)); |
| 155 | BOOST_CHECK_EQUAL(ca.get<bool>("a"), true); |
| 156 | |
| 157 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "value"}), CommandDefinition::Error); |
| 158 | } |
| 159 | |
| 160 | BOOST_AUTO_TEST_CASE(AnyType) |
| 161 | { |
| 162 | CommandDefinition cs("noun", "verb"); |
| 163 | cs.addArg("a", ArgValueType::ANY, Required::NO, Positional::YES); |
| 164 | |
| 165 | CommandArguments ca; |
| 166 | |
| 167 | ca = cs.parse(std::vector<std::string>{}); |
| 168 | BOOST_CHECK_EQUAL(ca.size(), 0); |
| 169 | |
| 170 | ca = cs.parse(std::vector<std::string>{"a"}); |
| 171 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 172 | BOOST_CHECK(ca.at("a").type() == typeid(std::vector<std::string>)); |
| 173 | std::vector<std::string> values = ca.get<std::vector<std::string>>("a"); |
| 174 | BOOST_CHECK_EQUAL(values.size(), 1); |
| 175 | BOOST_CHECK_EQUAL(values.at(0), "a"); |
| 176 | |
| 177 | ca = cs.parse(std::vector<std::string>{"b", "c"}); |
| 178 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 179 | BOOST_CHECK(ca.at("a").type() == typeid(std::vector<std::string>)); |
| 180 | values = ca.get<std::vector<std::string>>("a"); |
| 181 | BOOST_CHECK_EQUAL(values.size(), 2); |
| 182 | BOOST_CHECK_EQUAL(values.at(0), "b"); |
| 183 | BOOST_CHECK_EQUAL(values.at(1), "c"); |
| 184 | } |
| 185 | |
| 186 | BOOST_AUTO_TEST_CASE(UnsignedType) |
| 187 | { |
| 188 | CommandDefinition cs("noun", "verb"); |
| 189 | cs.addArg("a", ArgValueType::UNSIGNED, Required::YES); |
| 190 | |
| 191 | CommandArguments ca; |
| 192 | |
| 193 | ca = cs.parse(std::vector<std::string>{"a", "0"}); |
| 194 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 195 | BOOST_CHECK(ca.at("a").type() == typeid(uint64_t)); |
| 196 | BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 0); |
| 197 | |
| 198 | ca = cs.parse(std::vector<std::string>{"a", "12923"}); |
| 199 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 200 | BOOST_CHECK(ca.at("a").type() == typeid(uint64_t)); |
| 201 | BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 12923); |
| 202 | |
| 203 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "-25705"}), CommandDefinition::Error); |
| 204 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-uint"}), CommandDefinition::Error); |
| 205 | } |
| 206 | |
| 207 | BOOST_AUTO_TEST_CASE(StringType) |
| 208 | { |
| 209 | CommandDefinition cs("noun", "verb"); |
| 210 | cs.addArg("a", ArgValueType::STRING, Required::YES); |
| 211 | |
| 212 | CommandArguments ca; |
| 213 | |
| 214 | ca = cs.parse(std::vector<std::string>{"a", "hello"}); |
| 215 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 216 | BOOST_CHECK(ca.at("a").type() == typeid(std::string)); |
| 217 | BOOST_CHECK_EQUAL(ca.get<std::string>("a"), "hello"); |
| 218 | } |
| 219 | |
| 220 | BOOST_AUTO_TEST_CASE(ReportFormatType) |
| 221 | { |
| 222 | CommandDefinition cs("noun", "verb"); |
| 223 | cs.addArg("a", ArgValueType::REPORT_FORMAT, Required::YES); |
| 224 | |
| 225 | CommandArguments ca; |
| 226 | |
| 227 | ca = cs.parse(std::vector<std::string>{"a", "xml"}); |
| 228 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 229 | BOOST_CHECK(ca.at("a").type() == typeid(ReportFormat)); |
| 230 | BOOST_CHECK_EQUAL(ca.get<ReportFormat>("a"), ReportFormat::XML); |
| 231 | |
| 232 | ca = cs.parse(std::vector<std::string>{"a", "text"}); |
| 233 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 234 | BOOST_CHECK(ca.at("a").type() == typeid(ReportFormat)); |
| 235 | BOOST_CHECK_EQUAL(ca.get<ReportFormat>("a"), ReportFormat::TEXT); |
| 236 | |
| 237 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-fmt"}), CommandDefinition::Error); |
| 238 | } |
| 239 | |
| 240 | BOOST_AUTO_TEST_CASE(NameType) |
| 241 | { |
| 242 | CommandDefinition cs("noun", "verb"); |
| 243 | cs.addArg("a", ArgValueType::NAME, Required::YES); |
| 244 | |
| 245 | CommandArguments ca; |
| 246 | |
| 247 | ca = cs.parse(std::vector<std::string>{"a", "/n"}); |
| 248 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 249 | BOOST_CHECK(ca.at("a").type() == typeid(Name)); |
| 250 | BOOST_CHECK_EQUAL(ca.get<Name>("a"), "/n"); |
| 251 | } |
| 252 | |
| 253 | BOOST_AUTO_TEST_CASE(FaceUriType) |
| 254 | { |
| 255 | CommandDefinition cs("noun", "verb"); |
| 256 | cs.addArg("a", ArgValueType::FACE_URI, Required::YES); |
| 257 | |
| 258 | CommandArguments ca; |
| 259 | |
| 260 | ca = cs.parse(std::vector<std::string>{"a", "udp4://192.0.2.1:6363"}); |
| 261 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 262 | BOOST_CHECK(ca.at("a").type() == typeid(FaceUri)); |
| 263 | BOOST_CHECK_EQUAL(ca.get<FaceUri>("a"), FaceUri("udp4://192.0.2.1:6363")); |
| 264 | |
| 265 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "208"}), CommandDefinition::Error); |
| 266 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-FaceUri"}), CommandDefinition::Error); |
| 267 | } |
| 268 | |
| 269 | BOOST_AUTO_TEST_CASE(FaceIdOrUriType) |
| 270 | { |
| 271 | CommandDefinition cs("noun", "verb"); |
| 272 | cs.addArg("a", ArgValueType::FACE_ID_OR_URI, Required::YES); |
| 273 | |
| 274 | CommandArguments ca; |
| 275 | |
| 276 | ca = cs.parse(std::vector<std::string>{"a", "208"}); |
| 277 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 278 | BOOST_CHECK(ca.at("a").type() == typeid(uint64_t)); |
| 279 | BOOST_CHECK_EQUAL(ca.get<uint64_t>("a"), 208); |
| 280 | |
| 281 | ca = cs.parse(std::vector<std::string>{"a", "udp4://192.0.2.1:6363"}); |
| 282 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 283 | BOOST_CHECK(ca.at("a").type() == typeid(FaceUri)); |
| 284 | BOOST_CHECK_EQUAL(ca.get<FaceUri>("a"), FaceUri("udp4://192.0.2.1:6363")); |
| 285 | |
| 286 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "not-FaceUri"}), CommandDefinition::Error); |
| 287 | } |
| 288 | |
| 289 | BOOST_AUTO_TEST_CASE(FacePersistencyType) |
| 290 | { |
| 291 | CommandDefinition cs("noun", "verb"); |
| 292 | cs.addArg("a", ArgValueType::FACE_PERSISTENCY, Required::YES); |
| 293 | |
| 294 | CommandArguments ca; |
| 295 | |
| 296 | ca = cs.parse(std::vector<std::string>{"a", "persistent"}); |
| 297 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 298 | BOOST_CHECK(ca.at("a").type() == typeid(FacePersistency)); |
| 299 | BOOST_CHECK_EQUAL(ca.get<FacePersistency>("a"), |
| 300 | FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
| 301 | |
| 302 | ca = cs.parse(std::vector<std::string>{"a", "permanent"}); |
| 303 | BOOST_CHECK_EQUAL(ca.size(), 1); |
| 304 | BOOST_CHECK(ca.at("a").type() == typeid(FacePersistency)); |
| 305 | BOOST_CHECK_EQUAL(ca.get<FacePersistency>("a"), |
| 306 | FacePersistency::FACE_PERSISTENCY_PERMANENT); |
| 307 | |
| 308 | // nfdc does not accept "on-demand" |
| 309 | BOOST_CHECK_THROW(cs.parse(std::vector<std::string>{"a", "on-demand"}), CommandDefinition::Error); |
| 310 | } |
| 311 | |
| 312 | BOOST_AUTO_TEST_SUITE_END() // ParseValue |
| 313 | |
| 314 | BOOST_AUTO_TEST_SUITE_END() // TestCommandDefinition |
| 315 | BOOST_AUTO_TEST_SUITE_END() // Nfdc |
| 316 | |
| 317 | } // namespace tests |
| 318 | } // namespace nfdc |
| 319 | } // namespace tools |
| 320 | } // namespace nfd |