blob: 7aae5cf4ce40f0befa58d806d4b3cc9238b38091 [file] [log] [blame]
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Eric Newberry78ffc632020-05-17 15:13:46 -07003 * Copyright (c) 2013-2020 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()
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
44protected:
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
56BOOST_FIXTURE_TEST_SUITE(TestMetadataObject, MetadataObjectFixture)
57
58BOOST_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 Newberry78ffc632020-05-17 15:13:46 -070065 security::SigningInfo(), metadataVerNo);
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070066
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
84BOOST_AUTO_TEST_CASE(InvalidFormat)
85{
86 Data data;
87
Chavoosh Ghasemi6b99b6f2018-10-26 12:08:33 -070088 // invalid metadata name
89 data.setName("/ndn/unit/test");
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 is not a valid MetadataObject name"s;
92 });
93 data.setName(Name("/ndn/unit/test").append(metadataComponent));
94 BOOST_CHECK_EXCEPTION(MetadataObject{data}, tlv::Error, [] (const auto& e) {
95 return e.what() == "Name /ndn/unit/test/32=metadata is not a valid MetadataObject name"s;
96 });
97
98 // invalid content type
99 data.setName(Name("/ndn/unit/test").append(metadataComponent).appendVersion().appendSegment(0));
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 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
118BOOST_AUTO_TEST_CASE(IsValidName)
119{
120 // valid name
121 Name name = Name("/ndn/unit/test")
122 .append(metadataComponent)
123 .appendVersion()
124 .appendSegment(0);
125 BOOST_CHECK(MetadataObject::isValidName(name));
126
127 // invalid names
128 // segment component is missing
129 BOOST_CHECK_EQUAL(MetadataObject::isValidName(name.getPrefix(-1)), false);
130
131 // version component is missing
132 BOOST_CHECK_EQUAL(MetadataObject::isValidName(name.getPrefix(-2)), false);
133
134 // keyword name component `32=keyword` is missing
135 BOOST_CHECK_EQUAL(MetadataObject::isValidName(name.getPrefix(-3)), false);
136
137 // too short name
138 BOOST_CHECK_EQUAL(MetadataObject::isValidName(name.getPrefix(-4)), false);
139
140 // out-of-order segment and version components
141 name = name.getPrefix(-2).appendSegment(0).appendVersion();
142 BOOST_CHECK_EQUAL(MetadataObject::isValidName(name), false);
143
144 // invalid name component keyword
145 name = name.getPrefix(-3)
146 .append(32, reinterpret_cast<const uint8_t*>("foo"), std::strlen("foo"))
147 .appendVersion()
148 .appendSegment(0);
149 BOOST_CHECK_EQUAL(MetadataObject::isValidName(name), false);
150}
151
152BOOST_AUTO_TEST_CASE(MakeDiscoveryInterest)
153{
154 Interest interest = MetadataObject::makeDiscoveryInterest(baseContentName);
155 BOOST_CHECK_EQUAL(interest.getName(), Name(baseContentName).append(metadataComponent));
156 BOOST_CHECK(interest.getCanBePrefix());
157 BOOST_CHECK(interest.getMustBeFresh());
158}
159
160BOOST_AUTO_TEST_SUITE_END() // TestMetadataObject
161
162} // namespace tests
163} // namespace ndn