Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/interest.hpp" |
| 23 | #include "ndn-cxx/data.hpp" |
| 24 | #include "ndn-cxx/security/digest-sha256.hpp" |
| 25 | #include "ndn-cxx/security/signature-sha256-with-rsa.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 26 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 27 | #include "tests/boost-test.hpp" |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 28 | #include "tests/make-interest-data.hpp" |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 30 | namespace ndn { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 31 | namespace tests { |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 32 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(TestInterest) |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 34 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 35 | // ---- constructor, encode, decode ---- |
| 36 | |
| 37 | BOOST_AUTO_TEST_CASE(DefaultConstructor) |
| 38 | { |
| 39 | Interest i; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 40 | BOOST_CHECK(!i.hasWire()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 41 | BOOST_CHECK_EQUAL(i.getName(), "/"); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 42 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 43 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 44 | BOOST_CHECK(i.getForwardingHint().empty()); |
| 45 | BOOST_CHECK(!i.hasNonce()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 46 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 47 | BOOST_CHECK(!i.hasSelectors()); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 48 | BOOST_CHECK(!i.hasApplicationParameters()); |
| 49 | BOOST_CHECK(i.getApplicationParameters().empty()); |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | BOOST_AUTO_TEST_CASE(DecodeNotInterest) |
| 53 | { |
| 54 | BOOST_CHECK_THROW(Interest("4202CAFE"_block), tlv::Error); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 57 | BOOST_AUTO_TEST_CASE(EncodeDecode02Basic) |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 58 | { |
| 59 | const uint8_t WIRE[] = { |
| 60 | 0x05, 0x1c, // Interest |
| 61 | 0x07, 0x14, // Name |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 62 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 63 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 64 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 65 | 0x0a, 0x04, // Nonce |
| 66 | 0x01, 0x00, 0x00, 0x00 |
| 67 | }; |
| 68 | |
| 69 | Interest i1("/local/ndn/prefix"); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 70 | i1.setCanBePrefix(true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 71 | i1.setNonce(1); |
| 72 | Block wire1 = i1.wireEncode(); |
| 73 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 74 | |
| 75 | Interest i2(wire1); |
| 76 | BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix"); |
| 77 | BOOST_CHECK(i2.getSelectors().empty()); |
| 78 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 79 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
| 80 | |
| 81 | BOOST_CHECK_EQUAL(i1, i2); |
| 82 | } |
| 83 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 84 | BOOST_AUTO_TEST_CASE(EncodeDecode02Full) |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 85 | { |
| 86 | const uint8_t WIRE[] = { |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 87 | 0x05, 0x31, // Interest |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 88 | 0x07, 0x14, // Name |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 89 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 90 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 91 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 92 | 0x09, 0x03, // Selectors |
| 93 | 0x0d, 0x01, 0x01, // MinSuffixComponents |
| 94 | 0x0a, 0x04, // Nonce |
| 95 | 0x01, 0x00, 0x00, 0x00, |
| 96 | 0x0c, 0x02, // InterestLifetime |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 97 | 0x03, 0xe8, |
| 98 | 0x1e, 0x0a, // ForwardingHint |
| 99 | 0x1f, 0x08, // Delegation |
| 100 | 0x1e, 0x01, 0x01, // Preference=1 |
| 101 | 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 102 | }; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 103 | |
| 104 | Interest i1; |
| 105 | i1.setName("/local/ndn/prefix"); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 106 | i1.setCanBePrefix(true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 107 | i1.setMinSuffixComponents(1); |
| 108 | i1.setNonce(1); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 109 | i1.setInterestLifetime(1000_ms); |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 110 | i1.setForwardingHint({{1, "/A"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 111 | Block wire1 = i1.wireEncode(); |
| 112 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 113 | |
| 114 | Interest i2(wire1); |
| 115 | BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix"); |
| 116 | BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 1); |
| 117 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 1000_ms); |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 119 | BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{1, "/A"}})); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 120 | |
| 121 | BOOST_CHECK_EQUAL(i1, i2); |
| 122 | } |
| 123 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 124 | BOOST_AUTO_TEST_CASE(EncodeDecode03Basic) |
| 125 | { |
| 126 | const uint8_t WIRE[] = { |
| 127 | 0x05, 0x22, // Interest |
| 128 | 0x07, 0x14, // Name |
| 129 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 130 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 131 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
| 132 | 0x0a, 0x04, // Nonce |
| 133 | 0x01, 0x00, 0x00, 0x00, |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 134 | 0x24, 0x04, // ApplicationParameters |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 135 | 0xc0, 0xc1, 0xc2, 0xc3}; |
| 136 | |
| 137 | Interest i1; |
| 138 | i1.setName("/local/ndn/prefix"); |
| 139 | i1.setCanBePrefix(false); |
| 140 | i1.setNonce(1); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 141 | i1.setApplicationParameters("2404C0C1C2C3"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 142 | Block wire1 = i1.wireEncode(); |
| 143 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 144 | |
| 145 | Interest i2(wire1); |
| 146 | BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix"); |
| 147 | BOOST_CHECK_EQUAL(i2.getCanBePrefix(), false); |
| 148 | BOOST_CHECK_EQUAL(i2.getMustBeFresh(), false); |
| 149 | BOOST_CHECK(i2.getForwardingHint().empty()); |
| 150 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 151 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 152 | BOOST_CHECK(i2.hasApplicationParameters()); |
| 153 | BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 154 | BOOST_CHECK(i2.getPublisherPublicKeyLocator().empty()); |
| 155 | } |
| 156 | |
| 157 | BOOST_AUTO_TEST_CASE(EncodeDecode03Full) |
| 158 | { |
| 159 | const uint8_t WIRE[] = { |
| 160 | 0x05, 0x37, // Interest |
| 161 | 0x07, 0x14, // Name |
| 162 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 163 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 164 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
| 165 | 0x21, 0x00, // CanBePrefix |
| 166 | 0x12, 0x00, // MustBeFresh |
| 167 | 0x1e, 0x0b, // ForwardingHint |
| 168 | 0x1f, 0x09, // Delegation List |
| 169 | 0x1e, 0x02, |
| 170 | 0x3e, 0x15, |
| 171 | 0x07, 0x03, |
| 172 | 0x08, 0x01, 0x48, |
| 173 | 0x0a, 0x04, // Nonce |
| 174 | 0x4a, 0xcb, 0x1e, 0x4c, |
| 175 | 0x0c, 0x02, // Interest Lifetime |
| 176 | 0x76, 0xa1, |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 177 | 0x24, 0x04, // ApplicationParameters |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 178 | 0xc0, 0xc1, 0xc2, 0xc3}; |
| 179 | Interest i1; |
| 180 | i1.setName("/local/ndn/prefix"); |
| 181 | i1.setMustBeFresh(true); |
| 182 | i1.setCanBePrefix(true); |
| 183 | i1.setForwardingHint(DelegationList({{15893, "/H"}})); |
| 184 | i1.setNonce(0x4c1ecb4a); |
| 185 | i1.setInterestLifetime(30369_ms); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 186 | i1.setApplicationParameters("2404C0C1C2C3"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 187 | i1.setMinSuffixComponents(1); // v0.2-only elements will not be encoded |
| 188 | i1.setExclude(Exclude().excludeAfter(name::Component("J"))); // v0.2-only elements will not be encoded |
| 189 | Block wire1 = i1.wireEncode(); |
| 190 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 191 | |
| 192 | Interest i2(wire1); |
| 193 | BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix"); |
| 194 | BOOST_CHECK_EQUAL(i2.getCanBePrefix(), true); |
| 195 | BOOST_CHECK_EQUAL(i2.getMustBeFresh(), true); |
| 196 | BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{15893, "/H"}})); |
| 197 | BOOST_CHECK(i2.hasNonce()); |
| 198 | BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a); |
| 199 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 200 | BOOST_CHECK_EQUAL(i2.getApplicationParameters(), "2404C0C1C2C3"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 201 | BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), -1); // Default because minSuffixComponents was not encoded |
| 202 | BOOST_CHECK(i2.getExclude().empty()); // Exclude was not encoded |
| 203 | } |
| 204 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 205 | class Decode03Fixture |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 206 | { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 207 | protected: |
| 208 | Decode03Fixture() |
| 209 | { |
| 210 | // initialize all elements to non-empty, to verify wireDecode clears them |
| 211 | i.setName("/A"); |
| 212 | i.setForwardingHint({{10309, "/F"}}); |
| 213 | i.setNonce(0x03d645a8); |
| 214 | i.setInterestLifetime(18554_ms); |
| 215 | i.setPublisherPublicKeyLocator(Name("/K")); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 216 | i.setApplicationParameters("2404A0A1A2A3"_block); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 217 | } |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 218 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 219 | protected: |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 220 | Interest i; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 221 | }; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 222 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 223 | BOOST_FIXTURE_TEST_SUITE(Decode03, Decode03Fixture) |
| 224 | |
| 225 | BOOST_AUTO_TEST_CASE(Minimal) |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 226 | { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 227 | i.wireDecode("0505 0703080149"_block); |
| 228 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 229 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), false); |
| 230 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 231 | BOOST_CHECK(i.getForwardingHint().empty()); |
| 232 | BOOST_CHECK(i.hasNonce()); // a random nonce is generated |
| 233 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
| 234 | BOOST_CHECK(i.getPublisherPublicKeyLocator().empty()); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 235 | BOOST_CHECK(!i.hasApplicationParameters()); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 236 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 237 | BOOST_CHECK(!i.hasWire()); // nonce generation resets wire encoding |
| 238 | |
| 239 | // modify then re-encode as v0.2 format |
| 240 | i.setNonce(0x54657c95); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 241 | BOOST_CHECK_EQUAL(i.wireEncode(), "0510 0703080149 09030E0101 0A04957C6554"_block); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 244 | BOOST_AUTO_TEST_CASE(Full) |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 245 | { |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 246 | i.wireDecode("0531 0703080149 FC00 2100 FC00 1200 " |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 247 | "FC00 1E0B(1F09 1E023E15 0703080148) FC00 0A044ACB1E4C " |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 248 | "FC00 0C0276A1 FC00 2201D6 FC00"_block); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 249 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 250 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 251 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), true); |
| 252 | BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}})); |
| 253 | BOOST_CHECK(i.hasNonce()); |
| 254 | BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a); |
| 255 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms); |
| 256 | // HopLimit=214 is not stored |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 257 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 258 | // encode without modification: retain original wire encoding |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 259 | BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 49); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 260 | |
| 261 | // modify then re-encode as v0.2 format |
| 262 | i.setName("/J"); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 263 | BOOST_CHECK_EQUAL(i.wireEncode(), |
| 264 | "0520 070308014A 09021200 0A044ACB1E4C 0C0276A1 1E0B(1F09 1E023E15 0703080148)"_block); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 267 | BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder) |
| 268 | { |
| 269 | BOOST_CHECK_THROW(i.wireDecode( |
| 270 | "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 271 | "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block), |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 272 | tlv::Error); |
| 273 | BOOST_CHECK_THROW(i.wireDecode( |
| 274 | "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 275 | "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block), |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 276 | tlv::Error); |
| 277 | BOOST_CHECK_THROW(i.wireDecode( |
| 278 | "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 279 | "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block), |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 280 | tlv::Error); |
| 281 | BOOST_CHECK_THROW(i.wireDecode( |
| 282 | "0529 0703080149 2100 1200 0A044ACB1E4C " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 283 | "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2404C0C1C2C3"_block), |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 284 | tlv::Error); |
| 285 | BOOST_CHECK_THROW(i.wireDecode( |
| 286 | "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 287 | "0C0276A1 0A044ACB1E4C 2201D6 2404C0C1C2C3"_block), |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 288 | tlv::Error); |
| 289 | BOOST_CHECK_THROW(i.wireDecode( |
| 290 | "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) " |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 291 | "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block), |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 292 | tlv::Error); |
| 293 | } |
| 294 | |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 295 | BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder) |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 296 | { |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 297 | // HopLimit |
| 298 | i.wireDecode("0514 0703080149 2201D6 2200 2404C0C1C2C3 22020101"_block); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 299 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 300 | // HopLimit=214 is not stored |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 301 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block); |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 302 | |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 303 | // ApplicationParameters |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 304 | i.wireDecode("051F 0703080149 2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block); |
| 305 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 306 | BOOST_CHECK_EQUAL(i.hasApplicationParameters(), true); |
| 307 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2404C0C1C2C3"_block); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | BOOST_AUTO_TEST_CASE(NameMissing) |
| 311 | { |
| 312 | BOOST_CHECK_THROW(i.wireDecode("0500"_block), tlv::Error); |
| 313 | BOOST_CHECK_THROW(i.wireDecode("0502 1200"_block), tlv::Error); |
| 314 | } |
| 315 | |
| 316 | BOOST_AUTO_TEST_CASE(NameEmpty) |
| 317 | { |
| 318 | BOOST_CHECK_THROW(i.wireDecode("0502 0700"_block), tlv::Error); |
| 319 | } |
| 320 | |
| 321 | BOOST_AUTO_TEST_CASE(BadCanBePrefix) |
| 322 | { |
| 323 | BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 210102"_block), tlv::Error); |
| 324 | } |
| 325 | |
| 326 | BOOST_AUTO_TEST_CASE(BadMustBeFresh) |
| 327 | { |
| 328 | BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 120102"_block), tlv::Error); |
| 329 | } |
| 330 | |
| 331 | BOOST_AUTO_TEST_CASE(BadNonce) |
| 332 | { |
| 333 | BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error); |
| 334 | BOOST_CHECK_THROW(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error); |
| 335 | BOOST_CHECK_THROW(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error); |
| 336 | } |
| 337 | |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 338 | BOOST_AUTO_TEST_CASE(BadHopLimit) |
| 339 | { |
| 340 | BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 2200"_block), tlv::Error); |
| 341 | BOOST_CHECK_THROW(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error); |
| 342 | } |
| 343 | |
Junxiao Shi | 8b753a2 | 2018-10-24 01:51:40 +0000 | [diff] [blame] | 344 | BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName) |
| 345 | { |
| 346 | BOOST_CHECK_THROW(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error); |
| 347 | } |
| 348 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame] | 349 | BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement) |
| 350 | { |
| 351 | BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error); |
| 352 | } |
| 353 | |
| 354 | BOOST_AUTO_TEST_SUITE_END() // Decode03 |
| 355 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 356 | // ---- matching ---- |
| 357 | |
| 358 | BOOST_AUTO_TEST_CASE(MatchesData) |
| 359 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 360 | auto interest = makeInterest("/A"); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 361 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 362 | auto data = makeData("/A"); |
| 363 | BOOST_CHECK_EQUAL(interest->matchesData(*data), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 364 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 365 | data->setName("/A/D"); |
| 366 | BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates CanBePrefix |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 367 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 368 | interest->setCanBePrefix(true); |
| 369 | BOOST_CHECK_EQUAL(interest->matchesData(*data), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 370 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 371 | interest->setMustBeFresh(true); |
| 372 | BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates MustBeFresh |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 373 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 374 | data->setFreshnessPeriod(1_s); |
| 375 | BOOST_CHECK_EQUAL(interest->matchesData(*data), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 376 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 377 | data->setName("/H/I"); |
| 378 | BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // Name does not match |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 379 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 380 | data->wireEncode(); |
| 381 | interest = makeInterest(data->getFullName()); |
| 382 | BOOST_CHECK_EQUAL(interest->matchesData(*data), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 383 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 384 | setNameComponent(*interest, -1, Name("/sha256digest=000000000000000000000000" |
| 385 | "0000000000000000000000000000000000000000").at(0)); |
| 386 | BOOST_CHECK_EQUAL(interest->matchesData(*data), false); // violates implicit digest |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1) |
| 390 | BOOST_AUTO_TEST_CASE(MatchesInterest) |
| 391 | { |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 392 | Interest interest("/A"); |
| 393 | interest.setCanBePrefix(true) |
| 394 | .setMustBeFresh(true) |
| 395 | .setForwardingHint({{1, "/H"}}) |
| 396 | .setNonce(2228) |
| 397 | .setInterestLifetime(5_s); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 398 | |
| 399 | Interest other; |
| 400 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 401 | |
| 402 | other.setName(interest.getName()); |
| 403 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 404 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 405 | other.setCanBePrefix(interest.getCanBePrefix()); |
| 406 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 407 | |
| 408 | other.setMustBeFresh(interest.getMustBeFresh()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 409 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented |
| 410 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 411 | other.setForwardingHint(interest.getForwardingHint()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 412 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 413 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 414 | other.setNonce(9336); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 415 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 416 | |
Junxiao Shi | 2ad2fbe | 2019-05-24 03:11:05 +0000 | [diff] [blame] | 417 | other.setInterestLifetime(3_s); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 418 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | // ---- field accessors ---- |
| 422 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 423 | BOOST_AUTO_TEST_CASE(CanBePrefix) |
| 424 | { |
| 425 | Interest i; |
| 426 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 427 | i.setCanBePrefix(false); |
| 428 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), false); |
| 429 | BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), 1); |
| 430 | i.setCanBePrefix(true); |
| 431 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 432 | BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), -1); |
| 433 | } |
| 434 | |
| 435 | BOOST_AUTO_TEST_CASE(MustBeFresh) |
| 436 | { |
| 437 | Interest i; |
| 438 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 439 | i.setMustBeFresh(true); |
| 440 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), true); |
| 441 | BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), true); |
| 442 | i.setMustBeFresh(false); |
| 443 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 444 | BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), false); |
| 445 | } |
| 446 | |
| 447 | BOOST_AUTO_TEST_CASE(ModifyForwardingHint) |
| 448 | { |
| 449 | Interest i; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 450 | i.setCanBePrefix(false); |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 451 | i.setForwardingHint({{1, "/A"}}); |
| 452 | i.wireEncode(); |
| 453 | BOOST_CHECK(i.hasWire()); |
| 454 | |
| 455 | i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); }); |
| 456 | BOOST_CHECK(!i.hasWire()); |
| 457 | BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}})); |
| 458 | } |
| 459 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 460 | BOOST_AUTO_TEST_CASE(GetNonce) |
| 461 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 462 | unique_ptr<Interest> i1, i2; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 463 | |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 464 | // getNonce automatically assigns a random Nonce. |
| 465 | // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of |
| 466 | // same Nonces in a row. |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 467 | int nIterations = 0; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 468 | uint32_t nonce1 = 0, nonce2 = 0; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 469 | do { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 470 | i1 = make_unique<Interest>(); |
| 471 | nonce1 = i1->getNonce(); |
| 472 | i2 = make_unique<Interest>(); |
| 473 | nonce2 = i2->getNonce(); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 474 | } |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 475 | while (nonce1 == nonce2 && ++nIterations < 100); |
| 476 | BOOST_CHECK_NE(nonce1, nonce2); |
| 477 | BOOST_CHECK(i1->hasNonce()); |
| 478 | BOOST_CHECK(i2->hasNonce()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 479 | |
| 480 | // Once a Nonce is assigned, it should not change. |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 481 | BOOST_CHECK_EQUAL(i1->getNonce(), nonce1); |
| 482 | } |
| 483 | |
| 484 | BOOST_AUTO_TEST_CASE(SetNonce) |
| 485 | { |
| 486 | Interest i1("/A"); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 487 | i1.setCanBePrefix(false); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 488 | i1.setNonce(1); |
| 489 | i1.wireEncode(); |
| 490 | BOOST_CHECK_EQUAL(i1.getNonce(), 1); |
| 491 | |
| 492 | Interest i2(i1); |
| 493 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 494 | |
| 495 | i2.setNonce(2); |
| 496 | BOOST_CHECK_EQUAL(i2.getNonce(), 2); |
| 497 | 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] | 498 | } |
| 499 | |
| 500 | BOOST_AUTO_TEST_CASE(RefreshNonce) |
| 501 | { |
| 502 | Interest i; |
| 503 | BOOST_CHECK(!i.hasNonce()); |
| 504 | i.refreshNonce(); |
| 505 | BOOST_CHECK(!i.hasNonce()); |
| 506 | |
| 507 | i.setNonce(1); |
| 508 | BOOST_CHECK(i.hasNonce()); |
| 509 | i.refreshNonce(); |
| 510 | BOOST_CHECK(i.hasNonce()); |
| 511 | BOOST_CHECK_NE(i.getNonce(), 1); |
| 512 | } |
| 513 | |
| 514 | BOOST_AUTO_TEST_CASE(SetInterestLifetime) |
| 515 | { |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 516 | BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 517 | BOOST_CHECK_NO_THROW(Interest("/A", 0_ms)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 518 | |
| 519 | Interest i("/local/ndn/prefix"); |
| 520 | i.setNonce(1); |
| 521 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame] | 522 | BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 523 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 524 | i.setInterestLifetime(0_ms); |
| 525 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms); |
| 526 | i.setInterestLifetime(1_ms); |
| 527 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 530 | BOOST_AUTO_TEST_CASE(SetApplicationParameters) |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 531 | { |
| 532 | const uint8_t PARAMETERS1[] = {0xc1}; |
| 533 | const uint8_t PARAMETERS2[] = {0xc2}; |
| 534 | |
| 535 | Interest i; |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 536 | BOOST_CHECK(!i.hasApplicationParameters()); |
| 537 | i.setApplicationParameters("2400"_block); |
| 538 | BOOST_CHECK(i.hasApplicationParameters()); |
| 539 | i.unsetApplicationParameters(); |
| 540 | BOOST_CHECK(!i.hasApplicationParameters()); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 541 | |
Davide Pesavento | 3891244 | 2019-04-06 22:03:39 -0400 | [diff] [blame] | 542 | // Block overload |
| 543 | i.setApplicationParameters(Block{}); |
| 544 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block); |
| 545 | i.setApplicationParameters("2401C0"_block); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 546 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C0"_block); |
Davide Pesavento | 3891244 | 2019-04-06 22:03:39 -0400 | [diff] [blame] | 547 | i.setApplicationParameters("8001C1"_block); |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 548 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "24038001C1"_block); |
Davide Pesavento | 3891244 | 2019-04-06 22:03:39 -0400 | [diff] [blame] | 549 | |
| 550 | // raw buffer+size overload |
| 551 | i.setApplicationParameters(PARAMETERS1, sizeof(PARAMETERS1)); |
| 552 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C1"_block); |
| 553 | i.setApplicationParameters(nullptr, 0); |
| 554 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block); |
| 555 | BOOST_CHECK_THROW(i.setApplicationParameters(nullptr, 42), std::invalid_argument); |
| 556 | |
| 557 | // ConstBufferPtr overload |
| 558 | i.setApplicationParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2))); |
| 559 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C2"_block); |
| 560 | i.setApplicationParameters(make_shared<Buffer>()); |
| 561 | BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block); |
| 562 | BOOST_CHECK_THROW(i.setApplicationParameters(nullptr), std::invalid_argument); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 565 | // ---- operators ---- |
| 566 | |
| 567 | BOOST_AUTO_TEST_CASE(Equality) |
| 568 | { |
| 569 | Interest a; |
| 570 | Interest b; |
| 571 | |
| 572 | // if nonce is not set, it would be set to a random value |
| 573 | a.setNonce(1); |
| 574 | b.setNonce(1); |
| 575 | |
| 576 | BOOST_CHECK_EQUAL(a == b, true); |
| 577 | BOOST_CHECK_EQUAL(a != b, false); |
| 578 | |
| 579 | // compare Name |
| 580 | a.setName("/A"); |
| 581 | BOOST_CHECK_EQUAL(a == b, false); |
| 582 | BOOST_CHECK_EQUAL(a != b, true); |
| 583 | |
| 584 | b.setName("/B"); |
| 585 | BOOST_CHECK_EQUAL(a == b, false); |
| 586 | BOOST_CHECK_EQUAL(a != b, true); |
| 587 | |
| 588 | b.setName("/A"); |
| 589 | BOOST_CHECK_EQUAL(a == b, true); |
| 590 | BOOST_CHECK_EQUAL(a != b, false); |
| 591 | |
| 592 | // compare Selectors |
| 593 | a.setChildSelector(1); |
| 594 | BOOST_CHECK_EQUAL(a == b, false); |
| 595 | BOOST_CHECK_EQUAL(a != b, true); |
| 596 | |
| 597 | b.setChildSelector(1); |
| 598 | BOOST_CHECK_EQUAL(a == b, true); |
| 599 | BOOST_CHECK_EQUAL(a != b, false); |
| 600 | |
| 601 | // compare Nonce |
| 602 | a.setNonce(100); |
| 603 | BOOST_CHECK_EQUAL(a == b, false); |
| 604 | BOOST_CHECK_EQUAL(a != b, true); |
| 605 | |
| 606 | b.setNonce(100); |
| 607 | BOOST_CHECK_EQUAL(a == b, true); |
| 608 | BOOST_CHECK_EQUAL(a != b, false); |
| 609 | |
| 610 | // compare InterestLifetime |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 611 | a.setInterestLifetime(10_s); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 612 | BOOST_CHECK_EQUAL(a == b, false); |
| 613 | BOOST_CHECK_EQUAL(a != b, true); |
| 614 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 615 | b.setInterestLifetime(10_s); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 616 | BOOST_CHECK_EQUAL(a == b, true); |
| 617 | BOOST_CHECK_EQUAL(a != b, false); |
| 618 | |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame] | 619 | // compare ForwardingHint |
| 620 | a.setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 621 | BOOST_CHECK_EQUAL(a == b, false); |
| 622 | BOOST_CHECK_EQUAL(a != b, true); |
| 623 | |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame] | 624 | b.setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 625 | BOOST_CHECK_EQUAL(a == b, true); |
| 626 | BOOST_CHECK_EQUAL(a != b, false); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 627 | |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 628 | // compare ApplicationParameters |
| 629 | a.setApplicationParameters("2404C0C1C2C3"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 630 | BOOST_CHECK_EQUAL(a == b, false); |
| 631 | BOOST_CHECK_EQUAL(a != b, true); |
| 632 | |
Davide Pesavento | 9c19a39 | 2019-04-06 15:07:54 -0400 | [diff] [blame] | 633 | b.setApplicationParameters("2404C0C1C2C3"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 634 | BOOST_CHECK_EQUAL(a == b, true); |
| 635 | BOOST_CHECK_EQUAL(a != b, false); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 638 | BOOST_AUTO_TEST_SUITE_END() // TestInterest |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 639 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 640 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 641 | } // namespace ndn |