exclude: ImplicitSha256Digest bugfix

This commit also improves name::Component test suite.

refs #3665

Change-Id: I8e9a575bf203f0983bacd1dbdb3491510e326d1f
diff --git a/tests/unit-tests/exclude.t.cpp b/tests/unit-tests/exclude.t.cpp
index ce2892e..843fadd 100644
--- a/tests/unit-tests/exclude.t.cpp
+++ b/tests/unit-tests/exclude.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2016 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -20,6 +20,7 @@
  */
 
 #include "exclude.hpp"
+#include "util/crypto.hpp"
 
 #include "boost-test.hpp"
 
@@ -28,7 +29,9 @@
 
 BOOST_AUTO_TEST_SUITE(TestExclude)
 
-BOOST_AUTO_TEST_CASE(Basic)
+BOOST_AUTO_TEST_SUITE(GenericComponent) // exclude generic NameComponent
+
+BOOST_AUTO_TEST_CASE(One)
 {
   Exclude e;
   e.excludeOne(name::Component("b"));
@@ -56,36 +59,6 @@
   BOOST_CHECK_EQUAL(e.toUri(), "a,b,c,d,aa,cc");
 }
 
-BOOST_AUTO_TEST_CASE(EqualityComparable)
-{
-  Exclude e1;
-  Exclude e2;
-  BOOST_CHECK_EQUAL(e1, e2);
-
-  e1.excludeOne(name::Component("T"));
-  BOOST_CHECK_NE(e1, e2);
-
-  e2.excludeOne(name::Component("D"));
-  BOOST_CHECK_NE(e1, e2);
-
-  e2.clear();
-  e2.excludeOne(name::Component("T"));
-  BOOST_CHECK_EQUAL(e1, e2);
-
-  e2.clear();
-  const uint8_t EXCLUDE[] = { 0x10, 0x15, 0x13, 0x00, 0x08, 0x01, 0x41, 0x08, 0x01, 0x42,
-                              0x08, 0x01, 0x43, 0x13, 0x00, 0x08, 0x01, 0x44, 0x08, 0x01,
-                              0x45, 0x13, 0x00 };
-  e2.wireDecode(Block(EXCLUDE, sizeof(EXCLUDE)));
-
-  e1.clear();
-  e1.excludeBefore(name::Component("A"));
-  e1.excludeOne(name::Component("B"));
-  e1.excludeRange(name::Component("C"), name::Component("D"));
-  e1.excludeAfter(name::Component("E"));
-  BOOST_CHECK_EQUAL(e1, e2);
-}
-
 BOOST_AUTO_TEST_CASE(Before)
 {
   // based on http://redmine.named-data.net/issues/1158
@@ -97,18 +70,18 @@
 
 BOOST_AUTO_TEST_CASE(Ranges)
 {
-// example: ANY /b /d ANY /f
+  // example: ANY /b /d ANY /f
 
   Exclude e;
   e.excludeOne(name::Component("b0"));
   BOOST_CHECK_EQUAL(e.size(), 1);
   BOOST_CHECK_EQUAL(e.toUri(), "b0");
 
-  e.excludeRange(name::Component(), name::Component("b1"));
+  e.excludeBefore(name::Component("b1"));
   BOOST_CHECK_EQUAL(e.size(), 2);
   BOOST_CHECK_EQUAL(e.toUri(), "*,b1");
 
-  e.excludeRange(name::Component(), name::Component("c0"));
+  e.excludeBefore(name::Component("c0"));
   BOOST_CHECK_EQUAL(e.size(), 2);
   BOOST_CHECK_EQUAL(e.toUri(), "*,c0");
 
@@ -148,6 +121,216 @@
                       Exclude::Error);
 }
 
