Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Eric Newberry | 78ffc63 | 2020-05-17 15:13:46 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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" |
| 25 | #include "tests/identity-management-fixture.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace tests { |
| 29 | |
| 30 | class MetadataObjectFixture : public IdentityManagementFixture |
| 31 | { |
| 32 | public: |
| 33 | MetadataObjectFixture() |
| 34 | : metadataComponent(32, reinterpret_cast<const uint8_t*>("metadata"), std::strlen("metadata")) |
| 35 | , versionedContentName(Name(baseContentName) |
| 36 | .appendVersion(342092199154ULL)) |
| 37 | , metadataFullName(Name(baseContentName) |
| 38 | .append(metadataComponent) |
| 39 | .appendVersion(metadataVerNo) |
| 40 | .appendSegment(0)) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | protected: |
| 45 | const name::Component metadataComponent; |
| 46 | |
| 47 | // content prefix |
| 48 | const Name baseContentName = "/ndn/unit/tests"; |
| 49 | const Name versionedContentName; |
| 50 | |
| 51 | // metadata prefix |
| 52 | const uint64_t metadataVerNo = 89400192181ULL; |
| 53 | const Name metadataFullName; |
| 54 | }; |
| 55 | |
| 56 | BOOST_FIXTURE_TEST_SUITE(TestMetadataObject, MetadataObjectFixture) |
| 57 | |
| 58 | BOOST_AUTO_TEST_CASE(EncodeDecode) |
| 59 | { |
| 60 | MetadataObject metadata1; |
| 61 | metadata1.setVersionedName(versionedContentName); |
| 62 | |
| 63 | // pass metadata version number |
| 64 | const Data data1 = metadata1.makeData(metadataFullName.getPrefix(-2), m_keyChain, |
Eric Newberry | 78ffc63 | 2020-05-17 15:13:46 -0700 | [diff] [blame] | 65 | security::SigningInfo(), metadataVerNo); |
Chavoosh Ghasemi | 6b99b6f | 2018-10-26 12:08:33 -0700 | [diff] [blame] | 66 | |
| 67 | BOOST_CHECK_EQUAL(metadata1.getVersionedName(), versionedContentName); |
| 68 | BOOST_CHECK_EQUAL(data1.getName(), metadataFullName); |
| 69 | BOOST_CHECK_EQUAL(data1.getFreshnessPeriod(), 10_ms); |
| 70 | |
| 71 | // do not pass metadata version number |
| 72 | metadata1.setVersionedName(versionedContentName); |
| 73 | const Data data2 = metadata1.makeData(metadataFullName.getPrefix(-2), m_keyChain); |
| 74 | BOOST_CHECK_NE(data2.getName()[-2].toVersion(), metadataVerNo); |
| 75 | |
| 76 | // construct a metadata object based on a valid metadata packet |
| 77 | MetadataObject metadata2(data1); |
| 78 | |
| 79 | BOOST_CHECK_EQUAL(metadata2.getVersionedName(), versionedContentName); |
| 80 | BOOST_CHECK(baseContentName.isPrefixOf(metadata2.makeData(metadataFullName.getPrefix(-2), |
| 81 | m_keyChain).getName())); |
| 82 | } |
| 83 | |
| 84 | BOOST_AUTO_TEST_CASE(InvalidFormat) |
| 85 | { |
| 86 | Data data; |
| 87 | |
| 88 | // invalid content type |
| 89 | data.setName(Name("/ndn/unit/test").append(metadataComponent)); |
| 90 | data.setContentType(tlv::ContentType_Key); |
| 91 | BOOST_CHECK_THROW(MetadataObject metadata(data), tlv::Error); |
| 92 | |
| 93 | // invalid metadata name |
| 94 | data.setName("/ndn/unit/test"); |
| 95 | data.setContentType(tlv::ContentType_Blob); |
| 96 | BOOST_CHECK_THROW(MetadataObject metadata(data), tlv::Error); |
| 97 | |
| 98 | // empty content |
| 99 | data.setName(Name("ndn/unit/test").append(metadataComponent)); |
| 100 | BOOST_CHECK_THROW(MetadataObject metadata(data), tlv::Error); |
| 101 | |
| 102 | // non-empty content with no name element |
| 103 | data.setContent("F000"_block); |
| 104 | BOOST_CHECK_THROW(MetadataObject metadata(data), tlv::Error); |
| 105 | } |
| 106 | |
| 107 | BOOST_AUTO_TEST_CASE(IsValidName) |
| 108 | { |
| 109 | // valid name |
| 110 | Name name = Name("/ndn/unit/test") |
| 111 | .append(metadataComponent) |
| 112 | .appendVersion() |
| 113 | .appendSegment(0); |
| 114 | BOOST_CHECK(MetadataObject::isValidName(name)); |
| 115 | |
| 116 | // invalid names |
| 117 | // segment component is missing |
| 118 | BOOST_CHECK_EQUAL(MetadataObject::isValidName(name.getPrefix(-1)), false); |
| 119 | |
| 120 | // version component is missing |
| 121 | BOOST_CHECK_EQUAL(MetadataObject::isValidName(name.getPrefix(-2)), false); |
| 122 | |
| 123 | // keyword name component `32=keyword` is missing |
| 124 | BOOST_CHECK_EQUAL(MetadataObject::isValidName(name.getPrefix(-3)), false); |
| 125 | |
| 126 | // too short name |
| 127 | BOOST_CHECK_EQUAL(MetadataObject::isValidName(name.getPrefix(-4)), false); |
| 128 | |
| 129 | // out-of-order segment and version components |
| 130 | name = name.getPrefix(-2).appendSegment(0).appendVersion(); |
| 131 | BOOST_CHECK_EQUAL(MetadataObject::isValidName(name), false); |
| 132 | |
| 133 | // invalid name component keyword |
| 134 | name = name.getPrefix(-3) |
| 135 | .append(32, reinterpret_cast<const uint8_t*>("foo"), std::strlen("foo")) |
| 136 | .appendVersion() |
| 137 | .appendSegment(0); |
| 138 | BOOST_CHECK_EQUAL(MetadataObject::isValidName(name), false); |
| 139 | } |
| 140 | |
| 141 | BOOST_AUTO_TEST_CASE(MakeDiscoveryInterest) |
| 142 | { |
| 143 | Interest interest = MetadataObject::makeDiscoveryInterest(baseContentName); |
| 144 | BOOST_CHECK_EQUAL(interest.getName(), Name(baseContentName).append(metadataComponent)); |
| 145 | BOOST_CHECK(interest.getCanBePrefix()); |
| 146 | BOOST_CHECK(interest.getMustBeFresh()); |
| 147 | } |
| 148 | |
| 149 | BOOST_AUTO_TEST_SUITE_END() // TestMetadataObject |
| 150 | |
| 151 | } // namespace tests |
| 152 | } // namespace ndn |