Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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" |
| 24 | #include "security/signature-sha256-with-rsa.hpp" |
| 25 | #include "security/signature-sha256.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 26 | #include "encoding/buffer-stream.hpp" |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 28 | #include "boost-test.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 | |
| 33 | BOOST_AUTO_TEST_SUITE(TestInterest) |
| 34 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 35 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Interest>)); |
| 36 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Selectors>)); |
| 37 | |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 38 | const uint8_t Interest1[] = { |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 39 | 0x05, 0x59, // NDN Interest |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 40 | 0x07, 0x14, // Name |
| 41 | 0x08, 0x5, // NameComponent |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 42 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 43 | 0x08, 0x3, // NameComponent |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 44 | 0x6e, 0x64, 0x6e, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 45 | 0x08, 0x6, // NameComponent |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 46 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 47 | 0x09, 0x37, // Selectors |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 48 | 0x0d, 0x1, 0x1, // MinSuffix |
| 49 | 0x0e, 0x1, 0x1, // MaxSuffix |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 50 | 0x1c, 0x16, // KeyLocator |
| 51 | 0x07, 0x14, // Name |
| 52 | 0x08, 0x04, |
| 53 | 0x74, 0x65, 0x73, 0x74, |
| 54 | 0x08, 0x03, |
| 55 | 0x6b, 0x65, 0x79, |
| 56 | 0x08, 0x07, |
| 57 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 58 | 0x10, 0x14, // Exclude |
| 59 | 0x08, 0x4, // NameComponent |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 60 | 0x61, 0x6c, 0x65, 0x78, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 61 | 0x08, 0x4, // NameComponent |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 62 | 0x78, 0x78, 0x78, 0x78, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 63 | 0x13, 0x0, // Any |
| 64 | 0x08, 0x4, // NameComponent |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 65 | 0x79, 0x79, 0x79, 0x79, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 66 | 0x11, 0x1, // ChildSelector |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 67 | 0x1, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 68 | 0x0a, 0x1, // Nonce |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 69 | 0x1, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 70 | 0x0b, 0x1, // Scope |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 71 | 0x1, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 72 | 0x0c, // InterestLifetime |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 73 | 0x2, 0x3, 0xe8 |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 76 | const uint8_t InterestWithLocalControlHeader[] = { |
| 77 | 0x50, 0x22, 0x51, 0x01, 0x0a, 0x05, 0x1d, 0x07, 0x14, 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, |
| 78 | 0x6c, 0x08, 0x03, 0x6e, 0x64, 0x6e, 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x09, |
| 79 | 0x02, 0x12, 0x00, 0x0a, 0x01, 0x01 |
| 80 | }; |
| 81 | |
| 82 | const uint8_t InterestWithoutLocalControlHeader[] = { |
| 83 | 0x05, 0x1d, 0x07, 0x14, 0x08, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x08, 0x03, 0x6e, 0x64, |
| 84 | 0x6e, 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, 0x09, 0x02, 0x12, 0x00, 0x0a, 0x01, |
| 85 | 0x01 |
| 86 | }; |
| 87 | |
Alexander Afanasyev | ff2d08f | 2014-04-07 18:28:25 -0700 | [diff] [blame] | 88 | BOOST_AUTO_TEST_CASE(InterestEqualityChecks) |
| 89 | { |
| 90 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 91 | // Name |
| 92 | // Selectors? |
| 93 | // Nonce |
| 94 | // Scope? |
| 95 | // InterestLifetime? |
| 96 | |
| 97 | Interest a; |
| 98 | Interest b; |
| 99 | |
| 100 | // if nonce is not set, it will be set to a random value |
| 101 | a.setNonce(1); |
| 102 | b.setNonce(1); |
| 103 | |
| 104 | BOOST_CHECK_EQUAL(a == b, true); |
| 105 | BOOST_CHECK_EQUAL(a != b, false); |
| 106 | |
| 107 | // check comparison on Name |
| 108 | a.setName("ndn:/A"); |
| 109 | BOOST_CHECK_EQUAL(a == b, false); |
| 110 | BOOST_CHECK_EQUAL(a != b, true); |
| 111 | |
| 112 | b.setName("ndn:/B"); |
| 113 | BOOST_CHECK_EQUAL(a == b, false); |
| 114 | BOOST_CHECK_EQUAL(a != b, true); |
| 115 | |
| 116 | b.setName("ndn:/A"); |
| 117 | BOOST_CHECK_EQUAL(a == b, true); |
| 118 | BOOST_CHECK_EQUAL(a != b, false); |
| 119 | |
| 120 | // check comparison on Selectors |
| 121 | a.setChildSelector(1); |
| 122 | BOOST_CHECK_EQUAL(a == b, false); |
| 123 | BOOST_CHECK_EQUAL(a != b, true); |
| 124 | |
| 125 | b.setChildSelector(1); |
| 126 | BOOST_CHECK_EQUAL(a == b, true); |
| 127 | BOOST_CHECK_EQUAL(a != b, false); |
| 128 | |
| 129 | // check comparison on Nonce |
| 130 | a.setNonce(100); |
| 131 | BOOST_CHECK_EQUAL(a == b, false); |
| 132 | BOOST_CHECK_EQUAL(a != b, true); |
| 133 | |
| 134 | b.setNonce(100); |
| 135 | BOOST_CHECK_EQUAL(a == b, true); |
| 136 | BOOST_CHECK_EQUAL(a != b, false); |
| 137 | |
| 138 | // check comparison on Nonce |
| 139 | a.setScope(1); |
| 140 | BOOST_CHECK_EQUAL(a == b, false); |
| 141 | BOOST_CHECK_EQUAL(a != b, true); |
| 142 | |
| 143 | b.setScope(1); |
| 144 | BOOST_CHECK_EQUAL(a == b, true); |
| 145 | BOOST_CHECK_EQUAL(a != b, false); |
| 146 | |
| 147 | // check comparison on InterestLifetime |
| 148 | a.setInterestLifetime(time::seconds(10)); |
| 149 | BOOST_CHECK_EQUAL(a == b, false); |
| 150 | BOOST_CHECK_EQUAL(a != b, true); |
| 151 | |
| 152 | b.setInterestLifetime(time::seconds(10)); |
| 153 | BOOST_CHECK_EQUAL(a == b, true); |
| 154 | BOOST_CHECK_EQUAL(a != b, false); |
| 155 | } |
| 156 | |
| 157 | BOOST_AUTO_TEST_CASE(SelectorsEqualityChecks) |
| 158 | { |
| 159 | // Selectors ::= SELECTORS-TYPE TLV-LENGTH |
| 160 | // MinSuffixComponents? |
| 161 | // MaxSuffixComponents? |
| 162 | // PublisherPublicKeyLocator? |
| 163 | // Exclude? |
| 164 | // ChildSelector? |
| 165 | // MustBeFresh? |
| 166 | |
| 167 | Selectors a; |
| 168 | Selectors b; |
| 169 | BOOST_CHECK_EQUAL(a == b, true); |
| 170 | BOOST_CHECK_EQUAL(a != b, false); |
| 171 | |
| 172 | // MinSuffixComponents |
| 173 | a.setMinSuffixComponents(1); |
| 174 | BOOST_CHECK_EQUAL(a == b, false); |
| 175 | BOOST_CHECK_EQUAL(a != b, true); |
| 176 | |
| 177 | b.setMinSuffixComponents(2); |
| 178 | BOOST_CHECK_EQUAL(a == b, false); |
| 179 | BOOST_CHECK_EQUAL(a != b, true); |
| 180 | |
| 181 | b.setMinSuffixComponents(1); |
| 182 | BOOST_CHECK_EQUAL(a == b, true); |
| 183 | BOOST_CHECK_EQUAL(a != b, false); |
| 184 | |
| 185 | // MaxSuffixComponents |
| 186 | a.setMaxSuffixComponents(10); |
| 187 | BOOST_CHECK_EQUAL(a == b, false); |
| 188 | BOOST_CHECK_EQUAL(a != b, true); |
| 189 | |
| 190 | b.setMaxSuffixComponents(10); |
| 191 | BOOST_CHECK_EQUAL(a == b, true); |
| 192 | BOOST_CHECK_EQUAL(a != b, false); |
| 193 | |
| 194 | // PublisherPublicKeyLocator |
| 195 | a.setPublisherPublicKeyLocator(KeyLocator("/key/Locator/name")); |
| 196 | BOOST_CHECK_EQUAL(a == b, false); |
| 197 | BOOST_CHECK_EQUAL(a != b, true); |
| 198 | |
| 199 | b.setPublisherPublicKeyLocator(KeyLocator("/key/Locator/name")); |
| 200 | BOOST_CHECK_EQUAL(a == b, true); |
| 201 | BOOST_CHECK_EQUAL(a != b, false); |
| 202 | |
| 203 | // Exclude |
| 204 | a.setExclude(Exclude().excludeOne(name::Component("exclude"))); |
| 205 | BOOST_CHECK_EQUAL(a == b, false); |
| 206 | BOOST_CHECK_EQUAL(a != b, true); |
| 207 | |
| 208 | b.setExclude(Exclude().excludeOne(name::Component("exclude"))); |
| 209 | BOOST_CHECK_EQUAL(a == b, true); |
| 210 | BOOST_CHECK_EQUAL(a != b, false); |
| 211 | |
| 212 | // ChildSelector |
| 213 | a.setChildSelector(1); |
| 214 | BOOST_CHECK_EQUAL(a == b, false); |
| 215 | BOOST_CHECK_EQUAL(a != b, true); |
| 216 | |
| 217 | b.setChildSelector(1); |
| 218 | BOOST_CHECK_EQUAL(a == b, true); |
| 219 | BOOST_CHECK_EQUAL(a != b, false); |
| 220 | |
| 221 | // MustBeFresh |
| 222 | a.setMustBeFresh(true); |
| 223 | BOOST_CHECK_EQUAL(a == b, false); |
| 224 | BOOST_CHECK_EQUAL(a != b, true); |
| 225 | |
| 226 | b.setMustBeFresh(true); |
| 227 | BOOST_CHECK_EQUAL(a == b, true); |
| 228 | BOOST_CHECK_EQUAL(a != b, false); |
| 229 | } |
| 230 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 231 | BOOST_AUTO_TEST_CASE(Decode) |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 232 | { |
| 233 | Block interestBlock(Interest1, sizeof(Interest1)); |
Alexander Afanasyev | 636e9f1 | 2014-01-07 12:01:03 -0800 | [diff] [blame] | 234 | |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 235 | ndn::Interest i; |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 236 | BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock)); |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 237 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 238 | BOOST_CHECK_EQUAL(i.getName().toUri(), "/local/ndn/prefix"); |
| 239 | BOOST_CHECK_EQUAL(i.getScope(), 1); |
| 240 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), time::milliseconds(1000)); |
| 241 | BOOST_CHECK_EQUAL(i.getMinSuffixComponents(), 1); |
| 242 | BOOST_CHECK_EQUAL(i.getMaxSuffixComponents(), 1); |
| 243 | BOOST_CHECK_EQUAL(i.getPublisherPublicKeyLocator().getType(), |
| 244 | static_cast<uint32_t>(KeyLocator::KeyLocator_Name)); |
| 245 | BOOST_CHECK_EQUAL(i.getPublisherPublicKeyLocator().getName(), "ndn:/test/key/locator"); |
| 246 | BOOST_CHECK_EQUAL(i.getChildSelector(), 1); |
| 247 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 248 | BOOST_CHECK_EQUAL(i.getExclude().toUri(), "alex,xxxx,*,yyyy"); |
| 249 | BOOST_CHECK_EQUAL(i.getNonce(), 1U); |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 250 | } |
| 251 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 252 | BOOST_AUTO_TEST_CASE(DecodeFromStream) |
Alexander Afanasyev | 6835ad8 | 2014-02-12 10:07:20 -0800 | [diff] [blame] | 253 | { |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 254 | boost::iostreams::stream<boost::iostreams::array_source> is( |
| 255 | reinterpret_cast<const char *>(Interest1), sizeof(Interest1)); |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 256 | |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 257 | Block interestBlock = Block::fromStream(is); |
Alexander Afanasyev | 6835ad8 | 2014-02-12 10:07:20 -0800 | [diff] [blame] | 258 | |
| 259 | ndn::Interest i; |
| 260 | BOOST_REQUIRE_NO_THROW(i.wireDecode(interestBlock)); |
| 261 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 262 | BOOST_CHECK_EQUAL(i.getName().toUri(), "/local/ndn/prefix"); |
| 263 | BOOST_CHECK_EQUAL(i.getScope(), 1); |
| 264 | BOOST_CHECK_EQUAL(i.getInterestLifetime(), time::milliseconds(1000)); |
| 265 | BOOST_CHECK_EQUAL(i.getMinSuffixComponents(), 1); |
| 266 | BOOST_CHECK_EQUAL(i.getMaxSuffixComponents(), 1); |
| 267 | BOOST_CHECK_EQUAL(i.getChildSelector(), 1); |
| 268 | BOOST_CHECK_EQUAL(i.getMustBeFresh(), false); |
| 269 | BOOST_CHECK_EQUAL(i.getExclude().toUri(), "alex,xxxx,*,yyyy"); |
| 270 | BOOST_CHECK_EQUAL(i.getNonce(), 1U); |
Alexander Afanasyev | 6835ad8 | 2014-02-12 10:07:20 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 273 | BOOST_AUTO_TEST_CASE(Encode) |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 274 | { |
| 275 | ndn::Interest i(ndn::Name("/local/ndn/prefix")); |
| 276 | i.setScope(1); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 277 | i.setInterestLifetime(time::milliseconds(1000)); |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 278 | i.setMinSuffixComponents(1); |
| 279 | i.setMaxSuffixComponents(1); |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 280 | i.setPublisherPublicKeyLocator(KeyLocator("ndn:/test/key/locator")); |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 281 | i.setChildSelector(1); |
| 282 | i.setMustBeFresh(false); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 283 | Exclude exclude; |
| 284 | exclude |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 285 | .excludeOne(name::Component("alex")) |
| 286 | .excludeRange(name::Component("xxxx"), name::Component("yyyy")); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 287 | i.setExclude(exclude); |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 288 | i.setNonce(1); |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 289 | |
| 290 | const Block &wire = i.wireEncode(); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 291 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 292 | BOOST_CHECK_EQUAL_COLLECTIONS(Interest1, Interest1 + sizeof(Interest1), |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 293 | wire.begin(), wire.end()); |
| 294 | } |
| 295 | |
| 296 | |
| 297 | BOOST_AUTO_TEST_CASE(EncodeWithLocalHeader) |
| 298 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 299 | ndn::Interest interest(ndn::Name("/local/ndn/prefix")); |
| 300 | interest.setMustBeFresh(true); |
| 301 | interest.setIncomingFaceId(10); |
| 302 | interest.setNonce(1); |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 303 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 304 | BOOST_CHECK(!interest.hasWire()); |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 305 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 306 | Block headerBlock = interest.getLocalControlHeader().wireEncode(interest, true, true); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 307 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 308 | BOOST_CHECK(interest.hasWire()); |
| 309 | BOOST_CHECK(headerBlock.hasWire()); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 310 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 311 | BOOST_CHECK_NE(headerBlock.wire(), interest.wireEncode().wire()); |
| 312 | BOOST_CHECK_NE(headerBlock.size(), interest.wireEncode().size()); |
| 313 | BOOST_CHECK_EQUAL(headerBlock.size(), 5); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 314 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 315 | BOOST_CHECK_EQUAL_COLLECTIONS(InterestWithLocalControlHeader, |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 316 | InterestWithLocalControlHeader + 5, |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 317 | headerBlock.begin(), headerBlock.end()); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 318 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 319 | interest.setNonce(1000); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 320 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 321 | Block updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true); |
| 322 | BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 323 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 324 | // only length should have changed |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 325 | BOOST_CHECK_EQUAL_COLLECTIONS(updatedHeaderBlock.begin() + 2, updatedHeaderBlock.end(), |
| 326 | headerBlock.begin() + 2, headerBlock.end()); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 327 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 328 | // updating IncomingFaceId that keeps the length |
| 329 | interest.setIncomingFaceId(100); |
| 330 | updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true); |
| 331 | BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 332 | BOOST_CHECK_NE(*(updatedHeaderBlock.begin() + 4), *(headerBlock.begin() + 4)); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 333 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 334 | // updating IncomingFaceId that increases the length by 2 |
| 335 | interest.setIncomingFaceId(1000); |
| 336 | updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true); |
| 337 | BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 338 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 339 | // adding NextHopId |
| 340 | interest.setNextHopFaceId(1); |
| 341 | updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, true); |
| 342 | BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 9); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 343 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 344 | // masking IncomingFaceId |
| 345 | updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, false, true); |
| 346 | BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 5); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 347 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 348 | // masking NextHopId |
| 349 | updatedHeaderBlock = interest.getLocalControlHeader().wireEncode(interest, true, false); |
| 350 | BOOST_CHECK_EQUAL(updatedHeaderBlock.size(), 6); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 351 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 352 | // masking everything |
| 353 | BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, false), |
| 354 | nfd::LocalControlHeader::Error); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 355 | } |
| 356 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 357 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 358 | BOOST_AUTO_TEST_CASE(DecodeWithLocalHeader) |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 359 | { |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 360 | Block wireBlock(InterestWithLocalControlHeader, sizeof(InterestWithLocalControlHeader)); |
| 361 | const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock); |
| 362 | BOOST_REQUIRE_NE(&payload, &wireBlock); |
Alexander Afanasyev | 6d48bc1 | 2014-02-18 00:10:51 -0800 | [diff] [blame] | 363 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 364 | BOOST_CHECK_EQUAL(payload.type(), static_cast<uint32_t>(Tlv::Interest)); |
| 365 | BOOST_CHECK_EQUAL(wireBlock.type(), static_cast<uint32_t>(tlv::nfd::LocalControlHeader)); |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 366 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 367 | Interest interest(payload); |
| 368 | BOOST_CHECK(!interest.getLocalControlHeader().hasIncomingFaceId()); |
| 369 | BOOST_CHECK(!interest.getLocalControlHeader().hasNextHopFaceId()); |
| 370 | |
| 371 | BOOST_REQUIRE_NO_THROW(interest.getLocalControlHeader().wireDecode(wireBlock)); |
| 372 | |
| 373 | BOOST_CHECK_EQUAL(interest.getLocalControlHeader().wireEncode(interest, true, true).size(), 5); |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 374 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 375 | BOOST_CHECK_EQUAL(interest.getIncomingFaceId(), 10); |
| 376 | BOOST_CHECK(!interest.getLocalControlHeader().hasNextHopFaceId()); |
| 377 | |
| 378 | BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, false), |
| 379 | nfd::LocalControlHeader::Error); |
| 380 | |
| 381 | BOOST_CHECK_THROW(interest.getLocalControlHeader().wireEncode(interest, false, true), |
| 382 | nfd::LocalControlHeader::Error); |
| 383 | |
| 384 | BOOST_CHECK_NO_THROW(interest.getLocalControlHeader().wireEncode(interest, true, false)); |
| 385 | BOOST_CHECK_NO_THROW(interest.getLocalControlHeader().wireEncode(interest, true, true)); |
Junxiao Shi | b332e78 | 2014-03-31 14:23:46 -0700 | [diff] [blame] | 386 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 387 | BOOST_CHECK_NE((void*)interest.getLocalControlHeader().wireEncode(interest, true, true).wire(), |
| 388 | (void*)wireBlock.wire()); |
| 389 | |
| 390 | BOOST_CHECK_EQUAL(interest.getLocalControlHeader().wireEncode(interest, true, true).size(), 5); |
| 391 | } |
| 392 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 393 | BOOST_AUTO_TEST_CASE(DecodeWithoutLocalHeader) |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 394 | { |
| 395 | Block wireBlock(InterestWithoutLocalControlHeader, sizeof(InterestWithoutLocalControlHeader)); |
| 396 | const Block& payload = nfd::LocalControlHeader::getPayload(wireBlock); |
| 397 | BOOST_CHECK_EQUAL(&payload, &wireBlock); |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 400 | BOOST_AUTO_TEST_CASE(MatchesData) |
| 401 | { |
| 402 | Interest interest; |
| 403 | interest.setName("ndn:/A") |
| 404 | .setMinSuffixComponents(2) |
| 405 | .setMaxSuffixComponents(2) |
| 406 | .setPublisherPublicKeyLocator(KeyLocator("ndn:/B")) |
| 407 | .setExclude(Exclude().excludeBefore(name::Component("C"))); |
| 408 | |
| 409 | Data data("ndn:/A/D"); |
| 410 | SignatureSha256WithRsa signature; |
| 411 | signature.setKeyLocator(KeyLocator("ndn:/B")); |
| 412 | data.setSignature(signature); |
| 413 | BOOST_CHECK_EQUAL(interest.matchesData(data), true); |
| 414 | |
| 415 | Data data1 = data; |
| 416 | data1.setName("ndn:/A");// violates MinSuffixComponents |
| 417 | BOOST_CHECK_EQUAL(interest.matchesData(data1), false); |
| 418 | |
| 419 | Data data2 = data; |
| 420 | data2.setName("ndn:/A/E/F");// violates MaxSuffixComponents |
| 421 | BOOST_CHECK_EQUAL(interest.matchesData(data2), false); |
| 422 | |
| 423 | Data data3 = data; |
| 424 | SignatureSha256WithRsa signature3; |
| 425 | signature3.setKeyLocator(KeyLocator("ndn:/G"));// violates PublisherPublicKeyLocator |
| 426 | data3.setSignature(signature3); |
| 427 | BOOST_CHECK_EQUAL(interest.matchesData(data3), false); |
| 428 | |
| 429 | Data data4 = data; |
| 430 | SignatureSha256 signature4;// violates PublisherPublicKeyLocator |
| 431 | data4.setSignature(signature4); |
| 432 | BOOST_CHECK_EQUAL(interest.matchesData(data4), false); |
| 433 | |
| 434 | Data data5 = data; |
| 435 | data5.setName("ndn:/A/C");// violates Exclude |
| 436 | BOOST_CHECK_EQUAL(interest.matchesData(data5), false); |
| 437 | |
| 438 | Data data6 = data; |
| 439 | data6.setName("ndn:/H/I");// violates Name |
| 440 | BOOST_CHECK_EQUAL(interest.matchesData(data6), false); |
| 441 | } |
| 442 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 443 | BOOST_AUTO_TEST_CASE(InterestFilterMatching) |
| 444 | { |
| 445 | BOOST_CHECK_EQUAL(InterestFilter("/a").doesMatch("/a/b"), true); |
| 446 | BOOST_CHECK_EQUAL(InterestFilter("/a/b").doesMatch("/a/b"), true); |
| 447 | BOOST_CHECK_EQUAL(InterestFilter("/a/b/c").doesMatch("/a/b"), false); |
| 448 | |
| 449 | BOOST_CHECK_EQUAL(InterestFilter("/a", "<b>").doesMatch("/a/b"), true); |
| 450 | BOOST_CHECK_EQUAL(InterestFilter("/a/b", "<b>").doesMatch("/a/b"), false); |
| 451 | |
| 452 | BOOST_CHECK_EQUAL(InterestFilter("/a/b", "<b>").doesMatch("/a/b/c/b"), false); |
| 453 | BOOST_CHECK_EQUAL(InterestFilter("/a/b", "<>*<b>").doesMatch("/a/b/c/b"), true); |
| 454 | |
| 455 | BOOST_CHECK_EQUAL(InterestFilter("/a", "<b>").doesMatch("/a/b/c/d"), false); |
| 456 | BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>*").doesMatch("/a/b/c/d"), true); |
| 457 | BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>*").doesMatch("/a/b"), true); |
| 458 | BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>+").doesMatch("/a/b"), false); |
| 459 | BOOST_CHECK_EQUAL(InterestFilter("/a", "<b><>+").doesMatch("/a/b/c"), true); |
| 460 | } |
| 461 | |
Alexander Afanasyev | 5fa9e9a | 2013-12-24 19:45:07 -0800 | [diff] [blame] | 462 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 463 | |
Alexander Afanasyev | 9016496 | 2014-03-06 08:29:59 +0000 | [diff] [blame] | 464 | } // namespace tests |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 465 | } // namespace ndn |