+BOOST_AUTO_TEST_SUITE_END() // GenericComponent
+
+
+BOOST_AUTO_TEST_SUITE(ImplicitDigest) // exclude ImplicitSha256DigestComponent
+
+/** \brief make a name::Component with an octet repeated crypto::SHA256_DIGEST_SIZE times
+ *  \param octet the octet to fill the component
+ *  \param isDigest whether to make an ImplicitSha256DigestComponent or a generic NameComponent
+ *  \param lastOctet if non-negative, set the last octet to a different value
+ */
+static name::Component
+makeComponent(uint8_t octet, bool isDigest, int lastOctet = -1)
+{
+  uint8_t wire[crypto::SHA256_DIGEST_SIZE];
+  std::memset(wire, octet, sizeof(wire));
+  if (lastOctet >= 0) {
+    wire[crypto::SHA256_DIGEST_SIZE - 1] = static_cast<uint8_t>(lastOctet);
+  }
+
+  if (isDigest) {
+    return name::Component::fromImplicitSha256Digest(wire, sizeof(wire));
+  }
+  else {
+    return name::Component(wire, sizeof(wire));
+  }
+}
+
+BOOST_AUTO_TEST_CASE(One)
+{
+  name::Component digestC = makeComponent(0xCC, true);;
+  name::Component genericC = makeComponent(0xCC, false);
+  name::Component digestD = makeComponent(0xDD, true);
+
+  Exclude e;
+  e.excludeOne(digestC);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestC), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(genericC), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestD), false);
+
+  e.clear();
+  e.excludeOne(genericC);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestC), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(genericC), true);
+}
+
+BOOST_AUTO_TEST_CASE(BeforeDigest)
+{
+  name::Component digestBA = makeComponent(0xBB, true, 0xBA);
+  name::Component digestBB = makeComponent(0xBB, true);
+  name::Component digestBC = makeComponent(0xBB, true, 0xBC);
+
+  Exclude e;
+  e.excludeBefore(digestBB);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestBA), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestBB), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestBC), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), false);
+}
+
+BOOST_AUTO_TEST_CASE(BeforeGeneric)
+{
+  name::Component digest0 = makeComponent(0x00, true);
+  name::Component digest9 = makeComponent(0x99, true);
+  name::Component digestF = makeComponent(0xFF, true);
+
+  Exclude e;
+  e.excludeBefore(name::Component(""));
+  BOOST_CHECK_EQUAL(e.isExcluded(digest0), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(digest9), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestF), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), false);
+}
+
+BOOST_AUTO_TEST_CASE(AfterDigest)
+{
+  name::Component digestBA = makeComponent(0xBB, true, 0xBA);
+  name::Component digestBB = makeComponent(0xBB, true);
+  name::Component digestBC = makeComponent(0xBB, true, 0xBC);
+
+  Exclude e;
+  e.excludeAfter(digestBB);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestBA), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestBB), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestBC), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), true);
+}
+
+BOOST_AUTO_TEST_CASE(AfterDigestFF)
+{
+  name::Component digest00 = makeComponent(0x00, true);
+  name::Component digest99 = makeComponent(0x99, true);
+  name::Component digestFE = makeComponent(0xFF, true, 0xFE);
+  name::Component digestFF = makeComponent(0xFF, true);
+
+  Exclude e;
+  e.excludeAfter(digestFF);
+  BOOST_CHECK_EQUAL(e.isExcluded(digest00), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(digest99), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestFE), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestFF), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), true);
+}
+
+BOOST_AUTO_TEST_CASE(AfterGeneric)
+{
+  name::Component digest0 = makeComponent(0x00, true);
+  name::Component digest9 = makeComponent(0x99, true);
+  name::Component digestF = makeComponent(0xFF, true);
+
+  Exclude e;
+  e.excludeAfter(name::Component(""));
+  BOOST_CHECK_EQUAL(e.isExcluded(digest0), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(digest9), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestF), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), true);
+}
+
+BOOST_AUTO_TEST_CASE(RangeDigest)
+{
+  name::Component digest0 = makeComponent(0x00, true);
+  name::Component digest7 = makeComponent(0x77, true);
+  name::Component digest8 = makeComponent(0x88, true);
+  name::Component digest9 = makeComponent(0x99, true);
+  name::Component digestF = makeComponent(0xFF, true);
+
+  Exclude e;
+  e.excludeRange(digest7, digest9);
+  BOOST_CHECK_EQUAL(e.isExcluded(digest0), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(digest7), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(digest8), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(digest9), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestF), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), false);
+}
+
+BOOST_AUTO_TEST_CASE(RangeDigestReverse)
+{
+  name::Component digest7 = makeComponent(0x77, true);
+  name::Component digest9 = makeComponent(0x99, true);
+
+  Exclude e;
+  BOOST_CHECK_THROW(e.excludeRange(digest9, digest7), Exclude::Error);
+}
+
+BOOST_AUTO_TEST_CASE(RangeDigestGeneric)
+{
+  name::Component digest0 = makeComponent(0x00, true);
+  name::Component digest7 = makeComponent(0x77, true);
+  name::Component digest9 = makeComponent(0x99, true);
+  name::Component digestF = makeComponent(0xFF, true);
+
+  Exclude e;
+  e.excludeRange(digest9, name::Component(""));
+  BOOST_CHECK_EQUAL(e.isExcluded(digest0), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(digest7), false);
+  BOOST_CHECK_EQUAL(e.isExcluded(digest9), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(digestF), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(name::Component("")), true);
+  BOOST_CHECK_EQUAL(e.isExcluded(name::Component("generic")), false);
+}
+
+BOOST_AUTO_TEST_CASE(RangeGenericDigest)
+{
+  name::Component digestF = makeComponent(0xFF, true);
+
+  Exclude e;
+  BOOST_CHECK_THROW(e.excludeRange(name::Component(""), digestF), Exclude::Error);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // ImplicitDigest
+
+
+BOOST_AUTO_TEST_SUITE(WireCompare) // wireEncode, wireDecode, operator==, operator!=
+
+BOOST_AUTO_TEST_CASE(EqualityComparable)
+{
+  Exclude e1;
+  Exclude e2;
+  BOOST_CHECK_EQUAL(e1, e2);
+
+  e1.excludeOne(name::Component("T"));
+  BOOST_CHECK_NE(e1, e2);
+
+  e2.excludeOne(name::Component("D"));
+  BOOST_CHECK_NE(e1, e2);
+
+  e2.clear();
+  e2.excludeOne(name::Component("T"));
+  BOOST_CHECK_EQUAL(e1, e2);
+
+  e2.clear();
+  const uint8_t EXCLUDE[] = { 0x10, 0x15, 0x13, 0x00, 0x08, 0x01, 0x41, 0x08, 0x01, 0x42,
+                              0x08, 0x01, 0x43, 0x13, 0x00, 0x08, 0x01, 0x44, 0x08, 0x01,
+                              0x45, 0x13, 0x00 };
+  e2.wireDecode(Block(EXCLUDE, sizeof(EXCLUDE)));
+
+  e1.clear();
+  e1.excludeBefore(name::Component("A"));
+  e1.excludeOne(name::Component("B"));
+  e1.excludeRange(name::Component("C"), name::Component("D"));
+  e1.excludeAfter(name::Component("E"));
+  BOOST_CHECK_EQUAL(e1, e2);
+}
+
 BOOST_AUTO_TEST_CASE(Malformed)
 {
   Exclude e1;
@@ -217,9 +400,10 @@
 
   Exclude exclude;
   BOOST_CHECK_NO_THROW(exclude.wireDecode(block));
+  BOOST_CHECK(exclude.wireEncode() == block);
 }
 
-BOOST_AUTO_TEST_CASE(ExcludeEmptyComponent) // Bug #2660
+BOOST_AUTO_TEST_CASE(EmptyComponent) // Bug #2660
 {
   Exclude e1, e2;
 
@@ -241,6 +425,8 @@
   BOOST_CHECK_EQUAL(e1.toUri(), e3.toUri());
 }
 
+BOOST_AUTO_TEST_SUITE_END() // WireCompare
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace tests
diff --git a/tests/unit-tests/interest.t.cpp b/tests/unit-tests/interest.t.cpp
index 15de273..7a8fbbe 100644
--- a/tests/unit-tests/interest.t.cpp
+++ b/tests/unit-tests/interest.t.cpp
@@ -1027,7 +1027,7 @@
           .setMinSuffixComponents(2)
           .setMaxSuffixComponents(2)
           .setPublisherPublicKeyLocator(KeyLocator("ndn:/B"))
-          .setExclude(Exclude().excludeBefore(name::Component("C")));
+          .setExclude(Exclude().excludeAfter(name::Component("J")));
 
   Data data("ndn:/A/D");
   SignatureSha256WithRsa signature(KeyLocator("ndn:/B"));
@@ -1040,18 +1040,18 @@
   data1.wireEncode();
   BOOST_CHECK_EQUAL(interest.matchesData(data1), false);
 
-  interest.setMinSuffixComponents(1);
-  BOOST_CHECK_EQUAL(interest.matchesData(data1), true);
-  interest.setMinSuffixComponents(2);
+  Interest interest1 = interest;
+  interest1.setMinSuffixComponents(1);
+  BOOST_CHECK_EQUAL(interest1.matchesData(data1), true);
 
   Data data2 = data;
   data2.setName("ndn:/A/E/F"); // violates MaxSuffixComponents
   data2.wireEncode();
   BOOST_CHECK_EQUAL(interest.matchesData(data2), false);
 
-  interest.setMaxSuffixComponents(3);
-  BOOST_CHECK_EQUAL(interest.matchesData(data2), true);
-  interest.setMaxSuffixComponents(2);
+  Interest interest2 = interest;
+  interest2.setMaxSuffixComponents(3);
+  BOOST_CHECK_EQUAL(interest2.matchesData(data2), true);
 
   Data data3 = data;
   SignatureSha256WithRsa signature3(KeyLocator("ndn:/G")); // violates PublisherPublicKeyLocator
@@ -1059,9 +1059,9 @@
   data3.wireEncode();
   BOOST_CHECK_EQUAL(interest.matchesData(data3), false);
 
-  interest.setPublisherPublicKeyLocator(KeyLocator("ndn:/G"));
-  BOOST_CHECK_EQUAL(interest.matchesData(data3), true);
-  interest.setPublisherPublicKeyLocator(KeyLocator("ndn:/B"));
+  Interest interest3 = interest;
+  interest3.setPublisherPublicKeyLocator(KeyLocator("ndn:/G"));
+  BOOST_CHECK_EQUAL(interest3.matchesData(data3), true);
 
   Data data4 = data;
   DigestSha256 signature4; // violates PublisherPublicKeyLocator
@@ -1069,18 +1069,18 @@
   data4.wireEncode();
   BOOST_CHECK_EQUAL(interest.matchesData(data4), false);
 
-  interest.setPublisherPublicKeyLocator(KeyLocator());
-  BOOST_CHECK_EQUAL(interest.matchesData(data4), true);
-  interest.setPublisherPublicKeyLocator(KeyLocator("ndn:/B"));
+  Interest interest4 = interest;
+  interest4.setPublisherPublicKeyLocator(KeyLocator());
+  BOOST_CHECK_EQUAL(interest4.matchesData(data4), true);
 
   Data data5 = data;
-  data5.setName("ndn:/A/C"); // violates Exclude
+  data5.setName("ndn:/A/J"); // violates Exclude
   data5.wireEncode();
   BOOST_CHECK_EQUAL(interest.matchesData(data5), false);
 
-  interest.setExclude(Exclude().excludeBefore(name::Component("A")));
-  BOOST_CHECK_EQUAL(interest.matchesData(data5), true);
-  interest.setExclude(Exclude().excludeBefore(name::Component("C")));
+  Interest interest5 = interest;
+  interest5.setExclude(Exclude().excludeAfter(name::Component("K")));
+  BOOST_CHECK_EQUAL(interest5.matchesData(data5), true);
 
   Data data6 = data;
   data6.setName("ndn:/H/I"); // violates Name
@@ -1091,14 +1091,11 @@
   data7.setName("ndn:/A/B");
   data7.wireEncode();
 
-  interest = Interest()
-    .setName("/A/B/sha256digest=D548DECEFC4B880720DC9257A8D815E9DF4465E63742EE55C29133055DAA67C2");
-  BOOST_CHECK_EQUAL(interest.matchesData(data7), true);
+  Interest interest7("/A/B/sha256digest=D548DECEFC4B880720DC9257A8D815E9DF4465E63742EE55C29133055DAA67C2");
+  BOOST_CHECK_EQUAL(interest7.matchesData(data7), true);
 
-  interest = Interest()
-    .setName("/A/B/%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00"
-                  "%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00");
-  BOOST_CHECK_EQUAL(interest.matchesData(data7), false); // violates implicit digest
+  Interest interest7b("/A/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000");
+  BOOST_CHECK_EQUAL(interest7b.matchesData(data7), false); // violates implicit digest
 }
 
 BOOST_AUTO_TEST_CASE(InterestFilterMatching)
