blob: d154484635b0ada08a718c5c906378c9085816ee [file] [log] [blame]
Junxiao Shi8127d1a2018-08-24 15:29:21 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento81bd6962020-06-17 16:03:23 -04003 * Copyright (c) 2013-2020 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
28namespace ndn {
29namespace tests {
30
31BOOST_AUTO_TEST_SUITE(TestPrefixAnnouncement)
32
33static Data
34makePrefixAnnData()
35{
36 return Data(
37 "067A 071A announced-name=/net/example 08036E6574 08076578616D706C65"
38 " keyword-prefix-ann=20025041 version=0802FD01 segment=08020000"
39 " 1403 content-type=prefix-ann 180105"
40 " 1530 expire in one hour 6D040036EE80"
41 " validity FD00FD26 FD00FE0F323031383130333054303030303030"
42 " FD00FF0F323031383131323454323335393539"
43 " 1603 1B0100 signature"
44 " 1720 0000000000000000000000000000000000000000000000000000000000000000"_block);
45}
46
Junxiao Shi8127d1a2018-08-24 15:29:21 -060047BOOST_AUTO_TEST_CASE(DecodeGood)
48{
49 Data data0 = makePrefixAnnData();
50 PrefixAnnouncement pa0(data0);
51 BOOST_CHECK_EQUAL(pa0.getAnnouncedName(), "/net/example");
52 BOOST_CHECK_EQUAL(pa0.getExpiration(), 1_h);
53 BOOST_CHECK(pa0.getValidityPeriod());
54 BOOST_CHECK_EQUAL(*pa0.getValidityPeriod(),
55 security::ValidityPeriod(time::fromIsoString("20181030T000000"),
56 time::fromIsoString("20181124T235959")));
57
58 // reorder ExpirationPeriod and ValidityPeriod, unrecognized non-critical element
59 Data data1 = makePrefixAnnData();
60 Block payload1 = data1.getContent();
61 payload1.parse();
62 Block expirationElement = payload1.get(tlv::nfd::ExpirationPeriod);
63 payload1.remove(tlv::nfd::ExpirationPeriod);
64 payload1.push_back(expirationElement);
65 payload1.push_back("2000"_block);
66 payload1.encode();
67 data1.setContent(payload1);
68 PrefixAnnouncement pa1(data1);
69 BOOST_CHECK_EQUAL(pa1.getAnnouncedName(), "/net/example");
70 BOOST_CHECK_EQUAL(pa1.getExpiration(), 1_h);
71 BOOST_CHECK(pa1.getValidityPeriod());
72 BOOST_CHECK_EQUAL(*pa1.getValidityPeriod(),
73 security::ValidityPeriod(time::fromIsoString("20181030T000000"),
74 time::fromIsoString("20181124T235959")));
75
76 // no ValidityPeriod
77 Data data2 = makePrefixAnnData();
78 Block payload2 = data2.getContent();
79 payload2.parse();
80 payload2.remove(tlv::ValidityPeriod);
81 payload2.encode();
82 data2.setContent(payload2);
83 PrefixAnnouncement pa2(data2);
84 BOOST_CHECK_EQUAL(pa2.getAnnouncedName(), "/net/example");
85 BOOST_CHECK_EQUAL(pa2.getExpiration(), 1_h);
86 BOOST_CHECK(!pa2.getValidityPeriod());
87}
88
89BOOST_AUTO_TEST_CASE(DecodeBad)
90{
91 // wrong ContentType
92 Data data0 = makePrefixAnnData();
93 data0.setContentType(tlv::ContentType_Blob);
Davide Pesavento81bd6962020-06-17 16:03:23 -040094 BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data0}, tlv::Error, [] (const auto& e) {
95 return e.what() == "Data is not a prefix announcement: ContentType is 0"s;
96 });
Junxiao Shi8127d1a2018-08-24 15:29:21 -060097
98 // Name has no "32=PA" keyword
99 Data data1 = makePrefixAnnData();
100 setNameComponent(data1, -3, name::Component::fromEscapedString("32=not-PA"));
Davide Pesavento81bd6962020-06-17 16:03:23 -0400101 BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data1}, tlv::Error, [] (const auto& e) {
102 return e.what() == "Data is not a prefix announcement: wrong name structure"s;
103 });
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600104
105 // Name has no version component
106 Data data2 = makePrefixAnnData();
107 setNameComponent(data2, -2, "not-version");
Davide Pesavento81bd6962020-06-17 16:03:23 -0400108 BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data2}, tlv::Error, [] (const auto& e) {
109 return e.what() == "Data is not a prefix announcement: wrong name structure"s;
110 });
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600111
112 // Name has no segment number component
113 Data data3 = makePrefixAnnData();
114 setNameComponent(data3, -2, "not-segment");
Davide Pesavento81bd6962020-06-17 16:03:23 -0400115 BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data3}, tlv::Error, [] (const auto& e) {
116 return e.what() == "Data is not a prefix announcement: wrong name structure"s;
117 });
118
119 // Data without Content
120 Data data4 = makePrefixAnnData();
121 data4.unsetContent();
122 BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data4}, tlv::Error, [] (const auto& e) {
123 return e.what() == "Prefix announcement is empty"s;
124 });
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600125
126 // Content has no ExpirationPeriod element
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600127 Data data5 = makePrefixAnnData();
128 Block payload5 = data5.getContent();
129 payload5.parse();
130 payload5.remove(tlv::nfd::ExpirationPeriod);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600131 data5.setContent(payload5);
Davide Pesavento81bd6962020-06-17 16:03:23 -0400132 BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data5}, tlv::Error, [] (const auto& e) {
133 return e.what() == "No sub-element of type 109 found in block of type 21"s;
134 });
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600135
Davide Pesavento81bd6962020-06-17 16:03:23 -0400136 // ExpirationPeriod is malformed
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600137 Data data6 = makePrefixAnnData();
138 Block payload6 = data6.getContent();
139 payload6.parse();
Davide Pesavento81bd6962020-06-17 16:03:23 -0400140 payload6.remove(tlv::nfd::ExpirationPeriod);
141 payload6.push_back("6D03010101"_block);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600142 data6.setContent(payload6);
Davide Pesavento81bd6962020-06-17 16:03:23 -0400143 BOOST_CHECK_THROW(PrefixAnnouncement{data6}, tlv::Error);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600144
Davide Pesavento81bd6962020-06-17 16:03:23 -0400145 // ValidityPeriod is malformed
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600146 Data data7 = makePrefixAnnData();
147 Block payload7 = data7.getContent();
148 payload7.parse();
Davide Pesavento81bd6962020-06-17 16:03:23 -0400149 payload7.remove(tlv::ValidityPeriod);
150 payload7.push_back("FD00FD00"_block);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600151 data7.setContent(payload7);
Davide Pesavento81bd6962020-06-17 16:03:23 -0400152 BOOST_CHECK_THROW(PrefixAnnouncement{data7}, tlv::Error);
153
154 // Content has unrecognized critical element
155 Data data8 = makePrefixAnnData();
156 Block payload8 = data8.getContent();
157 payload8.parse();
158 payload8.push_back("0200"_block);
159 data8.setContent(payload8);
160 BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data8}, tlv::Error, [] (const auto& e) {
161 return e.what() == "Unrecognized element of critical type 2"s;
162 });
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600163}
164
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500165BOOST_FIXTURE_TEST_CASE(EncodeEmpty, KeyChainFixture)
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000166{
167 PrefixAnnouncement pa;
168 BOOST_CHECK(!pa.getData());
169 BOOST_CHECK_EQUAL(pa.getAnnouncedName(), "/");
170 BOOST_CHECK_EQUAL(pa.getExpiration(), 0_ms);
171 BOOST_CHECK(!pa.getValidityPeriod());
172
173 const Data& data = pa.toData(m_keyChain, signingWithSha256(), 5);
174 BOOST_CHECK_EQUAL(data.getName(), "/32=PA/%FD%05/%00%00");
175 BOOST_CHECK_EQUAL(data.getContentType(), tlv::ContentType_PrefixAnn);
176 BOOST_REQUIRE(pa.getData());
177 BOOST_CHECK_EQUAL(*pa.getData(), data);
178
179 PrefixAnnouncement decoded(data);
180 BOOST_CHECK_EQUAL(decoded.getAnnouncedName(), "/");
181 BOOST_CHECK_EQUAL(decoded.getExpiration(), 0_s);
182 BOOST_CHECK(!decoded.getValidityPeriod());
183
184 BOOST_CHECK_EQUAL(pa, decoded);
185}
186
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500187BOOST_FIXTURE_TEST_CASE(EncodeNoValidity, KeyChainFixture)
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000188{
189 PrefixAnnouncement pa;
190 pa.setAnnouncedName("/net/example");
191 BOOST_CHECK_THROW(pa.setExpiration(-1_ms), std::invalid_argument);
192 pa.setExpiration(1_h);
193
194 const Data& data = pa.toData(m_keyChain, signingWithSha256(), 1);
195 BOOST_CHECK_EQUAL(data.getName(), "/net/example/32=PA/%FD%01/%00%00");
196 BOOST_CHECK_EQUAL(data.getContentType(), tlv::ContentType_PrefixAnn);
197
198 const Block& payload = data.getContent();
199 payload.parse();
200 BOOST_CHECK_EQUAL(readNonNegativeInteger(payload.get(tlv::nfd::ExpirationPeriod)), 3600000);
201 BOOST_CHECK(payload.find(tlv::ValidityPeriod) == payload.elements_end());
202
203 PrefixAnnouncement decoded(data);
204 BOOST_CHECK_EQUAL(decoded.getAnnouncedName(), "/net/example");
205 BOOST_CHECK_EQUAL(decoded.getExpiration(), 1_h);
206 BOOST_CHECK(!decoded.getValidityPeriod());
207
208 BOOST_CHECK_EQUAL(pa, decoded);
209}
210
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500211BOOST_FIXTURE_TEST_CASE(EncodeWithValidity, KeyChainFixture)
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000212{
213 PrefixAnnouncement pa;
214 pa.setAnnouncedName("/net/example");
215 pa.setExpiration(1_h);
216 security::ValidityPeriod validity(time::fromIsoString("20181030T000000"),
217 time::fromIsoString("20181124T235959"));
218 pa.setValidityPeriod(validity);
219
220 const Data& data = pa.toData(m_keyChain);
221 const Block& payload = data.getContent();
222 payload.parse();
223 BOOST_CHECK_EQUAL(readNonNegativeInteger(payload.get(tlv::nfd::ExpirationPeriod)), 3600000);
224 BOOST_CHECK_EQUAL(payload.get(tlv::ValidityPeriod), validity.wireEncode());
225
226 PrefixAnnouncement decoded(data);
227 BOOST_CHECK_EQUAL(decoded.getAnnouncedName(), "/net/example");
228 BOOST_CHECK_EQUAL(decoded.getExpiration(), 1_h);
229 BOOST_REQUIRE(decoded.getValidityPeriod());
230 BOOST_CHECK_EQUAL(*decoded.getValidityPeriod(), validity);
231
232 BOOST_CHECK_EQUAL(pa, decoded);
233}
234
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600235BOOST_AUTO_TEST_CASE(Modify)
236{
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000237 PrefixAnnouncement pa(makePrefixAnnData());
238
239 PrefixAnnouncement pa0(pa);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600240 BOOST_REQUIRE(pa0.getData());
241 BOOST_CHECK_EQUAL(*pa0.getData(), makePrefixAnnData());
242 pa0.setAnnouncedName("/com/example");
243 BOOST_CHECK(!pa0.getData());
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000244 BOOST_CHECK_NE(pa0, pa);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600245
246 PrefixAnnouncement pa1(makePrefixAnnData());
247 pa1.setExpiration(5_min);
248 BOOST_CHECK(!pa1.getData());
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000249 BOOST_CHECK_NE(pa1, pa);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600250
251 PrefixAnnouncement pa2(makePrefixAnnData());
252 pa2.setValidityPeriod(security::ValidityPeriod(time::fromIsoString("20180118T000000"),
253 time::fromIsoString("20180212T235959")));
254 BOOST_CHECK(!pa2.getData());
Junxiao Shi1748b1e2018-09-06 04:13:42 +0000255 BOOST_CHECK_NE(pa2, pa);
Junxiao Shi8127d1a2018-08-24 15:29:21 -0600256}
257
258BOOST_AUTO_TEST_SUITE_END() // TestPrefixAnnouncement
259
260} // namespace tests
261} // namespace ndn