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