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