Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2025 Regents of the University of California. |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/mgmt/control-response.hpp" |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 23 | #include "ndn-cxx/util/concepts.hpp" |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 26 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 27 | namespace ndn::tests { |
| 28 | |
| 29 | using namespace ndn::mgmt; |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 30 | |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 31 | BOOST_CONCEPT_ASSERT((WireEncodable<ControlResponse>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireDecodable<ControlResponse>)); |
| 33 | static_assert(std::is_convertible_v<ControlResponse::Error*, tlv::Error*>, |
| 34 | "ControlResponse::Error must inherit from tlv::Error"); |
| 35 | |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 37 | BOOST_AUTO_TEST_SUITE(TestControlResponse) |
| 38 | |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame] | 39 | const uint8_t WIRE[] = { |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 40 | 0x65, 0x17, // ControlResponse |
| 41 | 0x66, 0x02, // StatusCode |
| 42 | 0x01, 0x94, |
| 43 | 0x67, 0x11, // StatusText |
| 44 | 0x4e, 0x6f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x20, 0x6e, 0x6f, 0x74, 0x20, |
| 45 | 0x66, 0x6f, 0x75, 0x6e, 0x64}; |
| 46 | |
| 47 | BOOST_AUTO_TEST_CASE(Encode) |
| 48 | { |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame] | 49 | ControlResponse cr1(404, "Nothing not found"); |
| 50 | BOOST_TEST(cr1.wireEncode() == WIRE, boost::test_tools::per_element()); |
| 51 | |
| 52 | ControlResponse cr2; |
| 53 | cr2.setCode(404); |
| 54 | cr2.setText("Nothing not found"); |
| 55 | BOOST_TEST(cr2.wireEncode() == WIRE, boost::test_tools::per_element()); |
| 56 | |
| 57 | ControlResponse cr3(cr1); |
| 58 | cr3.setCode(405); |
| 59 | BOOST_TEST(cr3.wireEncode() != Block{WIRE}); |
| 60 | |
| 61 | ControlResponse cr4(cr1); |
| 62 | cr4.setText("foo"); |
| 63 | BOOST_TEST(cr4.wireEncode() != Block{WIRE}); |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | BOOST_AUTO_TEST_CASE(Decode) |
| 67 | { |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 68 | ControlResponse cr(Block{WIRE}); |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 69 | BOOST_CHECK_EQUAL(cr.getCode(), 404); |
| 70 | BOOST_CHECK_EQUAL(cr.getText(), "Nothing not found"); |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame] | 71 | |
| 72 | // wrong outer TLV type |
| 73 | BOOST_CHECK_EXCEPTION(cr.wireDecode("6406660201946700"_block), tlv::Error, [] (const auto& e) { |
| 74 | return e.what() == "Expecting ControlResponse element, but TLV has type 100"sv; |
| 75 | }); |
| 76 | |
Davide Pesavento | 3e3887c | 2025-01-09 21:55:02 -0500 | [diff] [blame] | 77 | // missing StatusCode |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame] | 78 | BOOST_CHECK_EXCEPTION(cr.wireDecode("6500"_block), tlv::Error, [] (const auto& e) { |
Davide Pesavento | 3e3887c | 2025-01-09 21:55:02 -0500 | [diff] [blame] | 79 | return e.what() == "StatusCode is missing or out of order"sv; |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame] | 80 | }); |
| 81 | |
Davide Pesavento | 3e3887c | 2025-01-09 21:55:02 -0500 | [diff] [blame] | 82 | // out-of-order StatusCode |
| 83 | BOOST_CHECK_EXCEPTION(cr.wireDecode("650567006601C8"_block), tlv::Error, [] (const auto& e) { |
| 84 | return e.what() == "StatusCode is missing or out of order"sv; |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame] | 85 | }); |
| 86 | |
| 87 | // missing StatusText |
| 88 | BOOST_CHECK_EXCEPTION(cr.wireDecode("650466020194"_block), tlv::Error, [] (const auto& e) { |
Davide Pesavento | 3e3887c | 2025-01-09 21:55:02 -0500 | [diff] [blame] | 89 | return e.what() == "StatusText is missing or out of order"sv; |
| 90 | }); |
| 91 | |
| 92 | // out-of-order StatusText |
| 93 | BOOST_CHECK_EXCEPTION(cr.wireDecode("65086602019469006700"_block), tlv::Error, [] (const auto& e) { |
| 94 | return e.what() == "StatusText is missing or out of order"sv; |
Davide Pesavento | cb385e3 | 2024-12-27 15:22:02 -0500 | [diff] [blame] | 95 | }); |
Junxiao Shi | 5174232 | 2017-08-13 17:04:52 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | BOOST_AUTO_TEST_SUITE_END() // TestControlResponse |
| 99 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| 100 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 101 | } // namespace ndn::tests |