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