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