blob: a13e9f5317cb31fac7a9236a8043a88c259ce4fd [file] [log] [blame]
Junxiao Shi8127d1a2018-08-24 15:29:21 -06001/* -*- 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.
Junxiao Shi8127d1a2018-08-24 15:29:21 -06004 *
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 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/prefix-announcement.hpp"
23#include "ndn-cxx/encoding/tlv-nfd.hpp"
Junxiao Shi8127d1a2018-08-24 15:29:21 -060024
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050025#include "tests/key-chain-fixture.hpp"
26#include "tests/test-common.hpp"
Junxiao Shi8127d1a2018-08-24 15:29:21 -060027
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040028namespace ndn::tests {
Junxiao Shi8127d1a2018-08-24 15:29:21 -060029
30BOOST_AUTO_TEST_SUITE(TestPrefixAnnouncement)
31
32static Data
33makePrefixAnnData()
34{
35 return Data(
Eric Newberryc25e4632021-02-11 10:48:11 -080036 "0678 0718 announced-name=/net/example 08036E6574 08076578616D706C65"
Davide Pesavento47a94d12021-10-31 16:41:05 -040037 " keyword-prefix-ann=20025041 version=360101 segment=320100"
Junxiao Shi8127d1a2018-08-24 15:29:21 -060038 " 1403 content-type=prefix-ann 180105"
39 " 1530 expire in one hour 6D040036EE80"
40 " validity FD00FD26 FD00FE0F323031383130333054303030303030"
41 " FD00FF0F323031383131323454323335393539"
42 " 1603 1B0100 signature"
43 " 1720 0000000000000000000000000000000000000000000000000000000000000000"_block);
44}
45
Junxiao Shi8127d1a2018-08-24 15:29:21 -060046BOOST_AUTO_TEST_CASE(DecodeGood)
47{
48 Data data0 = makePrefixAnnData();
49 PrefixAnnouncement pa0(data0);
50 BOOST_CHECK_EQUAL(pa0.getAnnouncedName(), "/net/example");
51 BOOST_CHECK_EQUAL(pa0.getExpiration(), 1_h);
52 BOOST_CHECK(pa0.getValidityPeriod());
53 BOOST_CHECK_EQUAL(*pa0.getValidityPeriod(),
54 security::ValidityPeriod(time::fromIsoString("20181030T000000"),
55 time::fromIsoString("20181124T235959")));
56
57 // reorder ExpirationPeriod and ValidityPeriod, unrecognized non-critical element
58 Data data1 = makePrefixAnnData();
59 Block payload1 = data1.getContent();
60 payload1.parse();
61 Block expirationElement = payload1.get(tlv::nfd::ExpirationPeriod);
62 payload1.remove(tlv::nfd::ExpirationPeriod);
63 payload1.push_back(expirationElement);
64 payload1.push_back("2000"_block);
65 payload1.encode();
66 data1.setContent(payload1);
67 PrefixAnnouncement pa1(data1);
68 BOOST_CHECK_EQUAL(pa1.getAnnouncedName(), "/net/example");
69 BOOST_CHECK_EQUAL(pa1.getExpiration(), 1_h);
70 BOOST_CHECK(pa1.getValidityPeriod());
71 BOOST_CHECK_EQUAL(*pa1.getValidityPeriod(),
72 security::ValidityPeriod(time::fromIsoString("20181030T000000"),
73 time::fromIsoString("20181124T235959")));
74
75 // no ValidityPeriod
76 Data data2 = makePrefixAnnData();
77 Block payload2 = data2.getContent();
78 payload2.parse();
79 payload2.remove(tlv::ValidityPeriod);
80 payload2.encode();
81 data2.setContent(payload2);
82 PrefixAnnouncement pa2(data2);
83 BOOST_CHECK_EQUAL(pa2.getAnnouncedName(), "/net/example");
84 BOOST_CHECK_EQUAL(pa2.getExpiration(), 1_h);
85 BOOST_CHECK(!pa2.getValidityPeriod());
86}
87
88BOOST_AUTO_TEST_CASE(DecodeBad)
89{
90 // wrong ContentType
91 Data data0 = makePrefixAnnData();
92 data0.setContentType(tlv::ContentType_Blob);
Davide Pesavento81bd6962020-06-17 16:03:23 -040093 BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data0}, tlv::Error, [] (const auto& e) {
94 return e.what() == "Data is not a prefix announcement: ContentType is 0"s;
95 });
Junxiao Shi8127d1a2018-08-24 15:29:21 -060096
97 // Name has no "32=PA" keyword
98 Data data1 = makePrefixAnnData();
99 setNameComponent(data1, -3, name::Component::fromEscapedString("32=not-PA"));
Davide Pesavento81bd6962020-06-17 16:03:23 -0400100 BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data1}, tlv::Error, [] (const auto& e) {
101 return e.what() == "Data is not a prefix announcement: wrong name structure"s;
102 });
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600103
104 // Name has no version component
105 Data data2 = makePrefixAnnData();
106 setNameComponent(data2, -2, "not-version");
Davide Pesavento81bd6962020-06-17 16:03:23 -0400107 BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data2}, tlv::Error, [] (const auto& e) {
108 return e.what() == "Data is not a prefix announcement: wrong name structure"s;
109 });
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600110
111 // Name has no segment number component
112 Data data3 = makePrefixAnnData();
113 setNameComponent(data3, -2, "not-segment");
Davide Pesavento81bd6962020-06-17 16:03:23 -0400114 BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data3}, tlv::Error, [] (const auto& e) {
115 return e.what() == "Data is not a prefix announcement: wrong name structure"s;
116 });
117
118 // Data without Content
119 Data data4 = makePrefixAnnData();
120 data4.unsetContent();
121 BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data4}, tlv::Error, [] (const auto& e) {
122 return e.what() == "Prefix announcement is empty"s;
123 });
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600124
125 // Content has no ExpirationPeriod element
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600126 Data data5 = makePrefixAnnData();
127 Block payload5 = data5.getContent();
128 payload5.parse();
129 payload5.remove(tlv::nfd::ExpirationPeriod);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600130 data5.setContent(payload5);
Davide Pesavento81bd6962020-06-17 16:03:23 -0400131 BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data5}, tlv::Error, [] (const auto& e) {
132 return e.what() == "No sub-element of type 109 found in block of type 21"s;
133 });
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600134
Davide Pesavento81bd6962020-06-17 16:03:23 -0400135 // ExpirationPeriod is malformed
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600136 Data data6 = makePrefixAnnData();
137 Block payload6 = data6.getContent();
138 payload6.parse();
Davide Pesavento81bd6962020-06-17 16:03:23 -0400139 payload6.remove(tlv::nfd::ExpirationPeriod);
140 payload6.push_back("6D03010101"_block);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600141 data6.setContent(payload6);
Davide Pesavento81bd6962020-06-17 16:03:23 -0400142 BOOST_CHECK_THROW(PrefixAnnouncement{data6}, tlv::Error);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600143
Davide Pesavento81bd6962020-06-17 16:03:23 -0400144 // ValidityPeriod is malformed
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600145 Data data7 = makePrefixAnnData();
146 Block payload7 = data7.getContent();
147 payload7.parse();
Davide Pesavento81bd6962020-06-17 16:03:23 -0400148 payload7.remove(tlv::ValidityPeriod);
149 payload7.push_back("FD00FD00"_block);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600150 data7.setContent(payload7);
Davide Pesavento81bd6962020-06-17 16:03:23 -0400151 BOOST_CHECK_THROW(PrefixAnnouncement{data7}, tlv::Error);
152
153 // Content has unrecognized critical element
154 Data data8 = makePrefixAnnData();
155 Block payload8 = data8.getContent();
156 payload8.parse();
157 payload8.push_back("0200"_block);
158 data8.setContent(payload8);
159 BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data8}, tlv::Error, [] (const auto& e) {
160 return e.what() == "Unrecognized element of critical type 2"s;
161 });
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600162}
163
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500164BOOST_FIXTURE_TEST_CASE(EncodeEmpty, KeyChainFixture)
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000165{
166 PrefixAnnouncement pa;
167 BOOST_CHECK(!pa.getData());
168 BOOST_CHECK_EQUAL(pa.getAnnouncedName(), "/");
169 BOOST_CHECK_EQUAL(pa.getExpiration(), 0_ms);
170 BOOST_CHECK(!pa.getValidityPeriod());
171
172 const Data& data = pa.toData(m_keyChain, signingWithSha256(), 5);
Eric Newberryc25e4632021-02-11 10:48:11 -0800173 BOOST_CHECK_EQUAL(data.getName(), "/32=PA/v=5/seg=0");
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000174 BOOST_CHECK_EQUAL(data.getContentType(), tlv::ContentType_PrefixAnn);
175 BOOST_REQUIRE(pa.getData());
176 BOOST_CHECK_EQUAL(*pa.getData(), data);
177
178 PrefixAnnouncement decoded(data);
179 BOOST_CHECK_EQUAL(decoded.getAnnouncedName(), "/");
180 BOOST_CHECK_EQUAL(decoded.getExpiration(), 0_s);
181 BOOST_CHECK(!decoded.getValidityPeriod());
182
183 BOOST_CHECK_EQUAL(pa, decoded);
184}
185
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500186BOOST_FIXTURE_TEST_CASE(EncodeNoValidity, KeyChainFixture)
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000187{
188 PrefixAnnouncement pa;
189 pa.setAnnouncedName("/net/example");
190 BOOST_CHECK_THROW(pa.setExpiration(-1_ms), std::invalid_argument);
191 pa.setExpiration(1_h);
192
193 const Data& data = pa.toData(m_keyChain, signingWithSha256(), 1);
Eric Newberryc25e4632021-02-11 10:48:11 -0800194 BOOST_CHECK_EQUAL(data.getName(), "/net/example/32=PA/v=1/seg=0");
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000195 BOOST_CHECK_EQUAL(data.getContentType(), tlv::ContentType_PrefixAnn);
196
197 const Block& payload = data.getContent();
198 payload.parse();
199 BOOST_CHECK_EQUAL(readNonNegativeInteger(payload.get(tlv::nfd::ExpirationPeriod)), 3600000);
200 BOOST_CHECK(payload.find(tlv::ValidityPeriod) == payload.elements_end());
201
202 PrefixAnnouncement decoded(data);
203 BOOST_CHECK_EQUAL(decoded.getAnnouncedName(), "/net/example");
204 BOOST_CHECK_EQUAL(decoded.getExpiration(), 1_h);
205 BOOST_CHECK(!decoded.getValidityPeriod());
206
207 BOOST_CHECK_EQUAL(pa, decoded);
208}
209
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500210BOOST_FIXTURE_TEST_CASE(EncodeWithValidity, KeyChainFixture)
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000211{
212 PrefixAnnouncement pa;
213 pa.setAnnouncedName("/net/example");
214 pa.setExpiration(1_h);
215 security::ValidityPeriod validity(time::fromIsoString("20181030T000000"),
216 time::fromIsoString("20181124T235959"));
217 pa.setValidityPeriod(validity);
218
219 const Data& data = pa.toData(m_keyChain);
220 const Block& payload = data.getContent();
221 payload.parse();
222 BOOST_CHECK_EQUAL(readNonNegativeInteger(payload.get(tlv::nfd::ExpirationPeriod)), 3600000);
223 BOOST_CHECK_EQUAL(payload.get(tlv::ValidityPeriod), validity.wireEncode());
224
225 PrefixAnnouncement decoded(data);
226 BOOST_CHECK_EQUAL(decoded.getAnnouncedName(), "/net/example");
227 BOOST_CHECK_EQUAL(decoded.getExpiration(), 1_h);
228 BOOST_REQUIRE(decoded.getValidityPeriod());
229 BOOST_CHECK_EQUAL(*decoded.getValidityPeriod(), validity);
230
231 BOOST_CHECK_EQUAL(pa, decoded);
232}
233
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600234BOOST_AUTO_TEST_CASE(Modify)
235{
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000236 PrefixAnnouncement pa(makePrefixAnnData());
237
238 PrefixAnnouncement pa0(pa);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600239 BOOST_REQUIRE(pa0.getData());
240 BOOST_CHECK_EQUAL(*pa0.getData(), makePrefixAnnData());
241 pa0.setAnnouncedName("/com/example");
242 BOOST_CHECK(!pa0.getData());
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000243 BOOST_CHECK_NE(pa0, pa);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600244
245 PrefixAnnouncement pa1(makePrefixAnnData());
246 pa1.setExpiration(5_min);
247 BOOST_CHECK(!pa1.getData());
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000248 BOOST_CHECK_NE(pa1, pa);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600249
250 PrefixAnnouncement pa2(makePrefixAnnData());
251 pa2.setValidityPeriod(security::ValidityPeriod(time::fromIsoString("20180118T000000"),
252 time::fromIsoString("20180212T235959")));
253 BOOST_CHECK(!pa2.getData());
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000254 BOOST_CHECK_NE(pa2, pa);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600255}
256
Davide Pesavento21ce3ad2021-11-01 22:00:45 -0400257BOOST_AUTO_TEST_CASE(KeywordComponent)
258{
259 BOOST_CHECK_EQUAL(PrefixAnnouncement::getKeywordComponent().wireEncode(),
260 "20 02 5041"_block);
261 BOOST_CHECK_EQUAL(PrefixAnnouncement::getKeywordComponent().toUri(name::UriFormat::CANONICAL),
262 "32=PA");
263}
264
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600265BOOST_AUTO_TEST_SUITE_END() // TestPrefixAnnouncement
266
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400267} // namespace ndn::tests