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" |
| 28 | #include "tests/identity-management-fixture.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 | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 48 | BOOST_CHECK(!i.hasParameters()); |
| 49 | BOOST_CHECK(i.getParameters().empty()); |
| 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 | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 134 | 0x24, 0x04, // Parameters |
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 | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 141 | i1.setParameters("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); |
| 152 | BOOST_CHECK(i2.hasParameters()); |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 153 | BOOST_CHECK_EQUAL(i2.getParameters(), "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 | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 177 | 0x24, 0x04, // Parameters |
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 | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 186 | i1.setParameters("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 | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 200 | BOOST_CHECK_EQUAL(i2.getParameters(), "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 | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 216 | i.setParameters("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()); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 235 | BOOST_CHECK(!i.hasParameters()); |
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 | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 301 | BOOST_CHECK_EQUAL(i.getParameters(), "2404C0C1C2C3"_block); |
| 302 | |
| 303 | // Parameters |
| 304 | i.wireDecode("051F 0703080149 2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block); |
| 305 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 306 | BOOST_CHECK_EQUAL(i.hasParameters(), true); |
| 307 | BOOST_CHECK_EQUAL(i.getParameters(), "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 | { |
| 360 | Interest interest; |
| 361 | interest.setName("ndn:/A") |
| 362 | .setMinSuffixComponents(2) |
| 363 | .setMaxSuffixComponents(2) |
| 364 | .setPublisherPublicKeyLocator(KeyLocator("ndn:/B")) |
| 365 | .setExclude(Exclude().excludeAfter(name::Component("J"))); |
| 366 | |
| 367 | Data data("ndn:/A/D"); |
| 368 | SignatureSha256WithRsa signature(KeyLocator("ndn:/B")); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 369 | signature.setValue(encoding::makeEmptyBlock(tlv::SignatureValue)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 370 | data.setSignature(signature); |
| 371 | data.wireEncode(); |
| 372 | BOOST_CHECK_EQUAL(interest.matchesData(data), true); |
| 373 | |
| 374 | Data data1 = data; |
| 375 | data1.setName("ndn:/A"); // violates MinSuffixComponents |
| 376 | data1.wireEncode(); |
| 377 | BOOST_CHECK_EQUAL(interest.matchesData(data1), false); |
| 378 | |
| 379 | Interest interest1 = interest; |
| 380 | interest1.setMinSuffixComponents(1); |
| 381 | BOOST_CHECK_EQUAL(interest1.matchesData(data1), true); |
| 382 | |
| 383 | Data data2 = data; |
| 384 | data2.setName("ndn:/A/E/F"); // violates MaxSuffixComponents |
| 385 | data2.wireEncode(); |
| 386 | BOOST_CHECK_EQUAL(interest.matchesData(data2), false); |
| 387 | |
| 388 | Interest interest2 = interest; |
| 389 | interest2.setMaxSuffixComponents(3); |
| 390 | BOOST_CHECK_EQUAL(interest2.matchesData(data2), true); |
| 391 | |
| 392 | Data data3 = data; |
| 393 | SignatureSha256WithRsa signature3(KeyLocator("ndn:/G")); // violates PublisherPublicKeyLocator |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 394 | signature3.setValue(encoding::makeEmptyBlock(tlv::SignatureValue)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 395 | data3.setSignature(signature3); |
| 396 | data3.wireEncode(); |
| 397 | BOOST_CHECK_EQUAL(interest.matchesData(data3), false); |
| 398 | |
| 399 | Interest interest3 = interest; |
| 400 | interest3.setPublisherPublicKeyLocator(KeyLocator("ndn:/G")); |
| 401 | BOOST_CHECK_EQUAL(interest3.matchesData(data3), true); |
| 402 | |
| 403 | Data data4 = data; |
| 404 | DigestSha256 signature4; // violates PublisherPublicKeyLocator |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 405 | signature4.setValue(encoding::makeEmptyBlock(tlv::SignatureValue)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 406 | data4.setSignature(signature4); |
| 407 | data4.wireEncode(); |
| 408 | BOOST_CHECK_EQUAL(interest.matchesData(data4), false); |
| 409 | |
| 410 | Interest interest4 = interest; |
| 411 | interest4.setPublisherPublicKeyLocator(KeyLocator()); |
| 412 | BOOST_CHECK_EQUAL(interest4.matchesData(data4), true); |
| 413 | |
| 414 | Data data5 = data; |
| 415 | data5.setName("ndn:/A/J"); // violates Exclude |
| 416 | data5.wireEncode(); |
| 417 | BOOST_CHECK_EQUAL(interest.matchesData(data5), false); |
| 418 | |
| 419 | Interest interest5 = interest; |
| 420 | interest5.setExclude(Exclude().excludeAfter(name::Component("K"))); |
| 421 | BOOST_CHECK_EQUAL(interest5.matchesData(data5), true); |
| 422 | |
| 423 | Data data6 = data; |
| 424 | data6.setName("ndn:/H/I"); // violates Name |
| 425 | data6.wireEncode(); |
| 426 | BOOST_CHECK_EQUAL(interest.matchesData(data6), false); |
| 427 | |
| 428 | Data data7 = data; |
| 429 | data7.setName("ndn:/A/B"); |
| 430 | data7.wireEncode(); |
| 431 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 432 | Interest interest7("/A/B/sha256digest=54008e240a7eea2714a161dfddf0dd6ced223b3856e9da96792151e180f3b128"); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 433 | BOOST_CHECK_EQUAL(interest7.matchesData(data7), true); |
| 434 | |
| 435 | Interest interest7b("/A/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"); |
| 436 | BOOST_CHECK_EQUAL(interest7b.matchesData(data7), false); // violates implicit digest |
| 437 | } |
| 438 | |
| 439 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1) |
| 440 | BOOST_AUTO_TEST_CASE(MatchesInterest) |
| 441 | { |
| 442 | Interest interest; |
| 443 | interest |
| 444 | .setName("/A") |
| 445 | .setMinSuffixComponents(2) |
| 446 | .setMaxSuffixComponents(2) |
| 447 | .setPublisherPublicKeyLocator(KeyLocator("/B")) |
| 448 | .setExclude(Exclude().excludeAfter(name::Component("J"))) |
| 449 | .setNonce(10) |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 450 | .setInterestLifetime(5_s) |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame] | 451 | .setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 452 | |
| 453 | Interest other; |
| 454 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 455 | |
| 456 | other.setName(interest.getName()); |
| 457 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 458 | |
| 459 | other.setSelectors(interest.getSelectors()); |
| 460 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented |
| 461 | |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame] | 462 | other.setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 463 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 464 | |
| 465 | other.setNonce(200); |
| 466 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 467 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 468 | other.setInterestLifetime(5_h); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 469 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | // ---- field accessors ---- |
| 473 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 474 | BOOST_AUTO_TEST_CASE(CanBePrefix) |
| 475 | { |
| 476 | Interest i; |
| 477 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 478 | i.setCanBePrefix(false); |
| 479 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), false); |
| 480 | BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), 1); |
| 481 | i.setCanBePrefix(true); |
| 482 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 483 | BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), -1); |
| 484 | } |
| 485 | |
| 486 | BOOST_AUTO_TEST_CASE(MustBeFresh) |
| 487 | { |
| 488 | Interest i; |
| 489 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 490 | i.setMustBeFresh(true); |
| 491 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), true); |
| 492 | BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), true); |
| 493 | i.setMustBeFresh(false); |
| 494 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 495 | BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), false); |
| 496 | } |
| 497 | |
| 498 | BOOST_AUTO_TEST_CASE(ModifyForwardingHint) |
| 499 | { |
| 500 | Interest i; |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 501 | i.setCanBePrefix(false); |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 502 | i.setForwardingHint({{1, "/A"}}); |
| 503 | i.wireEncode(); |
| 504 | BOOST_CHECK(i.hasWire()); |
| 505 | |
| 506 | i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); }); |
| 507 | BOOST_CHECK(!i.hasWire()); |
| 508 | BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}})); |
| 509 | } |
| 510 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 511 | BOOST_AUTO_TEST_CASE(GetNonce) |
| 512 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 513 | unique_ptr<Interest> i1, i2; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 514 | |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 515 | // getNonce automatically assigns a random Nonce. |
| 516 | // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of |
| 517 | // same Nonces in a row. |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 518 | int nIterations = 0; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 519 | uint32_t nonce1 = 0, nonce2 = 0; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 520 | do { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 521 | i1 = make_unique<Interest>(); |
| 522 | nonce1 = i1->getNonce(); |
| 523 | i2 = make_unique<Interest>(); |
| 524 | nonce2 = i2->getNonce(); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 525 | } |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 526 | while (nonce1 == nonce2 && ++nIterations < 100); |
| 527 | BOOST_CHECK_NE(nonce1, nonce2); |
| 528 | BOOST_CHECK(i1->hasNonce()); |
| 529 | BOOST_CHECK(i2->hasNonce()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 530 | |
| 531 | // Once a Nonce is assigned, it should not change. |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 532 | BOOST_CHECK_EQUAL(i1->getNonce(), nonce1); |
| 533 | } |
| 534 | |
| 535 | BOOST_AUTO_TEST_CASE(SetNonce) |
| 536 | { |
| 537 | Interest i1("/A"); |
Junxiao Shi | b55e5d3 | 2018-07-18 13:32:00 -0600 | [diff] [blame] | 538 | i1.setCanBePrefix(false); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 539 | i1.setNonce(1); |
| 540 | i1.wireEncode(); |
| 541 | BOOST_CHECK_EQUAL(i1.getNonce(), 1); |
| 542 | |
| 543 | Interest i2(i1); |
| 544 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 545 | |
| 546 | i2.setNonce(2); |
| 547 | BOOST_CHECK_EQUAL(i2.getNonce(), 2); |
| 548 | 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] | 549 | } |
| 550 | |
| 551 | BOOST_AUTO_TEST_CASE(RefreshNonce) |
| 552 | { |
| 553 | Interest i; |
| 554 | BOOST_CHECK(!i.hasNonce()); |
| 555 | i.refreshNonce(); |
| 556 | BOOST_CHECK(!i.hasNonce()); |
| 557 | |
| 558 | i.setNonce(1); |
| 559 | BOOST_CHECK(i.hasNonce()); |
| 560 | i.refreshNonce(); |
| 561 | BOOST_CHECK(i.hasNonce()); |
| 562 | BOOST_CHECK_NE(i.getNonce(), 1); |
| 563 | } |
| 564 | |
| 565 | BOOST_AUTO_TEST_CASE(SetInterestLifetime) |
| 566 | { |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 567 | BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 568 | BOOST_CHECK_NO_THROW(Interest("/A", 0_ms)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 569 | |
| 570 | Interest i("/local/ndn/prefix"); |
| 571 | i.setNonce(1); |
| 572 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 573 | BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 574 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 575 | i.setInterestLifetime(0_ms); |
| 576 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms); |
| 577 | i.setInterestLifetime(1_ms); |
| 578 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 581 | BOOST_AUTO_TEST_CASE(SetParameters) |
| 582 | { |
| 583 | const uint8_t PARAMETERS1[] = {0xc1}; |
| 584 | const uint8_t PARAMETERS2[] = {0xc2}; |
| 585 | |
| 586 | Interest i; |
| 587 | BOOST_CHECK(!i.hasParameters()); |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 588 | i.setParameters("2400"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 589 | BOOST_CHECK(i.hasParameters()); |
| 590 | i.unsetParameters(); |
| 591 | BOOST_CHECK(!i.hasParameters()); |
| 592 | |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 593 | i.setParameters("2401C0"_block); // Block overload |
| 594 | BOOST_CHECK_EQUAL(i.getParameters(), "2401C0"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 595 | i.setParameters(PARAMETERS1, sizeof(PARAMETERS1)); // raw buffer overload |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 596 | BOOST_CHECK_EQUAL(i.getParameters(), "2401C1"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 597 | i.setParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2))); // ConstBufferPtr overload |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 598 | BOOST_CHECK_EQUAL(i.getParameters(), "2401C2"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 599 | i.setParameters("8001C1"_block); // Block of non-Parameters type |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 600 | BOOST_CHECK_EQUAL(i.getParameters(), "24038001C1"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 603 | // ---- operators ---- |
| 604 | |
| 605 | BOOST_AUTO_TEST_CASE(Equality) |
| 606 | { |
| 607 | Interest a; |
| 608 | Interest b; |
| 609 | |
| 610 | // if nonce is not set, it would be set to a random value |
| 611 | a.setNonce(1); |
| 612 | b.setNonce(1); |
| 613 | |
| 614 | BOOST_CHECK_EQUAL(a == b, true); |
| 615 | BOOST_CHECK_EQUAL(a != b, false); |
| 616 | |
| 617 | // compare Name |
| 618 | a.setName("/A"); |
| 619 | BOOST_CHECK_EQUAL(a == b, false); |
| 620 | BOOST_CHECK_EQUAL(a != b, true); |
| 621 | |
| 622 | b.setName("/B"); |
| 623 | BOOST_CHECK_EQUAL(a == b, false); |
| 624 | BOOST_CHECK_EQUAL(a != b, true); |
| 625 | |
| 626 | b.setName("/A"); |
| 627 | BOOST_CHECK_EQUAL(a == b, true); |
| 628 | BOOST_CHECK_EQUAL(a != b, false); |
| 629 | |
| 630 | // compare Selectors |
| 631 | a.setChildSelector(1); |
| 632 | BOOST_CHECK_EQUAL(a == b, false); |
| 633 | BOOST_CHECK_EQUAL(a != b, true); |
| 634 | |
| 635 | b.setChildSelector(1); |
| 636 | BOOST_CHECK_EQUAL(a == b, true); |
| 637 | BOOST_CHECK_EQUAL(a != b, false); |
| 638 | |
| 639 | // compare Nonce |
| 640 | a.setNonce(100); |
| 641 | BOOST_CHECK_EQUAL(a == b, false); |
| 642 | BOOST_CHECK_EQUAL(a != b, true); |
| 643 | |
| 644 | b.setNonce(100); |
| 645 | BOOST_CHECK_EQUAL(a == b, true); |
| 646 | BOOST_CHECK_EQUAL(a != b, false); |
| 647 | |
| 648 | // compare InterestLifetime |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 649 | a.setInterestLifetime(10_s); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 650 | BOOST_CHECK_EQUAL(a == b, false); |
| 651 | BOOST_CHECK_EQUAL(a != b, true); |
| 652 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 653 | b.setInterestLifetime(10_s); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 654 | BOOST_CHECK_EQUAL(a == b, true); |
| 655 | BOOST_CHECK_EQUAL(a != b, false); |
| 656 | |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame] | 657 | // compare ForwardingHint |
| 658 | a.setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 659 | BOOST_CHECK_EQUAL(a == b, false); |
| 660 | BOOST_CHECK_EQUAL(a != b, true); |
| 661 | |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame] | 662 | b.setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 663 | BOOST_CHECK_EQUAL(a == b, true); |
| 664 | BOOST_CHECK_EQUAL(a != b, false); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 665 | |
| 666 | // compare Parameters |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 667 | a.setParameters("2404C0C1C2C3"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 668 | BOOST_CHECK_EQUAL(a == b, false); |
| 669 | BOOST_CHECK_EQUAL(a != b, true); |
| 670 | |
Davide Pesavento | fccb2dc | 2019-02-09 01:02:35 -0500 | [diff] [blame^] | 671 | b.setParameters("2404C0C1C2C3"_block); |
Arthi Padmanabhan | b38664e | 2018-07-18 11:13:12 -0700 | [diff] [blame] | 672 | BOOST_CHECK_EQUAL(a == b, true); |
| 673 | BOOST_CHECK_EQUAL(a != b, false); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 676 | BOOST_AUTO_TEST_SUITE_END() // TestInterest |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 677 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 678 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 679 | } // namespace ndn |