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