blob: 0cbe73773d023d6569231230c8fb9b8200381ef8 [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 Pesaventofffdd622023-08-28 22:50:43 -040029static_assert(std::is_convertible_v<MetadataObject::Error*, tlv::Error*>,
30 "MetadataObject::Error must inherit from tlv::Error");
31
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050032class MetadataObjectFixture : public KeyChainFixture
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070033{
34public:
35 MetadataObjectFixture()
Davide Pesavento21ce3ad2021-11-01 22:00:45 -040036 : versionedContentName(Name(baseContentName)
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070037 .appendVersion(342092199154ULL))
38 , metadataFullName(Name(baseContentName)
Davide Pesavento21ce3ad2021-11-01 22:00:45 -040039 .append(MetadataObject::getKeywordComponent())
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070040 .appendVersion(metadataVerNo)
41 .appendSegment(0))
42 {
43 }
44
45protected:
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070046 // 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
55BOOST_FIXTURE_TEST_SUITE(TestMetadataObject, MetadataObjectFixture)
56
57BOOST_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 Newberry78ffc632020-05-17 15:13:46 -070064 security::SigningInfo(), metadataVerNo);
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070065
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
83BOOST_AUTO_TEST_CASE(InvalidFormat)
84{
85 Data data;
86
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070087 // invalid metadata name
88 data.setName("/ndn/unit/test");
Davide Pesavento81bd6962020-06-17 16:03:23 -040089 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 Pesavento21ce3ad2021-11-01 22:00:45 -040092 data.setName(Name("/ndn/unit/test").append(MetadataObject::getKeywordComponent()));
Davide Pesavento81bd6962020-06-17 16:03:23 -040093 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 Pesavento21ce3ad2021-11-01 22:00:45 -040098 data.setName(Name("/ndn/unit/test")
99 .append(MetadataObject::getKeywordComponent())
100 .appendVersion()
101 .appendSegment(0));
Davide Pesavento81bd6962020-06-17 16:03:23 -0400102 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 Ghasemi6b99b6f2018-10-26 12:08:33 -0700106
107 // empty content
Davide Pesavento81bd6962020-06-17 16:03:23 -0400108 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 Ghasemi6b99b6f2018-10-26 12:08:33 -0700112
113 // non-empty content with no name element
114 data.setContent("F000"_block);
Davide Pesavento81bd6962020-06-17 16:03:23 -0400115 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 Ghasemi6b99b6f2018-10-26 12:08:33 -0700118}
119
Davide Pesavento21ce3ad2021-11-01 22:00:45 -0400120BOOST_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 Ghasemi6b99b6f2018-10-26 12:08:33 -0700128BOOST_AUTO_TEST_CASE(IsValidName)
129{
130 // valid name
131 Name name = Name("/ndn/unit/test")
Davide Pesavento21ce3ad2021-11-01 22:00:45 -0400132 .append(MetadataObject::getKeywordComponent())
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700133 .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 Pesavento21ce3ad2021-11-01 22:00:45 -0400144 // keyword component is missing
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700145 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 Pesavento21ce3ad2021-11-01 22:00:45 -0400154 // invalid keyword name component
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700155 name = name.getPrefix(-3)
Davide Pesaventodf8fd8a2022-02-21 20:04:21 -0500156 .append(tlv::KeywordNameComponent, {'f', 'o', 'o'})
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700157 .appendVersion()
158 .appendSegment(0);
159 BOOST_CHECK_EQUAL(MetadataObject::isValidName(name), false);
160}
161
162BOOST_AUTO_TEST_CASE(MakeDiscoveryInterest)
163{
164 Interest interest = MetadataObject::makeDiscoveryInterest(baseContentName);
Davide Pesavento21ce3ad2021-11-01 22:00:45 -0400165 BOOST_CHECK_EQUAL(interest.getName(), Name(baseContentName).append(MetadataObject::getKeywordComponent()));
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700166 BOOST_CHECK(interest.getCanBePrefix());
167 BOOST_CHECK(interest.getMustBeFresh());
168}
169
170BOOST_AUTO_TEST_SUITE_END() // TestMetadataObject
171
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400172} // namespace ndn::tests