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 | /** |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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" |
| 23 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 24 | #include "boost-test.hpp" |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 27 | namespace tests { |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 28 | |
| 29 | BOOST_AUTO_TEST_SUITE(TestExclude) |
| 30 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 31 | BOOST_AUTO_TEST_CASE(Basic) |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 32 | { |
| 33 | Exclude e; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 34 | e.excludeOne(name::Component("b")); |
| 35 | BOOST_CHECK_EQUAL(e.size(), 1); |
| 36 | BOOST_CHECK_EQUAL(e.toUri(), "b"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 38 | e.excludeOne(name::Component("d")); |
| 39 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 40 | BOOST_CHECK_EQUAL(e.toUri(), "b,d"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 42 | e.excludeOne(name::Component("a")); |
| 43 | BOOST_CHECK_EQUAL(e.size(), 3); |
| 44 | BOOST_CHECK_EQUAL(e.toUri(), "a,b,d"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 45 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 46 | e.excludeOne(name::Component("aa")); |
| 47 | BOOST_CHECK_EQUAL(e.size(), 4); |
| 48 | BOOST_CHECK_EQUAL(e.toUri(), "a,b,d,aa"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 50 | e.excludeOne(name::Component("cc")); |
| 51 | BOOST_CHECK_EQUAL(e.size(), 5); |
| 52 | BOOST_CHECK_EQUAL(e.toUri(), "a,b,d,aa,cc"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 54 | e.excludeOne(name::Component("c")); |
| 55 | BOOST_CHECK_EQUAL(e.size(), 6); |
| 56 | BOOST_CHECK_EQUAL(e.toUri(), "a,b,c,d,aa,cc"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame] | 59 | BOOST_AUTO_TEST_CASE(EqualityComparable) |
| 60 | { |
| 61 | Exclude e1; |
| 62 | Exclude e2; |
| 63 | BOOST_CHECK_EQUAL(e1, e2); |
| 64 | |
| 65 | e1.excludeOne(name::Component("T")); |
| 66 | BOOST_CHECK_NE(e1, e2); |
| 67 | |
| 68 | e2.excludeOne(name::Component("D")); |
| 69 | BOOST_CHECK_NE(e1, e2); |
| 70 | |
| 71 | e2.clear(); |
| 72 | e2.excludeOne(name::Component("T")); |
| 73 | BOOST_CHECK_EQUAL(e1, e2); |
| 74 | |
| 75 | e2.clear(); |
| 76 | const uint8_t EXCLUDE[] = { 0x10, 0x15, 0x13, 0x00, 0x08, 0x01, 0x41, 0x08, 0x01, 0x42, |
| 77 | 0x08, 0x01, 0x43, 0x13, 0x00, 0x08, 0x01, 0x44, 0x08, 0x01, |
| 78 | 0x45, 0x13, 0x00 }; |
| 79 | e2.wireDecode(Block(EXCLUDE, sizeof(EXCLUDE))); |
| 80 | |
| 81 | e1.clear(); |
| 82 | e1.excludeBefore(name::Component("A")); |
| 83 | e1.excludeOne(name::Component("B")); |
| 84 | e1.excludeRange(name::Component("C"), name::Component("D")); |
| 85 | e1.excludeAfter(name::Component("E")); |
| 86 | BOOST_CHECK_EQUAL(e1, e2); |
| 87 | } |
| 88 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 89 | BOOST_AUTO_TEST_CASE(Before) |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 90 | { |
| 91 | // based on http://redmine.named-data.net/issues/1158 |
| 92 | ndn::Exclude e; |
| 93 | BOOST_REQUIRE_NO_THROW(e.excludeBefore(name::Component("PuQxMaf91"))); |
| 94 | |
| 95 | BOOST_CHECK_EQUAL(e.toUri(), "*,PuQxMaf91"); |
| 96 | } |
| 97 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 98 | BOOST_AUTO_TEST_CASE(Ranges) |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 99 | { |
| 100 | // example: ANY /b /d ANY /f |
| 101 | |
| 102 | Exclude e; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 103 | e.excludeOne(name::Component("b0")); |
| 104 | BOOST_CHECK_EQUAL(e.size(), 1); |
| 105 | BOOST_CHECK_EQUAL(e.toUri(), "b0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 106 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 107 | e.excludeRange(name::Component(), name::Component("b1")); |
| 108 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 109 | BOOST_CHECK_EQUAL(e.toUri(), "*,b1"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 110 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 111 | e.excludeRange(name::Component(), name::Component("c0")); |
| 112 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 113 | BOOST_CHECK_EQUAL(e.toUri(), "*,c0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 114 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 115 | e.excludeRange(name::Component("a0"), name::Component("c0")); |
| 116 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 117 | BOOST_CHECK_EQUAL(e.toUri(), "*,c0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 118 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 119 | e.excludeRange(name::Component("d0"), name::Component("e0")); |
| 120 | BOOST_CHECK_EQUAL(e.size(), 4); |
| 121 | BOOST_CHECK_EQUAL(e.toUri(), "*,c0,d0,*,e0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 122 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 123 | e.excludeRange(name::Component("c1"), name::Component("d1")); |
| 124 | BOOST_CHECK_EQUAL(e.size(), 4); |
| 125 | BOOST_CHECK_EQUAL(e.toUri(), "*,c0,c1,*,e0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 127 | e.excludeRange(name::Component("a1"), name::Component("d1")); |
| 128 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 129 | BOOST_CHECK_EQUAL(e.toUri(), "*,e0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 131 | e.excludeBefore(name::Component("e2")); |
| 132 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 133 | BOOST_CHECK_EQUAL(e.toUri(), "*,e2"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 134 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 135 | e.excludeAfter(name::Component("f0")); |
| 136 | BOOST_CHECK_EQUAL(e.size(), 3); |
| 137 | BOOST_CHECK_EQUAL(e.toUri(), "*,e2,f0,*"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 138 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 139 | e.excludeAfter(name::Component("e5")); |
| 140 | BOOST_CHECK_EQUAL(e.size(), 3); |
| 141 | BOOST_CHECK_EQUAL(e.toUri(), "*,e2,e5,*"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 142 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 143 | e.excludeAfter(name::Component("b2")); |
| 144 | BOOST_CHECK_EQUAL(e.size(), 1); |
| 145 | BOOST_CHECK_EQUAL(e.toUri(), "*"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 146 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 147 | BOOST_REQUIRE_THROW(e.excludeRange(name::Component("d0"), name::Component("a0")), |
| 148 | Exclude::Error); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 151 | BOOST_AUTO_TEST_CASE(Malformed) |
| 152 | { |
| 153 | Exclude e1; |
| 154 | BOOST_CHECK_THROW(e1.wireEncode(), Exclude::Error); |
| 155 | |
| 156 | Exclude e2; |
| 157 | |
| 158 | // top-level TLV-TYPE is not tlv::Exclude |
| 159 | const uint8_t NON_EXCLUDE[] = { 0x01, 0x02, 0x13, 0x00 }; |
| 160 | BOOST_CHECK_THROW(e2.wireDecode(Block(NON_EXCLUDE, sizeof(NON_EXCLUDE))), |
| 161 | tlv::Error); |
| 162 | |
| 163 | // Exclude element is empty |
| 164 | const uint8_t EMPTY_EXCLUDE[] = { 0x10, 0x00 }; |
| 165 | BOOST_CHECK_THROW(e2.wireDecode(Block(EMPTY_EXCLUDE, sizeof(EMPTY_EXCLUDE))), |
| 166 | Exclude::Error); |
| 167 | |
| 168 | // Exclude element contains unknown element |
| 169 | const uint8_t UNKNOWN_COMP1[] = { 0x10, 0x02, 0xAA, 0x00 }; |
| 170 | BOOST_CHECK_THROW(e2.wireDecode(Block(UNKNOWN_COMP1, sizeof(UNKNOWN_COMP1))), |
| 171 | Exclude::Error); |
| 172 | |
| 173 | // Exclude element contains unknown element |
| 174 | const uint8_t UNKNOWN_COMP2[] = { 0x10, 0x05, 0x08, 0x01, 0x54, 0xAA, 0x00 }; |
| 175 | BOOST_CHECK_THROW(e2.wireDecode(Block(UNKNOWN_COMP2, sizeof(UNKNOWN_COMP2))), |
| 176 | Exclude::Error); |
| 177 | |
| 178 | // // <Exclude><Any/></Exclude> |
| 179 | // const uint8_t ONLY_ANY[] = { 0x10, 0x02, 0x13, 0x00 }; |
| 180 | // BOOST_CHECK_THROW(e2.wireDecode(Block(ONLY_ANY, sizeof(ONLY_ANY))), |
| 181 | // Exclude::Error); |
| 182 | |
| 183 | // <Exclude><Any/><Any/></Exclude> |
| 184 | const uint8_t ANY_ANY[] = { 0x10, 0x04, 0x13, 0x00, 0x13, 0x00 }; |
| 185 | BOOST_CHECK_THROW(e2.wireDecode(Block(ANY_ANY, sizeof(ANY_ANY))), |
| 186 | Exclude::Error); |
| 187 | |
| 188 | // // <Exclude><Any/><NameComponent>T</NameComponent><Any/></Exclude> |
| 189 | // const uint8_t ANY_COMPONENT_ANY[] = { 0x10, 0x07, 0x13, 0x00, 0x08, 0x01, 0x54, 0x13, 0x00 }; |
| 190 | // BOOST_CHECK_THROW(e2.wireDecode(Block(ANY_COMPONENT_ANY, sizeof(ANY_COMPONENT_ANY))), |
| 191 | // Exclude::Error); |
Ilya Moiseenko | 741771f | 2015-03-10 16:53:44 -0700 | [diff] [blame] | 192 | |
| 193 | uint8_t WIRE[] = { |
| 194 | 0x10, 0x20, // Exclude |
| 195 | 0x01, 0x1E, // ImplicitSha256DigestComponent with incorrect length |
| 196 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 197 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 198 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 199 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd |
| 200 | }; |
| 201 | |
| 202 | BOOST_CHECK_THROW(Exclude().wireDecode(Block(WIRE, sizeof(WIRE))), Exclude::Error); |
| 203 | } |
| 204 | |
| 205 | BOOST_AUTO_TEST_CASE(ImplicitSha256Digest) |
| 206 | { |
| 207 | uint8_t WIRE[] = { |
| 208 | 0x10, 0x22, // Exclude |
| 209 | 0x01, 0x20, // ImplicitSha256DigestComponent |
| 210 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 211 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 212 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, |
| 213 | 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd |
| 214 | }; |
| 215 | |
| 216 | Block block(WIRE, sizeof(WIRE)); |
| 217 | |
| 218 | Exclude exclude; |
| 219 | BOOST_CHECK_NO_THROW(exclude.wireDecode(block)); |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Alexander Afanasyev | c076e6d | 2015-04-02 20:07:13 -0700 | [diff] [blame] | 222 | BOOST_AUTO_TEST_CASE(ExcludeEmptyComponent) // Bug #2660 |
| 223 | { |
| 224 | Exclude e1, e2; |
| 225 | |
| 226 | e1.excludeOne(name::Component()); |
| 227 | e2.excludeOne(name::Component("")); |
| 228 | |
| 229 | BOOST_CHECK_EQUAL(e1, e2); |
| 230 | BOOST_CHECK_EQUAL(e1.toUri(), e2.toUri()); |
| 231 | BOOST_CHECK(e1.wireEncode() == e2.wireEncode()); |
| 232 | |
| 233 | BOOST_CHECK_EQUAL("...", e1.toUri()); |
| 234 | |
| 235 | uint8_t WIRE[] {0x10, 0x02, 0x08, 0x00}; |
| 236 | BOOST_CHECK_EQUAL_COLLECTIONS(e1.wireEncode().begin(), e1.wireEncode().end(), |
| 237 | WIRE, WIRE + sizeof(WIRE)); |
| 238 | |
| 239 | Exclude e3(Block(WIRE, sizeof(WIRE))); |
| 240 | BOOST_CHECK_EQUAL(e1, e3); |
| 241 | BOOST_CHECK_EQUAL(e1.toUri(), e3.toUri()); |
| 242 | } |
| 243 | |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 244 | BOOST_AUTO_TEST_SUITE_END() |
| 245 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 246 | } // namespace tests |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 247 | } // namespace ndn |