Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -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 | * @author Chavoosh Ghasemi <chghasemi@cs.arizona.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "ndn-cxx/metadata-object.hpp" |
| 23 | |
| 24 | #include "tests/boost-test.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 25 | #include "tests/key-chain-fixture.hpp" |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 27 | namespace ndn::tests { |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 28 | |
Davide Pesavento | fffdd62 | 2023-08-28 22:50:43 -0400 | [diff] [blame] | 29 | static_assert(std::is_convertible_v<MetadataObject::Error*, tlv::Error*>, |
| 30 | "MetadataObject::Error must inherit from tlv::Error"); |
| 31 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 32 | class MetadataObjectFixture : public KeyChainFixture |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 33 | { |
| 34 | public: |
| 35 | MetadataObjectFixture() |
Davide Pesavento | 21ce3ad | 2021-11-01 22:00:45 -0400 | [diff] [blame] | 36 | : versionedContentName(Name(baseContentName) |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 37 | .appendVersion(342092199154ULL)) |
| 38 | , metadataFullName(Name(baseContentName) |
Davide Pesavento | 21ce3ad | 2021-11-01 22:00:45 -0400 | [diff] [blame] | 39 | .append(MetadataObject::getKeywordComponent()) |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 40 | .appendVersion(metadataVerNo) |
| 41 | .appendSegment(0)) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | protected: |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 46 | // content prefix |
| 47 | const Name baseContentName = "/ndn/unit/tests"; |
| 48 | const Name versionedContentName; |
| 49 | |
| 50 | // metadata prefix |
| 51 | const uint64_t metadataVerNo = 89400192181ULL; |
| 52 | const Name metadataFullName; |
| 53 | }; |
| 54 | |
| 55 | BOOST_FIXTURE_TEST_SUITE(TestMetadataObject, MetadataObjectFixture) |
| 56 | |
| 57 | BOOST_AUTO_TEST_CASE(EncodeDecode) |
| 58 | { |
| 59 | MetadataObject metadata1; |
| 60 | metadata1.setVersionedName(versionedContentName); |
| 61 | |
| 62 | // pass metadata version number |
| 63 | const Data data1 = metadata1.makeData(metadataFullName.getPrefix(-2), m_keyChain, |
Eric Newberry | 78ffc63 | 2020-05-17 15:13:46 -0700 | [diff] [blame] | 64 | security::SigningInfo(), metadataVerNo); |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 65 | |
| 66 | BOOST_CHECK_EQUAL(metadata1.getVersionedName(), versionedContentName); |
| 67 | BOOST_CHECK_EQUAL(data1.getName(), metadataFullName); |
| 68 | BOOST_CHECK_EQUAL(data1.getFreshnessPeriod(), 10_ms); |
| 69 | |
| 70 | // do not pass metadata version number |
| 71 | metadata1.setVersionedName(versionedContentName); |
| 72 | const Data data2 = metadata1.makeData(metadataFullName.getPrefix(-2), m_keyChain); |
| 73 | BOOST_CHECK_NE(data2.getName()[-2].toVersion(), metadataVerNo); |
| 74 | |
| 75 | // construct a metadata object based on a valid metadata packet |
| 76 | MetadataObject metadata2(data1); |
| 77 | |
| 78 | BOOST_CHECK_EQUAL(metadata2.getVersionedName(), versionedContentName); |
| 79 | BOOST_CHECK(baseContentName.isPrefixOf(metadata2.makeData(metadataFullName.getPrefix(-2), |
| 80 | m_keyChain).getName())); |
| 81 | } |
| 82 | |
| 83 | BOOST_AUTO_TEST_CASE(InvalidFormat) |
| 84 | { |
| 85 | Data data; |
| 86 | |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 87 | // invalid metadata name |
| 88 | data.setName("/ndn/unit/test"); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 89 | BOOST_CHECK_EXCEPTION(MetadataObject{data}, tlv::Error, [] (const auto& e) { |
| 90 | return e.what() == "Name /ndn/unit/test is not a valid MetadataObject name"s; |
| 91 | }); |
Davide Pesavento | 21ce3ad | 2021-11-01 22:00:45 -0400 | [diff] [blame] | 92 | data.setName(Name("/ndn/unit/test").append(MetadataObject::getKeywordComponent())); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 93 | BOOST_CHECK_EXCEPTION(MetadataObject{data}, tlv::Error, [] (const auto& e) { |
| 94 | return e.what() == "Name /ndn/unit/test/32=metadata is not a valid MetadataObject name"s; |
| 95 | }); |
| 96 | |
| 97 | // invalid content type |
Davide Pesavento | 21ce3ad | 2021-11-01 22:00:45 -0400 | [diff] [blame] | 98 | data.setName(Name("/ndn/unit/test") |
| 99 | .append(MetadataObject::getKeywordComponent()) |
| 100 | .appendVersion() |
| 101 | .appendSegment(0)); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 102 | data.setContentType(tlv::ContentType_Key); |
| 103 | BOOST_CHECK_EXCEPTION(MetadataObject{data}, tlv::Error, [] (const auto& e) { |
| 104 | return e.what() == "MetadataObject has invalid ContentType 2"s; |
| 105 | }); |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 106 | |
| 107 | // empty content |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 108 | data.setContentType(tlv::ContentType_Blob); |
| 109 | BOOST_CHECK_EXCEPTION(MetadataObject{data}, tlv::Error, [] (const auto& e) { |
| 110 | return e.what() == "MetadataObject is empty"s; |
| 111 | }); |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 112 | |
| 113 | // non-empty content with no name element |
| 114 | data.setContent("F000"_block); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 115 | BOOST_CHECK_EXCEPTION(MetadataObject{data}, tlv::Error, [] (const auto& e) { |
| 116 | return e.what() == "No sub-element of type 7 found in block of type 21"s; |
| 117 | }); |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Davide Pesavento | 21ce3ad | 2021-11-01 22:00:45 -0400 | [diff] [blame] | 120 | BOOST_AUTO_TEST_CASE(KeywordComponent) |
| 121 | { |
| 122 | BOOST_CHECK_EQUAL(MetadataObject::getKeywordComponent().wireEncode(), |
| 123 | "20 08 6D65746164617461"_block); |
| 124 | BOOST_CHECK_EQUAL(MetadataObject::getKeywordComponent().toUri(name::UriFormat::CANONICAL), |
| 125 | "32=metadata"); |
| 126 | } |
| 127 | |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 128 | BOOST_AUTO_TEST_CASE(IsValidName) |
| 129 | { |
| 130 | // valid name |
| 131 | Name name = Name("/ndn/unit/test") |
Davide Pesavento | 21ce3ad | 2021-11-01 22:00:45 -0400 | [diff] [blame] | 132 | .append(MetadataObject::getKeywordComponent()) |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 133 | .appendVersion() |
| 134 | .appendSegment(0); |
| 135 | BOOST_CHECK(MetadataObject::isValidName(name)); |
| 136 | |
| 137 | // invalid names |
| 138 | // segment component is missing |
| 139 | BOOST_CHECK_EQUAL(MetadataObject::isValidName(name.getPrefix(-1)), false); |
| 140 | |
| 141 | // version component is missing |
| 142 | BOOST_CHECK_EQUAL(MetadataObject::isValidName(name.getPrefix(-2)), false); |
| 143 | |
Davide Pesavento | 21ce3ad | 2021-11-01 22:00:45 -0400 | [diff] [blame] | 144 | // keyword component is missing |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 145 | BOOST_CHECK_EQUAL(MetadataObject::isValidName(name.getPrefix(-3)), false); |
| 146 | |
| 147 | // too short name |
| 148 | BOOST_CHECK_EQUAL(MetadataObject::isValidName(name.getPrefix(-4)), false); |
| 149 | |
| 150 | // out-of-order segment and version components |
| 151 | name = name.getPrefix(-2).appendSegment(0).appendVersion(); |
| 152 | BOOST_CHECK_EQUAL(MetadataObject::isValidName(name), false); |
| 153 | |
Davide Pesavento | 21ce3ad | 2021-11-01 22:00:45 -0400 | [diff] [blame] | 154 | // invalid keyword name component |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 155 | name = name.getPrefix(-3) |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 156 | .append(tlv::KeywordNameComponent, {'f', 'o', 'o'}) |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 157 | .appendVersion() |
| 158 | .appendSegment(0); |
| 159 | BOOST_CHECK_EQUAL(MetadataObject::isValidName(name), false); |
| 160 | } |
| 161 | |
| 162 | BOOST_AUTO_TEST_CASE(MakeDiscoveryInterest) |
| 163 | { |
| 164 | Interest interest = MetadataObject::makeDiscoveryInterest(baseContentName); |
Davide Pesavento | 21ce3ad | 2021-11-01 22:00:45 -0400 | [diff] [blame] | 165 | BOOST_CHECK_EQUAL(interest.getName(), Name(baseContentName).append(MetadataObject::getKeywordComponent())); |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 166 | BOOST_CHECK(interest.getCanBePrefix()); |
| 167 | BOOST_CHECK(interest.getMustBeFresh()); |
| 168 | } |
| 169 | |
| 170 | BOOST_AUTO_TEST_SUITE_END() // TestMetadataObject |
| 171 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 172 | } // namespace ndn::tests |