data: do not encode a Content element if not set

Add Data::hasContent() and Data::unsetContent().
Change the behavior of Interest::setApplicationParameters(Block{}) for
consistency with the corresponding getter and with Data::setContent().

Change-Id: I4cc9058912510db0dfe3da614adee727db149415
diff --git a/.travis.yml b/.travis.yml
index fd734d6..5b91104 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -42,9 +42,6 @@
       osx_image: xcode11.3
       env: # default compiler
     - os: osx
-      osx_image: xcode11.4
-      env: # default compiler
-    - os: osx
       osx_image: xcode11.6
       env: # default compiler
     - os: osx
diff --git a/docs/doxygen.conf.in b/docs/doxygen.conf.in
index fd5237b..c6aeeb0 100644
--- a/docs/doxygen.conf.in
+++ b/docs/doxygen.conf.in
@@ -836,7 +836,7 @@
 # Note that the wildcards are matched against the file with absolute path, so to
 # exclude all test directories for example use the pattern */test/*
 
-EXCLUDE_PATTERNS       = */ndn-cxx/impl/* */ndn-cxx/*/impl/*
+EXCLUDE_PATTERNS       = */ndn-cxx/impl/* */ndn-cxx/*/impl/* */ndn-cxx/util/nonstd/*
 
 # The EXCLUDE_SYMBOLS tag can be used to specify one or more symbol names
 # (namespaces, classes, functions, etc.) that should be excluded from the
diff --git a/ndn-cxx/data.cpp b/ndn-cxx/data.cpp
index 598002f..8106fd3 100644
--- a/ndn-cxx/data.cpp
+++ b/ndn-cxx/data.cpp
@@ -20,7 +20,7 @@
  */
 
 #include "ndn-cxx/data.hpp"
-#include "ndn-cxx/encoding/block-helpers.hpp"
+#include "ndn-cxx/signature.hpp"
 #include "ndn-cxx/util/sha256.hpp"
 
 namespace ndn {
@@ -34,7 +34,6 @@
 
 Data::Data(const Name& name)
   : m_name(name)
-  , m_content(tlv::Content)
 {
 }
 
@@ -69,13 +68,15 @@
   totalLength += m_signatureInfo.wireEncode(encoder, SignatureInfo::Type::Data);
 
   // Content
-  totalLength += encoder.prependBlock(getContent());
+  if (hasContent()) {
+    totalLength += encoder.prependBlock(m_content);
+  }
 
   // MetaInfo
-  totalLength += getMetaInfo().wireEncode(encoder);
+  totalLength += m_metaInfo.wireEncode(encoder);
 
   // Name
-  totalLength += getName().wireEncode(encoder);
+  totalLength += m_name.wireEncode(encoder);
 
   if (!wantUnsignedPortionOnly) {
     totalLength += encoder.prependVarNumber(totalLength);
@@ -142,7 +143,7 @@
   m_name.wireDecode(*element);
 
   m_metaInfo = {};
-  m_content = Block(tlv::Content);
+  m_content = {};
   m_signatureInfo = {};
   m_signatureValue = {};
   m_fullName.clear();
@@ -240,32 +241,32 @@
   return *this;
 }
 
-const Block&
-Data::getContent() const
-{
-  if (!m_content.hasWire()) {
-    const_cast<Block&>(m_content).encode();
-  }
-  return m_content;
-}
-
 Data&
 Data::setContent(const Block& block)
 {
+  if (!block.isValid()) {
+    NDN_THROW(std::invalid_argument("Content block must be valid"));
+  }
+
   if (block.type() == tlv::Content) {
     m_content = block;
   }
   else {
     m_content = Block(tlv::Content, block);
   }
+  m_content.encode();
   resetWire();
   return *this;
 }
 
 Data&
-Data::setContent(const uint8_t* value, size_t valueSize)
+Data::setContent(const uint8_t* value, size_t length)
 {
-  m_content = makeBinaryBlock(tlv::Content, value, valueSize);
+  if (value == nullptr && length != 0) {
+    NDN_THROW(std::invalid_argument("Content buffer cannot be nullptr"));
+  }
+
+  m_content = makeBinaryBlock(tlv::Content, value, length);
   resetWire();
   return *this;
 }
@@ -276,11 +277,20 @@
   if (value == nullptr) {
     NDN_THROW(std::invalid_argument("Content buffer cannot be nullptr"));
   }
+
   m_content = Block(tlv::Content, std::move(value));
   resetWire();
   return *this;
 }
 
