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