name: fix Component wireDecode logic error
Change-Id: Ie1c79dc256af0920f8f8ba58d6f69b0ba71a9791
Refs: #2227
diff --git a/AUTHORS.md b/AUTHORS.md
index 3760340..b8ce63a 100644
--- a/AUTHORS.md
+++ b/AUTHORS.md
@@ -30,3 +30,4 @@
* Vince Lehman <http://vslehman.com>
* Mathias Gibbens <gibmat@cs.arizona.edu>
* Chengyu Fan <chengyu@cs.colostate.edu>
+* Qiuhan Ding <http://irl.cs.ucla.edu/~qiuhanding/>
diff --git a/src/name-component.cpp b/src/name-component.cpp
index 8aad977..8d2db29 100644
--- a/src/name-component.cpp
+++ b/src/name-component.cpp
@@ -58,7 +58,7 @@
Component::Component(const Block& wire)
: Block(wire)
{
- if (type() != tlv::NameComponent && type() != tlv::ImplicitSha256DigestComponent)
+ if (!isGeneric() && !isImplicitSha256Digest())
throw Error("Cannot construct name::Component from not a NameComponent "
"or ImplicitSha256DigestComponent TLV wire block");
}
@@ -475,11 +475,8 @@
void
Component::wireDecode(const Block& wire)
{
- if (wire.type() != tlv::NameComponent || wire.type() != tlv::ImplicitSha256DigestComponent)
- throw Error("name::Component::wireDecode called on not a NameComponent "
- "or ImplicitSha256DigestComponent TLV wire block");
-
*this = wire;
+ // validity check is done within Component(const Block& wire)
}
} // namespace name
diff --git a/tests/unit-tests/test-name.cpp b/tests/unit-tests/test-name.cpp
index 01bd884..8c86d79 100644
--- a/tests/unit-tests/test-name.cpp
+++ b/tests/unit-tests/test-name.cpp
@@ -50,6 +50,20 @@
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)
{
@@ -98,6 +112,22 @@
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");