+Data&
+Data::unsetContent()
+{
+  m_content = {};
+  resetWire();
+  return *this;
+}
+
 Signature
 Data::getSignature() const
 {
@@ -310,6 +320,7 @@
   if (value == nullptr) {
     NDN_THROW(std::invalid_argument("SignatureValue buffer cannot be nullptr"));
   }
+
   m_signatureValue = Block(tlv::SignatureValue, std::move(value));
   resetWire();
   return *this;
@@ -358,12 +369,15 @@
 std::ostream&
 operator<<(std::ostream& os, const Data& data)
 {
-  os << "Name: " << data.getName() << "\n";
-  os << "MetaInfo: " << data.getMetaInfo() << "\n";
-  os << "Content: (size: " << data.getContent().value_size() << ")\n";
-  os << "Signature: (type: " << static_cast<tlv::SignatureTypeValue>(data.getSignatureType())
-     << ", value_length: "<< data.getSignatureValue().value_size() << ")";
-  os << std::endl;
+  os << "Name: " << data.getName() << "\n"
+     << "MetaInfo: [" << data.getMetaInfo() << "]\n";
+
+  if (data.hasContent()) {
+    os << "Content: [" << data.getContent().value_size() << " bytes]\n";
+  }
+
+  os << "Signature: [type: " << static_cast<tlv::SignatureTypeValue>(data.getSignatureType())
+     << ", length: "<< data.getSignatureValue().value_size() << "]\n";
 
   return os;
 }
diff --git a/ndn-cxx/data.hpp b/ndn-cxx/data.hpp
index 26a64b3..ac25c07 100644
--- a/ndn-cxx/data.hpp
+++ b/ndn-cxx/data.hpp
@@ -26,10 +26,12 @@
 #include "ndn-cxx/encoding/block.hpp"
 #include "ndn-cxx/meta-info.hpp"
 #include "ndn-cxx/name.hpp"
-#include "ndn-cxx/signature.hpp"
+#include "ndn-cxx/signature-info.hpp"
 
 namespace ndn {
 
+class Signature;
+
 /** @brief Represents a %Data packet.
  *  @sa https://named-data.net/doc/NDN-packet-spec/0.3/data.html
  */
@@ -145,39 +147,68 @@
   Data&
   setMetaInfo(const MetaInfo& metaInfo);
 
-  /** @brief Get Content
+  /**
+   * @brief Return whether this Data has a Content element
+   */
+  bool
+  hasContent() const noexcept
+  {
+    return m_content.isValid();
+  }
+
+  /**
+   * @brief Get the Content element
    *
-   *  The Content value is accessible through value()/value_size() or value_begin()/value_end()
-   *  methods of the Block class.
+   * If the element is not present (hasContent() == false), an invalid Block will be returned.
+   *
+   * The value of the returned Content Block (if valid) can be accessed through
+   * Block::value() / Block::value_size() or Block::value_begin() / Block::value_end().
+   *
+   * @sa hasContent()
+   * @sa Block::blockFromValue(), Block::parse()
    */
   const Block&
-  getContent() const;
+  getContent() const noexcept
+  {
+    return m_content;
+  }
 
-  /** @brief Set Content from a Block
+  /**
+   * @brief Set Content from a Block
+   * @param block TLV block to be used as Content; must be valid
+   * @return a reference to this Data, to allow chaining
    *
-   *  If the block's TLV-TYPE is Content, it will be used directly as this Data's Content element.
-   *  Otherwise, the block will be nested into a Content element.
-   *
-   *  @return a reference to this Data, to allow chaining
+   * If the block's TLV-TYPE is tlv::Content, it will be used directly as this Data's
+   * Content element. Otherwise, the block will be nested into a Content element.
    */
   Data&
   setContent(const Block& block);
 
-  /** @brief Set Content by copying from a raw buffer
-   *  @param value pointer to the first octet of the value
-   *  @param valueSize size of the buffer
-   *  @return a reference to this Data, to allow chaining
+  /**
+   * @brief Set Content by copying from a raw buffer
+   * @param value buffer with the TLV-VALUE of the content; may be nullptr if @p length is zero
+   * @param length size of the buffer
+   * @return a reference to this Data, to allow chaining
    */
   Data&
-  setContent(const uint8_t* value, size_t valueSize);
+  setContent(const uint8_t* value, size_t length);
 
-  /** @brief Set Content from a shared buffer
-   *  @param value buffer containing the TLV-VALUE of the content; must not be nullptr
-   *  @return a reference to this Data, to allow chaining
+  /**
+   * @brief Set Content from a shared buffer
+   * @param value buffer with the TLV-VALUE of the content; must not be nullptr
+   * @return a reference to this Data, to allow chaining
    */
   Data&
   setContent(ConstBufferPtr value);
 
+  /**
+   * @brief Remove the Content element
+   * @return a reference to this Data, to allow chaining
+   * @post hasContent() == false
+   */
+  Data&
+  unsetContent();
+
   /** @brief Get Signature
    *  @deprecated Use getSignatureInfo and getSignatureValue
    */
diff --git a/ndn-cxx/interest.cpp b/ndn-cxx/interest.cpp
index 935bd1c..dccf4a2 100644
--- a/ndn-cxx/interest.cpp
+++ b/ndn-cxx/interest.cpp
@@ -378,6 +378,7 @@
   if (digestIndex == -2) {
     NDN_THROW(std::invalid_argument("Name cannot have more than one ParametersSha256DigestComponent"));
   }
+
   if (name != m_name) {
     m_name = name;
     if (hasApplicationParameters()) {
@@ -444,6 +445,7 @@
   if (lifetime < 0_ms) {
     NDN_THROW(std::invalid_argument("InterestLifetime must be >= 0"));
   }
+
   if (lifetime != m_interestLifetime) {
     m_interestLifetime = lifetime;
     m_wire.reset();
@@ -478,9 +480,10 @@
 Interest::setApplicationParameters(const Block& parameters)
 {
   if (!parameters.isValid()) {
-    setApplicationParametersInternal(Block(tlv::ApplicationParameters));
+    NDN_THROW(std::invalid_argument("ApplicationParameters block must be valid"));
   }
-  else if (parameters.type() == tlv::ApplicationParameters) {
+
+  if (parameters.type() == tlv::ApplicationParameters) {
     setApplicationParametersInternal(parameters);
   }
   else {
@@ -497,6 +500,7 @@
   if (value == nullptr && length != 0) {
     NDN_THROW(std::invalid_argument("ApplicationParameters buffer cannot be nullptr"));
   }
+
   setApplicationParametersInternal(makeBinaryBlock(tlv::ApplicationParameters, value, length));
   addOrReplaceParametersDigestComponent();
   m_wire.reset();
@@ -509,6 +513,7 @@
   if (value == nullptr) {
     NDN_THROW(std::invalid_argument("ApplicationParameters buffer cannot be nullptr"));
   }
+
   setApplicationParametersInternal(Block(tlv::ApplicationParameters, std::move(value)));
   addOrReplaceParametersDigestComponent();
   m_wire.reset();
diff --git a/ndn-cxx/interest.hpp b/ndn-cxx/interest.hpp
index 0594b3b..fde93c6 100644
--- a/ndn-cxx/interest.hpp
+++ b/ndn-cxx/interest.hpp
@@ -324,15 +324,21 @@
   Interest&
   setHopLimit(optional<uint8_t> hopLimit);
 
+  /**
+   * @brief Return whether this Interest has any ApplicationParameters.
+   */
   bool
   hasApplicationParameters() const noexcept
   {
     return !m_parameters.empty();
   }
 
-  /** @brief Get the ApplicationParameters
+  /**
+   * @brief Get the ApplicationParameters.
    *
-   *  If the element is not present, an invalid Block will be returned.
+   * If the element is not present, an invalid Block will be returned.
+   *
+   * @sa hasApplicationParameters()
    */
   Block
   getApplicationParameters() const
@@ -343,50 +349,55 @@
       return m_parameters.front();
   }
 
-  /** @brief Set ApplicationParameters from a Block.
-   *  @return a reference to this Interest
+  /**
+   * @brief Set ApplicationParameters from a Block.
+   * @param block TLV block to be used as ApplicationParameters; must be valid
+   * @return a reference to this Interest
    *
-   *  If the block is default-constructed, this will set a zero-length ApplicationParameters
-   *  element. Else, if the block's TLV-TYPE is ApplicationParameters, it will be used directly
-   *  as this Interest's ApplicationParameters element. Else, the block will be nested into an
-   *  ApplicationParameters element.
+   * If the block's TLV-TYPE is tlv::ApplicationParameters, it will be used directly as
+   * this Interest's ApplicationParameters element. Otherwise, the block will be nested
+   * into an ApplicationParameters element.
    *
-   *  This function will also recompute the value of the ParametersSha256DigestComponent in the
-   *  Interest's name. If the name does not contain a ParametersSha256DigestComponent, one will
-   *  be appended to it.
+   * This function will also recompute the value of the ParametersSha256DigestComponent in the
+   * Interest's name. If the name does not contain a ParametersSha256DigestComponent, one will
+   * be appended to it.
    */
   Interest&
-  setApplicationParameters(const Block& parameters);
+  setApplicationParameters(const Block& block);
 
-  /** @brief Set ApplicationParameters by copying from a raw buffer.
-   *  @param value points to a buffer from which the TLV-VALUE of the parameters will be copied;
-   *               may be nullptr if @p length is zero
-   *  @param length size of the buffer
-   *  @return a reference to this Interest
+  /**
+   * @brief Set ApplicationParameters by copying from a raw buffer.
+   * @param value points to a buffer from which the TLV-VALUE of the parameters will be copied;
+   *              may be nullptr if @p length is zero
+   * @param length size of the buffer
+   * @return a reference to this Interest
    *
-   *  This function will also recompute the value of the ParametersSha256DigestComponent in the
-   *  Interest's name. If the name does not contain a ParametersSha256DigestComponent, one will
-   *  be appended to it.
+   * This function will also recompute the value of the ParametersSha256DigestComponent in the
+   * Interest's name. If the name does not contain a ParametersSha256DigestComponent, one will
+   * be appended to it.
    */
   Interest&
   setApplicationParameters(const uint8_t* value, size_t length);
 
-  /** @brief Set ApplicationParameters from a shared buffer.
-   *  @param value buffer containing the TLV-VALUE of the parameters; must not be nullptr
-   *  @return a reference to this Interest
+  /**
+   * @brief Set ApplicationParameters from a shared buffer.
+   * @param value buffer containing the TLV-VALUE of the parameters; must not be nullptr
+   * @return a reference to this Interest
    *
-   *  This function will also recompute the value of the ParametersSha256DigestComponent in the
-   *  Interest's name. If the name does not contain a ParametersSha256DigestComponent, one will
-   *  be appended to it.
+   * This function will also recompute the value of the ParametersSha256DigestComponent in the
+   * Interest's name. If the name does not contain a ParametersSha256DigestComponent, one will
+   * be appended to it.
    */
   Interest&
   setApplicationParameters(ConstBufferPtr value);
 
-  /** @brief Remove the ApplicationParameters element from this Interest.
-   *  @post hasApplicationParameters() == false
+  /**
+   * @brief Remove the ApplicationParameters element from this Interest.
+   * @return a reference to this Interest
+   * @post hasApplicationParameters() == false
    *
-   *  This function will also remove any InterestSignatureInfo and InterestSignatureValue elements
-   *  in the Interest, as well as any ParametersSha256DigestComponents in the Interest's name.
+   * This function will also remove any InterestSignatureInfo and InterestSignatureValue elements
+   * in the Interest, as well as any ParametersSha256DigestComponents in the Interest's name.
    */
   Interest&
   unsetApplicationParameters();
diff --git a/ndn-cxx/metadata-object.cpp b/ndn-cxx/metadata-object.cpp
index 43212b6..0c8ca69 100644
--- a/ndn-cxx/metadata-object.cpp
+++ b/ndn-cxx/metadata-object.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2013-2019 Regents of the University of California.
+ * Copyright (c) 2013-2020 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -32,14 +32,18 @@
 
 MetadataObject::MetadataObject(const Data& data)
 {
-  if (data.getContentType() != tlv::ContentType_Blob) {
-    NDN_THROW(Error("Expecting ContentType Blob, got " + to_string(data.getContentType())));
-  }
-
   if (!isValidName(data.getName())) {
     NDN_THROW(Error("Name " + data.getName().toUri() + " is not a valid MetadataObject name"));
   }
 
+  if (data.getContentType() != tlv::ContentType_Blob) {
+    NDN_THROW(Error("MetadataObject has invalid ContentType " + to_string(data.getContentType())));
+  }
+
+  if (data.getContent().value_size() == 0) {
+    NDN_THROW(Error("MetadataObject is empty"));
+  }
+
   data.getContent().parse();
   // ignore non-Name elements before the first one
   m_versionedName.wireDecode(data.getContent().get(tlv::Name));
diff --git a/ndn-cxx/prefix-announcement.cpp b/ndn-cxx/prefix-announcement.cpp
index da65e93..3d706d3 100644
--- a/ndn-cxx/prefix-announcement.cpp
+++ b/ndn-cxx/prefix-announcement.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /*
- * Copyright (c) 2013-2019 Regents of the University of California.
+ * Copyright (c) 2013-2020 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -24,18 +24,13 @@
 
 namespace ndn {
 
-static const name::Component KEYWORD_PA_COMP = "20025041"_block;
+const name::Component KEYWORD_PA_COMP = "20 02 5041"_block; // 32=PA
 
 PrefixAnnouncement::PrefixAnnouncement() = default;
 
 PrefixAnnouncement::PrefixAnnouncement(Data data)
   : m_data(std::move(data))
 {
-  if (m_data->getContentType() != tlv::ContentType_PrefixAnn) {
-    NDN_THROW(Error("Data is not a prefix announcement: ContentType is " +
-                    to_string(m_data->getContentType())));
-  }
-
   const Name& dataName = m_data->getName();
   if (dataName.size() < 3 || dataName[-3] != KEYWORD_PA_COMP ||
       !dataName[-2].isVersion() || !dataName[-1].isSegment()) {
@@ -43,6 +38,15 @@
   }
   m_announcedName = dataName.getPrefix(-3);
 
+  if (m_data->getContentType() != tlv::ContentType_PrefixAnn) {
+    NDN_THROW(Error("Data is not a prefix announcement: ContentType is " +
+                    to_string(m_data->getContentType())));
+  }
+
+  if (m_data->getContent().value_size() == 0) {
+    NDN_THROW(Error("Prefix announcement is empty"));
+  }
+
   const Block& payload = m_data->getContent();
   payload.parse();
 
@@ -56,7 +60,7 @@
   for (const Block& element : payload.elements()) {
     if (element.type() != tlv::nfd::ExpirationPeriod && element.type() != tlv::ValidityPeriod &&
         tlv::isCriticalType(element.type())) {
-      NDN_THROW(Error("unrecognized element of critical type " + to_string(element.type())));
+      NDN_THROW(Error("Unrecognized element of critical type " + to_string(element.type())));
     }
   }
 }
diff --git a/ndn-cxx/security/key-chain.cpp b/ndn-cxx/security/key-chain.cpp
index 50eaa22..8fbf9cf 100644
--- a/ndn-cxx/security/key-chain.cpp
+++ b/ndn-cxx/security/key-chain.cpp
@@ -322,11 +322,16 @@
 {
   BOOST_ASSERT(static_cast<bool>(key));
 
+  const auto& certContent = certificate.getContent();
+  if (certContent.value_size() == 0) {
+    NDN_THROW(std::invalid_argument("Certificate `" + certificate.getName().toUri() + "` is empty"));
+  }
+
   if (key.getName() != certificate.getKeyName() ||
-      !std::equal(certificate.getContent().value_begin(), certificate.getContent().value_end(),
-                  key.getPublicKey().begin()))
+      !std::equal(certContent.value_begin(), certContent.value_end(), key.getPublicKey().begin())) {
     NDN_THROW(std::invalid_argument("Key `" + key.getName().toUri() + "` "
                                     "does not match certificate `" + certificate.getName().toUri() + "`"));
+  }
 
   key.addCertificate(certificate);
 }
diff --git a/tests/unit/data.t.cpp b/tests/unit/data.t.cpp
index e6c7b4b..4bb0f64 100644
--- a/tests/unit/data.t.cpp
+++ b/tests/unit/data.t.cpp
@@ -89,7 +89,8 @@
   BOOST_CHECK_EQUAL(d.getContentType(), tlv::ContentType_Blob);
   BOOST_CHECK_EQUAL(d.getFreshnessPeriod(), DEFAULT_FRESHNESS_PERIOD);
   BOOST_CHECK(!d.getFinalBlock());
-  BOOST_CHECK_EQUAL(d.getContent().type(), tlv::Content);
+  BOOST_CHECK_EQUAL(d.hasContent(), false);
+  BOOST_CHECK_EQUAL(d.getContent().isValid(), false);
   BOOST_CHECK_EQUAL(d.getContent().value_size(), 0);
   BOOST_CHECK(!d.getSignatureInfo());
   BOOST_CHECK_EQUAL(d.getSignatureType(), -1);
@@ -173,7 +174,7 @@
   Data d;
   d.setSignatureInfo(SignatureInfo(tlv::DigestSha256));
   d.setSignatureValue(std::make_shared<Buffer>());
-  BOOST_CHECK_EQUAL(d.wireEncode(), "060D 0700 1400 1500 16031B0100 1700"_block);
+  BOOST_CHECK_EQUAL(d.wireEncode(), "060B 0700 1400 16031B0100 1700"_block);
 }
 
 BOOST_FIXTURE_TEST_CASE(Full, DataSigningKeyFixture)
@@ -249,6 +250,7 @@
   BOOST_CHECK_EQUAL(d.getContentType(), tlv::ContentType_Blob);
   BOOST_CHECK_EQUAL(d.getFreshnessPeriod(), 0_ms);
   BOOST_CHECK_EQUAL(d.getFinalBlock().has_value(), false);
+  BOOST_CHECK_EQUAL(d.hasContent(), false);
   BOOST_CHECK_EQUAL(d.getContent().value_size(), 0);
   BOOST_CHECK_EQUAL(d.getSignatureType(), tlv::DigestSha256);
   BOOST_CHECK_EQUAL(d.getKeyLocator().has_value(), false);
@@ -262,7 +264,7 @@
   d.setName("/E");
   BOOST_CHECK_EQUAL(d.hasWire(), false);
   BOOST_CHECK_EQUAL(d.wireEncode(),
-                    "0630 0703080145 1400 1500 16031B0100 "
+                    "062E 0703(080145) 1400 1603(1B0100) "
                     "1720612A79399E60304A9F701C1ECAC7956BF2F1B046E6C6F0D6C29B3FE3A29BAD76"_block);
 }
 
@@ -273,6 +275,7 @@
   BOOST_CHECK_EQUAL(d.getContentType(), tlv::ContentType_Blob);
   BOOST_CHECK_EQUAL(d.getFreshnessPeriod(), 0_ms);
   BOOST_CHECK_EQUAL(d.getFinalBlock().has_value(), false);
+  BOOST_CHECK_EQUAL(d.hasContent(), false);
   BOOST_CHECK_EQUAL(d.getContent().value_size(), 0);
   BOOST_CHECK_EQUAL(d.getSignatureType(), tlv::DigestSha256);
   BOOST_CHECK_EQUAL(d.getKeyLocator().has_value(), false);
@@ -286,6 +289,7 @@
   BOOST_CHECK_EQUAL(d.getContentType(), tlv::ContentType_Blob);
   BOOST_CHECK_EQUAL(d.getFreshnessPeriod(), 10_s);
   BOOST_CHECK_EQUAL(d.getFinalBlock().has_value(), false);
+  BOOST_CHECK_EQUAL(d.hasContent(), true);
   BOOST_CHECK_EQUAL(std::string(reinterpret_cast<const char*>(d.getContent().value()),
                                 d.getContent().value_size()), "SUCCESS!");
   BOOST_CHECK_EQUAL(d.getSignatureType(), tlv::SignatureSha256WithRsa);
@@ -302,6 +306,7 @@
   BOOST_CHECK_EQUAL(d.getContentType(), tlv::ContentType_Blob);
   BOOST_CHECK_EQUAL(d.getFreshnessPeriod(), 0_ms);
   BOOST_CHECK_EQUAL(d.getFinalBlock().has_value(), false);
+  BOOST_CHECK_EQUAL(d.hasContent(), true);
   BOOST_CHECK_EQUAL(d.getContent().value_size(), 0);
   BOOST_CHECK_EQUAL(d.getSignatureType(), tlv::DigestSha256);
   BOOST_CHECK_EQUAL(d.getKeyLocator().has_value(), false);
@@ -499,36 +504,58 @@
 BOOST_AUTO_TEST_CASE(SetContent)
 {
   Data d;
-  BOOST_CHECK_EQUAL(d.getContent().type(), tlv::Content);
+  BOOST_CHECK_EQUAL(d.hasContent(), false);
+  BOOST_CHECK_EQUAL(d.getContent().type(), tlv::Invalid);
   BOOST_CHECK_EQUAL(d.getContent().value_size(), 0);
 
+  // Block overload, used directly as Content
   const uint8_t direct[] = {0xca, 0xfe};
-  d.setContent("1502CAFE"_block); // Block overload, used directly as Content
+  d.setContent("1502CAFE"_block);
+  BOOST_CHECK_EQUAL(d.hasContent(), true);
   BOOST_CHECK_EQUAL(d.getContent().type(), tlv::Content);
   BOOST_CHECK_EQUAL_COLLECTIONS(d.getContent().value_begin(), d.getContent().value_end(),
                                 direct, direct + sizeof(direct));
 
+  // Block overload, nested inside Content element
   const uint8_t nested[] = {0x99, 0x02, 0xca, 0xfe};
-  d.setContent(Block(nested, sizeof(nested))); // Block overload, nested inside Content element
+  d.setContent(Block(nested, sizeof(nested)));
+  BOOST_CHECK_EQUAL(d.hasContent(), true);
   BOOST_CHECK_EQUAL(d.getContent().type(), tlv::Content);
   BOOST_CHECK_EQUAL_COLLECTIONS(d.getContent().value_begin(), d.getContent().value_end(),
                                 nested, nested + sizeof(nested));
 
-  d.setContent(nested, sizeof(nested)); // raw buffer overload
+  // Block overload, default constructed (invalid)
+  BOOST_CHECK_THROW(d.setContent(Block{}), std::invalid_argument);
+
+  // raw buffer overload
+  d.setContent(nested, sizeof(nested));
+  BOOST_CHECK_EQUAL(d.hasContent(), true);
   BOOST_CHECK_EQUAL(d.getContent().type(), tlv::Content);
   BOOST_CHECK_EQUAL_COLLECTIONS(d.getContent().value_begin(), d.getContent().value_end(),
                                 nested, nested + sizeof(nested));
-
-  d.setContent(std::make_shared<Buffer>(direct, sizeof(direct))); // ConstBufferPtr overload
-  BOOST_CHECK_EQUAL(d.getContent().type(), tlv::Content);
-  BOOST_CHECK_EQUAL_COLLECTIONS(d.getContent().value_begin(), d.getContent().value_end(),
-                                direct, direct + sizeof(direct));
-
-  d.setContent(std::make_shared<Buffer>()); // ConstBufferPtr overload, empty buffer
+  d.setContent(nullptr, 0);
+  BOOST_CHECK_EQUAL(d.hasContent(), true);
   BOOST_CHECK_EQUAL(d.getContent().type(), tlv::Content);
   BOOST_CHECK_EQUAL(d.getContent().value_size(), 0);
+  BOOST_CHECK_THROW(d.setContent(nullptr, 1), std::invalid_argument);
 
+  // ConstBufferPtr overload
+  d.setContent(std::make_shared<Buffer>(direct, sizeof(direct)));
+  BOOST_CHECK_EQUAL(d.hasContent(), true);
+  BOOST_CHECK_EQUAL(d.getContent().type(), tlv::Content);
+  BOOST_CHECK_EQUAL_COLLECTIONS(d.getContent().value_begin(), d.getContent().value_end(),
+                                direct, direct + sizeof(direct));
+  d.setContent(std::make_shared<Buffer>());
+  BOOST_CHECK_EQUAL(d.hasContent(), true);
+  BOOST_CHECK_EQUAL(d.getContent().type(), tlv::Content);
+  BOOST_CHECK_EQUAL(d.getContent().value_size(), 0);
   BOOST_CHECK_THROW(d.setContent(nullptr), std::invalid_argument);
+
+  // unset
+  d.unsetContent();
+  BOOST_CHECK_EQUAL(d.hasContent(), false);
+  BOOST_CHECK_EQUAL(d.getContent().type(), tlv::Invalid);
+  BOOST_CHECK_EQUAL(d.getContent().value_size(), 0);
 }
 
 BOOST_AUTO_TEST_CASE(SetSignatureValue)
@@ -594,12 +621,18 @@
 
 BOOST_AUTO_TEST_CASE(Print)
 {
-  Data d(Block(DATA1, sizeof(DATA1)));
-  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(d),
+  Data d1(Block(DATA1, sizeof(DATA1)));
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(d1),
                     "Name: /local/ndn/prefix\n"
-                    "MetaInfo: ContentType: 0, FreshnessPeriod: 10000 milliseconds\n"
-                    "Content: (size: 8)\n"
-                    "Signature: (type: SignatureSha256WithRsa, value_length: 128)\n");
+                    "MetaInfo: [ContentType: 0, FreshnessPeriod: 10000 milliseconds]\n"
+                    "Content: [8 bytes]\n"
+                    "Signature: [type: SignatureSha256WithRsa, length: 128]\n");
+
+  Data d2("/foo");
+  BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(d2),
+                    "Name: /foo\n"
+                    "MetaInfo: [ContentType: 0]\n"
+                    "Signature: [type: Unknown(65535), length: 0]\n");
 }
 
 BOOST_AUTO_TEST_SUITE_END() // TestData
diff --git a/tests/unit/interest.t.cpp b/tests/unit/interest.t.cpp
index b675503..2887c99 100644
--- a/tests/unit/interest.t.cpp
+++ b/tests/unit/interest.t.cpp
@@ -897,12 +897,11 @@
   BOOST_CHECK(!i.hasApplicationParameters());
 
   // Block overload
-  i.setApplicationParameters(Block{});
-  BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2400"_block);
   i.setApplicationParameters("2401C0"_block);
   BOOST_CHECK_EQUAL(i.getApplicationParameters(), "2401C0"_block);
   i.setApplicationParameters("8001C1"_block);
   BOOST_CHECK_EQUAL(i.getApplicationParameters(), "24038001C1"_block);
+  BOOST_CHECK_THROW(i.setApplicationParameters(Block{}), std::invalid_argument);
 
   // raw buffer+size overload
   i.setApplicationParameters(PARAMETERS1, sizeof(PARAMETERS1));
@@ -1159,7 +1158,7 @@
   });
 
   // Test failure with missing InterestSignatureInfo
-  i3.setApplicationParameters(Block());
+  i3.setApplicationParameters(nullptr, 0);
   BOOST_CHECK_EXCEPTION(i3.extractSignedRanges(), tlv::Error, [] (const auto& e) {
     return e.what() == "Interest missing InterestSignatureInfo"s;
   });
diff --git a/tests/unit/metadata-object.t.cpp b/tests/unit/metadata-object.t.cpp
index 364600e..632e15f 100644
--- a/tests/unit/metadata-object.t.cpp
+++ b/tests/unit/metadata-object.t.cpp
@@ -85,23 +85,34 @@
 {
   Data data;
 
-  // invalid content type
-  data.setName(Name("/ndn/unit/test").append(metadataComponent));
-  data.setContentType(tlv::ContentType_Key);
-  BOOST_CHECK_THROW(MetadataObject metadata(data), tlv::Error);
-
   // invalid metadata name
   data.setName("/ndn/unit/test");
-  data.setContentType(tlv::ContentType_Blob);
-  BOOST_CHECK_THROW(MetadataObject metadata(data), tlv::Error);
+  BOOST_CHECK_EXCEPTION(MetadataObject{data}, tlv::Error, [] (const auto& e) {
+    return e.what() == "Name /ndn/unit/test is not a valid MetadataObject name"s;
+  });
+  data.setName(Name("/ndn/unit/test").append(metadataComponent));
+  BOOST_CHECK_EXCEPTION(MetadataObject{data}, tlv::Error, [] (const auto& e) {
+    return e.what() == "Name /ndn/unit/test/32=metadata is not a valid MetadataObject name"s;
+  });
+
+  // invalid content type
+  data.setName(Name("/ndn/unit/test").append(metadataComponent).appendVersion().appendSegment(0));
+  data.setContentType(tlv::ContentType_Key);
+  BOOST_CHECK_EXCEPTION(MetadataObject{data}, tlv::Error, [] (const auto& e) {
+    return e.what() == "MetadataObject has invalid ContentType 2"s;
+  });
 
   // empty content
-  data.setName(Name("ndn/unit/test").append(metadataComponent));
-  BOOST_CHECK_THROW(MetadataObject metadata(data), tlv::Error);
+  data.setContentType(tlv::ContentType_Blob);
+  BOOST_CHECK_EXCEPTION(MetadataObject{data}, tlv::Error, [] (const auto& e) {
+    return e.what() == "MetadataObject is empty"s;
+  });
 
   // non-empty content with no name element
   data.setContent("F000"_block);
-  BOOST_CHECK_THROW(MetadataObject metadata(data), tlv::Error);
+  BOOST_CHECK_EXCEPTION(MetadataObject{data}, tlv::Error, [] (const auto& e) {
+    return e.what() == "No sub-element of type 7 found in block of type 21"s;
+  });
 }
 
 BOOST_AUTO_TEST_CASE(IsValidName)
diff --git a/tests/unit/prefix-announcement.t.cpp b/tests/unit/prefix-announcement.t.cpp
index 31449e4..03fa7f9 100644
--- a/tests/unit/prefix-announcement.t.cpp
+++ b/tests/unit/prefix-announcement.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-2020 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -92,60 +92,75 @@
   // wrong ContentType
   Data data0 = makePrefixAnnData();
   data0.setContentType(tlv::ContentType_Blob);
-  BOOST_CHECK_THROW(PrefixAnnouncement pa0(data0), tlv::Error);
+  BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data0}, tlv::Error, [] (const auto& e) {
+    return e.what() == "Data is not a prefix announcement: ContentType is 0"s;
+  });
 
   // Name has no "32=PA" keyword
   Data data1 = makePrefixAnnData();
   setNameComponent(data1, -3, name::Component::fromEscapedString("32=not-PA"));
-  BOOST_CHECK_THROW(PrefixAnnouncement pa1(data1), tlv::Error);
+  BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data1}, tlv::Error, [] (const auto& e) {
+    return e.what() == "Data is not a prefix announcement: wrong name structure"s;
+  });
 
   // Name has no version component
   Data data2 = makePrefixAnnData();
   setNameComponent(data2, -2, "not-version");
-  BOOST_CHECK_THROW(PrefixAnnouncement pa2(data2), tlv::Error);
+  BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data2}, tlv::Error, [] (const auto& e) {
+    return e.what() == "Data is not a prefix announcement: wrong name structure"s;
+  });
 
   // Name has no segment number component
   Data data3 = makePrefixAnnData();
   setNameComponent(data3, -2, "not-segment");
-  BOOST_CHECK_THROW(PrefixAnnouncement pa3(data3), tlv::Error);
+  BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data3}, tlv::Error, [] (const auto& e) {
+    return e.what() == "Data is not a prefix announcement: wrong name structure"s;
+  });
+
+  // Data without Content
+  Data data4 = makePrefixAnnData();
+  data4.unsetContent();
+  BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data4}, tlv::Error, [] (const auto& e) {
+    return e.what() == "Prefix announcement is empty"s;
+  });
 
   // Content has no ExpirationPeriod element
-  Data data4 = makePrefixAnnData();
-  Block payload4 = data4.getContent();
-  payload4.parse();
-  payload4.remove(tlv::nfd::ExpirationPeriod);
-  payload4.encode();
-  data4.setContent(payload4);
-  BOOST_CHECK_THROW(PrefixAnnouncement pa4(data4), tlv::Error);
-
-  // ExpirationPeriod is malformed
   Data data5 = makePrefixAnnData();
   Block payload5 = data5.getContent();
   payload5.parse();
   payload5.remove(tlv::nfd::ExpirationPeriod);
-  payload5.push_back("6D03010101"_block);
-  payload5.encode();
   data5.setContent(payload5);
-  BOOST_CHECK_THROW(PrefixAnnouncement pa5(data5), tlv::Error);
+  BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data5}, tlv::Error, [] (const auto& e) {
+    return e.what() == "No sub-element of type 109 found in block of type 21"s;
+  });
 
-  // ValidityPeriod is malformed
+  // ExpirationPeriod is malformed
   Data data6 = makePrefixAnnData();
   Block payload6 = data6.getContent();
   payload6.parse();
-  payload6.remove(tlv::ValidityPeriod);
-  payload6.push_back("FD00FD00"_block);
-  payload6.encode();
+  payload6.remove(tlv::nfd::ExpirationPeriod);
+  payload6.push_back("6D03010101"_block);
   data6.setContent(payload6);
-  BOOST_CHECK_THROW(PrefixAnnouncement pa6(data6), tlv::Error);
+  BOOST_CHECK_THROW(PrefixAnnouncement{data6}, tlv::Error);
 
-  // Content has unrecognized critical element
+  // ValidityPeriod is malformed
   Data data7 = makePrefixAnnData();
   Block payload7 = data7.getContent();
   payload7.parse();
-  payload7.push_back("0200"_block);
-  payload7.encode();
+  payload7.remove(tlv::ValidityPeriod);
+  payload7.push_back("FD00FD00"_block);
   data7.setContent(payload7);
-  BOOST_CHECK_THROW(PrefixAnnouncement pa7(data7), tlv::Error);
+  BOOST_CHECK_THROW(PrefixAnnouncement{data7}, tlv::Error);
+
+  // Content has unrecognized critical element
+  Data data8 = makePrefixAnnData();
+  Block payload8 = data8.getContent();
+  payload8.parse();
+  payload8.push_back("0200"_block);
+  data8.setContent(payload8);
+  BOOST_CHECK_EXCEPTION(PrefixAnnouncement{data8}, tlv::Error, [] (const auto& e) {
+    return e.what() == "Unrecognized element of critical type 2"s;
+  });
 }
 
 BOOST_FIXTURE_TEST_CASE(EncodeEmpty, IdentityManagementFixture)
diff --git a/tests/unit/security/key-chain.t.cpp b/tests/unit/security/key-chain.t.cpp
index ba2bd7c..b6dd0c6 100644
--- a/tests/unit/security/key-chain.t.cpp
+++ b/tests/unit/security/key-chain.t.cpp
@@ -291,6 +291,10 @@
   key3Cert2.setName(key3Cert2Name);
   m_keyChain.addCertificate(key3, key3Cert2);
   BOOST_CHECK_EQUAL(key3.getCertificates().size(), 2);
+  // Add empty cert
+  Certificate key3Cert3 = key3Cert1;
+  key3Cert3.unsetContent();
+  BOOST_CHECK_THROW(m_keyChain.addCertificate(key3, key3Cert3), std::invalid_argument);
 
   // Default certificate setting
   BOOST_CHECK_EQUAL(key3.getDefaultCertificate().getName(), key3CertName);