encoding: Improving structure and documentation of block helpers
This commit add several new helpers to simplify operations with
std::string:
- prependStringBlock
- makeStringBlock
- readString
The following functions are deprecated and their replacements:
- nonNegativeIntegerBlock (use makeNonNegativeIntegerBlock)
- prependBooleanBlock (use prependEmptyBlock)
- booleanBlock (use makeEmptyBlock)
- dataBlock (use makeBinaryBlock)
- nestedBlock (use makeNestedBlock)
Change-Id: Ic595ae64fc9c80c2c04e5fde1d8812e8b9debd60
Refs: #2951
diff --git a/tests/unit-tests/data.t.cpp b/tests/unit-tests/data.t.cpp
index 262f2ac..ab9cee6 100644
--- a/tests/unit-tests/data.t.cpp
+++ b/tests/unit-tests/data.t.cpp
@@ -194,7 +194,7 @@
b = SignatureSha256WithRsa();
static const uint8_t someData[256] = {};
- Block signatureValue = dataBlock(tlv::SignatureValue, someData, sizeof(someData));
+ Block signatureValue = makeBinaryBlock(tlv::SignatureValue, someData, sizeof(someData));
b.setValue(signatureValue);
BOOST_CHECK_EQUAL(a == b, false);
BOOST_CHECK_EQUAL(a != b, true);
@@ -382,8 +382,7 @@
Block signatureInfo(tlv::SignatureInfo);
// SignatureType
{
- signatureInfo.push_back
- (nonNegativeIntegerBlock(tlv::SignatureType, Signature::Sha256WithRsa));
+ signatureInfo.push_back(makeNonNegativeIntegerBlock(tlv::SignatureType, Signature::Sha256WithRsa));
}
// KeyLocator
{
diff --git a/tests/unit-tests/encoding/block-helpers.t.cpp b/tests/unit-tests/encoding/block-helpers.t.cpp
new file mode 100644
index 0000000..01f0ddb
--- /dev/null
+++ b/tests/unit-tests/encoding/block-helpers.t.cpp
@@ -0,0 +1,93 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 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.
+ */
+
+#include "encoding/block-helpers.hpp"
+
+#include "boost-test.hpp"
+#include "name.hpp"
+
+namespace ndn {
+namespace encoding {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(EncodingBlockHelpers)
+
+BOOST_AUTO_TEST_CASE(NonNegativeInteger)
+{
+ Block b = makeNonNegativeIntegerBlock(100, 1000);
+ BOOST_CHECK_EQUAL(b.type(), 100);
+ BOOST_CHECK_GT(b.value_size(), 0);
+ BOOST_CHECK_EQUAL(readNonNegativeInteger(b), 1000);
+
+ BOOST_CHECK_THROW(readNonNegativeInteger(Block()), tlv::Error);
+}
+
+BOOST_AUTO_TEST_CASE(Empty)
+{
+ Block b = makeEmptyBlock(200);
+ BOOST_CHECK_EQUAL(b.type(), 200);
+ BOOST_CHECK_EQUAL(b.value_size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(String)
+{
+ Block b = makeStringBlock(100, "Hello, world!");
+ BOOST_CHECK_EQUAL(b.type(), 100);
+ BOOST_CHECK_GT(b.value_size(), 0);
+ BOOST_CHECK_EQUAL(readString(b), "Hello, world!");
+}
+
+BOOST_AUTO_TEST_CASE(Data)
+{
+ std::string buf1{1, 1, 1, 1};
+ const uint8_t buf2[]{1, 1, 1, 1};
+ std::list<uint8_t> buf3{1, 1, 1, 1};
+
+ Block b1 = makeBinaryBlock(100, buf1.data(), buf1.size());
+ Block b2 = makeBinaryBlock(100, buf2, sizeof(buf2));
+ Block b3 = makeBinaryBlock(100, buf1.begin(), buf1.end()); // fast encoding (random access iterator)
+ Block b4 = makeBinaryBlock(100, buf3.begin(), buf3.end()); // slow encoding (general iterator)
+
+ BOOST_CHECK(b1 == b2);
+ BOOST_CHECK(b1 == b3);
+ BOOST_CHECK_EQUAL(b1.type(), 100);
+ BOOST_CHECK_EQUAL(b1.value_size(), buf1.size());
+ BOOST_CHECK_EQUAL_COLLECTIONS(b1.value_begin(), b1.value_end(),
+ buf2, buf2 + sizeof(buf2));
+}
+
+BOOST_AUTO_TEST_CASE(Nested)
+{
+ Name name("ndn:/Hello/World!");
+ Block b1 = makeNestedBlock(100, name);
+
+ BOOST_CHECK_EQUAL(b1.type(), 100);
+ b1.parse();
+ BOOST_CHECK_EQUAL(b1.elements().size(), 1);
+ BOOST_CHECK_EQUAL(b1.elements().begin()->type(), name.wireEncode().type());
+ BOOST_CHECK(*b1.elements().begin() == name.wireEncode());
+}
+
+BOOST_AUTO_TEST_SUITE_END() // EncodingBlockHelpers
+
+} // namespace tests
+} // namespace encoding
+} // namespace ndn
diff --git a/tests/unit-tests/key-locator.t.cpp b/tests/unit-tests/key-locator.t.cpp
index 671a362..94eb5a2 100644
--- a/tests/unit-tests/key-locator.t.cpp
+++ b/tests/unit-tests/key-locator.t.cpp
@@ -92,7 +92,7 @@
{
char digestOctets[] = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD";
ConstBufferPtr digestBuffer = make_shared<Buffer>(digestOctets, 8);
- Block expectedDigestBlock = dataBlock(tlv::KeyDigest, digestOctets, 8);
+ Block expectedDigestBlock = makeBinaryBlock(tlv::KeyDigest, digestOctets, 8);
KeyLocator a;
a.setKeyDigest(digestBuffer);
diff --git a/tests/unit-tests/make-interest-data.hpp b/tests/unit-tests/make-interest-data.hpp
index 23fcc22..ca3bff5 100644
--- a/tests/unit-tests/make-interest-data.hpp
+++ b/tests/unit-tests/make-interest-data.hpp
@@ -44,8 +44,7 @@
signData(const shared_ptr<Data>& data)
{
ndn::SignatureSha256WithRsa fakeSignature;
- fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
- reinterpret_cast<const uint8_t*>(0), 0));
+ fakeSignature.setValue(makeEmptyBlock(tlv::SignatureValue));
data->setSignature(fakeSignature);
data->wireEncode();
diff --git a/tests/unit-tests/meta-info.t.cpp b/tests/unit-tests/meta-info.t.cpp
index 6c158a2..77bdd3e 100644
--- a/tests/unit-tests/meta-info.t.cpp
+++ b/tests/unit-tests/meta-info.t.cpp
@@ -122,18 +122,18 @@
for (int i = 0; i < 5; i++) {
uint32_t type = 128 + i * 10;
- info1.addAppMetaInfo(nonNegativeIntegerBlock(type, ints[i]));
+ info1.addAppMetaInfo(makeNonNegativeIntegerBlock(type, ints[i]));
const std::string& s = ss[i];
type += 5;
- info1.addAppMetaInfo(dataBlock(type, s.c_str(), s.size()));
+ info1.addAppMetaInfo(makeStringBlock(type, s));
}
BOOST_CHECK(info1.findAppMetaInfo(252) == 0);
- info1.addAppMetaInfo(nonNegativeIntegerBlock(252, 1000));
+ info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000));
BOOST_CHECK(info1.findAppMetaInfo(252) != 0);
- info1.addAppMetaInfo(nonNegativeIntegerBlock(252, 1000));
+ info1.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000));
BOOST_CHECK(info1.findAppMetaInfo(252) != 0);
info1.removeAppMetaInfo(252);
@@ -187,11 +187,11 @@
{
MetaInfo info;
- BOOST_CHECK_NO_THROW(info.addAppMetaInfo(nonNegativeIntegerBlock(128, 1000)));
- BOOST_CHECK_NO_THROW(info.addAppMetaInfo(nonNegativeIntegerBlock(252, 1000)));
+ BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(128, 1000)));
+ BOOST_CHECK_NO_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(252, 1000)));
- BOOST_CHECK_THROW(info.addAppMetaInfo(nonNegativeIntegerBlock(127, 1000)), MetaInfo::Error);
- BOOST_CHECK_THROW(info.addAppMetaInfo(nonNegativeIntegerBlock(253, 1000)), MetaInfo::Error);
+ BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(127, 1000)), MetaInfo::Error);
+ BOOST_CHECK_THROW(info.addAppMetaInfo(makeNonNegativeIntegerBlock(253, 1000)), MetaInfo::Error);
}
BOOST_AUTO_TEST_SUITE_END()
diff --git a/tests/unit-tests/util/test-in-memory-storage-common.cpp b/tests/unit-tests/util/test-in-memory-storage-common.cpp
index 5fe9805..f2c1b9f 100644
--- a/tests/unit-tests/util/test-in-memory-storage-common.cpp
+++ b/tests/unit-tests/util/test-in-memory-storage-common.cpp
@@ -510,8 +510,7 @@
const ndn::KeyLocator locator(keyName);
ndn::SignatureSha256WithRsa fakeSignature;
- fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue,
- reinterpret_cast<const uint8_t*>(0), 0));
+ fakeSignature.setValue(makeEmptyBlock(tlv::SignatureValue));
fakeSignature.setKeyLocator(locator);
data2->setSignature(fakeSignature);