ensure default-constructed Selectors is empty
refs #3944
Change-Id: Iaf5b6176529c4533214c4e27069734f8bdb7e556
diff --git a/src/selectors.cpp b/src/selectors.cpp
index dc741a3..8890b58 100644
--- a/src/selectors.cpp
+++ b/src/selectors.cpp
@@ -52,7 +52,7 @@
m_maxSuffixComponents < 0 &&
m_publisherPublicKeyLocator.empty() &&
m_exclude.empty() &&
- m_childSelector < 0 &&
+ m_childSelector == DEFAULT_CHILD_SELECTOR &&
!m_mustBeFresh;
}
diff --git a/tests/unit-tests/interest.t.cpp b/tests/unit-tests/interest.t.cpp
index 295af50..6b2f2cf 100644
--- a/tests/unit-tests/interest.t.cpp
+++ b/tests/unit-tests/interest.t.cpp
@@ -544,57 +544,6 @@
BOOST_CHECK(!i.hasSelectedDelegation());
}
-BOOST_AUTO_TEST_CASE(EncodeChildSelector)
-{
- Interest i;
- i.setName("/local/ndn/prefix");
- i.setNonce(1);
- BOOST_CHECK_EQUAL(i.getChildSelector(), 0);
- BOOST_CHECK_THROW(i.setChildSelector(-1), std::invalid_argument);
- BOOST_CHECK_THROW(i.setChildSelector(2), std::invalid_argument);
- BOOST_CHECK_NO_THROW(i.setChildSelector(1));
- BOOST_CHECK_EQUAL(i.getChildSelector(), 1);
- BOOST_CHECK_NO_THROW(i.setChildSelector(0));
- BOOST_CHECK_EQUAL(i.getChildSelector(), 0);
-
- const uint8_t expectedDefault[] = {
- 0x05, 0x1e, // MetaInfo
- 0x07, 0x14, // Name
- 0x08, 0x5, // NameComponent
- 0x6c, 0x6f, 0x63, 0x61, 0x6c,
- 0x08, 0x3, // NameComponent
- 0x6e, 0x64, 0x6e,
- 0x08, 0x6, // NameComponent
- 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
- 0x09, 0x00, // Selectors
- 0x0a, 0x04, // Nonce
- 0x01, 0x00, 0x00, 0x00, // 1
- };
-
- BOOST_CHECK_EQUAL_COLLECTIONS(i.wireEncode().begin(), i.wireEncode().end(),
- expectedDefault, expectedDefault + sizeof(expectedDefault));
-
- const uint8_t expected1[] = {
- 0x05, 0x21, // MetaInfo
- 0x07, 0x14, // Name
- 0x08, 0x5, // NameComponent
- 0x6c, 0x6f, 0x63, 0x61, 0x6c,
- 0x08, 0x3, // NameComponent
- 0x6e, 0x64, 0x6e,
- 0x08, 0x6, // NameComponent
- 0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
- 0x09, 0x03, // Selectors
- 0x11, 0x01, // ChildSelector
- 0x01, // 1
- 0x0a, 0x04, // Nonce
- 0x01, 0x00, 0x00, 0x00, // 1
- };
-
- i.setChildSelector(1);
- BOOST_CHECK_EQUAL_COLLECTIONS(i.wireEncode().begin(), i.wireEncode().end(),
- expected1, expected1 + sizeof(expected1));
-}
-
BOOST_AUTO_TEST_CASE(EncodeInterestLifetime)
{
Interest i;
@@ -608,7 +557,7 @@
BOOST_CHECK_NO_THROW(i.setInterestLifetime(DEFAULT_INTEREST_LIFETIME));
const uint8_t expectedDefault[] = {
- 0x05, 0x1e, // MetaInfo
+ 0x05, 0x1c, // Interest
0x07, 0x14, // Name
0x08, 0x5, // NameComponent
0x6c, 0x6f, 0x63, 0x61, 0x6c,
@@ -616,7 +565,6 @@
0x6e, 0x64, 0x6e,
0x08, 0x6, // NameComponent
0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
- 0x09, 0x00, // Selectors
0x0a, 0x04, // Nonce
0x01, 0x00, 0x00, 0x00, // 1
};
@@ -625,7 +573,7 @@
expectedDefault, expectedDefault + sizeof(expectedDefault));
const uint8_t expected1000ms[] = {
- 0x05, 0x22, // MetaInfo
+ 0x05, 0x20, // Interest
0x07, 0x14, // Name
0x08, 0x5, // NameComponent
0x6c, 0x6f, 0x63, 0x61, 0x6c,
@@ -633,7 +581,6 @@
0x6e, 0x64, 0x6e,
0x08, 0x6, // NameComponent
0x70, 0x72, 0x65, 0x66, 0x69, 0x78,
- 0x09, 0x00, // Selectors
0x0a, 0x04, // Nonce
0x01, 0x00, 0x00, 0x00, // 1
0x0c, 0x02, // InterestLifetime
@@ -726,80 +673,6 @@
BOOST_CHECK_EQUAL(a != b, false);
}
-BOOST_AUTO_TEST_CASE(SelectorsEqualityChecks)
-{
- // Selectors ::= SELECTORS-TYPE TLV-LENGTH
- // MinSuffixComponents?
- // MaxSuffixComponents?
- // PublisherPublicKeyLocator?
- // Exclude?
- // ChildSelector?
- // MustBeFresh?
-
- Selectors a;
- Selectors b;
- BOOST_CHECK_EQUAL(a == b, true);
- BOOST_CHECK_EQUAL(a != b, false);
-
- // MinSuffixComponents
- a.setMinSuffixComponents(1);
- BOOST_CHECK_EQUAL(a == b, false);
- BOOST_CHECK_EQUAL(a != b, true);
-
- b.setMinSuffixComponents(2);
- BOOST_CHECK_EQUAL(a == b, false);
- BOOST_CHECK_EQUAL(a != b, true);
-
- b.setMinSuffixComponents(1);
- BOOST_CHECK_EQUAL(a == b, true);
- BOOST_CHECK_EQUAL(a != b, false);
-
- // MaxSuffixComponents
- a.setMaxSuffixComponents(10);
- BOOST_CHECK_EQUAL(a == b, false);
- BOOST_CHECK_EQUAL(a != b, true);
-
- b.setMaxSuffixComponents(10);
- BOOST_CHECK_EQUAL(a == b, true);
- BOOST_CHECK_EQUAL(a != b, false);
-
- // PublisherPublicKeyLocator
- a.setPublisherPublicKeyLocator(KeyLocator("/key/Locator/name"));
- BOOST_CHECK_EQUAL(a == b, false);
- BOOST_CHECK_EQUAL(a != b, true);
-
- b.setPublisherPublicKeyLocator(KeyLocator("/key/Locator/name"));
- BOOST_CHECK_EQUAL(a == b, true);
- BOOST_CHECK_EQUAL(a != b, false);
-
- // Exclude
- a.setExclude(Exclude().excludeOne(name::Component("exclude")));
- BOOST_CHECK_EQUAL(a == b, false);
- BOOST_CHECK_EQUAL(a != b, true);
-
- b.setExclude(Exclude().excludeOne(name::Component("exclude")));
- BOOST_CHECK_EQUAL(a == b, true);
- BOOST_CHECK_EQUAL(a != b, false);
-
- // ChildSelector
- a.setChildSelector(1);
- BOOST_CHECK_EQUAL(a == b, false);
- BOOST_CHECK_EQUAL(a != b, true);
-
- b.setChildSelector(1);
- BOOST_CHECK_EQUAL(a == b, true);
- BOOST_CHECK_EQUAL(a != b, false);
-
- // MustBeFresh
- a.setMustBeFresh(true);
- BOOST_CHECK_EQUAL(a == b, false);
- BOOST_CHECK_EQUAL(a != b, true);
-
- b.setMustBeFresh(true);
- BOOST_CHECK_EQUAL(a == b, true);
- BOOST_CHECK_EQUAL(a != b, false);
-}
-
BOOST_AUTO_TEST_CASE(LinkObject)
{
Link link1("test", {{100, "/test3"}, {20, "/test2"}, {10, "/test1"}});
diff --git a/tests/unit-tests/selectors.t.cpp b/tests/unit-tests/selectors.t.cpp
new file mode 100644
index 0000000..0b5d70f
--- /dev/null
+++ b/tests/unit-tests/selectors.t.cpp
@@ -0,0 +1,199 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2017 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "selectors.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(TestSelectors)
+
+BOOST_AUTO_TEST_CASE(DefaultConstructor)
+{
+ Selectors s;
+ BOOST_CHECK(s.empty());
+ BOOST_CHECK_EQUAL(s.getMinSuffixComponents(), -1);
+ BOOST_CHECK_EQUAL(s.getMaxSuffixComponents(), -1);
+ BOOST_CHECK(s.getPublisherPublicKeyLocator().empty());
+ BOOST_CHECK(s.getExclude().empty());
+ BOOST_CHECK_EQUAL(s.getChildSelector(), 0);
+ BOOST_CHECK_EQUAL(s.getMustBeFresh(), false);
+}
+
+BOOST_AUTO_TEST_CASE(EncodeDecodeEmpty)
+{
+ const uint8_t WIRE[] = {
+ 0x09, 0x00 // Selectors
+ };
+
+ Selectors s1;
+ Block wire1 = s1.wireEncode();
+ BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
+
+ Selectors s2(wire1);
+ BOOST_CHECK(s2.empty());
+ BOOST_CHECK_EQUAL(s2.getMinSuffixComponents(), -1);
+ BOOST_CHECK_EQUAL(s2.getMaxSuffixComponents(), -1);
+ BOOST_CHECK(s2.getPublisherPublicKeyLocator().empty());
+ BOOST_CHECK(s2.getExclude().empty());
+ BOOST_CHECK_EQUAL(s2.getChildSelector(), 0);
+ BOOST_CHECK_EQUAL(s2.getMustBeFresh(), false);
+
+ BOOST_CHECK(s1 == s2);
+}
+
+BOOST_AUTO_TEST_CASE(EncodeDecodeFull)
+{
+ const uint8_t WIRE[] = {
+ 0x09, 0x39, // Selectors
+ 0x0d, 0x01, 0x02, // MinSuffixComponents
+ 0x0e, 0x01, 0x06, // MaxSuffixComponent
+ 0x1c, 0x16, // KeyLocator
+ 0x07, 0x14, // Name
+ 0x08, 0x04, 0x74, 0x65, 0x73, 0x74,
+ 0x08, 0x03, 0x6b, 0x65, 0x79,
+ 0x08, 0x07, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72,
+ 0x10, 0x14, // Exclude
+ 0x08, 0x04, 0x61, 0x6c, 0x65, 0x78, // NameComponent
+ 0x08, 0x04, 0x78, 0x78, 0x78, 0x78, // NameComponent
+ 0x13, 0x00, // Any
+ 0x08, 0x04, 0x79, 0x79, 0x79, 0x79, // NameComponent
+ 0x11, 0x01, 0x01, // ChildSelector
+ 0x12, 0x00 // MustBeFresh
+ };
+
+ Selectors s1;
+ s1.setMinSuffixComponents(2);
+ s1.setMaxSuffixComponents(6);
+ s1.setPublisherPublicKeyLocator(KeyLocator("/test/key/locator"));
+ s1.setExclude(Exclude().excludeOne(name::Component("alex"))
+ .excludeRange(name::Component("xxxx"), name::Component("yyyy")));
+ s1.setChildSelector(1);
+ s1.setMustBeFresh(true);
+
+ Block wire1 = s1.wireEncode();
+ BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
+
+ Selectors s2(wire1);
+ BOOST_CHECK(!s2.empty());
+ BOOST_CHECK_EQUAL(s2.getMinSuffixComponents(), 2);
+ BOOST_CHECK_EQUAL(s2.getMaxSuffixComponents(), 6);
+ BOOST_CHECK_EQUAL(s2.getPublisherPublicKeyLocator().getType(), KeyLocator::KeyLocator_Name);
+ BOOST_CHECK_EQUAL(s2.getPublisherPublicKeyLocator().getName(), "ndn:/test/key/locator");
+ BOOST_CHECK_EQUAL(s2.getExclude().toUri(), "alex,xxxx,*,yyyy");
+ BOOST_CHECK_EQUAL(s2.getChildSelector(), 1);
+ BOOST_CHECK_EQUAL(s2.getMustBeFresh(), true);
+
+ BOOST_CHECK(s1 == s2);
+}
+
+BOOST_AUTO_TEST_CASE(SetChildSelector)
+{
+ Selectors s;
+ BOOST_CHECK_EQUAL(s.getChildSelector(), 0);
+ BOOST_CHECK_THROW(s.setChildSelector(-1), std::invalid_argument);
+ BOOST_CHECK_THROW(s.setChildSelector(2), std::invalid_argument);
+ s.setChildSelector(1);
+ BOOST_CHECK_EQUAL(s.getChildSelector(), 1);
+ s.setChildSelector(0);
+ BOOST_CHECK_EQUAL(s.getChildSelector(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(Equality)
+{
+ // Selectors ::= SELECTORS-TYPE TLV-LENGTH
+ // MinSuffixComponents?
+ // MaxSuffixComponents?
+ // PublisherPublicKeyLocator?
+ // Exclude?
+ // ChildSelector?
+ // MustBeFresh?
+
+ Selectors a;
+ Selectors b;
+ BOOST_CHECK_EQUAL(a == b, true);
+ BOOST_CHECK_EQUAL(a != b, false);
+
+ // MinSuffixComponents
+ a.setMinSuffixComponents(1);
+ BOOST_CHECK_EQUAL(a == b, false);
+ BOOST_CHECK_EQUAL(a != b, true);
+
+ b.setMinSuffixComponents(2);
+ BOOST_CHECK_EQUAL(a == b, false);
+ BOOST_CHECK_EQUAL(a != b, true);
+
+ b.setMinSuffixComponents(1);
+ BOOST_CHECK_EQUAL(a == b, true);
+ BOOST_CHECK_EQUAL(a != b, false);
+
+ // MaxSuffixComponents
+ a.setMaxSuffixComponents(10);
+ BOOST_CHECK_EQUAL(a == b, false);
+ BOOST_CHECK_EQUAL(a != b, true);
+
+ b.setMaxSuffixComponents(10);
+ BOOST_CHECK_EQUAL(a == b, true);
+ BOOST_CHECK_EQUAL(a != b, false);
+
+ // PublisherPublicKeyLocator
+ a.setPublisherPublicKeyLocator(KeyLocator("/key/Locator/name"));
+ BOOST_CHECK_EQUAL(a == b, false);
+ BOOST_CHECK_EQUAL(a != b, true);
+
+ b.setPublisherPublicKeyLocator(KeyLocator("/key/Locator/name"));
+ BOOST_CHECK_EQUAL(a == b, true);
+ BOOST_CHECK_EQUAL(a != b, false);
+
+ // Exclude
+ a.setExclude(Exclude().excludeOne(name::Component("exclude")));
+ BOOST_CHECK_EQUAL(a == b, false);
+ BOOST_CHECK_EQUAL(a != b, true);
+
+ b.setExclude(Exclude().excludeOne(name::Component("exclude")));
+ BOOST_CHECK_EQUAL(a == b, true);
+ BOOST_CHECK_EQUAL(a != b, false);
+
+ // ChildSelector
+ a.setChildSelector(1);
+ BOOST_CHECK_EQUAL(a == b, false);
+ BOOST_CHECK_EQUAL(a != b, true);
+
+ b.setChildSelector(1);
+ BOOST_CHECK_EQUAL(a == b, true);
+ BOOST_CHECK_EQUAL(a != b, false);
+
+ // MustBeFresh
+ a.setMustBeFresh(true);
+ BOOST_CHECK_EQUAL(a == b, false);
+ BOOST_CHECK_EQUAL(a != b, true);
+
+ b.setMustBeFresh(true);
+ BOOST_CHECK_EQUAL(a == b, true);
+ BOOST_CHECK_EQUAL(a != b, false);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestSelectors
+
+} // namespace tests
+} // namespace ndn