blob: 9606db79c3b444dfa06d7db0e169c755be59f6da [file] [log] [blame]
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento21ce3ad2021-11-01 22:00:45 -04003 * Copyright (c) 2013-2021 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
27namespace ndn {
28namespace tests {
29
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050030class MetadataObjectFixture : public KeyChainFixture
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070031{
32public:
33 MetadataObjectFixture()
Davide Pesavento21ce3ad2021-11-01 22:00:45 -040034 : versionedContentName(Name(baseContentName)
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070035 .appendVersion(342092199154ULL))
36 , metadataFullName(Name(baseContentName)
Davide Pesavento21ce3ad2021-11-01 22:00:45 -040037 .append(MetadataObject::getKeywordComponent())
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070038 .appendVersion(metadataVerNo)
39 .appendSegment(0))
40 {
41 }
42
43protected:
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070044 // 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
53BOOST_FIXTURE_TEST_SUITE(TestMetadataObject, MetadataObjectFixture)
54
55BOOST_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 Newberry78ffc632020-05-17 15:13:46 -070062 security::SigningInfo(), metadataVerNo);
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070063
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
81BOOST_AUTO_TEST_CASE(InvalidFormat)
82{
83 Data data;
84
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070085 // invalid metadata name
86 data.setName("/ndn/unit/test");
Davide Pesavento81bd6962020-06-17 16:03:23 -040087 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 Pesavento21ce3ad2021-11-01 22:00:45 -040090 data.setName(Name("/ndn/unit/test").append(MetadataObject::getKeywordComponent()));
Davide Pesavento81bd6962020-06-17 16:03:23 -040091 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 Pesavento21ce3ad2021-11-01 22:00:45 -040096 data.setName(Name("/ndn/unit/test")
97 .append(MetadataObject::getKeywordComponent())
98 .appendVersion()
99 .appendSegment(0));
Davide Pesavento81bd6962020-06-17 16:03:23 -0400100 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 Ghasemi6b99b6f2018-10-26 12:08:33 -0700104
105 // empty content
Davide Pesavento81bd6962020-06-17 16:03:23 -0400106 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 Ghasemi6b99b6f2018-10-26 12:08:33 -0700110
111 // non-empty content with no name element
112 data.setContent("F000"_block);
Davide Pesavento81bd6962020-06-17 16:03:23 -0400113 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 Ghasemi6b99b6f2018-10-26 12:08:33 -0700116}
117
Davide Pesavento21ce3ad2021-11-01 22:00:45 -0400118BOOST_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 Ghasemi6b99b6f2018-10-26 12:08:33 -0700126BOOST_AUTO_TEST_CASE(IsValidName)
127{
128 // valid name
129 Name name = Name("/ndn/unit/test")
Davide Pesavento21ce3ad2021-11-01 22:00:45 -0400130 .append(MetadataObject::getKeywordComponent())
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700131 .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 Pesavento21ce3ad2021-11-01 22:00:45 -0400142 // keyword component is missing
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700143 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 Pesavento21ce3ad2021-11-01 22:00:45 -0400152 // invalid keyword name component
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700153 name = name.getPrefix(-3)
Davide Pesavento21ce3ad2021-11-01 22:00:45 -0400154 .append(tlv::KeywordNameComponent, reinterpret_cast<const uint8_t*>("foo"), std::strlen("foo"))
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700155 .appendVersion()
156 .appendSegment(0);
157 BOOST_CHECK_EQUAL(MetadataObject::isValidName(name), false);
158}
159
160BOOST_AUTO_TEST_CASE(MakeDiscoveryInterest)
161{
162 Interest interest = MetadataObject::makeDiscoveryInterest(baseContentName);
Davide Pesavento21ce3ad2021-11-01 22:00:45 -0400163 BOOST_CHECK_EQUAL(interest.getName(), Name(baseContentName).append(MetadataObject::getKeywordComponent()));
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -0700164 BOOST_CHECK(interest.getCanBePrefix());
165 BOOST_CHECK(interest.getMustBeFresh());
166}
167
168BOOST_AUTO_TEST_SUITE_END() // TestMetadataObject
169
170} // namespace tests
171} // namespace ndn