name: select canonical vs alternate URI representation
refs #4777
Change-Id: Ic644593228bd155de9dcd7005e1b9dc5303a6e66
diff --git a/tests/unit/name-component.t.cpp b/tests/unit/name-component.t.cpp
index c009175..fe62ee0 100644
--- a/tests/unit/name-component.t.cpp
+++ b/tests/unit/name-component.t.cpp
@@ -48,6 +48,8 @@
BOOST_CHECK_EQUAL(comp.type(), tlv::GenericNameComponent);
BOOST_CHECK_EQUAL(comp.isGeneric(), true);
BOOST_CHECK_EQUAL(comp.toUri(), "ndn-cxx");
+ BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), "8=ndn-cxx");
+ BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), "ndn-cxx");
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), "ndn-cxx");
BOOST_CHECK_EQUAL(Component::fromEscapedString("ndn-cxx"), comp);
BOOST_CHECK_EQUAL(Component::fromEscapedString("8=ndn-cxx"), comp);
@@ -98,15 +100,19 @@
for (size_t i = 0; i < hexUpper.size(); i += 2) {
hexPct += "%" + hexUpper.substr(i, 2);
}
+ const std::string hexPctCanonical = "%28%BA%D4%B5%27%5B%D3%92%DB%B6p%C7%5C%F0%B6o%13%F7%94%2B%21%E8%0FU%C0%E8k7GS%A5H";
Component comp(Block(type, fromHex(hexLower)));
BOOST_CHECK_EQUAL(comp.type(), type);
BOOST_CHECK_EQUAL(comp.toUri(), uriPrefix + hexLower);
+ BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=" + hexPctCanonical);
+ BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), uriPrefix + hexLower);
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), uriPrefix + hexLower);
BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexLower));
BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(uriPrefix + hexUpper));
BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPct));
+ BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=" + hexPctCanonical));
CHECK_COMP_ERR(comp.wireDecode(Block(type, fromHex("A791806951F25C4D"))), "TLV-LENGTH must be 32");
CHECK_COMP_ERR(Component::fromEscapedString(uriPrefix), "TLV-LENGTH must be 32");
@@ -133,6 +139,8 @@
BOOST_CHECK_EQUAL(comp.isNumber(), true);
const auto compUri = uriPrefix + "42";
BOOST_CHECK_EQUAL(comp.toUri(), compUri);
+ BOOST_CHECK_EQUAL(comp.toUri(UriFormat::CANONICAL), to_string(type) + "=%2A");
+ BOOST_CHECK_EQUAL(comp.toUri(UriFormat::ALTERNATE), compUri);
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(comp), compUri);
BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(compUri));
BOOST_CHECK_EQUAL(comp, Component::fromEscapedString(to_string(type) + "=%2A"));
diff --git a/tests/unit/name.t.cpp b/tests/unit/name.t.cpp
index 44c261e..21ecc00 100644
--- a/tests/unit/name.t.cpp
+++ b/tests/unit/name.t.cpp
@@ -29,6 +29,7 @@
namespace tests {
using Component = name::Component;
+using UriFormat = name::UriFormat;
BOOST_AUTO_TEST_SUITE(TestName)
@@ -47,6 +48,10 @@
BOOST_CHECK_EQUAL(name[4], Component("\x1C\x9F"));
BOOST_CHECK(name[5].isImplicitSha256Digest());
+ BOOST_CHECK_EQUAL(name.toUri(UriFormat::CANONICAL),
+ "/8=Emid/25042=P3/8=.../8=..../8=%1C%9F/"
+ "1=%04%15%E3bJ%15%18P%AChl%84%F1U%F2%98%08%C0%DDs%81%9A%A4%A4%C2%0B%E7%3AM%8A%87L");
+
Block wire = name.wireEncode();
BOOST_CHECK_EQUAL(wire,
"0737 0804456D6964 FD61D2025033 0800 08012E 08021C9F "
@@ -58,6 +63,9 @@
BOOST_AUTO_TEST_CASE(ParseUri)
{
+ // canonical URI
+ BOOST_CHECK_EQUAL(Name("/8=hello/8=world").toUri(), "/hello/world");
+
// URI with correct scheme
BOOST_CHECK_EQUAL(Name("ndn:/hello/world").toUri(), "/hello/world");