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