diff --git a/tests/unit-tests/name-component.t.cpp b/tests/unit-tests/name-component.t.cpp
new file mode 100644
index 0000000..8a99b2f
--- /dev/null
+++ b/tests/unit-tests/name-component.t.cpp
@@ -0,0 +1,211 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2016 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 "name-component.hpp"
+
+#include "boost-test.hpp"
+#include <boost/mpl/vector.hpp>
+
+namespace ndn {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(TestNameComponent)
+
+static const uint8_t NAME_COMPONENT_WIRE[] = {
+        0x08, 0x03, // NameComponent
+          0x6e, 0x64, 0x6e};
+
+static const uint8_t NAME_COMPONENT2_WIRE[] = {
+        0x08, 0x20, // ImplicitSha256DigestComponent
+          0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
+          0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
+          0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
+          0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x49 };
+
+static const uint8_t DIGEST_COMPONENT_WIRE[] = {
+        0x01, 0x20, // ImplicitSha256DigestComponent
+          0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
+          0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
+          0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
+          0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x48 };
+
+static const uint8_t DIGEST_COMPONENT2_WIRE[] = {
+        0x01, 0x20, // ImplicitSha256DigestComponent
+          0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
+          0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
+          0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
+          0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x49 };
+
+static const uint8_t INVALID_COMPONENT_WIRE[] = {
+        0x07, 0x03, // unknown component type
+          0x6e, 0x64, 0x6e};
+
+BOOST_AUTO_TEST_SUITE(Decode)
+
+BOOST_AUTO_TEST_CASE(Generic)
+{
+  Block block(NAME_COMPONENT_WIRE, sizeof(NAME_COMPONENT_WIRE));
+  name::Component comp;
+  BOOST_REQUIRE_NO_THROW(comp.wireDecode(block));
+  BOOST_CHECK_EQUAL(comp.toUri(), "ndn");
+}
+
+BOOST_AUTO_TEST_CASE(Digest)
+{
+  Block block(DIGEST_COMPONENT_WIRE, sizeof(DIGEST_COMPONENT_WIRE));
+  name::Component comp;
+  BOOST_REQUIRE_NO_THROW(comp.wireDecode(block));
+  BOOST_REQUIRE_EQUAL(comp.toUri(), "sha256digest=28bad4b5275bd392dbb670c75cf0b66f"
+                                                 "13f7942b21e80f55c0e86b374753a548");
+}
+
+BOOST_AUTO_TEST_CASE(Invalid)
+{
+  Block block(INVALID_COMPONENT_WIRE, sizeof(INVALID_COMPONENT_WIRE));
+  name::Component comp;
+  BOOST_REQUIRE_THROW(comp.wireDecode(block), name::Component::Error);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // Decode
+
+BOOST_AUTO_TEST_SUITE(Compare)
+
+BOOST_AUTO_TEST_CASE(Generic)
+{
+  name::Component compD("D");
+  name::Component compD2("D");
+  name::Component compF("F");
+  name::Component compAA("AA");
+
+  BOOST_CHECK_EQUAL(compD == compD2, true);
+  BOOST_CHECK_EQUAL(compD != compD2, false);
+  BOOST_CHECK_EQUAL(compD < compD2, false);
+  BOOST_CHECK_EQUAL(compD <= compD2, true);
+  BOOST_CHECK_EQUAL(compD > compD2, false);
+  BOOST_CHECK_EQUAL(compD >= compD2, true);
+
+  BOOST_CHECK_EQUAL(compD == compF, false);
+  BOOST_CHECK_EQUAL(compD != compF, true);
+  BOOST_CHECK_EQUAL(compD < compF, true);
+  BOOST_CHECK_EQUAL(compD <= compF, true);
+  BOOST_CHECK_EQUAL(compD > compF, false);
+  BOOST_CHECK_EQUAL(compD >= compF, false);
+
+  BOOST_CHECK_EQUAL(compF == compAA, false);
+  BOOST_CHECK_EQUAL(compF != compAA, true);
+  BOOST_CHECK_EQUAL(compF < compAA, true);
+  BOOST_CHECK_EQUAL(compF <= compAA, true);
+  BOOST_CHECK_EQUAL(compF > compAA, false);
+  BOOST_CHECK_EQUAL(compF >= compAA, false);
+}
+
+BOOST_AUTO_TEST_CASE(ZeroLength)
+{
+  name::Component comp0("");
+  BOOST_REQUIRE_EQUAL(comp0.value_size(), 0);
+
+  BOOST_CHECK_EQUAL(comp0, comp0);
+  BOOST_CHECK_EQUAL(comp0, name::Component(""));
+  BOOST_CHECK_LT(comp0, name::Component("A"));
+  BOOST_CHECK_LE(comp0, name::Component("A"));
+  BOOST_CHECK_NE(comp0, name::Component("A"));
+  BOOST_CHECK_GT(name::Component("A"), comp0);
+  BOOST_CHECK_GE(name::Component("A"), comp0);
+}
+
+BOOST_AUTO_TEST_CASE(Digest)
+{
+  name::Component digest1(Block(DIGEST_COMPONENT_WIRE, sizeof(DIGEST_COMPONENT_WIRE)));
+  name::Component digest1b(Block(DIGEST_COMPONENT_WIRE, sizeof(DIGEST_COMPONENT_WIRE)));
+  name::Component digest2(Block(DIGEST_COMPONENT2_WIRE, sizeof(DIGEST_COMPONENT2_WIRE)));
+  name::Component generic0;
+  name::Component generic2(Block(NAME_COMPONENT2_WIRE, sizeof(NAME_COMPONENT2_WIRE)));
+
+  BOOST_CHECK_EQUAL(digest1 == digest1b, true);
+  BOOST_CHECK_EQUAL(digest1 != digest1b, false);
+  BOOST_CHECK_EQUAL(digest1 < digest1b, false);
+  BOOST_CHECK_EQUAL(digest1 <= digest1b, true);
+  BOOST_CHECK_EQUAL(digest1 > digest1b, false);
+  BOOST_CHECK_EQUAL(digest1 >= digest1b, true);
+
+  BOOST_CHECK_EQUAL(digest1 == digest2, false);
+  BOOST_CHECK_EQUAL(digest1 != digest2, true);
+  BOOST_CHECK_EQUAL(digest1 < digest2, true);
+  BOOST_CHECK_EQUAL(digest1 <= digest2, true);
+  BOOST_CHECK_EQUAL(digest1 > digest2, false);
+  BOOST_CHECK_EQUAL(digest1 >= digest2, false);
+
+  BOOST_CHECK_EQUAL(digest1 == generic0, false);
+  BOOST_CHECK_EQUAL(digest1 != generic0, true);
+  BOOST_CHECK_EQUAL(digest1 < generic0, true);
+  BOOST_CHECK_EQUAL(digest1 <= generic0, true);
+  BOOST_CHECK_EQUAL(digest1 > generic0, false);
+  BOOST_CHECK_EQUAL(digest1 >= generic0, false);
+
+  BOOST_CHECK_EQUAL(digest2 == generic2, false);
+  BOOST_CHECK_EQUAL(digest2 != generic2, true);
+  BOOST_CHECK_EQUAL(digest2 < generic2, true);
+  BOOST_CHECK_EQUAL(digest2 <= generic2, true);
+  BOOST_CHECK_EQUAL(digest2 > generic2, false);
+  BOOST_CHECK_EQUAL(digest2 >= generic2, false);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // Compare
+
+BOOST_AUTO_TEST_SUITE(CreateFromIterators) // Bug 2490
+
+typedef boost::mpl::vector<
+  std::vector<uint8_t>,
+  std::list<uint8_t>,
+  std::vector<int8_t>,
+  std::list<int8_t>
+> ContainerTypes;
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctet, T, ContainerTypes)
+{
+  T bytes;
+  name::Component c(bytes.begin(), bytes.end());
+  BOOST_CHECK_EQUAL(c.value_size(), 0);
+  BOOST_CHECK_EQUAL(c.size(), 2);
+}
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes)
+{
+  T bytes{1};
+  name::Component c(bytes.begin(), bytes.end());
+  BOOST_CHECK_EQUAL(c.value_size(), 1);
+  BOOST_CHECK_EQUAL(c.size(), 3);
+}
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes)
+{
+  T bytes{1, 2, 3, 4};
+  name::Component c(bytes.begin(), bytes.end());
+  BOOST_CHECK_EQUAL(c.value_size(), 4);
+  BOOST_CHECK_EQUAL(c.size(), 6);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // CreateFromIterators
+
+BOOST_AUTO_TEST_SUITE_END() // TestNameComponent
+
+} // namespace tests
+} // namespace ndn
diff --git a/tests/unit-tests/name.t.cpp b/tests/unit-tests/name.t.cpp
index c1b82aa..64691c4 100644
--- a/tests/unit-tests/name.t.cpp
+++ b/tests/unit-tests/name.t.cpp
@@ -51,21 +51,6 @@
                            0x8,  0x3, // NameComponent
                              0x6e,  0x64,  0x6e};
 
