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 | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 22 | #include "interest.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 23 | #include "data.hpp" |
Yingdi Yu | bf6a281 | 2014-06-17 15:32:11 -0700 | [diff] [blame] | 24 | #include "security/digest-sha256.hpp" |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 25 | #include "security/signature-sha256-with-rsa.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 26 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 27 | #include "block-literal.hpp" |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 28 | #include "boost-test.hpp" |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 29 | #include "identity-management-fixture.hpp" |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 31 | namespace ndn { |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 32 | namespace tests { |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 33 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(TestInterest) |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 35 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 36 | // ---- constructor, encode, decode ---- |
| 37 | |
| 38 | BOOST_AUTO_TEST_CASE(DefaultConstructor) |
| 39 | { |
| 40 | Interest i; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 41 | BOOST_CHECK(!i.hasWire()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 42 | BOOST_CHECK_EQUAL(i.getName(), "/"); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 43 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 44 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 45 | BOOST_CHECK(i.getForwardingHint().empty()); |
| 46 | BOOST_CHECK(!i.hasNonce()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 48 | BOOST_CHECK(!i.hasSelectors()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 51 | BOOST_AUTO_TEST_CASE(EncodeDecode02Basic) |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 52 | { |
| 53 | const uint8_t WIRE[] = { |
| 54 | 0x05, 0x1c, // Interest |
| 55 | 0x07, 0x14, // Name |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 56 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 57 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 58 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 59 | 0x0a, 0x04, // Nonce |
| 60 | 0x01, 0x00, 0x00, 0x00 |
| 61 | }; |
| 62 | |
| 63 | Interest i1("/local/ndn/prefix"); |
| 64 | i1.setNonce(1); |
| 65 | Block wire1 = i1.wireEncode(); |
| 66 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 67 | |
| 68 | Interest i2(wire1); |
| 69 | BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix"); |
| 70 | BOOST_CHECK(i2.getSelectors().empty()); |
| 71 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 72 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
| 73 | |
| 74 | BOOST_CHECK_EQUAL(i1, i2); |
| 75 | } |
| 76 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 77 | BOOST_AUTO_TEST_CASE(EncodeDecode02Full) |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 78 | { |
| 79 | const uint8_t WIRE[] = { |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 80 | 0x05, 0x31, // Interest |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 81 | 0x07, 0x14, // Name |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 82 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 83 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 84 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 85 | 0x09, 0x03, // Selectors |
| 86 | 0x0d, 0x01, 0x01, // MinSuffixComponents |
| 87 | 0x0a, 0x04, // Nonce |
| 88 | 0x01, 0x00, 0x00, 0x00, |
| 89 | 0x0c, 0x02, // InterestLifetime |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 90 | 0x03, 0xe8, |
| 91 | 0x1e, 0x0a, // ForwardingHint |
| 92 | 0x1f, 0x08, // Delegation |
| 93 | 0x1e, 0x01, 0x01, // Preference=1 |
| 94 | 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 95 | }; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 96 | |
| 97 | Interest i1; |
| 98 | i1.setName("/local/ndn/prefix"); |
| 99 | i1.setMinSuffixComponents(1); |
| 100 | i1.setNonce(1); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 101 | i1.setInterestLifetime(1000_ms); |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 102 | i1.setForwardingHint({{1, "/A"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 103 | Block wire1 = i1.wireEncode(); |
| 104 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 105 | |
| 106 | Interest i2(wire1); |
| 107 | BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix"); |
| 108 | BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 1); |
| 109 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 1000_ms); |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 111 | BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{1, "/A"}})); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 112 | |
| 113 | BOOST_CHECK_EQUAL(i1, i2); |
| 114 | } |
| 115 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 116 | class Decode03Fixture |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 117 | { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 118 | protected: |
| 119 | Decode03Fixture() |
| 120 | { |
| 121 | // initialize all elements to non-empty, to verify wireDecode clears them |
| 122 | i.setName("/A"); |
| 123 | i.setForwardingHint({{10309, "/F"}}); |
| 124 | i.setNonce(0x03d645a8); |
| 125 | i.setInterestLifetime(18554_ms); |
| 126 | i.setPublisherPublicKeyLocator(Name("/K")); |
| 127 | } |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 128 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 129 | protected: |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 130 | Interest i; |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 131 | }; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 132 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 133 | BOOST_FIXTURE_TEST_SUITE(Decode03, Decode03Fixture) |
| 134 | |
| 135 | BOOST_AUTO_TEST_CASE(Minimal) |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 136 | { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 137 | i.wireDecode("0505 0703080149"_block); |
| 138 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 139 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), false); |
| 140 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 141 | BOOST_CHECK(i.getForwardingHint().empty()); |
| 142 | BOOST_CHECK(i.hasNonce()); // a random nonce is generated |
| 143 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
| 144 | BOOST_CHECK(i.getPublisherPublicKeyLocator().empty()); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 145 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 146 | BOOST_CHECK(!i.hasWire()); // nonce generation resets wire encoding |
| 147 | |
| 148 | // modify then re-encode as v0.2 format |
| 149 | i.setNonce(0x54657c95); |
| 150 | BOOST_CHECK(i.wireEncode() == "0510 0703080149 09030E0101 0A04957C6554"_block); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 153 | BOOST_AUTO_TEST_CASE(Full) |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 154 | { |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 155 | i.wireDecode("053B FC00 0703080149 FC00 2100 FC00 1200 " |
| 156 | "FC00 1E0B(1F09 1E023E15 0703080148) FC00 0A044ACB1E4C " |
| 157 | "FC00 0C0276A1 FC00 2201D6 FC00 2304C0C1C2C3 FC00"_block); |
| 158 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 159 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 160 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), true); |
| 161 | BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{15893, "/H"}})); |
| 162 | BOOST_CHECK(i.hasNonce()); |
| 163 | BOOST_CHECK_EQUAL(i.getNonce(), 0x4c1ecb4a); |
| 164 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 30369_ms); |
| 165 | // HopLimit=214 is not stored |
| 166 | // Parameters="C0C1C2C3" is not stored |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 167 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 168 | // encode without modification: retain original wire encoding |
| 169 | BOOST_CHECK_EQUAL(i.wireEncode().value_size(), 59); |
| 170 | |
| 171 | // modify then re-encode as v0.2 format |
| 172 | i.setName("/J"); |
| 173 | BOOST_CHECK(i.wireEncode() == |
| 174 | "0520 070308014A 09021200 0A044ACB1E4C 0C0276A1 " |
| 175 | "1E0B(1F09 1E023E15 0703080148)"_block); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Junxiao Shi | 6efa3b7 | 2018-04-14 15:54:08 +0000 | [diff] [blame^] | 178 | BOOST_AUTO_TEST_CASE(CriticalElementOutOfOrder) |
| 179 | { |
| 180 | BOOST_CHECK_THROW(i.wireDecode( |
| 181 | "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) " |
| 182 | "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3"_block), |
| 183 | tlv::Error); |
| 184 | BOOST_CHECK_THROW(i.wireDecode( |
| 185 | "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) " |
| 186 | "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3"_block), |
| 187 | tlv::Error); |
| 188 | BOOST_CHECK_THROW(i.wireDecode( |
| 189 | "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 " |
| 190 | "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3"_block), |
| 191 | tlv::Error); |
| 192 | BOOST_CHECK_THROW(i.wireDecode( |
| 193 | "0529 0703080149 2100 1200 0A044ACB1E4C " |
| 194 | "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2304C0C1C2C3"_block), |
| 195 | tlv::Error); |
| 196 | BOOST_CHECK_THROW(i.wireDecode( |
| 197 | "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) " |
| 198 | "0C0276A1 0A044ACB1E4C 2201D6 2304C0C1C2C3"_block), |
| 199 | tlv::Error); |
| 200 | BOOST_CHECK_THROW(i.wireDecode( |
| 201 | "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) " |
| 202 | "0A044ACB1E4C 2201D6 0C0276A1 2304C0C1C2C3"_block), |
| 203 | tlv::Error); |
| 204 | BOOST_CHECK_THROW(i.wireDecode( |
| 205 | "052F 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) " |
| 206 | "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3 2304C0C1C2C3"_block), |
| 207 | tlv::Error); |
| 208 | } |
| 209 | |
| 210 | BOOST_AUTO_TEST_CASE(HopLimitOutOfOrder) |
| 211 | { |
| 212 | // HopLimit is non-critical, its out-of-order appearances are ignored |
| 213 | i.wireDecode("0514 0703080149 2201D6 2200 2304C0C1C2C3 22020101"_block); |
| 214 | BOOST_CHECK_EQUAL(i.getName(), "/I"); |
| 215 | // HopLimit=214 is not stored |
| 216 | // Parameters="C0C1C2C3" is not stored |
| 217 | } |
| 218 | |
| 219 | BOOST_AUTO_TEST_CASE(NameMissing) |
| 220 | { |
| 221 | BOOST_CHECK_THROW(i.wireDecode("0500"_block), tlv::Error); |
| 222 | BOOST_CHECK_THROW(i.wireDecode("0502 1200"_block), tlv::Error); |
| 223 | } |
| 224 | |
| 225 | BOOST_AUTO_TEST_CASE(NameEmpty) |
| 226 | { |
| 227 | BOOST_CHECK_THROW(i.wireDecode("0502 0700"_block), tlv::Error); |
| 228 | } |
| 229 | |
| 230 | BOOST_AUTO_TEST_CASE(BadCanBePrefix) |
| 231 | { |
| 232 | BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 210102"_block), tlv::Error); |
| 233 | } |
| 234 | |
| 235 | BOOST_AUTO_TEST_CASE(BadMustBeFresh) |
| 236 | { |
| 237 | BOOST_CHECK_THROW(i.wireDecode("0508 0703080149 120102"_block), tlv::Error); |
| 238 | } |
| 239 | |
| 240 | BOOST_AUTO_TEST_CASE(BadNonce) |
| 241 | { |
| 242 | BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 0A00"_block), tlv::Error); |
| 243 | BOOST_CHECK_THROW(i.wireDecode("050A 0703080149 0A0304C263"_block), tlv::Error); |
| 244 | BOOST_CHECK_THROW(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error); |
| 245 | } |
| 246 | |
| 247 | BOOST_AUTO_TEST_CASE(UnrecognizedCriticalElement) |
| 248 | { |
| 249 | BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 FB00"_block), tlv::Error); |
| 250 | } |
| 251 | |
| 252 | BOOST_AUTO_TEST_SUITE_END() // Decode03 |
| 253 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 254 | // ---- matching ---- |
| 255 | |
| 256 | BOOST_AUTO_TEST_CASE(MatchesData) |
| 257 | { |
| 258 | Interest interest; |
| 259 | interest.setName("ndn:/A") |
| 260 | .setMinSuffixComponents(2) |
| 261 | .setMaxSuffixComponents(2) |
| 262 | .setPublisherPublicKeyLocator(KeyLocator("ndn:/B")) |
| 263 | .setExclude(Exclude().excludeAfter(name::Component("J"))); |
| 264 | |
| 265 | Data data("ndn:/A/D"); |
| 266 | SignatureSha256WithRsa signature(KeyLocator("ndn:/B")); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 267 | signature.setValue(encoding::makeEmptyBlock(tlv::SignatureValue)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 268 | data.setSignature(signature); |
| 269 | data.wireEncode(); |
| 270 | BOOST_CHECK_EQUAL(interest.matchesData(data), true); |
| 271 | |
| 272 | Data data1 = data; |
| 273 | data1.setName("ndn:/A"); // violates MinSuffixComponents |
| 274 | data1.wireEncode(); |
| 275 | BOOST_CHECK_EQUAL(interest.matchesData(data1), false); |
| 276 | |
| 277 | Interest interest1 = interest; |
| 278 | interest1.setMinSuffixComponents(1); |
| 279 | BOOST_CHECK_EQUAL(interest1.matchesData(data1), true); |
| 280 | |
| 281 | Data data2 = data; |
| 282 | data2.setName("ndn:/A/E/F"); // violates MaxSuffixComponents |
| 283 | data2.wireEncode(); |
| 284 | BOOST_CHECK_EQUAL(interest.matchesData(data2), false); |
| 285 | |
| 286 | Interest interest2 = interest; |
| 287 | interest2.setMaxSuffixComponents(3); |
| 288 | BOOST_CHECK_EQUAL(interest2.matchesData(data2), true); |
| 289 | |
| 290 | Data data3 = data; |
| 291 | SignatureSha256WithRsa signature3(KeyLocator("ndn:/G")); // violates PublisherPublicKeyLocator |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 292 | signature3.setValue(encoding::makeEmptyBlock(tlv::SignatureValue)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 293 | data3.setSignature(signature3); |
| 294 | data3.wireEncode(); |
| 295 | BOOST_CHECK_EQUAL(interest.matchesData(data3), false); |
| 296 | |
| 297 | Interest interest3 = interest; |
| 298 | interest3.setPublisherPublicKeyLocator(KeyLocator("ndn:/G")); |
| 299 | BOOST_CHECK_EQUAL(interest3.matchesData(data3), true); |
| 300 | |
| 301 | Data data4 = data; |
| 302 | DigestSha256 signature4; // violates PublisherPublicKeyLocator |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 303 | signature4.setValue(encoding::makeEmptyBlock(tlv::SignatureValue)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 304 | data4.setSignature(signature4); |
| 305 | data4.wireEncode(); |
| 306 | BOOST_CHECK_EQUAL(interest.matchesData(data4), false); |
| 307 | |
| 308 | Interest interest4 = interest; |
| 309 | interest4.setPublisherPublicKeyLocator(KeyLocator()); |
| 310 | BOOST_CHECK_EQUAL(interest4.matchesData(data4), true); |
| 311 | |
| 312 | Data data5 = data; |
| 313 | data5.setName("ndn:/A/J"); // violates Exclude |
| 314 | data5.wireEncode(); |
| 315 | BOOST_CHECK_EQUAL(interest.matchesData(data5), false); |
| 316 | |
| 317 | Interest interest5 = interest; |
| 318 | interest5.setExclude(Exclude().excludeAfter(name::Component("K"))); |
| 319 | BOOST_CHECK_EQUAL(interest5.matchesData(data5), true); |
| 320 | |
| 321 | Data data6 = data; |
| 322 | data6.setName("ndn:/H/I"); // violates Name |
| 323 | data6.wireEncode(); |
| 324 | BOOST_CHECK_EQUAL(interest.matchesData(data6), false); |
| 325 | |
| 326 | Data data7 = data; |
| 327 | data7.setName("ndn:/A/B"); |
| 328 | data7.wireEncode(); |
| 329 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 330 | Interest interest7("/A/B/sha256digest=54008e240a7eea2714a161dfddf0dd6ced223b3856e9da96792151e180f3b128"); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 331 | BOOST_CHECK_EQUAL(interest7.matchesData(data7), true); |
| 332 | |
| 333 | Interest interest7b("/A/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"); |
| 334 | BOOST_CHECK_EQUAL(interest7b.matchesData(data7), false); // violates implicit digest |
| 335 | } |
| 336 | |
| 337 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1) |
| 338 | BOOST_AUTO_TEST_CASE(MatchesInterest) |
| 339 | { |
| 340 | Interest interest; |
| 341 | interest |
| 342 | .setName("/A") |
| 343 | .setMinSuffixComponents(2) |
| 344 | .setMaxSuffixComponents(2) |
| 345 | .setPublisherPublicKeyLocator(KeyLocator("/B")) |
| 346 | .setExclude(Exclude().excludeAfter(name::Component("J"))) |
| 347 | .setNonce(10) |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 348 | .setInterestLifetime(5_s) |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame] | 349 | .setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 350 | |
| 351 | Interest other; |
| 352 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 353 | |
| 354 | other.setName(interest.getName()); |
| 355 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 356 | |
| 357 | other.setSelectors(interest.getSelectors()); |
| 358 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented |
| 359 | |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame] | 360 | other.setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 361 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 362 | |
| 363 | other.setNonce(200); |
| 364 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 365 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 366 | other.setInterestLifetime(5_h); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 367 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | // ---- field accessors ---- |
| 371 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 372 | BOOST_AUTO_TEST_CASE(CanBePrefix) |
| 373 | { |
| 374 | Interest i; |
| 375 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 376 | i.setCanBePrefix(false); |
| 377 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), false); |
| 378 | BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), 1); |
| 379 | i.setCanBePrefix(true); |
| 380 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 381 | BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), -1); |
| 382 | } |
| 383 | |
| 384 | BOOST_AUTO_TEST_CASE(MustBeFresh) |
| 385 | { |
| 386 | Interest i; |
| 387 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 388 | i.setMustBeFresh(true); |
| 389 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), true); |
| 390 | BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), true); |
| 391 | i.setMustBeFresh(false); |
| 392 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 393 | BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), false); |
| 394 | } |
| 395 | |
| 396 | BOOST_AUTO_TEST_CASE(ModifyForwardingHint) |
| 397 | { |
| 398 | Interest i; |
| 399 | i.setForwardingHint({{1, "/A"}}); |
| 400 | i.wireEncode(); |
| 401 | BOOST_CHECK(i.hasWire()); |
| 402 | |
| 403 | i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); }); |
| 404 | BOOST_CHECK(!i.hasWire()); |
| 405 | BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}})); |
| 406 | } |
| 407 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 408 | BOOST_AUTO_TEST_CASE(GetNonce) |
| 409 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 410 | unique_ptr<Interest> i1, i2; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 411 | |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 412 | // getNonce automatically assigns a random Nonce. |
| 413 | // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of |
| 414 | // same Nonces in a row. |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 415 | int nIterations = 0; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 416 | uint32_t nonce1 = 0, nonce2 = 0; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 417 | do { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 418 | i1 = make_unique<Interest>(); |
| 419 | nonce1 = i1->getNonce(); |
| 420 | i2 = make_unique<Interest>(); |
| 421 | nonce2 = i2->getNonce(); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 422 | } |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 423 | while (nonce1 == nonce2 && ++nIterations < 100); |
| 424 | BOOST_CHECK_NE(nonce1, nonce2); |
| 425 | BOOST_CHECK(i1->hasNonce()); |
| 426 | BOOST_CHECK(i2->hasNonce()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 427 | |
| 428 | // Once a Nonce is assigned, it should not change. |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 429 | BOOST_CHECK_EQUAL(i1->getNonce(), nonce1); |
| 430 | } |
| 431 | |
| 432 | BOOST_AUTO_TEST_CASE(SetNonce) |
| 433 | { |
| 434 | Interest i1("/A"); |
| 435 | i1.setNonce(1); |
| 436 | i1.wireEncode(); |
| 437 | BOOST_CHECK_EQUAL(i1.getNonce(), 1); |
| 438 | |
| 439 | Interest i2(i1); |
| 440 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 441 | |
| 442 | i2.setNonce(2); |
| 443 | BOOST_CHECK_EQUAL(i2.getNonce(), 2); |
| 444 | 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] | 445 | } |
| 446 | |
| 447 | BOOST_AUTO_TEST_CASE(RefreshNonce) |
| 448 | { |
| 449 | Interest i; |
| 450 | BOOST_CHECK(!i.hasNonce()); |
| 451 | i.refreshNonce(); |
| 452 | BOOST_CHECK(!i.hasNonce()); |
| 453 | |
| 454 | i.setNonce(1); |
| 455 | BOOST_CHECK(i.hasNonce()); |
| 456 | i.refreshNonce(); |
| 457 | BOOST_CHECK(i.hasNonce()); |
| 458 | BOOST_CHECK_NE(i.getNonce(), 1); |
| 459 | } |
| 460 | |
| 461 | BOOST_AUTO_TEST_CASE(SetInterestLifetime) |
| 462 | { |
| 463 | BOOST_CHECK_THROW(Interest("/A", time::milliseconds(-1)), std::invalid_argument); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 464 | BOOST_CHECK_NO_THROW(Interest("/A", 0_ms)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 465 | |
| 466 | Interest i("/local/ndn/prefix"); |
| 467 | i.setNonce(1); |
| 468 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
| 469 | BOOST_CHECK_THROW(i.setInterestLifetime(time::milliseconds(-1)), std::invalid_argument); |
| 470 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 471 | i.setInterestLifetime(0_ms); |
| 472 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms); |
| 473 | i.setInterestLifetime(1_ms); |
| 474 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | // ---- operators ---- |
| 478 | |
| 479 | BOOST_AUTO_TEST_CASE(Equality) |
| 480 | { |
| 481 | Interest a; |
| 482 | Interest b; |
| 483 | |
| 484 | // if nonce is not set, it would be set to a random value |
| 485 | a.setNonce(1); |
| 486 | b.setNonce(1); |
| 487 | |
| 488 | BOOST_CHECK_EQUAL(a == b, true); |
| 489 | BOOST_CHECK_EQUAL(a != b, false); |
| 490 | |
| 491 | // compare Name |
| 492 | a.setName("/A"); |
| 493 | BOOST_CHECK_EQUAL(a == b, false); |
| 494 | BOOST_CHECK_EQUAL(a != b, true); |
| 495 | |
| 496 | b.setName("/B"); |
| 497 | BOOST_CHECK_EQUAL(a == b, false); |
| 498 | BOOST_CHECK_EQUAL(a != b, true); |
| 499 | |
| 500 | b.setName("/A"); |
| 501 | BOOST_CHECK_EQUAL(a == b, true); |
| 502 | BOOST_CHECK_EQUAL(a != b, false); |
| 503 | |
| 504 | // compare Selectors |
| 505 | a.setChildSelector(1); |
| 506 | BOOST_CHECK_EQUAL(a == b, false); |
| 507 | BOOST_CHECK_EQUAL(a != b, true); |
| 508 | |
| 509 | b.setChildSelector(1); |
| 510 | BOOST_CHECK_EQUAL(a == b, true); |
| 511 | BOOST_CHECK_EQUAL(a != b, false); |
| 512 | |
| 513 | // compare Nonce |
| 514 | a.setNonce(100); |
| 515 | BOOST_CHECK_EQUAL(a == b, false); |
| 516 | BOOST_CHECK_EQUAL(a != b, true); |
| 517 | |
| 518 | b.setNonce(100); |
| 519 | BOOST_CHECK_EQUAL(a == b, true); |
| 520 | BOOST_CHECK_EQUAL(a != b, false); |
| 521 | |
| 522 | // compare InterestLifetime |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 523 | a.setInterestLifetime(10_s); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 524 | BOOST_CHECK_EQUAL(a == b, false); |
| 525 | BOOST_CHECK_EQUAL(a != b, true); |
| 526 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 527 | b.setInterestLifetime(10_s); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 528 | BOOST_CHECK_EQUAL(a == b, true); |
| 529 | BOOST_CHECK_EQUAL(a != b, false); |
| 530 | |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame] | 531 | // compare ForwardingHint |
| 532 | a.setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 533 | BOOST_CHECK_EQUAL(a == b, false); |
| 534 | BOOST_CHECK_EQUAL(a != b, true); |
| 535 | |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame] | 536 | b.setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 537 | BOOST_CHECK_EQUAL(a == b, true); |
| 538 | BOOST_CHECK_EQUAL(a != b, false); |
| 539 | } |
| 540 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 541 | BOOST_AUTO_TEST_SUITE_END() // TestInterest |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 542 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 543 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 544 | } // namespace ndn |