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/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);