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 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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 { |
| 27 | |
| 28 | BOOST_AUTO_TEST_SUITE(TestExclude) |
| 29 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 30 | BOOST_AUTO_TEST_CASE(Basic) |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 31 | { |
| 32 | Exclude e; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 33 | e.excludeOne(name::Component("b")); |
| 34 | BOOST_CHECK_EQUAL(e.size(), 1); |
| 35 | BOOST_CHECK_EQUAL(e.toUri(), "b"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 37 | e.excludeOne(name::Component("d")); |
| 38 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 39 | BOOST_CHECK_EQUAL(e.toUri(), "b,d"); |
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("a")); |
| 42 | BOOST_CHECK_EQUAL(e.size(), 3); |
| 43 | BOOST_CHECK_EQUAL(e.toUri(), "a,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("aa")); |
| 46 | BOOST_CHECK_EQUAL(e.size(), 4); |
| 47 | BOOST_CHECK_EQUAL(e.toUri(), "a,b,d,aa"); |
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("cc")); |
| 50 | BOOST_CHECK_EQUAL(e.size(), 5); |
| 51 | BOOST_CHECK_EQUAL(e.toUri(), "a,b,d,aa,cc"); |
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("c")); |
| 54 | BOOST_CHECK_EQUAL(e.size(), 6); |
| 55 | BOOST_CHECK_EQUAL(e.toUri(), "a,b,c,d,aa,cc"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Alexander Afanasyev | ba09605 | 2014-09-19 15:36:37 -0700 | [diff] [blame] | 58 | BOOST_AUTO_TEST_CASE(EqualityComparable) |
| 59 | { |
| 60 | Exclude e1; |
| 61 | Exclude e2; |
| 62 | BOOST_CHECK_EQUAL(e1, e2); |
| 63 | |
| 64 | e1.excludeOne(name::Component("T")); |
| 65 | BOOST_CHECK_NE(e1, e2); |
| 66 | |
| 67 | e2.excludeOne(name::Component("D")); |
| 68 | BOOST_CHECK_NE(e1, e2); |
| 69 | |
| 70 | e2.clear(); |
| 71 | e2.excludeOne(name::Component("T")); |
| 72 | BOOST_CHECK_EQUAL(e1, e2); |
| 73 | |
| 74 | e2.clear(); |
| 75 | const uint8_t EXCLUDE[] = { 0x10, 0x15, 0x13, 0x00, 0x08, 0x01, 0x41, 0x08, 0x01, 0x42, |
| 76 | 0x08, 0x01, 0x43, 0x13, 0x00, 0x08, 0x01, 0x44, 0x08, 0x01, |
| 77 | 0x45, 0x13, 0x00 }; |
| 78 | e2.wireDecode(Block(EXCLUDE, sizeof(EXCLUDE))); |
| 79 | |
| 80 | e1.clear(); |
| 81 | e1.excludeBefore(name::Component("A")); |
| 82 | e1.excludeOne(name::Component("B")); |
| 83 | e1.excludeRange(name::Component("C"), name::Component("D")); |
| 84 | e1.excludeAfter(name::Component("E")); |
| 85 | BOOST_CHECK_EQUAL(e1, e2); |
| 86 | } |
| 87 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 88 | BOOST_AUTO_TEST_CASE(Before) |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 89 | { |
| 90 | // based on http://redmine.named-data.net/issues/1158 |
| 91 | ndn::Exclude e; |
| 92 | BOOST_REQUIRE_NO_THROW(e.excludeBefore(name::Component("PuQxMaf91"))); |
| 93 | |
| 94 | BOOST_CHECK_EQUAL(e.toUri(), "*,PuQxMaf91"); |
| 95 | } |
| 96 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 97 | BOOST_AUTO_TEST_CASE(Ranges) |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 98 | { |
| 99 | // example: ANY /b /d ANY /f |
| 100 | |
| 101 | Exclude e; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 102 | e.excludeOne(name::Component("b0")); |
| 103 | BOOST_CHECK_EQUAL(e.size(), 1); |
| 104 | BOOST_CHECK_EQUAL(e.toUri(), "b0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 105 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 106 | e.excludeRange(name::Component(), name::Component("b1")); |
| 107 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 108 | BOOST_CHECK_EQUAL(e.toUri(), "*,b1"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 109 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 110 | e.excludeRange(name::Component(), name::Component("c0")); |
| 111 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 112 | BOOST_CHECK_EQUAL(e.toUri(), "*,c0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 114 | e.excludeRange(name::Component("a0"), name::Component("c0")); |
| 115 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 116 | BOOST_CHECK_EQUAL(e.toUri(), "*,c0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 117 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 118 | e.excludeRange(name::Component("d0"), name::Component("e0")); |
| 119 | BOOST_CHECK_EQUAL(e.size(), 4); |
| 120 | BOOST_CHECK_EQUAL(e.toUri(), "*,c0,d0,*,e0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 122 | e.excludeRange(name::Component("c1"), name::Component("d1")); |
| 123 | BOOST_CHECK_EQUAL(e.size(), 4); |
| 124 | BOOST_CHECK_EQUAL(e.toUri(), "*,c0,c1,*,e0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 125 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 126 | e.excludeRange(name::Component("a1"), name::Component("d1")); |
| 127 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 128 | BOOST_CHECK_EQUAL(e.toUri(), "*,e0"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 129 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 130 | e.excludeBefore(name::Component("e2")); |
| 131 | BOOST_CHECK_EQUAL(e.size(), 2); |
| 132 | BOOST_CHECK_EQUAL(e.toUri(), "*,e2"); |
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.excludeAfter(name::Component("f0")); |
| 135 | BOOST_CHECK_EQUAL(e.size(), 3); |
| 136 | BOOST_CHECK_EQUAL(e.toUri(), "*,e2,f0,*"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 137 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 138 | e.excludeAfter(name::Component("e5")); |
| 139 | BOOST_CHECK_EQUAL(e.size(), 3); |
| 140 | BOOST_CHECK_EQUAL(e.toUri(), "*,e2,e5,*"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 141 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 142 | e.excludeAfter(name::Component("b2")); |
| 143 | BOOST_CHECK_EQUAL(e.size(), 1); |
| 144 | BOOST_CHECK_EQUAL(e.toUri(), "*"); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 145 | |
Junxiao Shi | 7520302 | 2014-09-11 10:01:50 -0700 | [diff] [blame] | 146 | BOOST_REQUIRE_THROW(e.excludeRange(name::Component("d0"), name::Component("a0")), |
| 147 | Exclude::Error); |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Junxiao Shi | 7284a40 | 2014-09-12 13:42:16 -0700 | [diff] [blame] | 150 | BOOST_AUTO_TEST_CASE(Malformed) |
| 151 | { |
| 152 | Exclude e1; |
| 153 | BOOST_CHECK_THROW(e1.wireEncode(), Exclude::Error); |
| 154 | |
| 155 | Exclude e2; |
| 156 | |
| 157 | // top-level TLV-TYPE is not tlv::Exclude |
| 158 | const uint8_t NON_EXCLUDE[] = { 0x01, 0x02, 0x13, 0x00 }; |
| 159 | BOOST_CHECK_THROW(e2.wireDecode(Block(NON_EXCLUDE, sizeof(NON_EXCLUDE))), |
| 160 | tlv::Error); |
| 161 | |
| 162 | // Exclude element is empty |
| 163 | const uint8_t EMPTY_EXCLUDE[] = { 0x10, 0x00 }; |
| 164 | BOOST_CHECK_THROW(e2.wireDecode(Block(EMPTY_EXCLUDE, sizeof(EMPTY_EXCLUDE))), |
| 165 | Exclude::Error); |
| 166 | |
| 167 | // Exclude element contains unknown element |
| 168 | const uint8_t UNKNOWN_COMP1[] = { 0x10, 0x02, 0xAA, 0x00 }; |
| 169 | BOOST_CHECK_THROW(e2.wireDecode(Block(UNKNOWN_COMP1, sizeof(UNKNOWN_COMP1))), |
| 170 | Exclude::Error); |
| 171 | |
| 172 | // Exclude element contains unknown element |
| 173 | const uint8_t UNKNOWN_COMP2[] = { 0x10, 0x05, 0x08, 0x01, 0x54, 0xAA, 0x00 }; |
| 174 | BOOST_CHECK_THROW(e2.wireDecode(Block(UNKNOWN_COMP2, sizeof(UNKNOWN_COMP2))), |
| 175 | Exclude::Error); |
| 176 | |
| 177 | // // <Exclude><Any/></Exclude> |
| 178 | // const uint8_t ONLY_ANY[] = { 0x10, 0x02, 0x13, 0x00 }; |
| 179 | // BOOST_CHECK_THROW(e2.wireDecode(Block(ONLY_ANY, sizeof(ONLY_ANY))), |
| 180 | // Exclude::Error); |
| 181 | |
| 182 | // <Exclude><Any/><Any/></Exclude> |
| 183 | const uint8_t ANY_ANY[] = { 0x10, 0x04, 0x13, 0x00, 0x13, 0x00 }; |
| 184 | BOOST_CHECK_THROW(e2.wireDecode(Block(ANY_ANY, sizeof(ANY_ANY))), |
| 185 | Exclude::Error); |
| 186 | |
| 187 | // // <Exclude><Any/><NameComponent>T</NameComponent><Any/></Exclude> |
| 188 | // const uint8_t ANY_COMPONENT_ANY[] = { 0x10, 0x07, 0x13, 0x00, 0x08, 0x01, 0x54, 0x13, 0x00 }; |
| 189 | // BOOST_CHECK_THROW(e2.wireDecode(Block(ANY_COMPONENT_ANY, sizeof(ANY_COMPONENT_ANY))), |
| 190 | // Exclude::Error); |
| 191 | } |
| 192 | |
Alexander Afanasyev | 035c7b4 | 2014-02-06 18:26:19 -0800 | [diff] [blame] | 193 | BOOST_AUTO_TEST_SUITE_END() |
| 194 | |
| 195 | } // namespace ndn |