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