lp: wrap Data in PrefixAnnouncement field
Change-Id: I47e33518cec60a29d4518433245f22283b96b747
refs: 4280
diff --git a/src/lp/field-decl.hpp b/src/lp/field-decl.hpp
index 755f595..fa584af 100644
--- a/src/lp/field-decl.hpp
+++ b/src/lp/field-decl.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -90,7 +90,7 @@
struct EncodeHelper
{
static
- BOOST_CONCEPT_REQUIRES(((WireEncodable<T>)), (size_t))
+ BOOST_CONCEPT_REQUIRES(((WireEncodableWithEncodingBuffer<T>)), (size_t))
encode(EncodingImpl<TAG>& encoder, const T& value)
{
return value.wireEncode(encoder);
diff --git a/src/lp/prefix-announcement.cpp b/src/lp/prefix-announcement.cpp
new file mode 100644
index 0000000..5524aeb
--- /dev/null
+++ b/src/lp/prefix-announcement.cpp
@@ -0,0 +1,108 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2018 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ *
+ * @author Teng Liang <philoliang@email.arizona.edu>
+ */
+
+#include "prefix-announcement.hpp"
+#include "tlv.hpp"
+
+namespace ndn {
+namespace lp {
+
+static const name::Component SELF_LEARNING_PREFIX("self-learning");
+
+PrefixAnnouncement::PrefixAnnouncement() = default;
+
+PrefixAnnouncement::PrefixAnnouncement(const Block& block)
+{
+ wireDecode(block);
+}
+
+PrefixAnnouncement::PrefixAnnouncement(shared_ptr<const Data> data)
+{
+ setData(std::move(data));
+}
+
+template<encoding::Tag TAG>
+size_t
+PrefixAnnouncement::wireEncode(EncodingImpl<TAG>& encoder) const
+{
+ size_t length = 0;
+ length += m_data->wireEncode(encoder);
+ length += encoder.prependVarNumber(length);
+ length += encoder.prependVarNumber(tlv::PrefixAnnouncement);
+ return length;
+}
+
+NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(PrefixAnnouncement);
+
+void
+PrefixAnnouncement::wireDecode(const Block& wire)
+{
+ if (wire.type() != tlv::PrefixAnnouncement) {
+ BOOST_THROW_EXCEPTION(Error("Unexpected TLV-TYPE " + to_string(wire.type())));
+ }
+
+ wire.parse();
+ setData(make_shared<Data>(wire.get(ndn::tlv::Data)));
+}
+
+Name
+PrefixAnnouncement::getAnnouncedName() const
+{
+ if (m_data == nullptr) {
+ BOOST_THROW_EXCEPTION(Error("Data is unset in PrefixAnnouncement"));
+ }
+
+ const Name& dataName = m_data->getName();
+ BOOST_ASSERT(dataName.at(0) == SELF_LEARNING_PREFIX);
+ BOOST_ASSERT(dataName.at(-1).isVersion());
+
+ return dataName.getSubName(1, dataName.size() - 2);
+}
+
+PrefixAnnouncement&
+PrefixAnnouncement::setData(shared_ptr<const Data> data)
+{
+ if (data == nullptr) {
+ BOOST_THROW_EXCEPTION(Error("Unexpected nullptr"));
+ }
+
+ if (data->getName().at(0) != SELF_LEARNING_PREFIX) {
+ BOOST_THROW_EXCEPTION(Error("Unexpected prefix in name " + data->getName().toUri()));
+ }
+
+ if (!data->getName().get(-1).isVersion()) {
+ BOOST_THROW_EXCEPTION(Error("Last name component of " + data->getName().toUri() +
+ " is not a version"));
+ }
+
+ if (data->getContent().value_size() != 0 ||
+ data->getMetaInfo().wireEncode().value_size() != 0) {
+ BOOST_THROW_EXCEPTION(Error("Both Data MetaInfo and Content must be empty"));
+ }
+
+ m_data = std::move(data);
+ return *this;
+}
+
+} // namespace lp
+} // namespace ndn
diff --git a/src/lp/prefix-announcement.hpp b/src/lp/prefix-announcement.hpp
index 208b0a6..135cc21 100644
--- a/src/lp/prefix-announcement.hpp
+++ b/src/lp/prefix-announcement.hpp
@@ -25,14 +25,66 @@
#define NDN_CXX_LP_PREFIX_ANNOUNCEMENT_HPP
#include "../data.hpp"
+#include "../name.hpp"
namespace ndn {
namespace lp {
-/**
- * \brief represents a Prefix Announcement field
+/** \brief represents a Prefix Announcement
+ *
+ * This type wraps a Data, and is intended for self-learning. The Name of Data
+ * starts with /self-learning prefix, ends with a version component, and the
+ * components in the middle refer to the announced name prefix. The MetaInfo
+ * and Content of the Data must be empty.
*/
-using PrefixAnnouncement = Data;
+class PrefixAnnouncement
+{
+public:
+ class Error : public ndn::tlv::Error
+ {
+ public:
+ explicit
+ Error(const std::string& what)
+ : ndn::tlv::Error(what)
+ {
+ }
+ };
+
+ PrefixAnnouncement();
+
+ explicit
+ PrefixAnnouncement(const Block& block);
+
+ explicit
+ PrefixAnnouncement(shared_ptr<const Data> data);
+
+ template<encoding::Tag TAG>
+ size_t
+ wireEncode(EncodingImpl<TAG>& encoder) const;
+
+ void
+ wireDecode(const Block& wire);
+
+ /** \brief Get announced name.
+ * \throw Error PrefixAnnouncement is empty
+ */
+ Name
+ getAnnouncedName() const;
+
+ shared_ptr<const Data>
+ getData() const
+ {
+ return m_data;
+ }
+
+ PrefixAnnouncement&
+ setData(shared_ptr<const Data> data);
+
+private:
+ shared_ptr<const Data> m_data;
+};
+
+NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(PrefixAnnouncement);
} // namespace lp
} // namespace ndn
diff --git a/tests/unit-tests/lp/packet.t.cpp b/tests/unit-tests/lp/packet.t.cpp
index bd6b671..5dafc20 100644
--- a/tests/unit-tests/lp/packet.t.cpp
+++ b/tests/unit-tests/lp/packet.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2013-2017 Regents of the University of California.
+ * Copyright (c) 2013-2018 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -20,6 +20,7 @@
*/
#include "lp/packet.hpp"
+#include "security/signature-sha256-with-rsa.hpp"
#include "boost-test.hpp"
@@ -402,6 +403,54 @@
encoded.begin(), encoded.end());
}
+BOOST_AUTO_TEST_CASE(DecodePrefixAnnouncement)
+{
+ static const uint8_t inputBlock[] = {
+ 0x64, 0x70, // LpPacket
+ 0xfd, 0x03, 0x50, 0x3a, // PrefixAnnouncement
+ 0x06, 0x38, // Data
+ 0x07, 0x29, 0x08, 0x0d, 0x73, 0x65, 0x6c, 0x66, 0x2d, 0x6c,
+ 0x65, 0x61, 0x72, 0x6e, 0x69, 0x6e, 0x67, 0x08, 0x03, 0x65,
+ 0x64, 0x75, 0x08, 0x02, 0x75, 0x61, 0x08, 0x02, 0x63, 0x73,
+ 0x08, 0x04, 0x6e, 0x65, 0x77, 0x73, 0x08, 0x05, 0xfd, 0x00,
+ 0x03, 0xa5, 0xfe, 0x14, 0x00, 0x15, 0x00, 0x16, 0x05, 0x1b,
+ 0x01, 0x01, 0x1c, 0x00, 0x17, 0x00,
+ 0x50, 0x30, // Fragment
+ 0x06, 0x2e, // Data
+ 0x07, 0x1f, 0x08, 0x03, 0x65, 0x64, 0x75, 0x08, 0x02, 0x75,
+ 0x61, 0x08, 0x02, 0x63, 0x73, 0x08, 0x04, 0x6e, 0x65, 0x77,
+ 0x73, 0x08, 0x0a, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x2e, 0x68,
+ 0x74, 0x6d, 0x6c, 0x14, 0x00, 0x15, 0x00, 0x16, 0x05, 0x1b,
+ 0x01, 0x01, 0x1c, 0x00, 0x17, 0x00,
+ };
+
+ Data data1("/edu/ua/cs/news/index.html");
+ ndn::SignatureSha256WithRsa fakeSignature;
+ fakeSignature.setValue(ndn::encoding::makeEmptyBlock(ndn::tlv::SignatureValue));
+ data1.setSignature(fakeSignature);
+
+ Block wire;
+ wire = data1.wireEncode();
+ Packet packet;
+ BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
+
+ Name name("/self-learning/edu/ua/cs/news");
+ name.appendVersion(239102);
+ Data data2(name);
+ fakeSignature.setValue(ndn::encoding::makeEmptyBlock(ndn::tlv::SignatureValue));
+ data2.setSignature(fakeSignature);
+ data2.wireEncode();
+
+ PrefixAnnouncement pa;
+ pa.setData(make_shared<Data>(data2));
+
+ BOOST_CHECK_NO_THROW(packet.add<PrefixAnnouncementField>(pa));
+ Block encoded;
+ BOOST_CHECK_NO_THROW(encoded = packet.wireEncode());
+ BOOST_CHECK_EQUAL_COLLECTIONS(inputBlock, inputBlock + sizeof(inputBlock),
+ encoded.begin(), encoded.end());
+}
+
BOOST_AUTO_TEST_CASE(DecodeUnrecognizedTlvType)
{
Packet packet;
diff --git a/tests/unit-tests/lp/prefix-announcement.t.cpp b/tests/unit-tests/lp/prefix-announcement.t.cpp
new file mode 100644
index 0000000..5fdde2c
--- /dev/null
+++ b/tests/unit-tests/lp/prefix-announcement.t.cpp
@@ -0,0 +1,80 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2018 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ *
+ * @author Teng Liang <philoliang@email.arizona.edu>
+ */
+
+#include "lp/prefix-announcement.hpp"
+#include "security/signature-sha256-with-rsa.hpp"
+
+#include "boost-test.hpp"
+#include "make-interest-data.hpp"
+
+namespace ndn {
+namespace lp {
+namespace tests {
+
+using namespace ndn::tests;
+
+BOOST_AUTO_TEST_SUITE(Lp)
+BOOST_AUTO_TEST_SUITE(TestPrefixAnnouncement)
+
+BOOST_AUTO_TEST_CASE(SetData)
+{
+ Name name1("/ndn/pa");
+ Name name2("/self-learning/ndn/pa");
+ Name name3("/self-learning/ndn/pa");
+ name3.appendVersion();
+
+ PrefixAnnouncement pa;
+ BOOST_CHECK_THROW(pa.setData(nullptr), PrefixAnnouncement::Error);
+ BOOST_CHECK_THROW(pa.setData(makeData(name1)), PrefixAnnouncement::Error);
+ BOOST_CHECK_THROW(pa.setData(makeData(name2)), PrefixAnnouncement::Error);
+ BOOST_CHECK_NO_THROW(pa.setData(makeData(name3)));
+ BOOST_CHECK_EQUAL(pa.getAnnouncedName(), "/ndn/pa");
+
+ shared_ptr<Data> data1 = makeData(name3);
+ shared_ptr<Data> data2 = makeData(name3);
+ static const uint8_t someData[] = "someData";
+ data1->setContent(someData, sizeof(someData));
+ data2->setFreshnessPeriod(10_s);
+ BOOST_CHECK_THROW(pa.setData(data1), PrefixAnnouncement::Error);
+ BOOST_CHECK_THROW(pa.setData(data2), PrefixAnnouncement::Error);
+}
+
+BOOST_AUTO_TEST_CASE(GetAnnouncedName)
+{
+ PrefixAnnouncement pa1;
+ BOOST_CHECK_THROW(pa1.getAnnouncedName(), PrefixAnnouncement::Error);
+
+ Name name("/self-learning/edu/ua/news");
+ name.appendVersion();
+
+ PrefixAnnouncement pa2(makeData(name));
+
+ BOOST_CHECK_EQUAL(pa2.getAnnouncedName(), "/edu/ua/news");
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestPrefixAnnouncement
+BOOST_AUTO_TEST_SUITE_END() // Lp
+
+} // namespace tests
+} // namespace lp
+} // namespace ndn