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