Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
| 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); |
| 95 | BOOST_CHECK_THROW(PrefixAnnouncement pa0(data0), tlv::Error); |
| 96 | |
| 97 | // Name has no "32=PA" keyword |
| 98 | Data data1 = makePrefixAnnData(); |
| 99 | setNameComponent(data1, -3, name::Component::fromEscapedString("32=not-PA")); |
| 100 | BOOST_CHECK_THROW(PrefixAnnouncement pa1(data1), tlv::Error); |
| 101 | |
| 102 | // Name has no version component |
| 103 | Data data2 = makePrefixAnnData(); |
| 104 | setNameComponent(data2, -2, "not-version"); |
| 105 | BOOST_CHECK_THROW(PrefixAnnouncement pa2(data2), tlv::Error); |
| 106 | |
| 107 | // Name has no segment number component |
| 108 | Data data3 = makePrefixAnnData(); |
| 109 | setNameComponent(data3, -2, "not-segment"); |
| 110 | BOOST_CHECK_THROW(PrefixAnnouncement pa3(data3), tlv::Error); |
| 111 | |
| 112 | // Content has no ExpirationPeriod element |
| 113 | Data data4 = makePrefixAnnData(); |
| 114 | Block payload4 = data4.getContent(); |
| 115 | payload4.parse(); |
| 116 | payload4.remove(tlv::nfd::ExpirationPeriod); |
| 117 | payload4.encode(); |
| 118 | data4.setContent(payload4); |
| 119 | BOOST_CHECK_THROW(PrefixAnnouncement pa4(data4), tlv::Error); |
| 120 | |
| 121 | // ExpirationPeriod is malformed |
| 122 | Data data5 = makePrefixAnnData(); |
| 123 | Block payload5 = data5.getContent(); |
| 124 | payload5.parse(); |
| 125 | payload5.remove(tlv::nfd::ExpirationPeriod); |
| 126 | payload5.push_back("6D03010101"_block); |
| 127 | payload5.encode(); |
| 128 | data5.setContent(payload5); |
| 129 | BOOST_CHECK_THROW(PrefixAnnouncement pa5(data5), tlv::Error); |
| 130 | |
| 131 | // ValidityPeriod is malformed |
| 132 | Data data6 = makePrefixAnnData(); |
| 133 | Block payload6 = data6.getContent(); |
| 134 | payload6.parse(); |
| 135 | payload6.remove(tlv::ValidityPeriod); |
| 136 | payload6.push_back("FD00FD00"_block); |
| 137 | payload6.encode(); |
| 138 | data6.setContent(payload6); |
| 139 | BOOST_CHECK_THROW(PrefixAnnouncement pa6(data6), tlv::Error); |
| 140 | |
| 141 | // Content has unrecognized critical element |
| 142 | Data data7 = makePrefixAnnData(); |
| 143 | Block payload7 = data7.getContent(); |
| 144 | payload7.parse(); |
| 145 | payload7.push_back("0200"_block); |
| 146 | payload7.encode(); |
| 147 | data7.setContent(payload7); |
| 148 | BOOST_CHECK_THROW(PrefixAnnouncement pa7(data7), tlv::Error); |
| 149 | } |
| 150 | |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 151 | BOOST_FIXTURE_TEST_CASE(EncodeEmpty, IdentityManagementFixture) |
| 152 | { |
| 153 | PrefixAnnouncement pa; |
| 154 | BOOST_CHECK(!pa.getData()); |
| 155 | BOOST_CHECK_EQUAL(pa.getAnnouncedName(), "/"); |
| 156 | BOOST_CHECK_EQUAL(pa.getExpiration(), 0_ms); |
| 157 | BOOST_CHECK(!pa.getValidityPeriod()); |
| 158 | |
| 159 | const Data& data = pa.toData(m_keyChain, signingWithSha256(), 5); |
| 160 | BOOST_CHECK_EQUAL(data.getName(), "/32=PA/%FD%05/%00%00"); |
| 161 | BOOST_CHECK_EQUAL(data.getContentType(), tlv::ContentType_PrefixAnn); |
| 162 | BOOST_REQUIRE(pa.getData()); |
| 163 | BOOST_CHECK_EQUAL(*pa.getData(), data); |
| 164 | |
| 165 | PrefixAnnouncement decoded(data); |
| 166 | BOOST_CHECK_EQUAL(decoded.getAnnouncedName(), "/"); |
| 167 | BOOST_CHECK_EQUAL(decoded.getExpiration(), 0_s); |
| 168 | BOOST_CHECK(!decoded.getValidityPeriod()); |
| 169 | |
| 170 | BOOST_CHECK_EQUAL(pa, decoded); |
| 171 | } |
| 172 | |
| 173 | BOOST_FIXTURE_TEST_CASE(EncodeNoValidity, IdentityManagementFixture) |
| 174 | { |
| 175 | PrefixAnnouncement pa; |
| 176 | pa.setAnnouncedName("/net/example"); |
| 177 | BOOST_CHECK_THROW(pa.setExpiration(-1_ms), std::invalid_argument); |
| 178 | pa.setExpiration(1_h); |
| 179 | |
| 180 | const Data& data = pa.toData(m_keyChain, signingWithSha256(), 1); |
| 181 | BOOST_CHECK_EQUAL(data.getName(), "/net/example/32=PA/%FD%01/%00%00"); |
| 182 | BOOST_CHECK_EQUAL(data.getContentType(), tlv::ContentType_PrefixAnn); |
| 183 | |
| 184 | const Block& payload = data.getContent(); |
| 185 | payload.parse(); |
| 186 | BOOST_CHECK_EQUAL(readNonNegativeInteger(payload.get(tlv::nfd::ExpirationPeriod)), 3600000); |
| 187 | BOOST_CHECK(payload.find(tlv::ValidityPeriod) == payload.elements_end()); |
| 188 | |
| 189 | PrefixAnnouncement decoded(data); |
| 190 | BOOST_CHECK_EQUAL(decoded.getAnnouncedName(), "/net/example"); |
| 191 | BOOST_CHECK_EQUAL(decoded.getExpiration(), 1_h); |
| 192 | BOOST_CHECK(!decoded.getValidityPeriod()); |
| 193 | |
| 194 | BOOST_CHECK_EQUAL(pa, decoded); |
| 195 | } |
| 196 | |
| 197 | BOOST_FIXTURE_TEST_CASE(EncodeWithValidity, IdentityManagementFixture) |
| 198 | { |
| 199 | PrefixAnnouncement pa; |
| 200 | pa.setAnnouncedName("/net/example"); |
| 201 | pa.setExpiration(1_h); |
| 202 | security::ValidityPeriod validity(time::fromIsoString("20181030T000000"), |
| 203 | time::fromIsoString("20181124T235959")); |
| 204 | pa.setValidityPeriod(validity); |
| 205 | |
| 206 | const Data& data = pa.toData(m_keyChain); |
| 207 | const Block& payload = data.getContent(); |
| 208 | payload.parse(); |
| 209 | BOOST_CHECK_EQUAL(readNonNegativeInteger(payload.get(tlv::nfd::ExpirationPeriod)), 3600000); |
| 210 | BOOST_CHECK_EQUAL(payload.get(tlv::ValidityPeriod), validity.wireEncode()); |
| 211 | |
| 212 | PrefixAnnouncement decoded(data); |
| 213 | BOOST_CHECK_EQUAL(decoded.getAnnouncedName(), "/net/example"); |
| 214 | BOOST_CHECK_EQUAL(decoded.getExpiration(), 1_h); |
| 215 | BOOST_REQUIRE(decoded.getValidityPeriod()); |
| 216 | BOOST_CHECK_EQUAL(*decoded.getValidityPeriod(), validity); |
| 217 | |
| 218 | BOOST_CHECK_EQUAL(pa, decoded); |
| 219 | } |
| 220 | |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 221 | BOOST_AUTO_TEST_CASE(Modify) |
| 222 | { |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 223 | PrefixAnnouncement pa(makePrefixAnnData()); |
| 224 | |
| 225 | PrefixAnnouncement pa0(pa); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 226 | BOOST_REQUIRE(pa0.getData()); |
| 227 | BOOST_CHECK_EQUAL(*pa0.getData(), makePrefixAnnData()); |
| 228 | pa0.setAnnouncedName("/com/example"); |
| 229 | BOOST_CHECK(!pa0.getData()); |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 230 | BOOST_CHECK_NE(pa0, pa); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 231 | |
| 232 | PrefixAnnouncement pa1(makePrefixAnnData()); |
| 233 | pa1.setExpiration(5_min); |
| 234 | BOOST_CHECK(!pa1.getData()); |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 235 | BOOST_CHECK_NE(pa1, pa); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 236 | |
| 237 | PrefixAnnouncement pa2(makePrefixAnnData()); |
| 238 | pa2.setValidityPeriod(security::ValidityPeriod(time::fromIsoString("20180118T000000"), |
| 239 | time::fromIsoString("20180212T235959"))); |
| 240 | BOOST_CHECK(!pa2.getData()); |
Junxiao Shi | 1748b1e | 2018-09-06 04:13:42 +0000 | [diff] [blame] | 241 | BOOST_CHECK_NE(pa2, pa); |
Junxiao Shi | 8127d1a | 2018-08-24 15:29:21 -0600 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | BOOST_AUTO_TEST_SUITE_END() // TestPrefixAnnouncement |
| 245 | |
| 246 | } // namespace tests |
| 247 | } // namespace ndn |