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 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 27 | #include "boost-test.hpp" |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 28 | #include "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; |
| 40 | BOOST_CHECK_EQUAL(i.getName(), "/"); |
| 41 | BOOST_CHECK(i.getSelectors().empty()); |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 42 | BOOST_CHECK_EQUAL(i.hasNonce(), false); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 43 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | BOOST_AUTO_TEST_CASE(EncodeDecodeBasic) |
| 47 | { |
| 48 | const uint8_t WIRE[] = { |
| 49 | 0x05, 0x1c, // Interest |
| 50 | 0x07, 0x14, // Name |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 51 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 52 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 53 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 54 | 0x0a, 0x04, // Nonce |
| 55 | 0x01, 0x00, 0x00, 0x00 |
| 56 | }; |
| 57 | |
| 58 | Interest i1("/local/ndn/prefix"); |
| 59 | i1.setNonce(1); |
| 60 | Block wire1 = i1.wireEncode(); |
| 61 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 62 | |
| 63 | Interest i2(wire1); |
| 64 | BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix"); |
| 65 | BOOST_CHECK(i2.getSelectors().empty()); |
| 66 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 67 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
| 68 | |
| 69 | BOOST_CHECK_EQUAL(i1, i2); |
| 70 | } |
| 71 | |
| 72 | BOOST_AUTO_TEST_CASE(EncodeDecodeFull) |
| 73 | { |
| 74 | const uint8_t WIRE[] = { |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 75 | 0x05, 0x31, // Interest |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 76 | 0x07, 0x14, // Name |
Junxiao Shi | 4ffbb9d | 2018-03-31 17:16:35 +0000 | [diff] [blame] | 77 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // GenericNameComponent |
| 78 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // GenericNameComponent |
| 79 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 80 | 0x09, 0x03, // Selectors |
| 81 | 0x0d, 0x01, 0x01, // MinSuffixComponents |
| 82 | 0x0a, 0x04, // Nonce |
| 83 | 0x01, 0x00, 0x00, 0x00, |
| 84 | 0x0c, 0x02, // InterestLifetime |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 85 | 0x03, 0xe8, |
| 86 | 0x1e, 0x0a, // ForwardingHint |
| 87 | 0x1f, 0x08, // Delegation |
| 88 | 0x1e, 0x01, 0x01, // Preference=1 |
| 89 | 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 90 | }; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 91 | |
| 92 | Interest i1; |
| 93 | i1.setName("/local/ndn/prefix"); |
| 94 | i1.setMinSuffixComponents(1); |
| 95 | i1.setNonce(1); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 96 | i1.setInterestLifetime(1000_ms); |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 97 | i1.setForwardingHint({{1, "/A"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 98 | Block wire1 = i1.wireEncode(); |
| 99 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 100 | |
| 101 | Interest i2(wire1); |
| 102 | BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix"); |
| 103 | BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 1); |
| 104 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 105 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 1000_ms); |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 106 | BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{1, "/A"}})); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 107 | |
| 108 | BOOST_CHECK_EQUAL(i1, i2); |
| 109 | } |
| 110 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 111 | BOOST_AUTO_TEST_CASE(WireDecodeReset) // checks wireDecode resets all fields |
| 112 | { |
| 113 | Interest i1; |
| 114 | i1.setName("/test"); |
| 115 | i1.setMinSuffixComponents(100); |
| 116 | i1.setNonce(10); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 117 | i1.setInterestLifetime(10_s); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 118 | |
| 119 | Interest i2(i1.wireEncode()); |
| 120 | BOOST_CHECK_EQUAL(i2.getName().toUri(), "/test"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 121 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 10_s); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 100); |
| 123 | BOOST_CHECK_EQUAL(i2.getNonce(), 10); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 124 | |
| 125 | i2.wireDecode(Interest().wireEncode()); |
| 126 | BOOST_CHECK_EQUAL(i2.getName().toUri(), "/"); |
| 127 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
| 128 | BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), -1); |
| 129 | BOOST_WARN_NE(i2.getNonce(), 10); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 132 | BOOST_AUTO_TEST_CASE(DecodeNoName) |
| 133 | { |
| 134 | Block b(tlv::Interest); |
| 135 | b.push_back(makeBinaryBlock(tlv::Nonce, "FISH", 4)); |
| 136 | b.encode(); |
| 137 | |
| 138 | Interest i; |
| 139 | BOOST_CHECK_THROW(i.wireDecode(b), tlv::Error); |
| 140 | } |
| 141 | |
| 142 | BOOST_AUTO_TEST_CASE(DecodeNoNonce) |
| 143 | { |
| 144 | Block b(tlv::Interest); |
| 145 | b.push_back(Name("/YvzNKtPWh").wireEncode()); |
| 146 | b.encode(); |
| 147 | |
| 148 | Interest i; |
| 149 | BOOST_CHECK_THROW(i.wireDecode(b), tlv::Error); |
| 150 | } |
| 151 | |
| 152 | BOOST_AUTO_TEST_CASE(DecodeBadNonce) |
| 153 | { |
| 154 | Block b(tlv::Interest); |
| 155 | b.push_back(Name("/BJzEHVxDJ").wireEncode()); |
| 156 | b.push_back(makeBinaryBlock(tlv::Nonce, "SKY", 3)); |
| 157 | b.encode(); |
| 158 | |
| 159 | Interest i; |
| 160 | BOOST_CHECK_THROW(i.wireDecode(b), tlv::Error); |
| 161 | } |
| 162 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 163 | // ---- matching ---- |
| 164 | |
| 165 | BOOST_AUTO_TEST_CASE(MatchesData) |
| 166 | { |
| 167 | Interest interest; |
| 168 | interest.setName("ndn:/A") |
| 169 | .setMinSuffixComponents(2) |
| 170 | .setMaxSuffixComponents(2) |
| 171 | .setPublisherPublicKeyLocator(KeyLocator("ndn:/B")) |
| 172 | .setExclude(Exclude().excludeAfter(name::Component("J"))); |
| 173 | |
| 174 | Data data("ndn:/A/D"); |
| 175 | SignatureSha256WithRsa signature(KeyLocator("ndn:/B")); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 176 | signature.setValue(encoding::makeEmptyBlock(tlv::SignatureValue)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 177 | data.setSignature(signature); |
| 178 | data.wireEncode(); |
| 179 | BOOST_CHECK_EQUAL(interest.matchesData(data), true); |
| 180 | |
| 181 | Data data1 = data; |
| 182 | data1.setName("ndn:/A"); // violates MinSuffixComponents |
| 183 | data1.wireEncode(); |
| 184 | BOOST_CHECK_EQUAL(interest.matchesData(data1), false); |
| 185 | |
| 186 | Interest interest1 = interest; |
| 187 | interest1.setMinSuffixComponents(1); |
| 188 | BOOST_CHECK_EQUAL(interest1.matchesData(data1), true); |
| 189 | |
| 190 | Data data2 = data; |
| 191 | data2.setName("ndn:/A/E/F"); // violates MaxSuffixComponents |
| 192 | data2.wireEncode(); |
| 193 | BOOST_CHECK_EQUAL(interest.matchesData(data2), false); |
| 194 | |
| 195 | Interest interest2 = interest; |
| 196 | interest2.setMaxSuffixComponents(3); |
| 197 | BOOST_CHECK_EQUAL(interest2.matchesData(data2), true); |
| 198 | |
| 199 | Data data3 = data; |
| 200 | SignatureSha256WithRsa signature3(KeyLocator("ndn:/G")); // violates PublisherPublicKeyLocator |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 201 | signature3.setValue(encoding::makeEmptyBlock(tlv::SignatureValue)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 202 | data3.setSignature(signature3); |
| 203 | data3.wireEncode(); |
| 204 | BOOST_CHECK_EQUAL(interest.matchesData(data3), false); |
| 205 | |
| 206 | Interest interest3 = interest; |
| 207 | interest3.setPublisherPublicKeyLocator(KeyLocator("ndn:/G")); |
| 208 | BOOST_CHECK_EQUAL(interest3.matchesData(data3), true); |
| 209 | |
| 210 | Data data4 = data; |
| 211 | DigestSha256 signature4; // violates PublisherPublicKeyLocator |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 212 | signature4.setValue(encoding::makeEmptyBlock(tlv::SignatureValue)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 213 | data4.setSignature(signature4); |
| 214 | data4.wireEncode(); |
| 215 | BOOST_CHECK_EQUAL(interest.matchesData(data4), false); |
| 216 | |
| 217 | Interest interest4 = interest; |
| 218 | interest4.setPublisherPublicKeyLocator(KeyLocator()); |
| 219 | BOOST_CHECK_EQUAL(interest4.matchesData(data4), true); |
| 220 | |
| 221 | Data data5 = data; |
| 222 | data5.setName("ndn:/A/J"); // violates Exclude |
| 223 | data5.wireEncode(); |
| 224 | BOOST_CHECK_EQUAL(interest.matchesData(data5), false); |
| 225 | |
| 226 | Interest interest5 = interest; |
| 227 | interest5.setExclude(Exclude().excludeAfter(name::Component("K"))); |
| 228 | BOOST_CHECK_EQUAL(interest5.matchesData(data5), true); |
| 229 | |
| 230 | Data data6 = data; |
| 231 | data6.setName("ndn:/H/I"); // violates Name |
| 232 | data6.wireEncode(); |
| 233 | BOOST_CHECK_EQUAL(interest.matchesData(data6), false); |
| 234 | |
| 235 | Data data7 = data; |
| 236 | data7.setName("ndn:/A/B"); |
| 237 | data7.wireEncode(); |
| 238 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 239 | Interest interest7("/A/B/sha256digest=54008e240a7eea2714a161dfddf0dd6ced223b3856e9da96792151e180f3b128"); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 240 | BOOST_CHECK_EQUAL(interest7.matchesData(data7), true); |
| 241 | |
| 242 | Interest interest7b("/A/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"); |
| 243 | BOOST_CHECK_EQUAL(interest7b.matchesData(data7), false); // violates implicit digest |
| 244 | } |
| 245 | |
| 246 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1) |
| 247 | BOOST_AUTO_TEST_CASE(MatchesInterest) |
| 248 | { |
| 249 | Interest interest; |
| 250 | interest |
| 251 | .setName("/A") |
| 252 | .setMinSuffixComponents(2) |
| 253 | .setMaxSuffixComponents(2) |
| 254 | .setPublisherPublicKeyLocator(KeyLocator("/B")) |
| 255 | .setExclude(Exclude().excludeAfter(name::Component("J"))) |
| 256 | .setNonce(10) |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 257 | .setInterestLifetime(5_s) |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame] | 258 | .setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 259 | |
| 260 | Interest other; |
| 261 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 262 | |
| 263 | other.setName(interest.getName()); |
| 264 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 265 | |
| 266 | other.setSelectors(interest.getSelectors()); |
| 267 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented |
| 268 | |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame] | 269 | other.setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 270 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 271 | |
| 272 | other.setNonce(200); |
| 273 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 274 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 275 | other.setInterestLifetime(5_h); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 276 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // ---- field accessors ---- |
| 280 | |
Junxiao Shi | 8d3f834 | 2018-04-04 12:46:37 +0000 | [diff] [blame] | 281 | BOOST_AUTO_TEST_CASE(CanBePrefix) |
| 282 | { |
| 283 | Interest i; |
| 284 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 285 | i.setCanBePrefix(false); |
| 286 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), false); |
| 287 | BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), 1); |
| 288 | i.setCanBePrefix(true); |
| 289 | BOOST_CHECK_EQUAL(i.getCanBePrefix(), true); |
| 290 | BOOST_CHECK_EQUAL(i.getSelectors().getMaxSuffixComponents(), -1); |
| 291 | } |
| 292 | |
| 293 | BOOST_AUTO_TEST_CASE(MustBeFresh) |
| 294 | { |
| 295 | Interest i; |
| 296 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 297 | i.setMustBeFresh(true); |
| 298 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), true); |
| 299 | BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), true); |
| 300 | i.setMustBeFresh(false); |
| 301 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 302 | BOOST_CHECK_EQUAL(i.getSelectors().getMustBeFresh(), false); |
| 303 | } |
| 304 | |
| 305 | BOOST_AUTO_TEST_CASE(ModifyForwardingHint) |
| 306 | { |
| 307 | Interest i; |
| 308 | i.setForwardingHint({{1, "/A"}}); |
| 309 | i.wireEncode(); |
| 310 | BOOST_CHECK(i.hasWire()); |
| 311 | |
| 312 | i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); }); |
| 313 | BOOST_CHECK(!i.hasWire()); |
| 314 | BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}})); |
| 315 | } |
| 316 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 317 | BOOST_AUTO_TEST_CASE(GetNonce) |
| 318 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 319 | unique_ptr<Interest> i1, i2; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 320 | |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 321 | // getNonce automatically assigns a random Nonce. |
| 322 | // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of |
| 323 | // same Nonces in a row. |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 324 | int nIterations = 0; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 325 | uint32_t nonce1 = 0, nonce2 = 0; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 326 | do { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 327 | i1 = make_unique<Interest>(); |
| 328 | nonce1 = i1->getNonce(); |
| 329 | i2 = make_unique<Interest>(); |
| 330 | nonce2 = i2->getNonce(); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 331 | } |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 332 | while (nonce1 == nonce2 && ++nIterations < 100); |
| 333 | BOOST_CHECK_NE(nonce1, nonce2); |
| 334 | BOOST_CHECK(i1->hasNonce()); |
| 335 | BOOST_CHECK(i2->hasNonce()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 336 | |
| 337 | // Once a Nonce is assigned, it should not change. |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 338 | BOOST_CHECK_EQUAL(i1->getNonce(), nonce1); |
| 339 | } |
| 340 | |
| 341 | BOOST_AUTO_TEST_CASE(SetNonce) |
| 342 | { |
| 343 | Interest i1("/A"); |
| 344 | i1.setNonce(1); |
| 345 | i1.wireEncode(); |
| 346 | BOOST_CHECK_EQUAL(i1.getNonce(), 1); |
| 347 | |
| 348 | Interest i2(i1); |
| 349 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 350 | |
| 351 | i2.setNonce(2); |
| 352 | BOOST_CHECK_EQUAL(i2.getNonce(), 2); |
| 353 | 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] | 354 | } |
| 355 | |
| 356 | BOOST_AUTO_TEST_CASE(RefreshNonce) |
| 357 | { |
| 358 | Interest i; |
| 359 | BOOST_CHECK(!i.hasNonce()); |
| 360 | i.refreshNonce(); |
| 361 | BOOST_CHECK(!i.hasNonce()); |
| 362 | |
| 363 | i.setNonce(1); |
| 364 | BOOST_CHECK(i.hasNonce()); |
| 365 | i.refreshNonce(); |
| 366 | BOOST_CHECK(i.hasNonce()); |
| 367 | BOOST_CHECK_NE(i.getNonce(), 1); |
| 368 | } |
| 369 | |
| 370 | BOOST_AUTO_TEST_CASE(SetInterestLifetime) |
| 371 | { |
| 372 | BOOST_CHECK_THROW(Interest("/A", time::milliseconds(-1)), std::invalid_argument); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 373 | BOOST_CHECK_NO_THROW(Interest("/A", 0_ms)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 374 | |
| 375 | Interest i("/local/ndn/prefix"); |
| 376 | i.setNonce(1); |
| 377 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
| 378 | BOOST_CHECK_THROW(i.setInterestLifetime(time::milliseconds(-1)), std::invalid_argument); |
| 379 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 380 | i.setInterestLifetime(0_ms); |
| 381 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms); |
| 382 | i.setInterestLifetime(1_ms); |
| 383 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), 1_ms); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | // ---- operators ---- |
| 387 | |
| 388 | BOOST_AUTO_TEST_CASE(Equality) |
| 389 | { |
| 390 | Interest a; |
| 391 | Interest b; |
| 392 | |
| 393 | // if nonce is not set, it would be set to a random value |
| 394 | a.setNonce(1); |
| 395 | b.setNonce(1); |
| 396 | |
| 397 | BOOST_CHECK_EQUAL(a == b, true); |
| 398 | BOOST_CHECK_EQUAL(a != b, false); |
| 399 | |
| 400 | // compare Name |
| 401 | a.setName("/A"); |
| 402 | BOOST_CHECK_EQUAL(a == b, false); |
| 403 | BOOST_CHECK_EQUAL(a != b, true); |
| 404 | |
| 405 | b.setName("/B"); |
| 406 | BOOST_CHECK_EQUAL(a == b, false); |
| 407 | BOOST_CHECK_EQUAL(a != b, true); |
| 408 | |
| 409 | b.setName("/A"); |
| 410 | BOOST_CHECK_EQUAL(a == b, true); |
| 411 | BOOST_CHECK_EQUAL(a != b, false); |
| 412 | |
| 413 | // compare Selectors |
| 414 | a.setChildSelector(1); |
| 415 | BOOST_CHECK_EQUAL(a == b, false); |
| 416 | BOOST_CHECK_EQUAL(a != b, true); |
| 417 | |
| 418 | b.setChildSelector(1); |
| 419 | BOOST_CHECK_EQUAL(a == b, true); |
| 420 | BOOST_CHECK_EQUAL(a != b, false); |
| 421 | |
| 422 | // compare Nonce |
| 423 | a.setNonce(100); |
| 424 | BOOST_CHECK_EQUAL(a == b, false); |
| 425 | BOOST_CHECK_EQUAL(a != b, true); |
| 426 | |
| 427 | b.setNonce(100); |
| 428 | BOOST_CHECK_EQUAL(a == b, true); |
| 429 | BOOST_CHECK_EQUAL(a != b, false); |
| 430 | |
| 431 | // compare InterestLifetime |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 432 | a.setInterestLifetime(10_s); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 433 | BOOST_CHECK_EQUAL(a == b, false); |
| 434 | BOOST_CHECK_EQUAL(a != b, true); |
| 435 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 436 | b.setInterestLifetime(10_s); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 437 | BOOST_CHECK_EQUAL(a == b, true); |
| 438 | BOOST_CHECK_EQUAL(a != b, false); |
| 439 | |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame] | 440 | // compare ForwardingHint |
| 441 | a.setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 442 | BOOST_CHECK_EQUAL(a == b, false); |
| 443 | BOOST_CHECK_EQUAL(a != b, true); |
| 444 | |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame] | 445 | b.setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 446 | BOOST_CHECK_EQUAL(a == b, true); |
| 447 | BOOST_CHECK_EQUAL(a != b, false); |
| 448 | } |
| 449 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 450 | BOOST_AUTO_TEST_SUITE_END() // TestInterest |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 451 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 452 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 453 | } // namespace ndn |