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); |
| 44 | BOOST_CHECK_EQUAL(i.hasLink(), false); |
| 45 | BOOST_CHECK(!i.hasSelectedDelegation()); |
| 46 | } |
| 47 | |
| 48 | BOOST_AUTO_TEST_CASE(EncodeDecodeBasic) |
| 49 | { |
| 50 | const uint8_t WIRE[] = { |
| 51 | 0x05, 0x1c, // Interest |
| 52 | 0x07, 0x14, // Name |
| 53 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // NameComponent |
| 54 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // NameComponent |
| 55 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // NameComponent |
| 56 | 0x0a, 0x04, // Nonce |
| 57 | 0x01, 0x00, 0x00, 0x00 |
| 58 | }; |
| 59 | |
| 60 | Interest i1("/local/ndn/prefix"); |
| 61 | i1.setNonce(1); |
| 62 | Block wire1 = i1.wireEncode(); |
| 63 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 64 | |
| 65 | Interest i2(wire1); |
| 66 | BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix"); |
| 67 | BOOST_CHECK(i2.getSelectors().empty()); |
| 68 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 69 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
| 70 | |
| 71 | BOOST_CHECK_EQUAL(i1, i2); |
| 72 | } |
| 73 | |
| 74 | BOOST_AUTO_TEST_CASE(EncodeDecodeFull) |
| 75 | { |
| 76 | const uint8_t WIRE[] = { |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 77 | 0x05, 0x31, // Interest |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 78 | 0x07, 0x14, // Name |
| 79 | 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, // NameComponent |
| 80 | 0x08, 0x03, 0x6e, 0x64, 0x6e, // NameComponent |
| 81 | 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // NameComponent |
| 82 | 0x09, 0x03, // Selectors |
| 83 | 0x0d, 0x01, 0x01, // MinSuffixComponents |
| 84 | 0x0a, 0x04, // Nonce |
| 85 | 0x01, 0x00, 0x00, 0x00, |
| 86 | 0x0c, 0x02, // InterestLifetime |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 87 | 0x03, 0xe8, |
| 88 | 0x1e, 0x0a, // ForwardingHint |
| 89 | 0x1f, 0x08, // Delegation |
| 90 | 0x1e, 0x01, 0x01, // Preference=1 |
| 91 | 0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 92 | }; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 93 | |
| 94 | Interest i1; |
| 95 | i1.setName("/local/ndn/prefix"); |
| 96 | i1.setMinSuffixComponents(1); |
| 97 | i1.setNonce(1); |
| 98 | i1.setInterestLifetime(time::milliseconds(1000)); |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 99 | i1.setForwardingHint({{1, "/A"}}); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 100 | Block wire1 = i1.wireEncode(); |
| 101 | BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE)); |
| 102 | |
| 103 | Interest i2(wire1); |
| 104 | BOOST_CHECK_EQUAL(i2.getName(), "/local/ndn/prefix"); |
| 105 | BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 1); |
| 106 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 107 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), time::milliseconds(1000)); |
Junxiao Shi | 9c154cb | 2017-07-07 22:14:54 +0000 | [diff] [blame] | 108 | BOOST_CHECK_EQUAL(i2.getForwardingHint(), DelegationList({{1, "/A"}})); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 109 | |
| 110 | BOOST_CHECK_EQUAL(i1, i2); |
| 111 | } |
| 112 | |
| 113 | const uint8_t LINK[] = { |
| 114 | 0x06, 0xda, // Data |
| 115 | 0x07, 0x14, // Name |
| 116 | 0x08, 0x05, |
| 117 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 118 | 0x08, 0x03, |
| 119 | 0x6e, 0x64, 0x6e, |
| 120 | 0x08, 0x06, |
| 121 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
| 122 | 0x14, 0x07, // MetaInfo |
| 123 | 0x18, 0x01, // ContentType |
| 124 | 0x01, |
| 125 | 0x19, 0x02, // FreshnessPeriod |
| 126 | 0x27, 0x10, |
| 127 | 0x15, 0x1a, // Content |
| 128 | 0x1f, 0x0c, // LinkDelegation |
| 129 | 0x1e, 0x01, // LinkPreference |
| 130 | 0x0a, |
| 131 | 0x07, 0x07, // Name |
| 132 | 0x08, 0x05, |
| 133 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 134 | 0x1f, 0x0a, // LinkDelegation |
| 135 | 0x1e, 0x01, // LinkPreference |
| 136 | 0x14, |
| 137 | 0x07, 0x05, // Name |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 138 | 0x08, 0x03, |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 139 | 0x6e, 0x64, 0x6e, |
| 140 | 0x16, 0x1b, // SignatureInfo |
| 141 | 0x1b, 0x01, // SignatureType |
| 142 | 0x01, |
| 143 | 0x1c, 0x16, // KeyLocator |
| 144 | 0x07, 0x14, // Name |
| 145 | 0x08, 0x04, |
| 146 | 0x74, 0x65, 0x73, 0x74, |
| 147 | 0x08, 0x03, |
| 148 | 0x6b, 0x65, 0x79, |
| 149 | 0x08, 0x07, |
| 150 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 151 | 0x17, 0x80, // SignatureValue |
| 152 | 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec, |
| 153 | 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6, |
| 154 | 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38, |
| 155 | 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc, |
| 156 | 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf, |
| 157 | 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9, |
| 158 | 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8, |
| 159 | 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7, |
| 160 | 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3, |
| 161 | 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1 |
Alexander Afanasyev | e881e93 | 2014-06-08 14:47:03 +0300 | [diff] [blame] | 162 | }; |
| 163 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 164 | BOOST_AUTO_TEST_CASE(WireDecodeReset) // checks wireDecode resets all fields |
| 165 | { |
| 166 | Interest i1; |
| 167 | i1.setName("/test"); |
| 168 | i1.setMinSuffixComponents(100); |
| 169 | i1.setNonce(10); |
| 170 | i1.setInterestLifetime(time::seconds(10)); |
| 171 | i1.setLink(Block(LINK, sizeof(LINK))); |
| 172 | i1.setSelectedDelegation(0); |
| 173 | |
| 174 | Interest i2(i1.wireEncode()); |
| 175 | BOOST_CHECK_EQUAL(i2.getName().toUri(), "/test"); |
| 176 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), time::seconds(10)); |
| 177 | BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), 100); |
| 178 | BOOST_CHECK_EQUAL(i2.getNonce(), 10); |
| 179 | BOOST_CHECK_EQUAL(i2.hasLink(), true); |
| 180 | BOOST_CHECK_EQUAL(i2.hasSelectedDelegation(), true); |
| 181 | |
| 182 | i2.wireDecode(Interest().wireEncode()); |
| 183 | BOOST_CHECK_EQUAL(i2.getName().toUri(), "/"); |
| 184 | BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
| 185 | BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), -1); |
| 186 | BOOST_WARN_NE(i2.getNonce(), 10); |
| 187 | BOOST_CHECK_EQUAL(i2.hasLink(), false); |
| 188 | BOOST_CHECK_EQUAL(i2.hasSelectedDelegation(), false); |
| 189 | } |
| 190 | |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 191 | BOOST_AUTO_TEST_CASE(DecodeNoName) |
| 192 | { |
| 193 | Block b(tlv::Interest); |
| 194 | b.push_back(makeBinaryBlock(tlv::Nonce, "FISH", 4)); |
| 195 | b.encode(); |
| 196 | |
| 197 | Interest i; |
| 198 | BOOST_CHECK_THROW(i.wireDecode(b), tlv::Error); |
| 199 | } |
| 200 | |
| 201 | BOOST_AUTO_TEST_CASE(DecodeNoNonce) |
| 202 | { |
| 203 | Block b(tlv::Interest); |
| 204 | b.push_back(Name("/YvzNKtPWh").wireEncode()); |
| 205 | b.encode(); |
| 206 | |
| 207 | Interest i; |
| 208 | BOOST_CHECK_THROW(i.wireDecode(b), tlv::Error); |
| 209 | } |
| 210 | |
| 211 | BOOST_AUTO_TEST_CASE(DecodeBadNonce) |
| 212 | { |
| 213 | Block b(tlv::Interest); |
| 214 | b.push_back(Name("/BJzEHVxDJ").wireEncode()); |
| 215 | b.push_back(makeBinaryBlock(tlv::Nonce, "SKY", 3)); |
| 216 | b.encode(); |
| 217 | |
| 218 | Interest i; |
| 219 | BOOST_CHECK_THROW(i.wireDecode(b), tlv::Error); |
| 220 | } |
| 221 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 222 | // ---- matching ---- |
| 223 | |
| 224 | BOOST_AUTO_TEST_CASE(MatchesData) |
| 225 | { |
| 226 | Interest interest; |
| 227 | interest.setName("ndn:/A") |
| 228 | .setMinSuffixComponents(2) |
| 229 | .setMaxSuffixComponents(2) |
| 230 | .setPublisherPublicKeyLocator(KeyLocator("ndn:/B")) |
| 231 | .setExclude(Exclude().excludeAfter(name::Component("J"))); |
| 232 | |
| 233 | Data data("ndn:/A/D"); |
| 234 | SignatureSha256WithRsa signature(KeyLocator("ndn:/B")); |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 235 | signature.setValue(encoding::makeEmptyBlock(tlv::SignatureValue)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 236 | data.setSignature(signature); |
| 237 | data.wireEncode(); |
| 238 | BOOST_CHECK_EQUAL(interest.matchesData(data), true); |
| 239 | |
| 240 | Data data1 = data; |
| 241 | data1.setName("ndn:/A"); // violates MinSuffixComponents |
| 242 | data1.wireEncode(); |
| 243 | BOOST_CHECK_EQUAL(interest.matchesData(data1), false); |
| 244 | |
| 245 | Interest interest1 = interest; |
| 246 | interest1.setMinSuffixComponents(1); |
| 247 | BOOST_CHECK_EQUAL(interest1.matchesData(data1), true); |
| 248 | |
| 249 | Data data2 = data; |
| 250 | data2.setName("ndn:/A/E/F"); // violates MaxSuffixComponents |
| 251 | data2.wireEncode(); |
| 252 | BOOST_CHECK_EQUAL(interest.matchesData(data2), false); |
| 253 | |
| 254 | Interest interest2 = interest; |
| 255 | interest2.setMaxSuffixComponents(3); |
| 256 | BOOST_CHECK_EQUAL(interest2.matchesData(data2), true); |
| 257 | |
| 258 | Data data3 = data; |
| 259 | SignatureSha256WithRsa signature3(KeyLocator("ndn:/G")); // violates PublisherPublicKeyLocator |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 260 | signature3.setValue(encoding::makeEmptyBlock(tlv::SignatureValue)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 261 | data3.setSignature(signature3); |
| 262 | data3.wireEncode(); |
| 263 | BOOST_CHECK_EQUAL(interest.matchesData(data3), false); |
| 264 | |
| 265 | Interest interest3 = interest; |
| 266 | interest3.setPublisherPublicKeyLocator(KeyLocator("ndn:/G")); |
| 267 | BOOST_CHECK_EQUAL(interest3.matchesData(data3), true); |
| 268 | |
| 269 | Data data4 = data; |
| 270 | DigestSha256 signature4; // violates PublisherPublicKeyLocator |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 271 | signature4.setValue(encoding::makeEmptyBlock(tlv::SignatureValue)); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 272 | data4.setSignature(signature4); |
| 273 | data4.wireEncode(); |
| 274 | BOOST_CHECK_EQUAL(interest.matchesData(data4), false); |
| 275 | |
| 276 | Interest interest4 = interest; |
| 277 | interest4.setPublisherPublicKeyLocator(KeyLocator()); |
| 278 | BOOST_CHECK_EQUAL(interest4.matchesData(data4), true); |
| 279 | |
| 280 | Data data5 = data; |
| 281 | data5.setName("ndn:/A/J"); // violates Exclude |
| 282 | data5.wireEncode(); |
| 283 | BOOST_CHECK_EQUAL(interest.matchesData(data5), false); |
| 284 | |
| 285 | Interest interest5 = interest; |
| 286 | interest5.setExclude(Exclude().excludeAfter(name::Component("K"))); |
| 287 | BOOST_CHECK_EQUAL(interest5.matchesData(data5), true); |
| 288 | |
| 289 | Data data6 = data; |
| 290 | data6.setName("ndn:/H/I"); // violates Name |
| 291 | data6.wireEncode(); |
| 292 | BOOST_CHECK_EQUAL(interest.matchesData(data6), false); |
| 293 | |
| 294 | Data data7 = data; |
| 295 | data7.setName("ndn:/A/B"); |
| 296 | data7.wireEncode(); |
| 297 | |
Junxiao Shi | db7464d | 2017-07-13 03:11:17 +0000 | [diff] [blame] | 298 | Interest interest7("/A/B/sha256digest=54008e240a7eea2714a161dfddf0dd6ced223b3856e9da96792151e180f3b128"); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 299 | BOOST_CHECK_EQUAL(interest7.matchesData(data7), true); |
| 300 | |
| 301 | Interest interest7b("/A/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"); |
| 302 | BOOST_CHECK_EQUAL(interest7b.matchesData(data7), false); // violates implicit digest |
| 303 | } |
| 304 | |
| 305 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(MatchesInterest, 1) |
| 306 | BOOST_AUTO_TEST_CASE(MatchesInterest) |
| 307 | { |
| 308 | Interest interest; |
| 309 | interest |
| 310 | .setName("/A") |
| 311 | .setMinSuffixComponents(2) |
| 312 | .setMaxSuffixComponents(2) |
| 313 | .setPublisherPublicKeyLocator(KeyLocator("/B")) |
| 314 | .setExclude(Exclude().excludeAfter(name::Component("J"))) |
| 315 | .setNonce(10) |
| 316 | .setInterestLifetime(time::seconds(5)) |
| 317 | .setLink(Block(LINK, sizeof(LINK))); |
| 318 | |
| 319 | Interest other; |
| 320 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 321 | |
| 322 | other.setName(interest.getName()); |
| 323 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); |
| 324 | |
| 325 | other.setSelectors(interest.getSelectors()); |
| 326 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), false); // will match until #3162 implemented |
| 327 | |
| 328 | other.setLink(interest.getLink().wireEncode()); |
| 329 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 330 | |
| 331 | other.setNonce(200); |
| 332 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 333 | |
| 334 | other.setInterestLifetime(time::hours(5)); |
| 335 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 336 | |
| 337 | other.setSelectedDelegation(0); |
| 338 | BOOST_CHECK_EQUAL(interest.matchesInterest(other), true); |
| 339 | } |
| 340 | |
| 341 | // ---- field accessors ---- |
| 342 | |
| 343 | BOOST_AUTO_TEST_CASE(GetNonce) |
| 344 | { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 345 | unique_ptr<Interest> i1, i2; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 346 | |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 347 | // getNonce automatically assigns a random Nonce. |
| 348 | // It's possible to assign the same Nonce to two Interest, but it's unlikely to get 100 pairs of |
| 349 | // same Nonces in a row. |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 350 | int nIterations = 0; |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 351 | uint32_t nonce1 = 0, nonce2 = 0; |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 352 | do { |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 353 | i1 = make_unique<Interest>(); |
| 354 | nonce1 = i1->getNonce(); |
| 355 | i2 = make_unique<Interest>(); |
| 356 | nonce2 = i2->getNonce(); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 357 | } |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 358 | while (nonce1 == nonce2 && ++nIterations < 100); |
| 359 | BOOST_CHECK_NE(nonce1, nonce2); |
| 360 | BOOST_CHECK(i1->hasNonce()); |
| 361 | BOOST_CHECK(i2->hasNonce()); |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 362 | |
| 363 | // Once a Nonce is assigned, it should not change. |
Junxiao Shi | 2dd711d | 2017-07-21 13:40:52 +0000 | [diff] [blame] | 364 | BOOST_CHECK_EQUAL(i1->getNonce(), nonce1); |
| 365 | } |
| 366 | |
| 367 | BOOST_AUTO_TEST_CASE(SetNonce) |
| 368 | { |
| 369 | Interest i1("/A"); |
| 370 | i1.setNonce(1); |
| 371 | i1.wireEncode(); |
| 372 | BOOST_CHECK_EQUAL(i1.getNonce(), 1); |
| 373 | |
| 374 | Interest i2(i1); |
| 375 | BOOST_CHECK_EQUAL(i2.getNonce(), 1); |
| 376 | |
| 377 | i2.setNonce(2); |
| 378 | BOOST_CHECK_EQUAL(i2.getNonce(), 2); |
| 379 | 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] | 380 | } |
| 381 | |
| 382 | BOOST_AUTO_TEST_CASE(RefreshNonce) |
| 383 | { |
| 384 | Interest i; |
| 385 | BOOST_CHECK(!i.hasNonce()); |
| 386 | i.refreshNonce(); |
| 387 | BOOST_CHECK(!i.hasNonce()); |
| 388 | |
| 389 | i.setNonce(1); |
| 390 | BOOST_CHECK(i.hasNonce()); |
| 391 | i.refreshNonce(); |
| 392 | BOOST_CHECK(i.hasNonce()); |
| 393 | BOOST_CHECK_NE(i.getNonce(), 1); |
| 394 | } |
| 395 | |
| 396 | BOOST_AUTO_TEST_CASE(SetInterestLifetime) |
| 397 | { |
| 398 | BOOST_CHECK_THROW(Interest("/A", time::milliseconds(-1)), std::invalid_argument); |
| 399 | BOOST_CHECK_NO_THROW(Interest("/A", time::milliseconds(0))); |
| 400 | |
| 401 | Interest i("/local/ndn/prefix"); |
| 402 | i.setNonce(1); |
| 403 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
| 404 | BOOST_CHECK_THROW(i.setInterestLifetime(time::milliseconds(-1)), std::invalid_argument); |
| 405 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
| 406 | i.setInterestLifetime(time::milliseconds(0)); |
| 407 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), time::milliseconds(0)); |
| 408 | i.setInterestLifetime(time::milliseconds(1)); |
| 409 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), time::milliseconds(1)); |
| 410 | } |
| 411 | |
Junxiao Shi | b2a7033 | 2017-07-07 22:15:03 +0000 | [diff] [blame] | 412 | BOOST_AUTO_TEST_CASE(ModifyForwardingHint) |
| 413 | { |
| 414 | Interest i; |
| 415 | i.setForwardingHint({{1, "/A"}}); |
| 416 | i.wireEncode(); |
| 417 | BOOST_CHECK(i.hasWire()); |
| 418 | |
| 419 | i.modifyForwardingHint([] (DelegationList& fh) { fh.insert(2, "/B"); }); |
| 420 | BOOST_CHECK(!i.hasWire()); |
| 421 | BOOST_CHECK_EQUAL(i.getForwardingHint(), DelegationList({{1, "/A"}, {2, "/B"}})); |
| 422 | } |
| 423 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 424 | // ---- operators ---- |
| 425 | |
| 426 | BOOST_AUTO_TEST_CASE(Equality) |
| 427 | { |
| 428 | Interest a; |
| 429 | Interest b; |
| 430 | |
| 431 | // if nonce is not set, it would be set to a random value |
| 432 | a.setNonce(1); |
| 433 | b.setNonce(1); |
| 434 | |
| 435 | BOOST_CHECK_EQUAL(a == b, true); |
| 436 | BOOST_CHECK_EQUAL(a != b, false); |
| 437 | |
| 438 | // compare Name |
| 439 | a.setName("/A"); |
| 440 | BOOST_CHECK_EQUAL(a == b, false); |
| 441 | BOOST_CHECK_EQUAL(a != b, true); |
| 442 | |
| 443 | b.setName("/B"); |
| 444 | BOOST_CHECK_EQUAL(a == b, false); |
| 445 | BOOST_CHECK_EQUAL(a != b, true); |
| 446 | |
| 447 | b.setName("/A"); |
| 448 | BOOST_CHECK_EQUAL(a == b, true); |
| 449 | BOOST_CHECK_EQUAL(a != b, false); |
| 450 | |
| 451 | // compare Selectors |
| 452 | a.setChildSelector(1); |
| 453 | BOOST_CHECK_EQUAL(a == b, false); |
| 454 | BOOST_CHECK_EQUAL(a != b, true); |
| 455 | |
| 456 | b.setChildSelector(1); |
| 457 | BOOST_CHECK_EQUAL(a == b, true); |
| 458 | BOOST_CHECK_EQUAL(a != b, false); |
| 459 | |
| 460 | // compare Nonce |
| 461 | a.setNonce(100); |
| 462 | BOOST_CHECK_EQUAL(a == b, false); |
| 463 | BOOST_CHECK_EQUAL(a != b, true); |
| 464 | |
| 465 | b.setNonce(100); |
| 466 | BOOST_CHECK_EQUAL(a == b, true); |
| 467 | BOOST_CHECK_EQUAL(a != b, false); |
| 468 | |
| 469 | // compare InterestLifetime |
| 470 | a.setInterestLifetime(time::seconds(10)); |
| 471 | BOOST_CHECK_EQUAL(a == b, false); |
| 472 | BOOST_CHECK_EQUAL(a != b, true); |
| 473 | |
| 474 | b.setInterestLifetime(time::seconds(10)); |
| 475 | BOOST_CHECK_EQUAL(a == b, true); |
| 476 | BOOST_CHECK_EQUAL(a != b, false); |
| 477 | |
| 478 | ///\todo #4055 compare ForwardingHint |
| 479 | |
| 480 | // compare Link |
| 481 | a.setLink(Block(LINK, sizeof(LINK))); |
| 482 | BOOST_CHECK_EQUAL(a == b, false); |
| 483 | BOOST_CHECK_EQUAL(a != b, true); |
| 484 | |
| 485 | b.setLink(Block(LINK, sizeof(LINK))); |
| 486 | BOOST_CHECK_EQUAL(a == b, true); |
| 487 | BOOST_CHECK_EQUAL(a != b, false); |
| 488 | |
| 489 | // compare SelectedDelegation |
| 490 | BOOST_CHECK_EQUAL(a.hasSelectedDelegation(), false); |
| 491 | BOOST_CHECK_EQUAL(b.hasSelectedDelegation(), false); |
| 492 | |
| 493 | a.setSelectedDelegation(Name("/local")); |
| 494 | BOOST_CHECK_EQUAL(a == b, false); |
| 495 | BOOST_CHECK_EQUAL(a != b, true); |
| 496 | |
| 497 | b.setSelectedDelegation(Name("/local")); |
| 498 | BOOST_CHECK_EQUAL(a == b, true); |
| 499 | BOOST_CHECK_EQUAL(a != b, false); |
| 500 | } |
| 501 | |
| 502 | BOOST_FIXTURE_TEST_SUITE(LinkSelectedDelegation, IdentityManagementFixture) |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 503 | |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 504 | const uint8_t InterestWithLink[] = { |
| 505 | 0x05, 0xfb, // Interest |
| 506 | 0x07, 0x14, // Name |
| 507 | 0x08, 0x5, // NameComponent |
| 508 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 509 | 0x08, 0x3, // NameComponent |
| 510 | 0x6e, 0x64, 0x6e, |
| 511 | 0x08, 0x6, // NameComponent |
| 512 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
| 513 | 0x0a, 0x4, // Nonce |
| 514 | 0x1, 0x0, 0x0, 0x00, |
| 515 | 0x06, 0xda, // Data |
| 516 | 0x07, 0x14, // Name |
| 517 | 0x08, 0x05, |
| 518 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 519 | 0x08, 0x03, |
| 520 | 0x6e, 0x64, 0x6e, |
| 521 | 0x08, 0x06, |
| 522 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
| 523 | 0x14, 0x07, // MetaInfo |
| 524 | 0x18, 0x01, // ContentType |
| 525 | 0x01, |
| 526 | 0x19, 0x02, // FreshnessPeriod |
| 527 | 0x27, 0x10, |
| 528 | 0x15, 0x1a, // Content |
| 529 | 0x1f, 0x0c, // LinkDelegation |
| 530 | 0x1e, 0x01, // LinkPreference |
| 531 | 0x0a, |
| 532 | 0x07, 0x07, // Name |
| 533 | 0x08, 0x05, |
| 534 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 535 | 0x1f, 0x0a, // LinkDelegation |
| 536 | 0x1e, 0x01, // LinkPreference |
| 537 | 0x14, |
| 538 | 0x07, 0x05, // Name |
| 539 | 0x08, 0x03, |
| 540 | 0x6e, 0x64, 0x6e, |
| 541 | 0x16, 0x1b, // SignatureInfo |
| 542 | 0x1b, 0x01, // SignatureType |
| 543 | 0x01, |
| 544 | 0x1c, 0x16, // KeyLocator |
| 545 | 0x07, 0x14, // Name |
| 546 | 0x08, 0x04, |
| 547 | 0x74, 0x65, 0x73, 0x74, |
| 548 | 0x08, 0x03, |
| 549 | 0x6b, 0x65, 0x79, |
| 550 | 0x08, 0x07, |
| 551 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 552 | 0x17, 0x80, // SignatureValue |
| 553 | 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec, |
| 554 | 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6, |
| 555 | 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38, |
| 556 | 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc, |
| 557 | 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf, |
| 558 | 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9, |
| 559 | 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8, |
| 560 | 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7, |
| 561 | 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3, |
| 562 | 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1, |
| 563 | 0x20, 0x01, // SelectedDelegation |
| 564 | 0x00 |
| 565 | }; |
| 566 | |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 567 | const uint8_t InterestWithSelectedDelegationButNoLink[] = { |
| 568 | 0x05, 0x1f, // Interest |
| 569 | 0x07, 0x14, // Name |
| 570 | 0x08, 0x5, // NameComponent |
| 571 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 572 | 0x08, 0x3, // NameComponent |
| 573 | 0x6e, 0x64, 0x6e, |
| 574 | 0x08, 0x6, // NameComponent |
| 575 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
| 576 | 0x0a, 0x4, // Nonce |
| 577 | 0x1, 0x0, 0x0, 0x00, |
| 578 | 0x20, 0x01, // SelectedDelegation |
| 579 | 0x00 |
| 580 | }; |
| 581 | |
| 582 | const uint8_t InterestWithLinkNotNonIntegerSelectedDelegation[] = { |
| 583 | 0x05, 0xfb, // Interest |
| 584 | 0x07, 0x14, // Name |
| 585 | 0x08, 0x5, // NameComponent |
| 586 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 587 | 0x08, 0x3, // NameComponent |
| 588 | 0x6e, 0x64, 0x6e, |
| 589 | 0x08, 0x6, // NameComponent |
| 590 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
| 591 | 0x0a, 0x4, // Nonce |
| 592 | 0x1, 0x0, 0x0, 0x00, |
| 593 | 0x06, 0xda, // Data |
| 594 | 0x07, 0x14, // Name |
| 595 | 0x08, 0x05, |
| 596 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 597 | 0x08, 0x03, |
| 598 | 0x6e, 0x64, 0x6e, |
| 599 | 0x08, 0x06, |
| 600 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
| 601 | 0x14, 0x07, // MetaInfo |
| 602 | 0x18, 0x01, // ContentType |
| 603 | 0x01, |
| 604 | 0x19, 0x02, // FreshnessPeriod |
| 605 | 0x27, 0x10, |
| 606 | 0x15, 0x1a, // Content |
| 607 | 0x1f, 0x0c, // LinkDelegation |
| 608 | 0x1e, 0x01, // LinkPreference |
| 609 | 0x0a, |
| 610 | 0x07, 0x07, // Name |
| 611 | 0x08, 0x05, |
| 612 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 613 | 0x1f, 0x0a, // LinkDelegation |
| 614 | 0x1e, 0x01, // LinkPreference |
| 615 | 0x14, |
| 616 | 0x07, 0x05, // Name |
| 617 | 0x08, 0x03, |
| 618 | 0x6e, 0x64, 0x6e, |
| 619 | 0x16, 0x1b, // SignatureInfo |
| 620 | 0x1b, 0x01, // SignatureType |
| 621 | 0x01, |
| 622 | 0x1c, 0x16, // KeyLocator |
| 623 | 0x07, 0x14, // Name |
| 624 | 0x08, 0x04, |
| 625 | 0x74, 0x65, 0x73, 0x74, |
| 626 | 0x08, 0x03, |
| 627 | 0x6b, 0x65, 0x79, |
| 628 | 0x08, 0x07, |
| 629 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 630 | 0x17, 0x78, // SignatureValue |
| 631 | 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec, |
| 632 | 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6, |
| 633 | 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38, |
| 634 | 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc, |
| 635 | 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf, |
| 636 | 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9, |
| 637 | 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8, |
| 638 | 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7, |
| 639 | 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3, |
| 640 | 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, |
| 641 | 0x20, 0x03, // SelectedDelegation |
| 642 | 0xAA, 0xAA, 0xAA |
| 643 | }; |
| 644 | |
| 645 | const uint8_t InterestWithLinkNonDecreasingOrder[] = { |
| 646 | 0x05, 0xfb, // Interest |
| 647 | 0x07, 0x14, // Name |
| 648 | 0x08, 0x5, // NameComponent |
| 649 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 650 | 0x08, 0x3, // NameComponent |
| 651 | 0x6e, 0x64, 0x6e, |
| 652 | 0x08, 0x6, // NameComponent |
| 653 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
| 654 | 0x0a, 0x4, // Nonce |
| 655 | 0x1, 0x0, 0x0, 0x00, |
| 656 | 0x06, 0xda, // Data |
| 657 | 0x07, 0x14, // Name |
| 658 | 0x08, 0x05, |
| 659 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 660 | 0x08, 0x03, |
| 661 | 0x6e, 0x64, 0x6e, |
| 662 | 0x08, 0x06, |
| 663 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
| 664 | 0x14, 0x07, // MetaInfo |
| 665 | 0x18, 0x01, // ContentType |
| 666 | 0x01, |
| 667 | 0x19, 0x02, // FreshnessPeriod |
| 668 | 0x27, 0x10, |
| 669 | 0x15, 0x1a, // Content |
| 670 | 0x1f, 0x0c, // LinkDelegation |
| 671 | 0x1e, 0x01, // LinkPreference |
| 672 | 0x14, |
| 673 | 0x07, 0x07, // Name |
| 674 | 0x08, 0x05, |
| 675 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 676 | 0x1f, 0x0a, // LinkDelegation |
| 677 | 0x1e, 0x01, // LinkPreference |
| 678 | 0x0a, |
| 679 | 0x07, 0x05, // Name |
| 680 | 0x08, 0x03, |
| 681 | 0x6e, 0x64, 0x6e, |
| 682 | 0x16, 0x1b, // SignatureInfo |
| 683 | 0x1b, 0x01, // SignatureType |
| 684 | 0x01, |
| 685 | 0x1c, 0x16, // KeyLocator |
| 686 | 0x07, 0x14, // Name |
| 687 | 0x08, 0x04, |
| 688 | 0x74, 0x65, 0x73, 0x74, |
| 689 | 0x08, 0x03, |
| 690 | 0x6b, 0x65, 0x79, |
| 691 | 0x08, 0x07, |
| 692 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 693 | 0x17, 0x80, // SignatureValue |
| 694 | 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec, |
| 695 | 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6, |
| 696 | 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38, |
| 697 | 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc, |
| 698 | 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf, |
| 699 | 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9, |
| 700 | 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8, |
| 701 | 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7, |
| 702 | 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3, |
| 703 | 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1, |
| 704 | 0x20, 0x01, // SelectedDelegation |
| 705 | 0x01 |
| 706 | }; |
| 707 | |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 708 | BOOST_AUTO_TEST_CASE(LinkObject) |
| 709 | { |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 710 | Link link1("test", {{100, "/test3"}, {20, "/test2"}, {10, "/test1"}}); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 711 | m_keyChain.sign(link1); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 712 | Block wire = link1.wireEncode(); |
| 713 | |
| 714 | Interest a; |
| 715 | BOOST_REQUIRE_NO_THROW(a.setLink(wire)); |
| 716 | |
| 717 | BOOST_REQUIRE_NO_THROW(a.getLink()); |
| 718 | |
| 719 | Link link2 = a.getLink(); |
| 720 | Name name = link2.getName(); |
| 721 | BOOST_CHECK_EQUAL(Name("test"), name); |
| 722 | BOOST_CHECK_EQUAL(a.hasLink(), true); |
| 723 | Link::DelegationSet delegations; |
| 724 | delegations = link2.getDelegations(); |
| 725 | |
| 726 | auto i = delegations.begin(); |
| 727 | BOOST_CHECK_EQUAL(std::get<0>(*i), 10); |
| 728 | BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test1")); |
| 729 | ++i; |
| 730 | BOOST_CHECK_EQUAL(std::get<0>(*i), 20); |
| 731 | BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test2")); |
| 732 | ++i; |
| 733 | BOOST_CHECK_EQUAL(std::get<0>(*i), 100); |
| 734 | BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test3")); |
| 735 | |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 736 | a.setLink(Block(LINK, sizeof(LINK))); |
| 737 | BOOST_CHECK_EQUAL(a.getLink().getDelegations().size(), 2); |
| 738 | |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 739 | a.unsetLink(); |
| 740 | BOOST_CHECK_EQUAL(a.hasLink(), false); |
| 741 | } |
| 742 | |
| 743 | BOOST_AUTO_TEST_CASE(SelectedDelegationChecks) |
| 744 | { |
| 745 | Link link("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 746 | m_keyChain.sign(link); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 747 | Block wire = link.wireEncode(); |
| 748 | |
| 749 | Interest a; |
| 750 | a.setLink(wire); |
| 751 | BOOST_CHECK_EQUAL(a.hasSelectedDelegation(), false); |
| 752 | |
| 753 | BOOST_REQUIRE_NO_THROW(a.setSelectedDelegation(Name("test2"))); |
| 754 | BOOST_CHECK_EQUAL(a.getSelectedDelegation(), Name("test2")); |
| 755 | |
| 756 | BOOST_REQUIRE_NO_THROW(a.setSelectedDelegation(uint32_t(2))); |
| 757 | BOOST_CHECK_EQUAL(a.getSelectedDelegation(), Name("test3")); |
| 758 | |
| 759 | a.unsetSelectedDelegation(); |
| 760 | BOOST_CHECK_EQUAL(a.hasSelectedDelegation(), false); |
| 761 | } |
| 762 | |
| 763 | BOOST_AUTO_TEST_CASE(EncodeDecodeWithLink) |
| 764 | { |
| 765 | Link link1("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 766 | m_keyChain.sign(link1); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 767 | Block wire = link1.wireEncode(); |
| 768 | |
| 769 | Interest a; |
| 770 | a.setName("/Test/Encode/Decode/With/Link"); |
| 771 | a.setChildSelector(1); |
| 772 | a.setNonce(100); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 773 | a.setInterestLifetime(time::seconds(10)); |
| 774 | a.setLink(wire); |
| 775 | |
| 776 | Block interestBlock = a.wireEncode(); |
| 777 | Interest b(interestBlock); |
| 778 | |
| 779 | BOOST_CHECK_EQUAL(a == b, true); |
| 780 | |
| 781 | Link link2 = b.getLink(); |
| 782 | Link::DelegationSet delegations; |
| 783 | delegations = link2.getDelegations(); |
| 784 | |
| 785 | auto i = delegations.begin(); |
| 786 | BOOST_CHECK_EQUAL(std::get<0>(*i), 10); |
| 787 | BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test1")); |
| 788 | ++i; |
| 789 | BOOST_CHECK_EQUAL(std::get<0>(*i), 20); |
| 790 | BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test2")); |
| 791 | ++i; |
| 792 | BOOST_CHECK_EQUAL(std::get<0>(*i), 100); |
| 793 | BOOST_CHECK_EQUAL(std::get<1>(*i), Name("test3")); |
| 794 | |
| 795 | } |
| 796 | |
| 797 | BOOST_AUTO_TEST_CASE(DecodeInterestWithLink) |
| 798 | { |
| 799 | Block interestBlock(InterestWithLink, sizeof(InterestWithLink)); |
| 800 | |
| 801 | ndn::Interest i; |
| 802 | BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock)); |
| 803 | Link link = i.getLink(); |
| 804 | BOOST_CHECK_EQUAL(link.getName(), Name("/local/ndn/prefix")); |
| 805 | Link::DelegationSet delegations = link.getDelegations(); |
| 806 | |
| 807 | auto it = delegations.begin(); |
| 808 | BOOST_CHECK_EQUAL(std::get<0>(*it), 10); |
| 809 | BOOST_CHECK_EQUAL(std::get<1>(*it), Name("local")); |
| 810 | ++it; |
| 811 | BOOST_CHECK_EQUAL(std::get<0>(*it), 20); |
| 812 | BOOST_CHECK_EQUAL(std::get<1>(*it), Name("ndn")); |
| 813 | |
| 814 | BOOST_REQUIRE_NO_THROW(i.getSelectedDelegation()); |
| 815 | BOOST_CHECK_EQUAL(i.getSelectedDelegation(), Name("local")); |
| 816 | } |
| 817 | |
| 818 | BOOST_AUTO_TEST_CASE(DecodeInterestWithLinkNonDecreasingOrder) |
| 819 | { |
| 820 | Block interestBlock(InterestWithLinkNonDecreasingOrder, |
| 821 | sizeof(InterestWithLinkNonDecreasingOrder)); |
| 822 | |
| 823 | ndn::Interest i; |
| 824 | BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock)); |
| 825 | BOOST_REQUIRE_NO_THROW(i.getSelectedDelegation()); |
| 826 | BOOST_CHECK_EQUAL(i.getSelectedDelegation(), Name("ndn")); |
| 827 | } |
| 828 | |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 829 | BOOST_AUTO_TEST_CASE(InterestContainingSelectedDelegationButNoLink) |
| 830 | { |
| 831 | Block interestBlock(InterestWithSelectedDelegationButNoLink, |
| 832 | sizeof(InterestWithSelectedDelegationButNoLink)); |
| 833 | |
| 834 | ndn::Interest i; |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 835 | BOOST_CHECK_THROW(i.wireDecode(interestBlock), Interest::Error); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | BOOST_AUTO_TEST_CASE(SelectedDelegationIsNotNonNegativeInteger) |
| 839 | { |
| 840 | Block interestBlock(InterestWithLinkNotNonIntegerSelectedDelegation, |
| 841 | sizeof(InterestWithLinkNotNonIntegerSelectedDelegation)); |
| 842 | |
| 843 | ndn::Interest i; |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 844 | BOOST_CHECK_THROW(i.wireDecode(interestBlock), tlv::Error); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | BOOST_AUTO_TEST_CASE(SelectedDelegationEqualToDelegationCount) |
| 848 | { |
| 849 | Link link1("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 850 | m_keyChain.sign(link1); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 851 | Block wire = link1.wireEncode(); |
| 852 | |
| 853 | Interest a; |
| 854 | a.setName("/Test/Encode/Decode/With/Link"); |
| 855 | a.setChildSelector(1); |
| 856 | a.setNonce(100); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 857 | a.setInterestLifetime(time::seconds(10)); |
| 858 | a.setLink(wire); |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 859 | BOOST_CHECK_THROW(a.setSelectedDelegation(3), Interest::Error); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | BOOST_AUTO_TEST_CASE(SelectedDelegationGreaterThanDelegationCount) |
| 863 | { |
| 864 | Link link1("test", {{10, "/test1"}, {20, "/test2"}, {100, "/test3"}}); |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 865 | m_keyChain.sign(link1); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 866 | Block wire = link1.wireEncode(); |
| 867 | |
| 868 | Interest a; |
| 869 | a.setName("/Test/Encode/Decode/With/Link"); |
| 870 | a.setChildSelector(1); |
| 871 | a.setNonce(100); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 872 | a.setInterestLifetime(time::seconds(10)); |
| 873 | a.setLink(wire); |
Alexander Afanasyev | cac0838 | 2015-09-02 14:52:40 -0700 | [diff] [blame] | 874 | BOOST_CHECK_THROW(a.setSelectedDelegation(4), Interest::Error); |
Spyridon Mastorakis | c8188b3 | 2015-04-18 18:33:38 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Junxiao Shi | 899277a | 2017-07-07 22:12:12 +0000 | [diff] [blame] | 877 | BOOST_AUTO_TEST_SUITE_END() // LinkSelectedDelegation |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 878 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 879 | BOOST_AUTO_TEST_SUITE_END() // TestInterest |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 880 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 881 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 882 | } // namespace ndn |