-static const uint8_t TestNameComponent[] = {
-        0x8, 0x3, // NameComponent
-          0x6e, 0x64, 0x6e};
-
-static const uint8_t TestDigestComponent[] = {
-        0x1, 0x20, // ImplicitSha256DigestComponent
-          0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92,
-          0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f,
-          0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55,
-          0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x48 };
-
-const uint8_t Component1[] = {0x7, 0x3, // Error in Type
-                                0x6e, 0x64, 0x6e};
-
-
 BOOST_AUTO_TEST_CASE(Basic)
 {
   Name name("/hello/world");
@@ -113,22 +98,6 @@
   BOOST_CHECK_EQUAL(name.toUri(), "/local/ndn/prefix");
 }
 
-BOOST_AUTO_TEST_CASE(DecodeComponent)
-{
-  Block componentBlock(TestNameComponent, sizeof(TestNameComponent));
-  name::Component nameComponent;
-  BOOST_REQUIRE_NO_THROW(nameComponent.wireDecode(componentBlock));
-  BOOST_CHECK_EQUAL(nameComponent.toUri(), "ndn");
-
-  Block digestComponentBlock(TestDigestComponent, sizeof(TestDigestComponent));
-  name::Component digestComponent;
-  BOOST_REQUIRE_NO_THROW(digestComponent.wireDecode(digestComponentBlock));
-
-  Block errorBlock(Component1, sizeof(Component1));
-  name::Component errorComponent;
-  BOOST_REQUIRE_THROW(errorComponent.wireDecode(errorBlock), name::Component::Error);
-}
-
 BOOST_AUTO_TEST_CASE(AppendsAndMultiEncode)
 {
   Name name("/local");
@@ -449,51 +418,6 @@
   BOOST_CHECK_GT   (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A"),   1), 0);
 }
 
-BOOST_AUTO_TEST_CASE(ZeroLengthComponentCompare)
-{
-  name::Component comp0("");
-  BOOST_REQUIRE_EQUAL(comp0.value_size(), 0);
-
-  BOOST_CHECK_EQUAL(comp0, comp0);
-  BOOST_CHECK_EQUAL(comp0, name::Component(""));
-  BOOST_CHECK_LT(comp0, name::Component("A"));
-  BOOST_CHECK_LE(comp0, name::Component("A"));
-  BOOST_CHECK_NE(comp0, name::Component("A"));
-  BOOST_CHECK_GT(name::Component("A"), comp0);
-  BOOST_CHECK_GE(name::Component("A"), comp0);
-}
-
-BOOST_AUTO_TEST_CASE(CreateComponentWithIterators) // Bug #2490
-{
-  {
-    std::vector<uint8_t> bytes = {1};
-    name::Component c(bytes.begin(), bytes.end());
-    BOOST_CHECK_EQUAL(c.value_size(), 1);
-    BOOST_CHECK_EQUAL(c.size(), 3);
-  }
-
-  {
-    std::list<uint8_t> bytes = {1, 2, 3, 4};
-    name::Component c(bytes.begin(), bytes.end());
-    BOOST_CHECK_EQUAL(c.value_size(), 4);
-    BOOST_CHECK_EQUAL(c.size(), 6);
-  }
-
-  {
-    std::vector<int8_t> bytes = {1};
-    name::Component c(bytes.begin(), bytes.end());
-    BOOST_CHECK_EQUAL(c.value_size(), 1);
-    BOOST_CHECK_EQUAL(c.size(), 3);
-  }
-
-  {
-    std::list<int8_t> bytes = {1, 2, 3, 4};
-    name::Component c(bytes.begin(), bytes.end());
-    BOOST_CHECK_EQUAL(c.value_size(), 4);
-    BOOST_CHECK_EQUAL(c.size(), 6);
-  }
-}
-
 BOOST_AUTO_TEST_CASE(NameWithSpaces)
 {
   Name name("/ hello\t/\tworld \r\n");