blob: 202fd710cdd1925a97c0c40bf7d15f40e46e3642 [file] [log] [blame]
Junxiao Shi9bf55812017-07-05 15:04:37 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +00002/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
Junxiao Shi9bf55812017-07-05 15:04:37 +00004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
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.
20 */
21
22#include "selectors.hpp"
23
24#include "boost-test.hpp"
25
26namespace ndn {
27namespace tests {
28
29BOOST_AUTO_TEST_SUITE(TestSelectors)
30
31BOOST_AUTO_TEST_CASE(DefaultConstructor)
32{
33 Selectors s;
34 BOOST_CHECK(s.empty());
35 BOOST_CHECK_EQUAL(s.getMinSuffixComponents(), -1);
36 BOOST_CHECK_EQUAL(s.getMaxSuffixComponents(), -1);
37 BOOST_CHECK(s.getPublisherPublicKeyLocator().empty());
38 BOOST_CHECK(s.getExclude().empty());
39 BOOST_CHECK_EQUAL(s.getChildSelector(), 0);
40 BOOST_CHECK_EQUAL(s.getMustBeFresh(), false);
41}
42
43BOOST_AUTO_TEST_CASE(EncodeDecodeEmpty)
44{
45 const uint8_t WIRE[] = {
46 0x09, 0x00 // Selectors
47 };
48
49 Selectors s1;
50 Block wire1 = s1.wireEncode();
51 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
52
53 Selectors s2(wire1);
54 BOOST_CHECK(s2.empty());
55 BOOST_CHECK_EQUAL(s2.getMinSuffixComponents(), -1);
56 BOOST_CHECK_EQUAL(s2.getMaxSuffixComponents(), -1);
57 BOOST_CHECK(s2.getPublisherPublicKeyLocator().empty());
58 BOOST_CHECK(s2.getExclude().empty());
59 BOOST_CHECK_EQUAL(s2.getChildSelector(), 0);
60 BOOST_CHECK_EQUAL(s2.getMustBeFresh(), false);
61
62 BOOST_CHECK(s1 == s2);
63}
64
65BOOST_AUTO_TEST_CASE(EncodeDecodeFull)
66{
67 const uint8_t WIRE[] = {
68 0x09, 0x39, // Selectors
69 0x0d, 0x01, 0x02, // MinSuffixComponents
70 0x0e, 0x01, 0x06, // MaxSuffixComponent
71 0x1c, 0x16, // KeyLocator
72 0x07, 0x14, // Name
73 0x08, 0x04, 0x74, 0x65, 0x73, 0x74,
74 0x08, 0x03, 0x6b, 0x65, 0x79,
75 0x08, 0x07, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
76 0x10, 0x14, // Exclude
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000077 0x08, 0x04, 0x61, 0x6c, 0x65, 0x78, // GenericNameComponent
78 0x08, 0x04, 0x78, 0x78, 0x78, 0x78, // GenericNameComponent
Junxiao Shi9bf55812017-07-05 15:04:37 +000079 0x13, 0x00, // Any
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +000080 0x08, 0x04, 0x79, 0x79, 0x79, 0x79, // GenericNameComponent
Junxiao Shi9bf55812017-07-05 15:04:37 +000081 0x11, 0x01, 0x01, // ChildSelector
82 0x12, 0x00 // MustBeFresh
83 };
84
85 Selectors s1;
86 s1.setMinSuffixComponents(2);
87 s1.setMaxSuffixComponents(6);
88 s1.setPublisherPublicKeyLocator(KeyLocator("/test/key/locator"));
89 s1.setExclude(Exclude().excludeOne(name::Component("alex"))
90 .excludeRange(name::Component("xxxx"), name::Component("yyyy")));
91 s1.setChildSelector(1);
92 s1.setMustBeFresh(true);
93
94 Block wire1 = s1.wireEncode();
95 BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
96
97 Selectors s2(wire1);
98 BOOST_CHECK(!s2.empty());
99 BOOST_CHECK_EQUAL(s2.getMinSuffixComponents(), 2);
100 BOOST_CHECK_EQUAL(s2.getMaxSuffixComponents(), 6);
101 BOOST_CHECK_EQUAL(s2.getPublisherPublicKeyLocator().getType(), KeyLocator::KeyLocator_Name);
102 BOOST_CHECK_EQUAL(s2.getPublisherPublicKeyLocator().getName(), "ndn:/test/key/locator");
103 BOOST_CHECK_EQUAL(s2.getExclude().toUri(), "alex,xxxx,*,yyyy");
104 BOOST_CHECK_EQUAL(s2.getChildSelector(), 1);
105 BOOST_CHECK_EQUAL(s2.getMustBeFresh(), true);
106
107 BOOST_CHECK(s1 == s2);
108}
109
110BOOST_AUTO_TEST_CASE(SetChildSelector)
111{
112 Selectors s;
113 BOOST_CHECK_EQUAL(s.getChildSelector(), 0);
114 BOOST_CHECK_THROW(s.setChildSelector(-1), std::invalid_argument);
115 BOOST_CHECK_THROW(s.setChildSelector(2), std::invalid_argument);
116 s.setChildSelector(1);
117 BOOST_CHECK_EQUAL(s.getChildSelector(), 1);
118 s.setChildSelector(0);
119 BOOST_CHECK_EQUAL(s.getChildSelector(), 0);
120}
121
122BOOST_AUTO_TEST_CASE(Equality)
123{
124 // Selectors ::= SELECTORS-TYPE TLV-LENGTH
125 // MinSuffixComponents?
126 // MaxSuffixComponents?
127 // PublisherPublicKeyLocator?
128 // Exclude?
129 // ChildSelector?
130 // MustBeFresh?
131
132 Selectors a;
133 Selectors b;
134 BOOST_CHECK_EQUAL(a == b, true);
135 BOOST_CHECK_EQUAL(a != b, false);
136
137 // MinSuffixComponents
138 a.setMinSuffixComponents(1);
139 BOOST_CHECK_EQUAL(a == b, false);
140 BOOST_CHECK_EQUAL(a != b, true);
141
142 b.setMinSuffixComponents(2);
143 BOOST_CHECK_EQUAL(a == b, false);
144 BOOST_CHECK_EQUAL(a != b, true);
145
146 b.setMinSuffixComponents(1);
147 BOOST_CHECK_EQUAL(a == b, true);
148 BOOST_CHECK_EQUAL(a != b, false);
149
150 // MaxSuffixComponents
151 a.setMaxSuffixComponents(10);
152 BOOST_CHECK_EQUAL(a == b, false);
153 BOOST_CHECK_EQUAL(a != b, true);
154
155 b.setMaxSuffixComponents(10);
156 BOOST_CHECK_EQUAL(a == b, true);
157 BOOST_CHECK_EQUAL(a != b, false);
158
159 // PublisherPublicKeyLocator
160 a.setPublisherPublicKeyLocator(KeyLocator("/key/Locator/name"));
161 BOOST_CHECK_EQUAL(a == b, false);
162 BOOST_CHECK_EQUAL(a != b, true);
163
164 b.setPublisherPublicKeyLocator(KeyLocator("/key/Locator/name"));
165 BOOST_CHECK_EQUAL(a == b, true);
166 BOOST_CHECK_EQUAL(a != b, false);
167
168 // Exclude
169 a.setExclude(Exclude().excludeOne(name::Component("exclude")));
170 BOOST_CHECK_EQUAL(a == b, false);
171 BOOST_CHECK_EQUAL(a != b, true);
172
173 b.setExclude(Exclude().excludeOne(name::Component("exclude")));
174 BOOST_CHECK_EQUAL(a == b, true);
175 BOOST_CHECK_EQUAL(a != b, false);
176
177 // ChildSelector
178 a.setChildSelector(1);
179 BOOST_CHECK_EQUAL(a == b, false);
180 BOOST_CHECK_EQUAL(a != b, true);
181
182 b.setChildSelector(1);
183 BOOST_CHECK_EQUAL(a == b, true);
184 BOOST_CHECK_EQUAL(a != b, false);
185
186 // MustBeFresh
187 a.setMustBeFresh(true);
188 BOOST_CHECK_EQUAL(a == b, false);
189 BOOST_CHECK_EQUAL(a != b, true);
190
191 b.setMustBeFresh(true);
192 BOOST_CHECK_EQUAL(a == b, true);
193 BOOST_CHECK_EQUAL(a != b, false);
194}
195
196BOOST_AUTO_TEST_SUITE_END() // TestSelectors
197
198} // namespace tests
199} // namespace ndn