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