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 | c8823bc | 2014-02-09 19:33:33 -0800 | [diff] [blame] | 2 | /** |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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 | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "exclude.hpp" |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 23 | #include "util/crypto.hpp" |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 25 | #include "boost-test.hpp" |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 28 | namespace tests { |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TestExclude) |
| 31 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 32 | BOOST_AUTO_TEST_SUITE(GenericComponent) // exclude generic NameComponent |
| 33 | |
| 34 | BOOST_AUTO_TEST_CASE(One) |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 35 | { |
| 36 | Exclude e; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 37 | e.excludeOne(name::Component("b")); |
| 38 | BOOST_CHECK_EQUAL(e.size(), 1); |
| 39 | BOOST_CHECK_EQUAL(e.toUri(), "b"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 40 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 41 | e.excludeOne(name::Component("d")); |
| 42 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 43 | BOOST_CHECK_EQUAL(e.toUri(), "b,d"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 44 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 45 | e.excludeOne(name::Component("a")); |
| 46 | BOOST_CHECK_EQUAL(e.size(), 3); |
| 47 | BOOST_CHECK_EQUAL(e.toUri(), "a,b,d"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 49 | e.excludeOne(name::Component("aa")); |
| 50 | BOOST_CHECK_EQUAL(e.size(), 4); |
| 51 | BOOST_CHECK_EQUAL(e.toUri(), "a,b,d,aa"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 52 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 53 | e.excludeOne(name::Component("cc")); |
| 54 | BOOST_CHECK_EQUAL(e.size(), 5); |
| 55 | BOOST_CHECK_EQUAL(e.toUri(), "a,b,d,aa,cc"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 57 | e.excludeOne(name::Component("c")); |
| 58 | BOOST_CHECK_EQUAL(e.size(), 6); |
| 59 | BOOST_CHECK_EQUAL(e.toUri(), "a,b,c,d,aa,cc"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 62 | BOOST_AUTO_TEST_CASE(Before) |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 63 | { |
| 64 | // based on http://redmine.named-data.net/issues/1158 |
| 65 | ndn::Exclude e; |
| 66 | BOOST_REQUIRE_NO_THROW(e.excludeBefore(name::Component("PuQxMaf91"))); |
| 67 | |
| 68 | BOOST_CHECK_EQUAL(e.toUri(), "*,PuQxMaf91"); |
| 69 | } |
| 70 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 71 | BOOST_AUTO_TEST_CASE(Ranges) |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 72 | { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 73 | // example: ANY /b /d ANY /f |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 74 | |
| 75 | Exclude e; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 76 | e.excludeOne(name::Component("b0")); |
| 77 | BOOST_CHECK_EQUAL(e.size(), 1); |
| 78 | BOOST_CHECK_EQUAL(e.toUri(), "b0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 79 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 80 | e.excludeBefore(name::Component("b1")); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 82 | BOOST_CHECK_EQUAL(e.toUri(), "*,b1"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 83 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 84 | e.excludeBefore(name::Component("c0")); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 86 | BOOST_CHECK_EQUAL(e.toUri(), "*,c0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 88 | e.excludeRange(name::Component("a0"), name::Component("c0")); |
| 89 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 90 | BOOST_CHECK_EQUAL(e.toUri(), "*,c0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 92 | e.excludeRange(name::Component("d0"), name::Component("e0")); |
| 93 | BOOST_CHECK_EQUAL(e.size(), 4); |
| 94 | BOOST_CHECK_EQUAL(e.toUri(), "*,c0,d0,*,e0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 96 | e.excludeRange(name::Component("c1"), name::Component("d1")); |
| 97 | BOOST_CHECK_EQUAL(e.size(), 4); |
| 98 | BOOST_CHECK_EQUAL(e.toUri(), "*,c0,c1,*,e0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 100 | e.excludeRange(name::Component("a1"), name::Component("d1")); |
| 101 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 102 | BOOST_CHECK_EQUAL(e.toUri(), "*,e0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 104 | e.excludeBefore(name::Component("e2")); |
| 105 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 106 | BOOST_CHECK_EQUAL(e.toUri(), "*,e2"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 108 | e.excludeAfter(name::Component("f0")); |
| 109 | BOOST_CHECK_EQUAL(e.size(), 3); |
| 110 | BOOST_CHECK_EQUAL(e.toUri(), "*,e2,f0,*"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 111 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 112 | e.excludeAfter(name::Component("e5")); |
| 113 | BOOST_CHECK_EQUAL(e.size(), 3); |
| 114 | BOOST_CHECK_EQUAL(e.toUri(), "*,e2,e5,*"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 115 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 116 | e.excludeAfter(name::Component("b2")); |
| 117 | BOOST_CHECK_EQUAL(e.size(), 1); |
| 118 | BOOST_CHECK_EQUAL(e.toUri(), "*"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 119 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 120 | BOOST_REQUIRE_THROW(e.excludeRange(name::Component("d0"), name::Component("a0")), |
| 121 | Exclude::Error); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 124 | BOOST_AUTO_TEST_SUITE_END() // GenericComponent |
| 125 | |
| 126 | |
| 127 | BOOST_AUTO_TEST_SUITE(ImplicitDigest) // exclude ImplicitSha256DigestComponent |
| 128 | |
| 129 | /** \brief make a name::Component with an octet repeated crypto::SHA256_DIGEST_SIZE times |
| 130 | * \param octet the octet to fill the component |
| 131 | * \param isDigest whether to make an ImplicitSha256DigestComponent or a generic NameComponent |
| 132 | * \param lastOctet if non-negative, set the last octet to a different value |
| 133 | */ |
| 134 | static name::Component |
| 135 | makeComponent(uint8_t octet, bool isDigest, int lastOctet = -1) |
| 136 | { |
| 137 | uint8_t wire[crypto::SHA256_DIGEST_SIZE]; |
| 138 | std::memset(wire, octet, sizeof(wire)); |
| 139 | if (lastOctet >= 0) { |
| 140 | wire[crypto::SHA256_DIGEST_SIZE - 1] = static_cast<uint8_t>(lastOctet); |
| 141 | } |
| 142 | |
| 143 | if (isDigest) { |
| 144 | return name::Component::fromImplicitSha256Digest(wire, sizeof(wire)); |
| 145 | } |
| 146 | else { |
| 147 | return name::Component(wire, sizeof(wire)); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | BOOST_AUTO_TEST_CASE(One) |
| 152 | { |
| 153 | name::Component digestC = makeComponent(0xCC, true);; |
| 154 | name::Component genericC = makeComponent(0xCC, false); |
| 155 | name::Component digestD = makeComponent(0xDD, true); |
| 156 | |
| 157 | Exclude e; |
| 158 | e.excludeOne(digestC); |
| 159 | BOOST_CHECK_EQUAL(e.isExcluded(digestC), true); |
| 160 | BOOST_CHECK_EQUAL(e.isExcluded(genericC), false); |
| 161 | BOOST_CHECK_EQUAL(e.isExcluded(digestD), false); |
| 162 | |
| 163 | e.clear(); |
| 164 | e.excludeOne(genericC); |
| 165 | BOOST_CHECK_EQUAL(e.isExcluded(digestC), false); |
| 166 | BOOST_CHECK_EQUAL(e.isExcluded(genericC), true); |
| 167 | } |
| 168 | |
| 169 | BOOST_AUTO_TEST_CASE(BeforeDigest) |
| 170 | { |
| 171 | name::Component digestBA = makeComponent(0xBB, true, 0xBA); |
| 172 | name::Component digestBB = makeComponent(0xBB, true); |
| 173 | name::Component digestBC = makeComponent(0xBB, true, 0xBC); |
| 174 | |
| 175 | Exclude e; |
| 176 | e.excludeBefore(digestBB); |
| 177 | BOOST_CHECK_EQUAL(e.isExcluded(digestBA), true); |
| 178 | BOOST_CHECK_EQUAL(e.isExcluded(digestBB), true); |
| 179 | BOOST_CHECK_EQUAL(e.isExcluded(digestBC), false); |
| 180 | BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), false); |
| 181 | BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), false); |
| 182 | } |
| 183 | |
| 184 | BOOST_AUTO_TEST_CASE(BeforeGeneric) |
| 185 | { |
| 186 | name::Component digest0 = makeComponent(0x00, true); |
| 187 | name::Component digest9 = makeComponent(0x99, true); |
| 188 | name::Component digestF = makeComponent(0xFF, true); |
| 189 | |
| 190 | Exclude e; |
| 191 | e.excludeBefore(name::Component("")); |
| 192 | BOOST_CHECK_EQUAL(e.isExcluded(digest0), true); |
| 193 | BOOST_CHECK_EQUAL(e.isExcluded(digest9), true); |
| 194 | BOOST_CHECK_EQUAL(e.isExcluded(digestF), true); |
| 195 | BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true); |
| 196 | BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), false); |
| 197 | } |
| 198 | |
| 199 | BOOST_AUTO_TEST_CASE(AfterDigest) |
| 200 | { |
| 201 | name::Component digestBA = makeComponent(0xBB, true, 0xBA); |
| 202 | name::Component digestBB = makeComponent(0xBB, true); |
| 203 | name::Component digestBC = makeComponent(0xBB, true, 0xBC); |
| 204 | |
| 205 | Exclude e; |
| 206 | e.excludeAfter(digestBB); |
| 207 | BOOST_CHECK_EQUAL(e.isExcluded(digestBA), false); |
| 208 | BOOST_CHECK_EQUAL(e.isExcluded(digestBB), true); |
| 209 | BOOST_CHECK_EQUAL(e.isExcluded(digestBC), true); |
| 210 | BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true); |
| 211 | BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), true); |
| 212 | } |
| 213 | |
| 214 | BOOST_AUTO_TEST_CASE(AfterDigestFF) |
| 215 | { |
| 216 | name::Component digest00 = makeComponent(0x00, true); |
| 217 | name::Component digest99 = makeComponent(0x99, true); |
| 218 | name::Component digestFE = makeComponent(0xFF, true, 0xFE); |
| 219 | name::Component digestFF = makeComponent(0xFF, true); |
| 220 | |
| 221 | Exclude e; |
| 222 | e.excludeAfter(digestFF); |
| 223 | BOOST_CHECK_EQUAL(e.isExcluded(digest00), false); |
| 224 | BOOST_CHECK_EQUAL(e.isExcluded(digest99), false); |
| 225 | BOOST_CHECK_EQUAL(e.isExcluded(digestFE), false); |
| 226 | BOOST_CHECK_EQUAL(e.isExcluded(digestFF), true); |
| 227 | BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true); |
| 228 | BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), true); |
| 229 | } |
| 230 | |
| 231 | BOOST_AUTO_TEST_CASE(AfterGeneric) |
| 232 | { |
| 233 | name::Component digest0 = makeComponent(0x00, true); |
| 234 | name::Component digest9 = makeComponent(0x99, true); |
| 235 | name::Component digestF = makeComponent(0xFF, true); |
| 236 | |
| 237 | Exclude e; |
| 238 | e.excludeAfter(name::Component("")); |
| 239 | BOOST_CHECK_EQUAL(e.isExcluded(digest0), false); |
| 240 | BOOST_CHECK_EQUAL(e.isExcluded(digest9), false); |
| 241 | BOOST_CHECK_EQUAL(e.isExcluded(digestF), false); |
| 242 | BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true); |
| 243 | BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), true); |
| 244 | } |
| 245 | |
| 246 | BOOST_AUTO_TEST_CASE(RangeDigest) |
| 247 | { |
| 248 | name::Component digest0 = makeComponent(0x00, true); |
| 249 | name::Component digest7 = makeComponent(0x77, true); |
| 250 | name::Component digest8 = makeComponent(0x88, true); |
| 251 | name::Component digest9 = makeComponent(0x99, true); |
| 252 | name::Component digestF = makeComponent(0xFF, true); |
| 253 | |
| 254 | Exclude e; |
| 255 | e.excludeRange(digest7, digest9); |
| 256 | BOOST_CHECK_EQUAL(e.isExcluded(digest0), false); |
| 257 | BOOST_CHECK_EQUAL(e.isExcluded(digest7), true); |
| 258 | BOOST_CHECK_EQUAL(e.isExcluded(digest8), true); |
| 259 | BOOST_CHECK_EQUAL(e.isExcluded(digest9), true); |
| 260 | BOOST_CHECK_EQUAL(e.isExcluded(digestF), false); |
| 261 | BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), false); |
| 262 | BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), false); |
| 263 | } |
| 264 | |
| 265 | BOOST_AUTO_TEST_CASE(RangeDigestReverse) |
| 266 | { |
| 267 | name::Component digest7 = makeComponent(0x77, true); |
| 268 | name::Component digest9 = makeComponent(0x99, true); |
| 269 | |
| 270 | Exclude e; |
| 271 | BOOST_CHECK_THROW(e.excludeRange(digest9, digest7), Exclude::Error); |
| 272 | } |
| 273 | |
| 274 | BOOST_AUTO_TEST_CASE(RangeDigestGeneric) |
| 275 | { |
| 276 | name::Component digest0 = makeComponent(0x00, true); |
| 277 | name::Component digest7 = makeComponent(0x77, true); |
| 278 | name::Component digest9 = makeComponent(0x99, true); |
| 279 | name::Component digestF = makeComponent(0xFF, true); |
| 280 | |
| 281 | Exclude e; |
| 282 | e.excludeRange(digest9, name::Component("")); |
| 283 | BOOST_CHECK_EQUAL(e.isExcluded(digest0), false); |
| 284 | BOOST_CHECK_EQUAL(e.isExcluded(digest7), false); |
| 285 | BOOST_CHECK_EQUAL(e.isExcluded(digest9), true); |
| 286 | BOOST_CHECK_EQUAL(e.isExcluded(digestF), true); |
| 287 | BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true); |
| 288 | BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), false); |
| 289 | } |
| 290 | |
| 291 | BOOST_AUTO_TEST_CASE(RangeGenericDigest) |
| 292 | { |
| 293 | name::Component digestF = makeComponent(0xFF, true); |
| 294 | |
| 295 | Exclude e; |
| 296 | BOOST_CHECK_THROW(e.excludeRange(name::Component(""), digestF), Exclude::Error); |
| 297 | } |
| 298 | |
| 299 | BOOST_AUTO_TEST_SUITE_END() // ImplicitDigest |
| 300 | |
| 301 | |
| 302 | BOOST_AUTO_TEST_SUITE(WireCompare) // wireEncode, wireDecode, operator==, operator!= |
| 303 | |
| 304 | BOOST_AUTO_TEST_CASE(EqualityComparable) |
| 305 | { |
| 306 | Exclude e1; |
| 307 | Exclude e2; |
| 308 | BOOST_CHECK_EQUAL(e1, e2); |
| 309 | |
| 310 | e1.excludeOne(name::Component("T")); |
| 311 | BOOST_CHECK_NE(e1, e2); |
| 312 | |
| 313 | e2.excludeOne(name::Component("D")); |
| 314 | BOOST_CHECK_NE(e1, e2); |
| 315 | |
| 316 | e2.clear(); |
| 317 | e2.excludeOne(name::Component("T")); |
| 318 | BOOST_CHECK_EQUAL(e1, e2); |
| 319 | |
| 320 | e2.clear(); |
| 321 | const uint8_t EXCLUDE[] = { 0x10, 0x15, 0x13, 0x00, 0x08, 0x01, 0x41, 0x08, 0x01, 0x42, |
| 322 | 0x08, 0x01, 0x43, 0x13, 0x00, 0x08, 0x01, 0x44, 0x08, 0x01, |
| 323 | 0x45, 0x13, 0x00 }; |
| 324 | e2.wireDecode(Block(EXCLUDE, sizeof(EXCLUDE))); |
| 325 | |
| 326 | e1.clear(); |
| 327 | e1.excludeBefore(name::Component("A")); |
| 328 | e1.excludeOne(name::Component("B")); |
| 329 | e1.excludeRange(name::Component("C"), name::Component("D")); |
| 330 | e1.excludeAfter(name::Component("E")); |
| 331 | BOOST_CHECK_EQUAL(e1, e2); |
| 332 | } |
| 333 | |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 334 | BOOST_AUTO_TEST_CASE(Malformed) |
| 335 | { |
| 336 | Exclude e1; |
| 337 | BOOST_CHECK_THROW(e1.wireEncode(), Exclude::Error); |
| 338 | |
| 339 | Exclude e2; |
| 340 | |
| 341 | // top-level TLV-TYPE is not tlv::Exclude |
| 342 | const uint8_t NON_EXCLUDE[] = { 0x01, 0x02, 0x13, 0x00 }; |
| 343 | BOOST_CHECK_THROW(e2.wireDecode(Block(NON_EXCLUDE, sizeof(NON_EXCLUDE))), |
| 344 | tlv::Error); |
| 345 | |
| 346 | // Exclude element is empty |
| 347 | const uint8_t EMPTY_EXCLUDE[] = { 0x10, 0x00 }; |
| 348 | BOOST_CHECK_THROW(e2.wireDecode(Block(EMPTY_EXCLUDE, sizeof(EMPTY_EXCLUDE))), |
| 349 | Exclude::Error); |
| 350 | |
| 351 | // Exclude element contains unknown element |
| 352 | const uint8_t UNKNOWN_COMP1[] = { 0x10, 0x02, 0xAA, 0x00 }; |
| 353 | BOOST_CHECK_THROW(e2.wireDecode(Block(UNKNOWN_COMP1, sizeof(UNKNOWN_COMP1))), |
| 354 | Exclude::Error); |
| 355 | |
| 356 | // Exclude element contains unknown element |
| 357 | const uint8_t UNKNOWN_COMP2[] = { 0x10, 0x05, 0x08, 0x01, 0x54, 0xAA, 0x00 }; |
| 358 | BOOST_CHECK_THROW(e2.wireDecode(Block(UNKNOWN_COMP2, sizeof(UNKNOWN_COMP2))), |
| 359 | Exclude::Error); |
| 360 | |
| 361 | // // <Exclude><Any/></Exclude> |
| 362 | // const uint8_t ONLY_ANY[] = { 0x10, 0x02, 0x13, 0x00 }; |
| 363 | // BOOST_CHECK_THROW(e2.wireDecode(Block(ONLY_ANY, sizeof(ONLY_ANY))), |
| 364 | // Exclude::Error); |
| 365 | |
| 366 | // <Exclude><Any/><Any/></Exclude> |
| 367 | const uint8_t ANY_ANY[] = { 0x10, 0x04, 0x13, 0x00, 0x13, 0x00 }; |
| 368 | BOOST_CHECK_THROW(e2.wireDecode(Block(ANY_ANY, sizeof(ANY_ANY))), |
| 369 | Exclude::Error); |
| 370 | |
| 371 | // // <Exclude><Any/><NameComponent>T</NameComponent><Any/></Exclude> |
| 372 | // const uint8_t ANY_COMPONENT_ANY[] = { 0x10, 0x07, 0x13, 0x00, 0x08, 0x01, 0x54, 0x13, 0x00 }; |
| 373 | // BOOST_CHECK_THROW(e2.wireDecode(Block(ANY_COMPONENT_ANY, sizeof(ANY_COMPONENT_ANY))), |
| 374 | // Exclude::Error); |
Ilya Moiseenko | 741771f | 2015-03-10 16:53:44 -0700 | [diff] [blame] | 375 | |
| 376 | uint8_t WIRE[] = { |
| 377 | 0x10, 0x20, // Exclude |
| 378 | 0x01, 0x1E, // ImplicitSha256DigestComponent with incorrect length |
| 379 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 380 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 381 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 382 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd |
| 383 | }; |
| 384 | |
| 385 | BOOST_CHECK_THROW(Exclude().wireDecode(Block(WIRE, sizeof(WIRE))), Exclude::Error); |
| 386 | } |
| 387 | |
| 388 | BOOST_AUTO_TEST_CASE(ImplicitSha256Digest) |
| 389 | { |
| 390 | uint8_t WIRE[] = { |
| 391 | 0x10, 0x22, // Exclude |
| 392 | 0x01, 0x20, // ImplicitSha256DigestComponent |
| 393 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 394 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 395 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 396 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd |
| 397 | }; |
| 398 | |
| 399 | Block block(WIRE, sizeof(WIRE)); |
| 400 | |
| 401 | Exclude exclude; |
| 402 | BOOST_CHECK_NO_THROW(exclude.wireDecode(block)); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 403 | BOOST_CHECK(exclude.wireEncode() == block); |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 406 | BOOST_AUTO_TEST_CASE(EmptyComponent) // Bug #2660 |
Alexander Afanasyev | c076e6d | 2015-04-02 20:07:13 -0700 | [diff] [blame] | 407 | { |
| 408 | Exclude e1, e2; |
| 409 | |
| 410 | e1.excludeOne(name::Component()); |
| 411 | e2.excludeOne(name::Component("")); |
| 412 | |
| 413 | BOOST_CHECK_EQUAL(e1, e2); |
| 414 | BOOST_CHECK_EQUAL(e1.toUri(), e2.toUri()); |
| 415 | BOOST_CHECK(e1.wireEncode() == e2.wireEncode()); |
| 416 | |
| 417 | BOOST_CHECK_EQUAL("...", e1.toUri()); |
| 418 | |
| 419 | uint8_t WIRE[] {0x10, 0x02, 0x08, 0x00}; |
| 420 | BOOST_CHECK_EQUAL_COLLECTIONS(e1.wireEncode().begin(), e1.wireEncode().end(), |
| 421 | WIRE, WIRE + sizeof(WIRE)); |
| 422 | |
| 423 | Exclude e3(Block(WIRE, sizeof(WIRE))); |
| 424 | BOOST_CHECK_EQUAL(e1, e3); |
| 425 | BOOST_CHECK_EQUAL(e1.toUri(), e3.toUri()); |
| 426 | } |
| 427 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 428 | BOOST_AUTO_TEST_SUITE_END() // WireCompare |
| 429 | |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 430 | BOOST_AUTO_TEST_SUITE_END() |
| 431 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 432 | } // namespace tests |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 433 | } // namespace ndn |