encoding+interest: change Parameters to use non-critical type 36

Refs: #4780
Change-Id: I2cde10da4586737bb5c687a8aa9589beff1a80e1
diff --git a/ndn-cxx/encoding/tlv.hpp b/ndn-cxx/encoding/tlv.hpp
index 516b409..f1cb59d 100644
--- a/ndn-cxx/encoding/tlv.hpp
+++ b/ndn-cxx/encoding/tlv.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2013-2018 Regents of the University of California.
+ * Copyright (c) 2013-2019 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -71,7 +71,7 @@
   Nonce                           = 10,
   InterestLifetime                = 12,
   HopLimit                        = 34,
-  Parameters                      = 35,
+  Parameters                      = 36,
   MetaInfo                        = 20,
   Content                         = 21,
   SignatureInfo                   = 22,
diff --git a/ndn-cxx/interest.cpp b/ndn-cxx/interest.cpp
index 0cbc4ae..1fb3f1f 100644
--- a/ndn-cxx/interest.cpp
+++ b/ndn-cxx/interest.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2013-2018 Regents of the University of California.
+ * Copyright (c) 2013-2019 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -48,7 +48,7 @@
   , m_isCanBePrefixSet(false)
   , m_interestLifetime(lifetime)
 {
-  if (lifetime < time::milliseconds::zero()) {
+  if (lifetime < 0_ms) {
     BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0"));
   }
 
@@ -114,9 +114,8 @@
 
   // InterestLifetime
   if (getInterestLifetime() != DEFAULT_INTEREST_LIFETIME) {
-    totalLength += prependNonNegativeIntegerBlock(encoder,
-                                                  tlv::InterestLifetime,
-                                                  getInterestLifetime().count());
+    totalLength += prependNonNegativeIntegerBlock(encoder, tlv::InterestLifetime,
+                                                  static_cast<uint64_t>(getInterestLifetime().count()));
   }
 
   // Nonce
@@ -174,7 +173,7 @@
   totalLength += encoder.prependByteArrayBlock(tlv::Nonce, reinterpret_cast<uint8_t*>(&nonce), sizeof(nonce));
 
   // ForwardingHint
-  if (getForwardingHint().size() > 0) {
+  if (!getForwardingHint().empty()) {
     totalLength += getForwardingHint().wireEncode(encoder);
   }
 
@@ -254,7 +253,7 @@
     ++element;
   }
   else {
-    m_selectors = Selectors();
+    m_selectors = {};
   }
 
   // Nonce
@@ -286,7 +285,7 @@
     ++element;
   }
   else {
-    m_forwardingHint = DelegationList();
+    m_forwardingHint = {};
   }
 
   return element == m_wire.elements_end();
@@ -318,8 +317,8 @@
   m_selectors = Selectors().setMaxSuffixComponents(1); // CanBePrefix=0
   m_nonce.reset();
   m_interestLifetime = DEFAULT_INTEREST_LIFETIME;
-  m_forwardingHint = DelegationList();
-  m_parameters = Block();
+  m_forwardingHint = {};
+  m_parameters = {};
 
   for (++element; element != m_wire.elements_end(); ++element) {
     switch (element->type()) {
@@ -387,7 +386,7 @@
       }
       case tlv::Parameters: {
         if (lastElement >= 8) {
-          BOOST_THROW_EXCEPTION(Error("Parameters element is out of order"));
+          break; // Parameters is non-critical, ignore out-of-order appearance
         }
         m_parameters = *element;
         lastElement = 8;
@@ -449,9 +448,7 @@
   size_t fullNameLength = dataName.size() + 1;
 
   // check MinSuffixComponents
-  bool hasMinSuffixComponents = getMinSuffixComponents() >= 0;
-  size_t minSuffixComponents = hasMinSuffixComponents ?
-                               static_cast<size_t>(getMinSuffixComponents()) : 0;
+  size_t minSuffixComponents = static_cast<size_t>(std::max(0, getMinSuffixComponents()));
   if (!(interestNameLength + minSuffixComponents <= fullNameLength))
     return false;
 
@@ -509,7 +506,7 @@
   }
 
   // check PublisherPublicKeyLocator
-  const KeyLocator& publisherPublicKeyLocator = this->getPublisherPublicKeyLocator();
+  const KeyLocator& publisherPublicKeyLocator = getPublisherPublicKeyLocator();
   if (!publisherPublicKeyLocator.empty()) {
     const Signature& signature = data.getSignature();
     const Block& signatureInfo = signature.getInfo();
@@ -529,8 +526,8 @@
 Interest::matchesInterest(const Interest& other) const
 {
   /// @todo #3162 match ForwardingHint field
-  return (this->getName() == other.getName() &&
-          this->getSelectors() == other.getSelectors());
+  return this->getName() == other.getName() &&
+         this->getSelectors() == other.getSelectors();
 }
 
 // ---- field accessors ----
@@ -569,7 +566,7 @@
 Interest&
 Interest::setInterestLifetime(time::milliseconds lifetime)
 {
-  if (lifetime < time::milliseconds::zero()) {
+  if (lifetime < 0_ms) {
     BOOST_THROW_EXCEPTION(std::invalid_argument("InterestLifetime must be >= 0"));
   }
   m_interestLifetime = lifetime;
@@ -617,7 +614,7 @@
 Interest&
 Interest::unsetParameters()
 {
-  m_parameters = Block();
+  m_parameters = {};
   m_wire.reset();
   return *this;
 }
@@ -666,7 +663,6 @@
     os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime().count();
     delim = '&';
   }
-
   if (interest.hasNonce()) {
     os << delim << "ndn.Nonce=" << interest.getNonce();
     delim = '&';
diff --git a/ndn-cxx/interest.hpp b/ndn-cxx/interest.hpp
index ad271d0..be3adce 100644
--- a/ndn-cxx/interest.hpp
+++ b/ndn-cxx/interest.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2013-2018 Regents of the University of California.
+ * Copyright (c) 2013-2019 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -252,7 +252,7 @@
   bool
   hasNonce() const
   {
-    return static_cast<bool>(m_nonce);
+    return m_nonce.has_value();
   }
 
   /** @brief Get nonce value.
diff --git a/tests/unit/interest.t.cpp b/tests/unit/interest.t.cpp
index ef646bb..c8c43c5 100644
--- a/tests/unit/interest.t.cpp
+++ b/tests/unit/interest.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2013-2018 Regents of the University of California.
+ * Copyright (c) 2013-2019 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -45,6 +45,13 @@
   BOOST_CHECK(!i.hasNonce());
   BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
   BOOST_CHECK(!i.hasSelectors());
+  BOOST_CHECK(!i.hasParameters());
+  BOOST_CHECK(i.getParameters().empty());
+}
+
+BOOST_AUTO_TEST_CASE(DecodeNotInterest)
+{
+  BOOST_CHECK_THROW(Interest("4202CAFE"_block), tlv::Error);
 }
 
 BOOST_AUTO_TEST_CASE(EncodeDecode02Basic)
@@ -124,14 +131,14 @@
                 0x08, 0x06, 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, // GenericNameComponent
           0x0a, 0x04, // Nonce
                 0x01, 0x00, 0x00, 0x00,
-          0x23, 0x04, // Parameters
+          0x24, 0x04, // Parameters
                 0xc0, 0xc1, 0xc2, 0xc3};
 
   Interest i1;
   i1.setName("/local/ndn/prefix");
   i1.setCanBePrefix(false);
   i1.setNonce(1);
-  i1.setParameters("2304C0C1C2C3"_block);
+  i1.setParameters("2404C0C1C2C3"_block);
   Block wire1 = i1.wireEncode();
   BOOST_CHECK_EQUAL_COLLECTIONS(wire1.begin(), wire1.end(), WIRE, WIRE + sizeof(WIRE));
 
@@ -143,7 +150,7 @@
   BOOST_CHECK_EQUAL(i2.getNonce(), 1);
   BOOST_CHECK_EQUAL(i2.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
   BOOST_CHECK(i2.hasParameters());
-  BOOST_CHECK_EQUAL(i2.getParameters(), "2304C0C1C2C3"_block);
+  BOOST_CHECK_EQUAL(i2.getParameters(), "2404C0C1C2C3"_block);
   BOOST_CHECK(i2.getPublisherPublicKeyLocator().empty());
 }
 
@@ -167,7 +174,7 @@
                 0x4a, 0xcb, 0x1e, 0x4c,
           0x0c, 0x02, // Interest Lifetime
                 0x76, 0xa1,
-          0x23, 0x04, // Parameters
+          0x24, 0x04, // Parameters
                 0xc0, 0xc1, 0xc2, 0xc3};
   Interest i1;
   i1.setName("/local/ndn/prefix");
@@ -176,7 +183,7 @@
   i1.setForwardingHint(DelegationList({{15893, "/H"}}));
   i1.setNonce(0x4c1ecb4a);
   i1.setInterestLifetime(30369_ms);
-  i1.setParameters("2304C0C1C2C3"_block);
+  i1.setParameters("2404C0C1C2C3"_block);
   i1.setMinSuffixComponents(1); // v0.2-only elements will not be encoded
   i1.setExclude(Exclude().excludeAfter(name::Component("J"))); // v0.2-only elements will not be encoded
   Block wire1 = i1.wireEncode();
@@ -190,7 +197,7 @@
   BOOST_CHECK(i2.hasNonce());
   BOOST_CHECK_EQUAL(i2.getNonce(), 0x4c1ecb4a);
   BOOST_CHECK_EQUAL(i2.getInterestLifetime(), 30369_ms);
-  BOOST_CHECK_EQUAL(i2.getParameters(), "2304C0C1C2C3"_block);
+  BOOST_CHECK_EQUAL(i2.getParameters(), "2404C0C1C2C3"_block);
   BOOST_CHECK_EQUAL(i2.getMinSuffixComponents(), -1); // Default because minSuffixComponents was not encoded
   BOOST_CHECK(i2.getExclude().empty()); // Exclude was not encoded
 }
@@ -206,7 +213,7 @@
     i.setNonce(0x03d645a8);
     i.setInterestLifetime(18554_ms);
     i.setPublisherPublicKeyLocator(Name("/K"));
-    i.setParameters("2304A0A1A2A3"_block);
+    i.setParameters("2404A0A1A2A3"_block);
   }
 
 protected:
@@ -261,41 +268,43 @@
 {
   BOOST_CHECK_THROW(i.wireDecode(
     "0529 2100 0703080149 1200 1E0B(1F09 1E023E15 0703080148) "
-    "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3"_block),
+    "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
     tlv::Error);
   BOOST_CHECK_THROW(i.wireDecode(
     "0529 0703080149 1200 2100 1E0B(1F09 1E023E15 0703080148) "
-    "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3"_block),
+    "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
     tlv::Error);
   BOOST_CHECK_THROW(i.wireDecode(
     "0529 0703080149 2100 1E0B(1F09 1E023E15 0703080148) 1200 "
-    "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3"_block),
+    "0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3"_block),
     tlv::Error);
   BOOST_CHECK_THROW(i.wireDecode(
     "0529 0703080149 2100 1200 0A044ACB1E4C "
-    "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2304C0C1C2C3"_block),
+    "1E0B(1F09 1E023E15 0703080148) 0C0276A1 2201D6 2404C0C1C2C3"_block),
     tlv::Error);
   BOOST_CHECK_THROW(i.wireDecode(
     "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
-    "0C0276A1 0A044ACB1E4C 2201D6 2304C0C1C2C3"_block),
+    "0C0276A1 0A044ACB1E4C 2201D6 2404C0C1C2C3"_block),
     tlv::Error);
   BOOST_CHECK_THROW(i.wireDecode(
     "0529 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
-    "0A044ACB1E4C 2201D6 0C0276A1 2304C0C1C2C3"_block),
-    tlv::Error);
-  BOOST_CHECK_THROW(i.wireDecode(
-    "052F 0703080149 2100 1200 1E0B(1F09 1E023E15 0703080148) "
-    "0A044ACB1E4C 0C0276A1 2201D6 2304C0C1C2C3 2304C0C1C2C3"_block),
+    "0A044ACB1E4C 2201D6 0C0276A1 2404C0C1C2C3"_block),
     tlv::Error);
 }
 
-BOOST_AUTO_TEST_CASE(HopLimitOutOfOrder)
+BOOST_AUTO_TEST_CASE(NonCriticalElementOutOfOrder)
 {
-  // HopLimit is non-critical, its out-of-order appearances are ignored
-  i.wireDecode("0514 0703080149 2201D6 2200 2304C0C1C2C3 22020101"_block);
+  // HopLimit
+  i.wireDecode("0514 0703080149 2201D6 2200 2404C0C1C2C3 22020101"_block);
   BOOST_CHECK_EQUAL(i.getName(), "/I");
   // HopLimit=214 is not stored
-  BOOST_CHECK_EQUAL(i.getParameters(), "2304C0C1C2C3"_block);
+  BOOST_CHECK_EQUAL(i.getParameters(), "2404C0C1C2C3"_block);
+
+  // Parameters
+  i.wireDecode("051F 0703080149 2100 1200 0A044ACB1E4C 0C0276A1 2201D6 2404C0C1C2C3 2401EE"_block);
+  BOOST_CHECK_EQUAL(i.getName(), "/I");
+  BOOST_CHECK_EQUAL(i.hasParameters(), true);
+  BOOST_CHECK_EQUAL(i.getParameters(), "2404C0C1C2C3"_block);
 }
 
 BOOST_AUTO_TEST_CASE(NameMissing)
@@ -326,6 +335,12 @@
   BOOST_CHECK_THROW(i.wireDecode("050C 0703080149 0A05EFA420B262"_block), tlv::Error);
 }
 
+BOOST_AUTO_TEST_CASE(BadHopLimit)
+{
+  BOOST_CHECK_THROW(i.wireDecode("0507 0703080149 2200"_block), tlv::Error);
+  BOOST_CHECK_THROW(i.wireDecode("0509 0703080149 22021356"_block), tlv::Error);
+}
+
 BOOST_AUTO_TEST_CASE(UnrecognizedNonCriticalElementBeforeName)
 {
   BOOST_CHECK_THROW(i.wireDecode("0507 FC00 0703080149"_block), tlv::Error);
@@ -549,13 +564,13 @@
 
 BOOST_AUTO_TEST_CASE(SetInterestLifetime)
 {
-  BOOST_CHECK_THROW(Interest("/A", time::milliseconds(-1)), std::invalid_argument);
+  BOOST_CHECK_THROW(Interest("/A", -1_ms), std::invalid_argument);
   BOOST_CHECK_NO_THROW(Interest("/A", 0_ms));
 
   Interest i("/local/ndn/prefix");
   i.setNonce(1);
   BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
-  BOOST_CHECK_THROW(i.setInterestLifetime(time::milliseconds(-1)), std::invalid_argument);
+  BOOST_CHECK_THROW(i.setInterestLifetime(-1_ms), std::invalid_argument);
   BOOST_CHECK_EQUAL(i.getInterestLifetime(), DEFAULT_INTEREST_LIFETIME);
   i.setInterestLifetime(0_ms);
   BOOST_CHECK_EQUAL(i.getInterestLifetime(), 0_ms);
@@ -570,19 +585,19 @@
 
   Interest i;
   BOOST_CHECK(!i.hasParameters());
-  i.setParameters("2300"_block);
+  i.setParameters("2400"_block);
   BOOST_CHECK(i.hasParameters());
   i.unsetParameters();
   BOOST_CHECK(!i.hasParameters());
 
-  i.setParameters("2301C0"_block); // Block overload
-  BOOST_CHECK_EQUAL(i.getParameters(), "2301C0"_block);
+  i.setParameters("2401C0"_block); // Block overload
+  BOOST_CHECK_EQUAL(i.getParameters(), "2401C0"_block);
   i.setParameters(PARAMETERS1, sizeof(PARAMETERS1)); // raw buffer overload
-  BOOST_CHECK_EQUAL(i.getParameters(), "2301C1"_block);
+  BOOST_CHECK_EQUAL(i.getParameters(), "2401C1"_block);
   i.setParameters(make_shared<Buffer>(PARAMETERS2, sizeof(PARAMETERS2))); // ConstBufferPtr overload
-  BOOST_CHECK_EQUAL(i.getParameters(), "2301C2"_block);
+  BOOST_CHECK_EQUAL(i.getParameters(), "2401C2"_block);
   i.setParameters("8001C1"_block); // Block of non-Parameters type
-  BOOST_CHECK_EQUAL(i.getParameters(), "23038001C1"_block);
+  BOOST_CHECK_EQUAL(i.getParameters(), "24038001C1"_block);
 }
 
 // ---- operators ----
@@ -649,11 +664,11 @@
   BOOST_CHECK_EQUAL(a != b, false);
 
   // compare Parameters
-  a.setParameters("2304C0C1C2C3"_block);
+  a.setParameters("2404C0C1C2C3"_block);
   BOOST_CHECK_EQUAL(a == b, false);
   BOOST_CHECK_EQUAL(a != b, true);
 
-  b.setParameters("2304C0C1C2C3"_block);
+  b.setParameters("2404C0C1C2C3"_block);
   BOOST_CHECK_EQUAL(a == b, true);
   BOOST_CHECK_EQUAL(a != b, false);
 }