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 | |
| 30 | BOOST_AUTO_TEST_SUITE(TestPrefixAnnouncement) |
| 31 | |
| 32 | static Data |
| 33 | makePrefixAnnData() |
| 34 | { |
| 35 | return Data( |
Eric Newberry | c25e463 | 2021-02-11 10:48:11 -0800 | [diff] [blame] | 36 | "0678 0718 announced-name=/net/example 08036E6574 08076578616D706C65" |
Davide Pesavento | 47a94d1 | 2021-10-31 16:41:05 -0400 | [diff] [blame] | 37 | " keyword-prefix-ann=20025041 version=360101 segment=320100" |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 38 | " 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 Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 46 | BOOST_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 | |
| 88 | BOOST_AUTO_TEST_CASE(DecodeBad) |
| 89 | { |
| 90 | // wrong ContentType |
| 91 | Data data0 = makePrefixAnnData(); |
| 92 | data0.setContentType(tlv::ContentType_Blob); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 93 | 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 Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 96 | |
| 97 | // Name has no "32=PA" keyword |
| 98 | Data data1 = makePrefixAnnData(); |
| 99 | setNameComponent(data1, -3, name::Component::fromEscapedString("32=not-PA")); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 100 | 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 Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 103 | |
| 104 | // Name has no version component |
| 105 | Data data2 = makePrefixAnnData(); |
| 106 | setNameComponent(data2, -2, "not-version"); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 107 | 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 Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 110 | |
| 111 | // Name has no segment number component |
| 112 | Data data3 = makePrefixAnnData(); |
| 113 | setNameComponent(data3, -2, "not-segment"); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 114 | 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 Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 124 | |
| 125 | // Content has no ExpirationPeriod element |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 126 | Data data5 = makePrefixAnnData(); |
| 127 | Block payload5 = data5.getContent(); |
| 128 | payload5.parse(); |
| 129 | payload5.remove(tlv::nfd::ExpirationPeriod); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 130 | data5.setContent(payload5); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 131 | 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 Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 134 | |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 135 | // ExpirationPeriod is malformed |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 136 | Data data6 = makePrefixAnnData(); |
| 137 | Block payload6 = data6.getContent(); |
| 138 | payload6.parse(); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 139 | payload6.remove(tlv::nfd::ExpirationPeriod); |
| 140 | payload6.push_back("6D03010101"_block); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 141 | data6.setContent(payload6); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 142 | BOOST_CHECK_THROW(PrefixAnnouncement{data6}, tlv::Error); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 143 | |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 144 | // ValidityPeriod is malformed |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 145 | Data data7 = makePrefixAnnData(); |
| 146 | Block payload7 = data7.getContent(); |
| 147 | payload7.parse(); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 148 | payload7.remove(tlv::ValidityPeriod); |
| 149 | payload7.push_back("FD00FD00"_block); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 150 | data7.setContent(payload7); |
Davide Pesavento | 81bd696 | 2020-06-17 16:03:23 -0400 | [diff] [blame] | 151 | 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 Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 162 | } |
| 163 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 164 | BOOST_FIXTURE_TEST_CASE(EncodeEmpty, KeyChainFixture) |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 165 | { |
| 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 Newberry | c25e463 | 2021-02-11 10:48:11 -0800 | [diff] [blame] | 173 | BOOST_CHECK_EQUAL(data.getName(), "/32=PA/v=5/seg=0"); |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 174 | 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 Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 186 | BOOST_FIXTURE_TEST_CASE(EncodeNoValidity, KeyChainFixture) |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 187 | { |
| 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 Newberry | c25e463 | 2021-02-11 10:48:11 -0800 | [diff] [blame] | 194 | 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] | 195 | 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 Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 210 | BOOST_FIXTURE_TEST_CASE(EncodeWithValidity, KeyChainFixture) |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 211 | { |
| 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 Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 234 | BOOST_AUTO_TEST_CASE(Modify) |
| 235 | { |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 236 | PrefixAnnouncement pa(makePrefixAnnData()); |
| 237 | |
| 238 | PrefixAnnouncement pa0(pa); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 239 | BOOST_REQUIRE(pa0.getData()); |
| 240 | BOOST_CHECK_EQUAL(*pa0.getData(), makePrefixAnnData()); |
| 241 | pa0.setAnnouncedName("/com/example"); |
| 242 | BOOST_CHECK(!pa0.getData()); |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 243 | BOOST_CHECK_NE(pa0, pa); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 244 | |
| 245 | PrefixAnnouncement pa1(makePrefixAnnData()); |
| 246 | pa1.setExpiration(5_min); |
| 247 | BOOST_CHECK(!pa1.getData()); |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 248 | BOOST_CHECK_NE(pa1, pa); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 249 | |
| 250 | PrefixAnnouncement pa2(makePrefixAnnData()); |
| 251 | pa2.setValidityPeriod(security::ValidityPeriod(time::fromIsoString("20180118T000000"), |
| 252 | time::fromIsoString("20180212T235959"))); |
| 253 | BOOST_CHECK(!pa2.getData()); |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 254 | BOOST_CHECK_NE(pa2, pa); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 255 | } |
| 256 | |
Davide Pesavento | 21ce3ad | 2021-11-01 22:00:45 -0400 | [diff] [blame] | 257 | BOOST_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 Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 265 | BOOST_AUTO_TEST_SUITE_END() // TestPrefixAnnouncement |
| 266 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 267 | } // namespace ndn::tests |