blob: 480f7d4048862f8b742b19a0d440c64b93b017a1 [file] [log] [blame]
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -07004 *
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 Pesavento4c1ad4c2020-11-16 21:12:02 -050025#include "tests/key-chain-fixture.hpp"
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070026
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040027namespace ndn::tests {
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070028
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050029class MetadataObjectFixture : public KeyChainFixture
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070030{
31public:
32 MetadataObjectFixture()
Davide Pesavento21ce3ad2021-11-01 22:00:45 -040033 : versionedContentName(Name(baseContentName)
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070034 .appendVersion(342092199154ULL))
35 , metadataFullName(Name(baseContentName)
Davide Pesavento21ce3ad2021-11-01 22:00:45 -040036 .append(MetadataObject::getKeywordComponent())
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070037 .appendVersion(metadataVerNo)
38 .appendSegment(0))
39 {
40 }
41
42protected:
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070043 // 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
52BOOST_FIXTURE_TEST_SUITE(TestMetadataObject, MetadataObjectFixture)
53
54BOOST_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 Newberry78ffc632020-05-17 15:13:46 -070061 security::SigningInfo(), metadataVerNo);
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070062
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
80BOOST_AUTO_TEST_CASE(InvalidFormat)
81{
82 Data data;
83
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070084 // invalid metadata name
85 data.setName("/ndn/unit/test");
Davide Pesavento81bd6962020-06-17 16:03:23 -040086 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 Pesavento21ce3ad2021-11-01 22:00:45 -040089 data.setName(Name("/ndn/unit/test").append(MetadataObject::getKeywordComponent()));
Davide Pesavento81bd6962020-06-17 16:03:23 -040090 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 Pesavento21ce3ad2021-11-01 22:00:45 -040095 data.setName(Name("/ndn/unit/test")
96 .append(MetadataObject::getKeywordComponent())
97 .appendVersion()
98 .appendSegment(0));
Davide Pesavento81bd6962020-06-17 16:03:23 -040099 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 Ghasemi6b99b6f2018-10-26 12:08:33 -0700103
104 // empty content
Davide Pesavento81bd6962020-06-17 16:03:23 -0400105 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 Ghasemi6b99b6f2018-10-26 12:08:33 -0700109
110 // non-empty content with no name element
111 data.setContent("F000"_block);
Davide Pesavento81bd6962020-06-17 16:03:23 -0400112 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 Ghasemi6b99b6f2018-10-26 12:08:33 -0700115}
116
Davide Pesavento21ce3ad2021-11-01 22:00:45 -0400117BOOST_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 Ghasemi6b99b6f2018-10-26 12:08:33 -0700125BOOST_AUTO_TEST_CASE(IsValidName)
126{
127 // valid name
128 Name name = Name("/ndn/unit/test")
Davide Pesavento21ce3ad2021-11-01 22:00:45 -0400129 .append(MetadataObject::getKeywordComponent())
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700130 .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 Pesavento21ce3ad2021-11-01 22:00:45 -0400141 // keyword component is missing
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700142 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 Pesavento21ce3ad2021-11-01 22:00:45 -0400151 // invalid keyword name component
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700152 name = name.getPrefix(-3)
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500153 .append(tlv::KeywordNameComponent, {'f', 'o', 'o'})
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700154 .appendVersion()
155 .appendSegment(0);
156 BOOST_CHECK_EQUAL(MetadataObject::isValidName(name), false);
157}
158
159BOOST_AUTO_TEST_CASE(MakeDiscoveryInterest)
160{
161 Interest interest = MetadataObject::makeDiscoveryInterest(baseContentName);
Davide Pesavento21ce3ad2021-11-01 22:00:45 -0400162 BOOST_CHECK_EQUAL(interest.getName(), Name(baseContentName).append(MetadataObject::getKeywordComponent()));
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700163 BOOST_CHECK(interest.getCanBePrefix());
164 BOOST_CHECK(interest.getMustBeFresh());
165}
166
167BOOST_AUTO_TEST_SUITE_END() // TestMetadataObject
168
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400169} // namespace ndn::tests