Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/interest.hpp" |
| 23 | #include "ndn-cxx/data.hpp" |
| 24 | #include "ndn-cxx/security/digest-sha256.hpp" |
| 25 | #include "ndn-cxx/security/signature-sha256-with-rsa.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 27 | #include "tests/boost-test.hpp" |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 28 | #include "tests/make-interest-data.hpp" |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 30 | namespace ndn { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 31 | namespace tests { |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 32 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(TestInterest) |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 34 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 35 | class DisableAutoCheckParametersDigest |
| 36 | { |
| 37 | public: |
| 38 | DisableAutoCheckParametersDigest() |
| 39 | : m_saved(Interest::getAutoCheckParametersDigest()) |
| 40 | { |
| 41 | Interest::setAutoCheckParametersDigest(false); |
| 42 | } |
| 43 | |
| 44 | ~DisableAutoCheckParametersDigest() |
| 45 | { |
| 46 | Interest::setAutoCheckParametersDigest(m_saved); |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | bool m_saved; |
| 51 | }; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 52 | |
| 53 | BOOST_AUTO_TEST_CASE(DefaultConstructor) |
| 54 | { |
| 55 | Interest i; |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(i.hasWire(), false); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 57 | BOOST_CHECK_EQUAL(i.getName(), "/"); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(i.hasSelectors(), false); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 59 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 60 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 61 | BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true); |
| 62 | BOOST_CHECK_EQUAL(i.hasNonce(), false); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 63 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 64 | BOOST_CHECK(i.getHopLimit() == nullopt); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false); |
| 66 | BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false); |
| 67 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | BOOST_AUTO_TEST_CASE(DecodeNotInterest) |
| 71 | { |
| 72 | BOOST_CHECK_THROW(Interest("4202CAFE"_block), tlv::Error); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 75 | BOOST_AUTO_TEST_SUITE(EncodeDecode02) |
| 76 | |
| 77 | BOOST_AUTO_TEST_CASE(Basic) |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 78 | { |
| 79 | const uint8_t WIRE[] = { |
| 80 | 0x05, 0x1c, // Interest |
| 81 | 0x07, 0x14, // Name |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 82 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 83 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 84 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 85 | 0x0a, 0x04, // Nonce |
| 86 | 0x01, 0x00, 0x00, 0x00 |
| 87 | }; |
| 88 | |
| 89 | Interest i1("/local/ndn/prefix"); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 90 | i1.setCanBePrefix(true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 91 | i1.setNonce(1); |
| 92 | Block wire1 = i1.wireEncode(); |
| 93 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 94 | |
| 95 | Interest i2(wire1); |
| 96 | BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix"); |
| 97 | BOOST_CHECK(i2.getSelectors().empty()); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 100 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 101 | BOOST_CHECK(i2.getHopLimit() == nullopt); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false); |
| 103 | BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 106 | BOOST_AUTO_TEST_CASE(Full) |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 107 | { |
| 108 | const uint8_t WIRE[] = { |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 109 | 0x05, 0x31, // Interest |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 110 | 0x07, 0x14, // Name |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 111 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 112 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 113 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 114 | 0x09, 0x03, // Selectors |
| 115 | 0x0d, 0x01, 0x01, // MinSuffixComponents |
| 116 | 0x0a, 0x04, // Nonce |
| 117 | 0x01, 0x00, 0x00, 0x00, |
| 118 | 0x0c, 0x02, // InterestLifetime |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 119 | 0x03, 0xe8, |
| 120 | 0x1e, 0x0a, // ForwardingHint |
| 121 | 0x1f, 0x08, // Delegation |
| 122 | 0x1e, 0x01, 0x01, // Preference=1 |
| 123 | 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 124 | }; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 125 | |
| 126 | Interest i1; |
| 127 | i1.setName("/local/ndn/prefix"); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 128 | i1.setCanBePrefix(true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 129 | i1.setMinSuffixComponents(1); |
| 130 | i1.setNonce(1); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 131 | i1.setInterestLifetime(1000_ms); |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 132 | i1.setForwardingHint({{1, "/A"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 133 | Block wire1 = i1.wireEncode(); |
| 134 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 135 | |
| 136 | Interest i2(wire1); |
| 137 | BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix"); |
| 138 | BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 1); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 139 | BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{1, "/A"}})); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 140 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 141 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 1000_ms); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 142 | BOOST_CHECK(i2.getHopLimit() == nullopt); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false); |
| 144 | BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 147 | BOOST_AUTO_TEST_CASE(ParametersSha256DigestComponent) |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 148 | { |
| 149 | const uint8_t WIRE[] = { |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 150 | 0x05, 0x60, // Interest |
| 151 | 0x07, 0x58, // Name |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 152 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 153 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 154 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 155 | 0x02, 0x20, // ParametersSha256DigestComponent |
| 156 | 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80, |
| 157 | 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc, |
| 158 | 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a, |
| 159 | 0x02, 0x20, // ParametersSha256DigestComponent |
| 160 | 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80, |
| 161 | 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc, |
| 162 | 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a, |
| 163 | 0x0a, 0x04, // Nonce |
| 164 | 0x01, 0x00, 0x00, 0x00, |
| 165 | }; |
| 166 | |
| 167 | Interest i1("/I"); |
| 168 | BOOST_CHECK_THROW(i1.wireDecode(Block(WIRE, sizeof(WIRE))), tlv::Error); |
| 169 | |
| 170 | // i1 is still in a valid state |
| 171 | BOOST_CHECK_EQUAL(i1.getName(), "/I"); |
| 172 | BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true); |
| 173 | |
| 174 | Interest i2("/I/params-sha256=f16db273f40436a852063f864d5072b01ead53151f5a688ea1560492bebedd05"); |
| 175 | i2.setCanBePrefix(true); |
| 176 | i2.setNonce(2); |
| 177 | BOOST_CHECK_EQUAL(i2.isParametersDigestValid(), false); |
| 178 | // encoding in v0.2 format does not validate the ParametersSha256DigestComponent |
| 179 | Block wire2 = i2.wireEncode(); |
| 180 | BOOST_CHECK_EQUAL(wire2, "052D 0725(080149 " |
| 181 | "0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) " |
| 182 | "0A0402000000"_block); |
| 183 | |
| 184 | // decoding from v0.2 format does not validate the ParametersSha256DigestComponent |
| 185 | Interest i3(wire2); |
| 186 | BOOST_CHECK_EQUAL(i3.getName(), i2.getName()); |
| 187 | BOOST_CHECK_EQUAL(i3.isParametersDigestValid(), false); |
| 188 | } |
| 189 | |
| 190 | BOOST_AUTO_TEST_SUITE_END() // EncodeDecode02 |
| 191 | |
| 192 | BOOST_AUTO_TEST_SUITE(Encode03) |
| 193 | |
| 194 | // Enable after #4567 |
| 195 | //BOOST_AUTO_TEST_CASE(Basic) |
| 196 | //{ |
| 197 | // const uint8_t WIRE[] = { |
| 198 | // 0x05, 0x1c, // Interest |
| 199 | // 0x07, 0x14, // Name |
| 200 | // 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 201 | // 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 202 | // 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
| 203 | // 0x0a, 0x04, // Nonce |
| 204 | // 0x01, 0x00, 0x00, 0x00, |
| 205 | // }; |
| 206 | |
| 207 | // Interest i1; |
| 208 | // i1.setName("/local/ndn/prefix"); |
| 209 | // i1.setCanBePrefix(false); |
| 210 | // i1.setNonce(1); |
| 211 | // BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true); |
| 212 | |
| 213 | // Block wire1 = i1.wireEncode(); |
| 214 | // BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 215 | |
| 216 | // Interest i2(wire1); |
| 217 | // BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix"); |
| 218 | // BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false); |
| 219 | // BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false); |
| 220 | // BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true); |
| 221 | // BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 222 | // BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 223 | // BOOST_CHECK(i2.getHopLimit() == nullopt); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 224 | // BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), false); |
| 225 | // BOOST_CHECK_EQUAL(i2.getApplicationParameters().isValid(), false); |
| 226 | // BOOST_CHECK_EQUAL(i2.getPublisherPublicKeyLocator().empty(), true); |
| 227 | //} |
| 228 | |
| 229 | BOOST_AUTO_TEST_CASE(WithParameters) |
| 230 | { |
| 231 | const uint8_t WIRE[] = { |
| 232 | 0x05, 0x44, // Interest |
| 233 | 0x07, 0x36, // Name |
| 234 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 235 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 236 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
| 237 | 0x02, 0x20, // ParametersSha256DigestComponent |
| 238 | 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80, |
| 239 | 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc, |
| 240 | 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a, |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 241 | 0x0a, 0x04, // Nonce |
| 242 | 0x01, 0x00, 0x00, 0x00, |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 243 | 0x24, 0x04, // ApplicationParameters |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 244 | 0xc0, 0xc1, 0xc2, 0xc3 |
| 245 | }; |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 246 | |
| 247 | Interest i1; |
| 248 | i1.setName("/local/ndn/prefix"); |
| 249 | i1.setCanBePrefix(false); |
| 250 | i1.setNonce(1); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 251 | i1.setApplicationParameters("2404C0C1C2C3"_block); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 252 | BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true); |
| 253 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 254 | Block wire1 = i1.wireEncode(); |
| 255 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 256 | |
| 257 | Interest i2(wire1); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 258 | BOOST_CHECK_EQUAL(i2.getName(), |
| 259 | "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a"); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 260 | BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false); |
| 261 | BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 262 | BOOST_CHECK_EQUAL(i2.getForwardingHint().empty(), true); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 263 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 264 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 265 | BOOST_CHECK(i2.getHopLimit() == nullopt); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 266 | BOOST_CHECK_EQUAL(i2.hasApplicationParameters(), true); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 267 | BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 268 | BOOST_CHECK_EQUAL(i2.getPublisherPublicKeyLocator().empty(), true); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 271 | BOOST_AUTO_TEST_CASE(Full) |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 272 | { |
| 273 | const uint8_t WIRE[] = { |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 274 | 0x05, 0x5c, // Interest |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 275 | 0x07, 0x36, // Name |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 276 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 277 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 278 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 279 | 0x02, 0x20, // ParametersSha256DigestComponent |
| 280 | 0xff, 0x91, 0x00, 0xe0, 0x4e, 0xaa, 0xdc, 0xf3, 0x06, 0x74, 0xd9, 0x80, |
| 281 | 0x26, 0xa0, 0x51, 0xba, 0x25, 0xf5, 0x6b, 0x69, 0xbf, 0xa0, 0x26, 0xdc, |
| 282 | 0xcc, 0xd7, 0x2c, 0x6e, 0xa0, 0xf7, 0x31, 0x5a, |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 283 | 0x21, 0x00, // CanBePrefix |
| 284 | 0x12, 0x00, // MustBeFresh |
| 285 | 0x1e, 0x0b, // ForwardingHint |
| 286 | 0x1f, 0x09, // Delegation List |
| 287 | 0x1e, 0x02, |
| 288 | 0x3e, 0x15, |
| 289 | 0x07, 0x03, |
| 290 | 0x08, 0x01, 0x48, |
| 291 | 0x0a, 0x04, // Nonce |
| 292 | 0x4a, 0xcb, 0x1e, 0x4c, |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 293 | 0x0c, 0x02, // InterestLifetime |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 294 | 0x76, 0xa1, |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 295 | 0x22, 0x01, // HopLimit |
| 296 | 0xdc, |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 297 | 0x24, 0x04, // ApplicationParameters |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 298 | 0xc0, 0xc1, 0xc2, 0xc3 |
| 299 | }; |
| 300 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 301 | Interest i1; |
| 302 | i1.setName("/local/ndn/prefix"); |
| 303 | i1.setMustBeFresh(true); |
| 304 | i1.setCanBePrefix(true); |
| 305 | i1.setForwardingHint(DelegationList({{15893, "/H"}})); |
| 306 | i1.setNonce(0x4c1ecb4a); |
| 307 | i1.setInterestLifetime(30369_ms); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 308 | i1.setHopLimit(220); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 309 | i1.setApplicationParameters("2404C0C1C2C3"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 310 | i1.setMinSuffixComponents(1); // v0.2-only elements will not be encoded |
| 311 | i1.setExclude(Exclude().excludeAfter(name::Component("J"))); // v0.2-only elements will not be encoded |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 312 | BOOST_CHECK_EQUAL(i1.isParametersDigestValid(), true); |
| 313 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 314 | Block wire1 = i1.wireEncode(); |
| 315 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 316 | |
| 317 | Interest i2(wire1); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 318 | BOOST_CHECK_EQUAL(i2.getName(), |
| 319 | "/local/ndn/prefix/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a"); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 320 | BOOST_CHECK_EQUAL(i2.getCanBePrefix(), true); |
| 321 | BOOST_CHECK_EQUAL(i2.getMustBeFresh(), true); |
| 322 | BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{15893, "/H"}})); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 323 | BOOST_CHECK_EQUAL(i2.hasNonce(), true); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 324 | BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a); |
| 325 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 326 | BOOST_CHECK_EQUAL(*i2.getHopLimit(), 220); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 327 | BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 328 | BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), -1); // Default because minSuffixComponents was not encoded |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 329 | BOOST_CHECK_EQUAL(i2.getExclude().empty(), true); // Exclude was not encoded |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 332 | // Enable after #4567 |
| 333 | //BOOST_AUTO_TEST_CASE(MissingApplicationParameters) |
| 334 | //{ |
| 335 | // Interest i; |
| 336 | // i.setName(Name("/A").appendParametersSha256DigestPlaceholder()); |
| 337 | // i.setCanBePrefix(false); |
| 338 | // BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false); |
| 339 | // BOOST_CHECK_THROW(i.wireEncode(), tlv::Error); |
| 340 | //} |
| 341 | |
| 342 | BOOST_AUTO_TEST_CASE(MissingParametersSha256DigestComponent) |
| 343 | { |
| 344 | // there's no way to create an Interest that fails this check via programmatic construction, |
| 345 | // so we have to decode an invalid Interest and force reencoding |
| 346 | |
| 347 | DisableAutoCheckParametersDigest disabler; |
| 348 | Interest i("050F 0703(080149) 0A04F000F000 2402CAFE"_block); |
| 349 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false); |
| 350 | BOOST_CHECK_NO_THROW(i.wireEncode()); // this succeeds because it uses the cached wire encoding |
| 351 | |
| 352 | i.setNonce(42); // trigger reencoding |
| 353 | BOOST_CHECK_THROW(i.wireEncode(), tlv::Error); // now the check fails while attempting to reencode |
| 354 | } |
| 355 | |
| 356 | BOOST_AUTO_TEST_SUITE_END() // Encode03 |
| 357 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 358 | class Decode03Fixture |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 359 | { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 360 | protected: |
| 361 | Decode03Fixture() |
| 362 | { |
| 363 | // initialize all elements to non-empty, to verify wireDecode clears them |
| 364 | i.setName("/A"); |
| 365 | i.setForwardingHint({{10309, "/F"}}); |
| 366 | i.setNonce(0x03d645a8); |
| 367 | i.setInterestLifetime(18554_ms); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 368 | i.setHopLimit(64); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 369 | i.setPublisherPublicKeyLocator(Name("/K")); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 370 | i.setApplicationParameters("2404A0A1A2A3"_block); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 371 | } |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 372 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 373 | protected: |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 374 | Interest i; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 375 | }; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 376 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 377 | BOOST_FIXTURE_TEST_SUITE(Decode03, Decode03Fixture) |
| 378 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 379 | BOOST_AUTO_TEST_CASE(NameOnly) |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 380 | { |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 381 | i.wireDecode("0505 0703(080149)"_block); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 382 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 383 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), false); |
| 384 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 385 | BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true); |
| 386 | BOOST_CHECK_EQUAL(i.hasNonce(), true); // a random nonce is generated |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 387 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 388 | BOOST_CHECK(i.getHopLimit() == nullopt); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 389 | BOOST_CHECK_EQUAL(i.getPublisherPublicKeyLocator().empty(), true); |
| 390 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false); |
| 391 | BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 392 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 393 | BOOST_CHECK(!i.hasWire()); // nonce generation resets wire encoding |
| 394 | |
| 395 | // modify then re-encode as v0.2 format |
| 396 | i.setNonce(0x54657c95); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 397 | BOOST_CHECK_EQUAL(i.wireEncode(), "0510 0703(080149) 09030E0101 0A04957C6554"_block); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 400 | BOOST_AUTO_TEST_CASE(NameCanBePrefix) |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 401 | { |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 402 | i.wireDecode("0507 0703(080149) 2100"_block); |
| 403 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 404 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 405 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 406 | BOOST_CHECK_EQUAL(i.getForwardingHint().empty(), true); |
| 407 | BOOST_CHECK_EQUAL(i.hasNonce(), true); // a random nonce is generated |
| 408 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 409 | BOOST_CHECK(i.getHopLimit() == nullopt); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 410 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false); |
| 411 | BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false); |
| 412 | } |
| 413 | |
| 414 | BOOST_AUTO_TEST_CASE(FullWithoutParameters) |
| 415 | { |
| 416 | i.wireDecode("0531 0703(080149) " |
| 417 | "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) " |
| 418 | "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00"_block); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 419 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 420 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 421 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), true); |
| 422 | BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}})); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 423 | BOOST_CHECK_EQUAL(i.hasNonce(), true); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 424 | BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a); |
| 425 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 426 | BOOST_CHECK_EQUAL(*i.getHopLimit(), 214); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 427 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false); |
| 428 | BOOST_CHECK_EQUAL(i.getApplicationParameters().isValid(), false); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 429 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 430 | // encode without modification: retain original wire encoding |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 431 | BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 49); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 432 | |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 433 | // modify then re-encode as v0.3 format: unrecognized elements are discarded |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 434 | i.setName("/J"); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 435 | BOOST_CHECK_EQUAL(i.wireEncode(), |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 436 | "0523 0703(08014A) " |
| 437 | "2100 1200 1E0B(1F09 1E023E15 0703080148) " |
| 438 | "0A044ACB1E4C 0C0276A1 2201D6"_block); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | BOOST_AUTO_TEST_CASE(FullWithParameters) |
| 442 | { |
| 443 | i.wireDecode("055B 0725(080149 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) " |
| 444 | "FC00 2100 FC00 1200 FC00 1E0B(1F09 1E023E15 0703080148) " |
| 445 | "FC00 0A044ACB1E4C FC00 0C0276A1 FC00 2201D6 FC00 2404C0C1C2C3 FC00"_block); |
| 446 | BOOST_CHECK_EQUAL(i.getName(), |
| 447 | "/I/params-sha256=f16db273f40436a852063f864d5072b01ead53151f5a688ea1560492bebedd05"); |
| 448 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 449 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), true); |
| 450 | BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}})); |
| 451 | BOOST_CHECK_EQUAL(i.hasNonce(), true); |
| 452 | BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a); |
| 453 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 454 | BOOST_CHECK_EQUAL(*i.getHopLimit(), 214); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 455 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true); |
| 456 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block); |
| 457 | |
| 458 | // encode without modification: retain original wire encoding |
| 459 | BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 91); |
| 460 | |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 461 | // modify then re-encode as v0.3 format: unrecognized elements |
| 462 | // after ApplicationParameters are preserved, the rest are discarded |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 463 | i.setName("/J"); |
| 464 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 465 | BOOST_CHECK_EQUAL(i.wireEncode(), |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 466 | "054D 0725(08014A 0220F16DB273F40436A852063F864D5072B01EAD53151F5A688EA1560492BEBEDD05) " |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 467 | "2100 1200 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 468 | "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 FC00"_block); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 469 | |
| 470 | // modify ApplicationParameters: unrecognized elements are preserved |
| 471 | i.setApplicationParameters("2402CAFE"_block); |
| 472 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 473 | BOOST_CHECK_EQUAL(i.wireEncode(), |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 474 | "054B 0725(08014A 02205FDA67967EE302FC457E41B7D3D51BA6A9379574D193FD88F64954BF16C2927A) " |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 475 | "2100 1200 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 476 | "0A044ACB1E4C 0C0276A1 2201D6 2402CAFE FC00"_block); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 479 | BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder) |
| 480 | { |
| 481 | BOOST_CHECK_THROW(i.wireDecode( |
| 482 | "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 483 | "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block), |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 484 | tlv::Error); |
| 485 | BOOST_CHECK_THROW(i.wireDecode( |
| 486 | "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 487 | "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block), |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 488 | tlv::Error); |
| 489 | BOOST_CHECK_THROW(i.wireDecode( |
| 490 | "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 491 | "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block), |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 492 | tlv::Error); |
| 493 | BOOST_CHECK_THROW(i.wireDecode( |
| 494 | "0529 0703080149 2100 1200 0A044ACB1E4C " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 495 | "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2404C0C1C2C3"_block), |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 496 | tlv::Error); |
| 497 | BOOST_CHECK_THROW(i.wireDecode( |
| 498 | "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 499 | "0C0276A1 0A044ACB1E4C 2201D6 2404C0C1C2C3"_block), |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 500 | tlv::Error); |
| 501 | BOOST_CHECK_THROW(i.wireDecode( |
| 502 | "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 503 | "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block), |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 504 | tlv::Error); |
| 505 | } |
| 506 | |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 507 | BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder) |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 508 | { |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 509 | // duplicate HopLimit |
| 510 | i.wireDecode("0536 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)" |
| 511 | "2201D6 2200 2404C0C1C2C3 22020101"_block); |
| 512 | BOOST_CHECK_EQUAL(i.getName(), |
| 513 | "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a"); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 514 | BOOST_CHECK_EQUAL(*i.getHopLimit(), 214); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 515 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 516 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block); |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 517 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 518 | // duplicate ApplicationParameters |
| 519 | i.wireDecode("0541 0725(080149 0220FF9100E04EAADCF30674D98026A051BA25F56B69BFA026DCCCD72C6EA0F7315A)" |
| 520 | "2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block); |
| 521 | BOOST_CHECK_EQUAL(i.getName(), |
| 522 | "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a"); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 523 | BOOST_CHECK_EQUAL(*i.getHopLimit(), 214); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 524 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true); |
| 525 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 528 | BOOST_AUTO_TEST_CASE(MissingName) |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 529 | { |
| 530 | BOOST_CHECK_THROW(i.wireDecode("0500"_block), tlv::Error); |
| 531 | BOOST_CHECK_THROW(i.wireDecode("0502 1200"_block), tlv::Error); |
| 532 | } |
| 533 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 534 | BOOST_AUTO_TEST_CASE(BadName) |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 535 | { |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 536 | // empty |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 537 | BOOST_CHECK_THROW(i.wireDecode("0502 0700"_block), tlv::Error); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 538 | |
| 539 | // more than one ParametersSha256DigestComponent |
| 540 | BOOST_CHECK_THROW(i.wireDecode("054C 074A(080149" |
| 541 | "02200000000000000000000000000000000000000000000000000000000000000000" |
| 542 | "080132" |
| 543 | "02200000000000000000000000000000000000000000000000000000000000000000)"_block), |
| 544 | tlv::Error); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | BOOST_AUTO_TEST_CASE(BadCanBePrefix) |
| 548 | { |
| 549 | BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 210102"_block), tlv::Error); |
| 550 | } |
| 551 | |
| 552 | BOOST_AUTO_TEST_CASE(BadMustBeFresh) |
| 553 | { |
| 554 | BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 120102"_block), tlv::Error); |
| 555 | } |
| 556 | |
| 557 | BOOST_AUTO_TEST_CASE(BadNonce) |
| 558 | { |
| 559 | BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error); |
| 560 | BOOST_CHECK_THROW(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error); |
| 561 | BOOST_CHECK_THROW(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error); |
| 562 | } |
| 563 | |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 564 | BOOST_AUTO_TEST_CASE(BadHopLimit) |
| 565 | { |
| 566 | BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 2200"_block), tlv::Error); |
| 567 | BOOST_CHECK_THROW(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error); |
| 568 | } |
| 569 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 570 | BOOST_AUTO_TEST_CASE(BadParametersDigest) |
| 571 | { |
| 572 | // ApplicationParameters without ParametersSha256DigestComponent |
| 573 | Block b1("0509 0703(080149) 2402CAFE"_block); |
| 574 | // ParametersSha256DigestComponent without ApplicationParameters |
| 575 | Block b2("0527 0725(080149 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855)"_block); |
| 576 | // digest mismatch |
| 577 | Block b3("052B 0725(080149 02200000000000000000000000000000000000000000000000000000000000000000) " |
| 578 | "2402CAFE"_block); |
| 579 | |
| 580 | BOOST_CHECK_THROW(i.wireDecode(b1), tlv::Error); |
| 581 | BOOST_CHECK_THROW(i.wireDecode(b2), tlv::Error); |
| 582 | BOOST_CHECK_THROW(i.wireDecode(b3), tlv::Error); |
| 583 | |
| 584 | DisableAutoCheckParametersDigest disabler; |
| 585 | BOOST_CHECK_NO_THROW(i.wireDecode(b1)); |
| 586 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false); |
| 587 | BOOST_CHECK_NO_THROW(i.wireDecode(b2)); |
| 588 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false); |
| 589 | BOOST_CHECK_NO_THROW(i.wireDecode(b3)); |
| 590 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false); |
| 591 | } |
| 592 | |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 593 | BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName) |
| 594 | { |
| 595 | BOOST_CHECK_THROW(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error); |
| 596 | } |
| 597 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 598 | BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement) |
| 599 | { |
| 600 | BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error); |
| 601 | } |
| 602 | |
| 603 | BOOST_AUTO_TEST_SUITE_END() // Decode03 |
| 604 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 605 | BOOST_AUTO_TEST_CASE(MatchesData) |
| 606 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 607 | auto interest = makeInterest("/A"); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 608 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 609 | auto data = makeData("/A"); |
| 610 | BOOST_CHECK_EQUAL(interest->matchesData(*data), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 611 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 612 | data->setName("/A/D"); |
| 613 | BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates CanBePrefix |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 614 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 615 | interest->setCanBePrefix(true); |
| 616 | BOOST_CHECK_EQUAL(interest->matchesData(*data), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 617 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 618 | interest->setMustBeFresh(true); |
| 619 | BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates MustBeFresh |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 620 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 621 | data->setFreshnessPeriod(1_s); |
| 622 | BOOST_CHECK_EQUAL(interest->matchesData(*data), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 623 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 624 | data->setName("/H/I"); |
| 625 | BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // Name does not match |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 626 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 627 | data->wireEncode(); |
| 628 | interest = makeInterest(data->getFullName()); |
| 629 | BOOST_CHECK_EQUAL(interest->matchesData(*data), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 630 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 631 | setNameComponent(*interest, -1, name::Component::fromEscapedString( |
| 632 | "sha256digest=0000000000000000000000000000000000000000000000000000000000000000")); |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 633 | BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates implicit digest |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1) |
| 637 | BOOST_AUTO_TEST_CASE(MatchesInterest) |
| 638 | { |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 639 | Interest interest; |
| 640 | interest.setName("/A") |
| 641 | .setCanBePrefix(true) |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 642 | .setMustBeFresh(true) |
| 643 | .setForwardingHint({{1, "/H"}}) |
| 644 | .setNonce(2228) |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 645 | .setInterestLifetime(5_s) |
| 646 | .setHopLimit(90); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 647 | |
| 648 | Interest other; |
| 649 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 650 | |
| 651 | other.setName(interest.getName()); |
| 652 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 653 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 654 | other.setCanBePrefix(interest.getCanBePrefix()); |
| 655 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 656 | |
| 657 | other.setMustBeFresh(interest.getMustBeFresh()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 658 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented |
| 659 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 660 | other.setForwardingHint(interest.getForwardingHint()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 661 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 662 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 663 | other.setNonce(9336); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 664 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 665 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 666 | other.setInterestLifetime(3_s); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 667 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 668 | |
| 669 | other.setHopLimit(31); |
| 670 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 673 | BOOST_AUTO_TEST_CASE(SetName) |
| 674 | { |
| 675 | Interest i; |
| 676 | BOOST_CHECK_EQUAL(i.getName(), "/"); |
| 677 | i.setName("/A/B"); |
| 678 | BOOST_CHECK_EQUAL(i.getName(), "/A/B"); |
| 679 | i.setName("/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); |
| 680 | BOOST_CHECK_EQUAL(i.getName(), |
| 681 | "/I/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); |
| 682 | BOOST_CHECK_THROW(i.setName("/I" |
| 683 | "/params-sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" |
| 684 | "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"), |
| 685 | std::invalid_argument); |
| 686 | } |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 687 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 688 | BOOST_AUTO_TEST_CASE(SetCanBePrefix) |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 689 | { |
| 690 | Interest i; |
| 691 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 692 | i.setCanBePrefix(false); |
| 693 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), false); |
| 694 | BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), 1); |
| 695 | i.setCanBePrefix(true); |
| 696 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 697 | BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), -1); |
| 698 | } |
| 699 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 700 | BOOST_AUTO_TEST_CASE(SetMustBeFresh) |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 701 | { |
| 702 | Interest i; |
| 703 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 704 | i.setMustBeFresh(true); |
| 705 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), true); |
| 706 | BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), true); |
| 707 | i.setMustBeFresh(false); |
| 708 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 709 | BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), false); |
| 710 | } |
| 711 | |
| 712 | BOOST_AUTO_TEST_CASE(ModifyForwardingHint) |
| 713 | { |
| 714 | Interest i; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 715 | i.setCanBePrefix(false); |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 716 | i.setForwardingHint({{1, "/A"}}); |
| 717 | i.wireEncode(); |
| 718 | BOOST_CHECK(i.hasWire()); |
| 719 | |
| 720 | i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); }); |
| 721 | BOOST_CHECK(!i.hasWire()); |
| 722 | BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}})); |
| 723 | } |
| 724 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 725 | BOOST_AUTO_TEST_CASE(GetNonce) |
| 726 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 727 | unique_ptr<Interest> i1, i2; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 728 | |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 729 | // getNonce automatically assigns a random Nonce. |
| 730 | // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of |
| 731 | // same Nonces in a row. |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 732 | int nIterations = 0; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 733 | uint32_t nonce1 = 0, nonce2 = 0; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 734 | do { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 735 | i1 = make_unique<Interest>(); |
| 736 | nonce1 = i1->getNonce(); |
| 737 | i2 = make_unique<Interest>(); |
| 738 | nonce2 = i2->getNonce(); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 739 | } |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 740 | while (nonce1 == nonce2 && ++nIterations < 100); |
| 741 | BOOST_CHECK_NE(nonce1, nonce2); |
| 742 | BOOST_CHECK(i1->hasNonce()); |
| 743 | BOOST_CHECK(i2->hasNonce()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 744 | |
| 745 | // Once a Nonce is assigned, it should not change. |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 746 | BOOST_CHECK_EQUAL(i1->getNonce(), nonce1); |
| 747 | } |
| 748 | |
| 749 | BOOST_AUTO_TEST_CASE(SetNonce) |
| 750 | { |
| 751 | Interest i1("/A"); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 752 | i1.setCanBePrefix(false); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 753 | i1.setNonce(1); |
| 754 | i1.wireEncode(); |
| 755 | BOOST_CHECK_EQUAL(i1.getNonce(), 1); |
| 756 | |
| 757 | Interest i2(i1); |
| 758 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 759 | |
| 760 | i2.setNonce(2); |
| 761 | BOOST_CHECK_EQUAL(i2.getNonce(), 2); |
| 762 | BOOST_CHECK_EQUAL(i1.getNonce(), 1); // should not affect i1 Nonce (Bug #4168) |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | BOOST_AUTO_TEST_CASE(RefreshNonce) |
| 766 | { |
| 767 | Interest i; |
| 768 | BOOST_CHECK(!i.hasNonce()); |
| 769 | i.refreshNonce(); |
| 770 | BOOST_CHECK(!i.hasNonce()); |
| 771 | |
| 772 | i.setNonce(1); |
| 773 | BOOST_CHECK(i.hasNonce()); |
| 774 | i.refreshNonce(); |
| 775 | BOOST_CHECK(i.hasNonce()); |
| 776 | BOOST_CHECK_NE(i.getNonce(), 1); |
| 777 | } |
| 778 | |
| 779 | BOOST_AUTO_TEST_CASE(SetInterestLifetime) |
| 780 | { |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 781 | BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 782 | BOOST_CHECK_NO_THROW(Interest("/A", 0_ms)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 783 | |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 784 | Interest i; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 785 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 786 | BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 787 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 788 | i.setInterestLifetime(0_ms); |
| 789 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms); |
| 790 | i.setInterestLifetime(1_ms); |
| 791 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms); |
Davide Pesavento | 2b0cc7b | 2019-07-14 16:50:04 -0400 | [diff] [blame] | 792 | |
| 793 | i = Interest("/B", 15_s); |
| 794 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 15_s); |
| 795 | } |
| 796 | |
| 797 | BOOST_AUTO_TEST_CASE(SetHopLimit) |
| 798 | { |
| 799 | Interest i; |
| 800 | BOOST_CHECK(i.getHopLimit() == nullopt); |
| 801 | i.setHopLimit(42); |
| 802 | BOOST_CHECK(i.getHopLimit() == 42); |
| 803 | i.setHopLimit(nullopt); |
| 804 | BOOST_CHECK(i.getHopLimit() == nullopt); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 807 | BOOST_AUTO_TEST_CASE(SetApplicationParameters) |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 808 | { |
| 809 | const uint8_t PARAMETERS1[] = {0xc1}; |
| 810 | const uint8_t PARAMETERS2[] = {0xc2}; |
| 811 | |
| 812 | Interest i; |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 813 | BOOST_CHECK(!i.hasApplicationParameters()); |
| 814 | i.setApplicationParameters("2400"_block); |
| 815 | BOOST_CHECK(i.hasApplicationParameters()); |
| 816 | i.unsetApplicationParameters(); |
| 817 | BOOST_CHECK(!i.hasApplicationParameters()); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 818 | |
Davide Pesavento | 3891244 | 2019-04-06 22:03:39 -0400 | [diff] [blame] | 819 | // Block overload |
| 820 | i.setApplicationParameters(Block{}); |
| 821 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block); |
| 822 | i.setApplicationParameters("2401C0"_block); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 823 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C0"_block); |
Davide Pesavento | 3891244 | 2019-04-06 22:03:39 -0400 | [diff] [blame] | 824 | i.setApplicationParameters("8001C1"_block); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 825 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "24038001C1"_block); |
Davide Pesavento | 3891244 | 2019-04-06 22:03:39 -0400 | [diff] [blame] | 826 | |
| 827 | // raw buffer+size overload |
| 828 | i.setApplicationParameters(PARAMETERS1, sizeof(PARAMETERS1)); |
| 829 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C1"_block); |
| 830 | i.setApplicationParameters(nullptr, 0); |
| 831 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block); |
| 832 | BOOST_CHECK_THROW(i.setApplicationParameters(nullptr, 42), std::invalid_argument); |
| 833 | |
| 834 | // ConstBufferPtr overload |
| 835 | i.setApplicationParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2))); |
| 836 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C2"_block); |
| 837 | i.setApplicationParameters(make_shared<Buffer>()); |
| 838 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block); |
| 839 | BOOST_CHECK_THROW(i.setApplicationParameters(nullptr), std::invalid_argument); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 842 | BOOST_AUTO_TEST_CASE(ParametersSha256DigestComponent) |
| 843 | { |
| 844 | Interest i("/I"); |
| 845 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 846 | |
| 847 | i.setApplicationParameters("2404C0C1C2C3"_block); // auto-appends ParametersSha256DigestComponent |
| 848 | BOOST_CHECK_EQUAL(i.getName(), |
| 849 | "/I/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a"); |
| 850 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 851 | |
| 852 | i.setApplicationParameters(nullptr, 0); // updates ParametersSha256DigestComponent |
| 853 | BOOST_CHECK_EQUAL(i.getName(), |
| 854 | "/I/params-sha256=33b67cb5385ceddad93d0ee960679041613bed34b8b4a5e6362fe7539ba2d3ce"); |
| 855 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true); |
| 856 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 857 | |
| 858 | i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent |
| 859 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 860 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 861 | |
| 862 | i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q")); |
| 863 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), false); |
| 864 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), false); |
| 865 | |
| 866 | i.unsetApplicationParameters(); // removes ParametersSha256DigestComponent |
| 867 | BOOST_CHECK_EQUAL(i.getName(), "/P/Q"); |
| 868 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 869 | |
| 870 | i.setName(Name("/P").appendParametersSha256DigestPlaceholder().append("Q")); |
| 871 | i.setApplicationParameters("2404C0C1C2C3"_block); // updates ParametersSha256DigestComponent |
| 872 | BOOST_CHECK_EQUAL(i.getName(), |
| 873 | "/P/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a/Q"); |
| 874 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 875 | |
| 876 | i.setName("/A/B/C"); // auto-appends ParametersSha256DigestComponent |
| 877 | BOOST_CHECK_EQUAL(i.getName(), |
| 878 | "/A/B/C/params-sha256=ff9100e04eaadcf30674d98026a051ba25f56b69bfa026dcccd72c6ea0f7315a"); |
| 879 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true); |
| 880 | BOOST_CHECK_EQUAL(i.isParametersDigestValid(), true); |
| 881 | } |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 882 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 883 | BOOST_AUTO_TEST_SUITE_END() // TestInterest |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 884 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 885 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 886 | } // namespace ndn |