Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 4 | * |
| 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 Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/prefix-announcement.hpp" |
| 23 | #include "ndn-cxx/encoding/tlv-nfd.hpp" |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 24 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 25 | #include "tests/key-chain-fixture.hpp" |
| 26 | #include "tests/test-common.hpp" |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 27 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 28 | namespace ndn::tests { |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 29 | |
Davide Pesavento | fffdd62 | 2023-08-28 22:50:43 -0400 | [diff] [blame] | 30 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<PrefixAnnouncement>)); |
| 31 | static_assert(std::is_convertible_v<PrefixAnnouncement::Error*, tlv::Error*>, |
| 32 | "PrefixAnnouncement::Error must inherit from tlv::Error"); |
| 33 | |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(TestPrefixAnnouncement) |
| 35 | |
| 36 | static Data |
| 37 | makePrefixAnnData() |
| 38 | { |
| 39 | return Data( |
Eric Newberry | c25e463 | 2021-02-11 10:48:11 -0800 | [diff] [blame] | 40 | "0678 0718 announced-name=/net/example 08036E6574 08076578616D706C65" |
Davide Pesavento | 47a94d1 | 2021-10-31 16:41:05 -0400 | [diff] [blame] | 41 | " keyword-prefix-ann=20025041 version=360101 segment=320100" |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 42 | " 1403 content-type=prefix-ann 180105" |
| 43 | " 1530 expire in one hour 6D040036EE80" |
| 44 | " validity FD00FD26 FD00FE0F323031383130333054303030303030" |
| 45 | " FD00FF0F323031383131323454323335393539" |
| 46 | " 1603 1B0100 signature" |
| 47 | " 1720 0000000000000000000000000000000000000000000000000000000000000000"_block); |
| 48 | } |
| 49 | |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 50 | BOOST_AUTO_TEST_CASE(DecodeGood) |
| 51 | { |
| 52 | Data data0 = makePrefixAnnData(); |
| 53 | PrefixAnnouncement pa0(data0); |
| 54 | BOOST_CHECK_EQUAL(pa0.getAnnouncedName(), "/net/example"); |
| 55 | BOOST_CHECK_EQUAL(pa0.getExpiration(), 1_h); |
| 56 | BOOST_CHECK(pa0.getValidityPeriod()); |
| 57 | BOOST_CHECK_EQUAL(*pa0.getValidityPeriod(), |
| 58 | security::ValidityPeriod(time::fromIsoString("20181030T000000"), |
| 59 | time::fromIsoString("20181124T235959"))); |
| 60 | |
| 61 | // reorder ExpirationPeriod and ValidityPeriod, unrecognized non-critical element |
| 62 | Data data1 = makePrefixAnnData(); |
| 63 | Block payload1 = data1.getContent(); |
| 64 | payload1.parse(); |
| 65 | Block expirationElement = payload1.get(tlv::nfd::ExpirationPeriod); |
| 66 | payload1.remove(tlv::nfd::ExpirationPeriod); |
| 67 | payload1.push_back(expirationElement); |
| 68 | payload1.push_back("2000"_block); |
| 69 | payload1.encode(); |
| 70 | data1.setContent(payload1); |
| 71 | PrefixAnnouncement pa1(data1); |
| 72 | BOOST_CHECK_EQUAL(pa1.getAnnouncedName(), "/net/example"); |
| 73 | BOOST_CHECK_EQUAL(pa1.getExpiration(), 1_h); |
| 74 | BOOST_CHECK(pa1.getValidityPeriod()); |
| 75 | BOOST_CHECK_EQUAL(*pa1.getValidityPeriod(), |
| 76 | security::ValidityPeriod(time::fromIsoString("20181030T000000"), |
| 77 | time::fromIsoString("20181124T235959"))); |
| 78 | |
| 79 | // no ValidityPeriod |
| 80 | Data data2 = makePrefixAnnData(); |
| 81 | Block payload2 = data2.getContent(); |
| 82 | payload2.parse(); |
| 83 | payload2.remove(tlv::ValidityPeriod); |
| 84 | payload2.encode(); |
| 85 | data2.setContent(payload2); |
| 86 | PrefixAnnouncement pa2(data2); |
| 87 | BOOST_CHECK_EQUAL(pa2.getAnnouncedName(), "/net/example"); |
| 88 | BOOST_CHECK_EQUAL(pa2.getExpiration(), 1_h); |
| 89 | BOOST_CHECK(!pa2.getValidityPeriod()); |
| 90 | } |
| 91 | |
| 92 | BOOST_AUTO_TEST_CASE(DecodeBad) |
| 93 | { |
| 94 | // wrong ContentType |
| 95 | Data data0 = makePrefixAnnData(); |
| 96 | data0.setContentType(tlv::ContentType_Blob); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 97 | BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data0}, tlv::Error, [] (const auto& e) { |
| 98 | return e.what() == "Data is not a prefix announcement: ContentType is 0"s; |
| 99 | }); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 100 | |
| 101 | // Name has no "32=PA" keyword |
| 102 | Data data1 = makePrefixAnnData(); |
| 103 | setNameComponent(data1, -3, name::Component::fromEscapedString("32=not-PA")); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 104 | BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data1}, tlv::Error, [] (const auto& e) { |
| 105 | return e.what() == "Data is not a prefix announcement: wrong name structure"s; |
| 106 | }); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 107 | |
| 108 | // Name has no version component |
| 109 | Data data2 = makePrefixAnnData(); |
| 110 | setNameComponent(data2, -2, "not-version"); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 111 | BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data2}, tlv::Error, [] (const auto& e) { |
| 112 | return e.what() == "Data is not a prefix announcement: wrong name structure"s; |
| 113 | }); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 114 | |
| 115 | // Name has no segment number component |
| 116 | Data data3 = makePrefixAnnData(); |
| 117 | setNameComponent(data3, -2, "not-segment"); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 118 | BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data3}, tlv::Error, [] (const auto& e) { |
| 119 | return e.what() == "Data is not a prefix announcement: wrong name structure"s; |
| 120 | }); |
| 121 | |
| 122 | // Data without Content |
| 123 | Data data4 = makePrefixAnnData(); |
| 124 | data4.unsetContent(); |
| 125 | BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data4}, tlv::Error, [] (const auto& e) { |
| 126 | return e.what() == "Prefix announcement is empty"s; |
| 127 | }); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 128 | |
| 129 | // Content has no ExpirationPeriod element |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 130 | Data data5 = makePrefixAnnData(); |
| 131 | Block payload5 = data5.getContent(); |
| 132 | payload5.parse(); |
| 133 | payload5.remove(tlv::nfd::ExpirationPeriod); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 134 | data5.setContent(payload5); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 135 | BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data5}, tlv::Error, [] (const auto& e) { |
| 136 | return e.what() == "No sub-element of type 109 found in block of type 21"s; |
| 137 | }); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 138 | |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 139 | // ExpirationPeriod is malformed |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 140 | Data data6 = makePrefixAnnData(); |
| 141 | Block payload6 = data6.getContent(); |
| 142 | payload6.parse(); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 143 | payload6.remove(tlv::nfd::ExpirationPeriod); |
| 144 | payload6.push_back("6D03010101"_block); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 145 | data6.setContent(payload6); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 146 | BOOST_CHECK_THROW(PrefixAnnouncement{data6}, tlv::Error); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 147 | |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 148 | // ValidityPeriod is malformed |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 149 | Data data7 = makePrefixAnnData(); |
| 150 | Block payload7 = data7.getContent(); |
| 151 | payload7.parse(); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 152 | payload7.remove(tlv::ValidityPeriod); |
| 153 | payload7.push_back("FD00FD00"_block); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 154 | data7.setContent(payload7); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 155 | BOOST_CHECK_THROW(PrefixAnnouncement{data7}, tlv::Error); |
| 156 | |
| 157 | // Content has unrecognized critical element |
| 158 | Data data8 = makePrefixAnnData(); |
| 159 | Block payload8 = data8.getContent(); |
| 160 | payload8.parse(); |
| 161 | payload8.push_back("0200"_block); |
| 162 | data8.setContent(payload8); |
| 163 | BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data8}, tlv::Error, [] (const auto& e) { |
| 164 | return e.what() == "Unrecognized element of critical type 2"s; |
| 165 | }); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 166 | } |
| 167 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 168 | BOOST_FIXTURE_TEST_CASE(EncodeEmpty, KeyChainFixture) |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 169 | { |
| 170 | PrefixAnnouncement pa; |
| 171 | BOOST_CHECK(!pa.getData()); |
| 172 | BOOST_CHECK_EQUAL(pa.getAnnouncedName(), "/"); |
| 173 | BOOST_CHECK_EQUAL(pa.getExpiration(), 0_ms); |
| 174 | BOOST_CHECK(!pa.getValidityPeriod()); |
| 175 | |
| 176 | const Data& data = pa.toData(m_keyChain, signingWithSha256(), 5); |
Eric Newberry | c25e463 | 2021-02-11 10:48:11 -0800 | [diff] [blame] | 177 | BOOST_CHECK_EQUAL(data.getName(), "/32=PA/v=5/seg=0"); |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 178 | BOOST_CHECK_EQUAL(data.getContentType(), tlv::ContentType_PrefixAnn); |
| 179 | BOOST_REQUIRE(pa.getData()); |
| 180 | BOOST_CHECK_EQUAL(*pa.getData(), data); |
| 181 | |
| 182 | PrefixAnnouncement decoded(data); |
| 183 | BOOST_CHECK_EQUAL(decoded.getAnnouncedName(), "/"); |
| 184 | BOOST_CHECK_EQUAL(decoded.getExpiration(), 0_s); |
| 185 | BOOST_CHECK(!decoded.getValidityPeriod()); |
| 186 | |
| 187 | BOOST_CHECK_EQUAL(pa, decoded); |
| 188 | } |
| 189 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 190 | BOOST_FIXTURE_TEST_CASE(EncodeNoValidity, KeyChainFixture) |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 191 | { |
| 192 | PrefixAnnouncement pa; |
| 193 | pa.setAnnouncedName("/net/example"); |
| 194 | BOOST_CHECK_THROW(pa.setExpiration(-1_ms), std::invalid_argument); |
| 195 | pa.setExpiration(1_h); |
| 196 | |
| 197 | const Data& data = pa.toData(m_keyChain, signingWithSha256(), 1); |
Eric Newberry | c25e463 | 2021-02-11 10:48:11 -0800 | [diff] [blame] | 198 | BOOST_CHECK_EQUAL(data.getName(), "/net/example/32=PA/v=1/seg=0"); |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 199 | BOOST_CHECK_EQUAL(data.getContentType(), tlv::ContentType_PrefixAnn); |
| 200 | |
| 201 | const Block& payload = data.getContent(); |
| 202 | payload.parse(); |
| 203 | BOOST_CHECK_EQUAL(readNonNegativeInteger(payload.get(tlv::nfd::ExpirationPeriod)), 3600000); |
| 204 | BOOST_CHECK(payload.find(tlv::ValidityPeriod) == payload.elements_end()); |
| 205 | |
| 206 | PrefixAnnouncement decoded(data); |
| 207 | BOOST_CHECK_EQUAL(decoded.getAnnouncedName(), "/net/example"); |
| 208 | BOOST_CHECK_EQUAL(decoded.getExpiration(), 1_h); |
| 209 | BOOST_CHECK(!decoded.getValidityPeriod()); |
| 210 | |
| 211 | BOOST_CHECK_EQUAL(pa, decoded); |
| 212 | } |
| 213 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 214 | BOOST_FIXTURE_TEST_CASE(EncodeWithValidity, KeyChainFixture) |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 215 | { |
| 216 | PrefixAnnouncement pa; |
| 217 | pa.setAnnouncedName("/net/example"); |
| 218 | pa.setExpiration(1_h); |
| 219 | security::ValidityPeriod validity(time::fromIsoString("20181030T000000"), |
| 220 | time::fromIsoString("20181124T235959")); |
| 221 | pa.setValidityPeriod(validity); |
| 222 | |
| 223 | const Data& data = pa.toData(m_keyChain); |
| 224 | const Block& payload = data.getContent(); |
| 225 | payload.parse(); |
| 226 | BOOST_CHECK_EQUAL(readNonNegativeInteger(payload.get(tlv::nfd::ExpirationPeriod)), 3600000); |
| 227 | BOOST_CHECK_EQUAL(payload.get(tlv::ValidityPeriod), validity.wireEncode()); |
| 228 | |
| 229 | PrefixAnnouncement decoded(data); |
| 230 | BOOST_CHECK_EQUAL(decoded.getAnnouncedName(), "/net/example"); |
| 231 | BOOST_CHECK_EQUAL(decoded.getExpiration(), 1_h); |
| 232 | BOOST_REQUIRE(decoded.getValidityPeriod()); |
| 233 | BOOST_CHECK_EQUAL(*decoded.getValidityPeriod(), validity); |
| 234 | |
| 235 | BOOST_CHECK_EQUAL(pa, decoded); |
| 236 | } |
| 237 | |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 238 | BOOST_AUTO_TEST_CASE(Modify) |
| 239 | { |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 240 | PrefixAnnouncement pa(makePrefixAnnData()); |
| 241 | |
| 242 | PrefixAnnouncement pa0(pa); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 243 | BOOST_REQUIRE(pa0.getData()); |
| 244 | BOOST_CHECK_EQUAL(*pa0.getData(), makePrefixAnnData()); |
| 245 | pa0.setAnnouncedName("/com/example"); |
| 246 | BOOST_CHECK(!pa0.getData()); |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 247 | BOOST_CHECK_NE(pa0, pa); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 248 | |
| 249 | PrefixAnnouncement pa1(makePrefixAnnData()); |
| 250 | pa1.setExpiration(5_min); |
| 251 | BOOST_CHECK(!pa1.getData()); |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 252 | BOOST_CHECK_NE(pa1, pa); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 253 | |
| 254 | PrefixAnnouncement pa2(makePrefixAnnData()); |
| 255 | pa2.setValidityPeriod(security::ValidityPeriod(time::fromIsoString("20180118T000000"), |
| 256 | time::fromIsoString("20180212T235959"))); |
| 257 | BOOST_CHECK(!pa2.getData()); |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 258 | BOOST_CHECK_NE(pa2, pa); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 259 | } |
| 260 | |
Davide Pesavento | 21ce3ad | 2021-11-01 22:00:45 -0400 | [diff] [blame] | 261 | BOOST_AUTO_TEST_CASE(KeywordComponent) |
| 262 | { |
| 263 | BOOST_CHECK_EQUAL(PrefixAnnouncement::getKeywordComponent().wireEncode(), |
| 264 | "20 02 5041"_block); |
| 265 | BOOST_CHECK_EQUAL(PrefixAnnouncement::getKeywordComponent().toUri(name::UriFormat::CANONICAL), |
| 266 | "32=PA"); |
| 267 | } |
| 268 | |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 269 | BOOST_AUTO_TEST_SUITE_END() // TestPrefixAnnouncement |
| 270 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 271 | } // namespace ndn::tests |