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