Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [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 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 22 | #include "ndn-cxx/security/additional-description.hpp" |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 23 | #include "ndn-cxx/util/concepts.hpp" |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 27 | namespace ndn::tests { |
| 28 | |
| 29 | using ndn::security::AdditionalDescription; |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 31 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<AdditionalDescription>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireEncodable<AdditionalDescription>)); |
| 33 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<AdditionalDescription>)); |
| 34 | BOOST_CONCEPT_ASSERT((WireDecodable<AdditionalDescription>)); |
| 35 | static_assert(std::is_convertible_v<AdditionalDescription::Error*, tlv::Error*>, |
| 36 | "AdditionalDescription::Error must inherit from tlv::Error"); |
| 37 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(Security) |
| 39 | BOOST_AUTO_TEST_SUITE(TestAdditionalDescription) |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 40 | |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 41 | const uint8_t DESC[] = { |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 42 | 0xfd, 0x01, 0x02, 0x28, |
| 43 | 0xfd, 0x02, 0x00, 0x10, // DescriptionEntry |
| 44 | 0xfd, 0x02, 0x01, 0x04, // DescriptionKey |
| 45 | 0x6b, 0x65, 0x79, 0x31, // "key1" |
| 46 | 0xfd, 0x02, 0x02, 0x04, // DescriptionValue |
| 47 | 0x76, 0x61, 0x6c, 0x31, // "val1" |
| 48 | 0xfd, 0x02, 0x00, 0x10, // DescriptionEntry |
| 49 | 0xfd, 0x02, 0x01, 0x04, // DescriptionKey |
| 50 | 0x6b, 0x65, 0x79, 0x32, // "key2" |
| 51 | 0xfd, 0x02, 0x02, 0x04, // DescriptionValue |
| 52 | 0x76, 0x61, 0x6c, 0x32, // "val2" |
| 53 | }; |
| 54 | |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 55 | BOOST_AUTO_TEST_CASE(Basic) |
| 56 | { |
| 57 | AdditionalDescription aDescription; |
| 58 | |
| 59 | aDescription.set("key2", "val2"); |
| 60 | aDescription.set("key1", "val1"); |
| 61 | |
| 62 | BOOST_REQUIRE_NO_THROW(aDescription.get("key1")); |
| 63 | BOOST_REQUIRE_NO_THROW(aDescription.get("key2")); |
| 64 | BOOST_REQUIRE_THROW(aDescription.get("key3"), AdditionalDescription::Error); |
| 65 | |
| 66 | BOOST_CHECK_EQUAL(aDescription.has("key1"), true); |
| 67 | BOOST_CHECK_EQUAL(aDescription.has("key2"), true); |
| 68 | BOOST_CHECK_EQUAL(aDescription.has("key3"), false); |
| 69 | |
| 70 | auto val1 = aDescription.get("key1"); |
| 71 | auto val2 = aDescription.get("key2"); |
| 72 | |
| 73 | BOOST_CHECK_EQUAL(val1, "val1"); |
| 74 | BOOST_CHECK_EQUAL(val2, "val2"); |
| 75 | |
| 76 | auto it = aDescription.begin(); |
| 77 | BOOST_CHECK_EQUAL(it->second, "val1"); |
| 78 | it++; |
| 79 | BOOST_CHECK_EQUAL(it->second, "val2"); |
| 80 | it++; |
| 81 | BOOST_CHECK(it == aDescription.end()); |
| 82 | |
Davide Pesavento | 258d51a | 2022-02-27 21:26:28 -0500 | [diff] [blame] | 83 | BOOST_TEST(aDescription.wireEncode() == DESC, boost::test_tools::per_element()); |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 84 | |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 85 | AdditionalDescription aDescription2(Block{DESC}); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 86 | BOOST_CHECK_EQUAL(aDescription2, aDescription); |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 87 | |
| 88 | AdditionalDescription aDescription3; |
| 89 | aDescription3.set("key3", "val3"); |
| 90 | aDescription3.set("key2", "val2"); |
| 91 | |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 92 | BOOST_CHECK_NE(aDescription2, aDescription3); |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 93 | |
| 94 | std::ostringstream os; |
| 95 | os << aDescription; |
Davide Pesavento | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(os.str(), "[(key1:val1), (key2:val2)]"); |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 99 | BOOST_AUTO_TEST_SUITE_END() // TestAdditionalDescription |
| 100 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | ea38294 | 2015-07-17 23:20:44 -0700 | [diff] [blame] | 101 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 102 | } // namespace ndn::tests |