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