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