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 | /* |
Alexander Afanasyev | 1013fd0 | 2017-01-03 13:19:03 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 |
| 51 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // NameComponent |
| 52 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // NameComponent |
| 53 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // NameComponent |
| 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 |
| 77 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // NameComponent |
| 78 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // NameComponent |
| 79 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // NameComponent |
| 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); |
| 96 | i1.setInterestLifetime(time::milliseconds(1000)); |
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); |
| 105 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), time::milliseconds(1000)); |
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); |
| 117 | i1.setInterestLifetime(time::seconds(10)); |
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"); |
| 121 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), time::seconds(10)); |
| 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) |
| 257 | .setInterestLifetime(time::seconds(5)) |
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 | |
| 275 | other.setInterestLifetime(time::hours(5)); |
| 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 | |
| 281 | BOOST_AUTO_TEST_CASE(GetNonce) |
| 282 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 283 | unique_ptr<Interest> i1, i2; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 284 | |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 285 | // getNonce automatically assigns a random Nonce. |
| 286 | // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of |
| 287 | // same Nonces in a row. |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 288 | int nIterations = 0; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 289 | uint32_t nonce1 = 0, nonce2 = 0; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 290 | do { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 291 | i1 = make_unique<Interest>(); |
| 292 | nonce1 = i1->getNonce(); |
| 293 | i2 = make_unique<Interest>(); |
| 294 | nonce2 = i2->getNonce(); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 295 | } |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 296 | while (nonce1 == nonce2 && ++nIterations < 100); |
| 297 | BOOST_CHECK_NE(nonce1, nonce2); |
| 298 | BOOST_CHECK(i1->hasNonce()); |
| 299 | BOOST_CHECK(i2->hasNonce()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 300 | |
| 301 | // Once a Nonce is assigned, it should not change. |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 302 | BOOST_CHECK_EQUAL(i1->getNonce(), nonce1); |
| 303 | } |
| 304 | |
| 305 | BOOST_AUTO_TEST_CASE(SetNonce) |
| 306 | { |
| 307 | Interest i1("/A"); |
| 308 | i1.setNonce(1); |
| 309 | i1.wireEncode(); |
| 310 | BOOST_CHECK_EQUAL(i1.getNonce(), 1); |
| 311 | |
| 312 | Interest i2(i1); |
| 313 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 314 | |
| 315 | i2.setNonce(2); |
| 316 | BOOST_CHECK_EQUAL(i2.getNonce(), 2); |
| 317 | 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] | 318 | } |
| 319 | |
| 320 | BOOST_AUTO_TEST_CASE(RefreshNonce) |
| 321 | { |
| 322 | Interest i; |
| 323 | BOOST_CHECK(!i.hasNonce()); |
| 324 | i.refreshNonce(); |
| 325 | BOOST_CHECK(!i.hasNonce()); |
| 326 | |
| 327 | i.setNonce(1); |
| 328 | BOOST_CHECK(i.hasNonce()); |
| 329 | i.refreshNonce(); |
| 330 | BOOST_CHECK(i.hasNonce()); |
| 331 | BOOST_CHECK_NE(i.getNonce(), 1); |
| 332 | } |
| 333 | |
| 334 | BOOST_AUTO_TEST_CASE(SetInterestLifetime) |
| 335 | { |
| 336 | BOOST_CHECK_THROW(Interest("/A", time::milliseconds(-1)), std::invalid_argument); |
| 337 | BOOST_CHECK_NO_THROW(Interest("/A", time::milliseconds(0))); |
| 338 | |
| 339 | Interest i("/local/ndn/prefix"); |
| 340 | i.setNonce(1); |
| 341 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
| 342 | BOOST_CHECK_THROW(i.setInterestLifetime(time::milliseconds(-1)), std::invalid_argument); |
| 343 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
| 344 | i.setInterestLifetime(time::milliseconds(0)); |
| 345 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), time::milliseconds(0)); |
| 346 | i.setInterestLifetime(time::milliseconds(1)); |
| 347 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), time::milliseconds(1)); |
| 348 | } |
| 349 | |
Junxiao Shi | b2a7033 | 2017-07-07 22:15:03 +0000 | [diff] [blame] | 350 | BOOST_AUTO_TEST_CASE(ModifyForwardingHint) |
| 351 | { |
| 352 | Interest i; |
| 353 | i.setForwardingHint({{1, "/A"}}); |
| 354 | i.wireEncode(); |
| 355 | BOOST_CHECK(i.hasWire()); |
| 356 | |
| 357 | i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); }); |
| 358 | BOOST_CHECK(!i.hasWire()); |
| 359 | BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}})); |
| 360 | } |
| 361 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 362 | // ---- operators ---- |
| 363 | |
| 364 | BOOST_AUTO_TEST_CASE(Equality) |
| 365 | { |
| 366 | Interest a; |
| 367 | Interest b; |
| 368 | |
| 369 | // if nonce is not set, it would be set to a random value |
| 370 | a.setNonce(1); |
| 371 | b.setNonce(1); |
| 372 | |
| 373 | BOOST_CHECK_EQUAL(a == b, true); |
| 374 | BOOST_CHECK_EQUAL(a != b, false); |
| 375 | |
| 376 | // compare Name |
| 377 | a.setName("/A"); |
| 378 | BOOST_CHECK_EQUAL(a == b, false); |
| 379 | BOOST_CHECK_EQUAL(a != b, true); |
| 380 | |
| 381 | b.setName("/B"); |
| 382 | BOOST_CHECK_EQUAL(a == b, false); |
| 383 | BOOST_CHECK_EQUAL(a != b, true); |
| 384 | |
| 385 | b.setName("/A"); |
| 386 | BOOST_CHECK_EQUAL(a == b, true); |
| 387 | BOOST_CHECK_EQUAL(a != b, false); |
| 388 | |
| 389 | // compare Selectors |
| 390 | a.setChildSelector(1); |
| 391 | BOOST_CHECK_EQUAL(a == b, false); |
| 392 | BOOST_CHECK_EQUAL(a != b, true); |
| 393 | |
| 394 | b.setChildSelector(1); |
| 395 | BOOST_CHECK_EQUAL(a == b, true); |
| 396 | BOOST_CHECK_EQUAL(a != b, false); |
| 397 | |
| 398 | // compare Nonce |
| 399 | a.setNonce(100); |
| 400 | BOOST_CHECK_EQUAL(a == b, false); |
| 401 | BOOST_CHECK_EQUAL(a != b, true); |
| 402 | |
| 403 | b.setNonce(100); |
| 404 | BOOST_CHECK_EQUAL(a == b, true); |
| 405 | BOOST_CHECK_EQUAL(a != b, false); |
| 406 | |
| 407 | // compare InterestLifetime |
| 408 | a.setInterestLifetime(time::seconds(10)); |
| 409 | BOOST_CHECK_EQUAL(a == b, false); |
| 410 | BOOST_CHECK_EQUAL(a != b, true); |
| 411 | |
| 412 | b.setInterestLifetime(time::seconds(10)); |
| 413 | BOOST_CHECK_EQUAL(a == b, true); |
| 414 | BOOST_CHECK_EQUAL(a != b, false); |
| 415 | |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame^] | 416 | // compare ForwardingHint |
| 417 | a.setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 418 | BOOST_CHECK_EQUAL(a == b, false); |
| 419 | BOOST_CHECK_EQUAL(a != b, true); |
| 420 | |
Junxiao Shi | d701e5b | 2017-07-26 01:30:41 +0000 | [diff] [blame^] | 421 | b.setForwardingHint({{1, "/H"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 422 | BOOST_CHECK_EQUAL(a == b, true); |
| 423 | BOOST_CHECK_EQUAL(a != b, false); |
| 424 | } |
| 425 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 426 | BOOST_AUTO_TEST_SUITE_END() // TestInterest |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 427 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 428 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 429 | } // namespace ndn |