util: io::load accepts base64 encoding without newlines
This commit also renames io::BASE_64 to io::BASE64.
refs #3741
Change-Id: I46286c72d12e685902a72ce8dab4a1385dc0fceb
diff --git a/tests/unit-tests/security/dummy-keychain.cpp b/tests/unit-tests/security/dummy-keychain.cpp
index 947bebf..c173117 100644
--- a/tests/unit-tests/security/dummy-keychain.cpp
+++ b/tests/unit-tests/security/dummy-keychain.cpp
@@ -114,9 +114,8 @@
{
static shared_ptr<v1::PublicKey> publicKey = nullptr;
if (publicKey == nullptr) {
- typedef boost::iostreams::stream<boost::iostreams::array_source> arrayStream;
- arrayStream
- is(reinterpret_cast<const char*>(DUMMY_CERT), sizeof(DUMMY_CERT));
+ typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
+ ArrayStream is(reinterpret_cast<const char*>(DUMMY_CERT), sizeof(DUMMY_CERT));
auto cert = io::load<v1::IdentityCertificate>(is, io::NO_ENCODING);
publicKey = make_shared<v1::PublicKey>(cert->getPublicKeyInfo());
}
@@ -146,10 +145,9 @@
{
static shared_ptr<v1::IdentityCertificate> cert = nullptr;
if (cert == nullptr) {
- typedef boost::iostreams::stream<boost::iostreams::array_source> arrayStream;
- arrayStream
- is(reinterpret_cast<const char*>(DUMMY_CERT), sizeof(DUMMY_CERT));
- cert = io::load<v1::IdentityCertificate>(is, io::BASE_64);
+ typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream;
+ ArrayStream is(reinterpret_cast<const char*>(DUMMY_CERT), sizeof(DUMMY_CERT));
+ cert = io::load<v1::IdentityCertificate>(is);
}
return cert;
diff --git a/tests/unit-tests/security/transform.t.cpp b/tests/unit-tests/security/transform.t.cpp
index 5d82444..ab5d612 100644
--- a/tests/unit-tests/security/transform.t.cpp
+++ b/tests/unit-tests/security/transform.t.cpp
@@ -50,6 +50,9 @@
transform::HexEncode* hexEncode = nullptr;
BOOST_CHECK(hexEncode == nullptr);
+ transform::StripSpace* stripSpace = nullptr;
+ BOOST_CHECK(stripSpace == nullptr);
+
transform::HexDecode* hexDecode = nullptr;
BOOST_CHECK(hexDecode == nullptr);
diff --git a/tests/unit-tests/security/transform/strip-space.t.cpp b/tests/unit-tests/security/transform/strip-space.t.cpp
new file mode 100644
index 0000000..d916f8d
--- /dev/null
+++ b/tests/unit-tests/security/transform/strip-space.t.cpp
@@ -0,0 +1,78 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2016 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 "security/transform/strip-space.hpp"
+#include "security/transform/step-source.hpp"
+#include "security/transform/stream-sink.hpp"
+#include "encoding/buffer-stream.hpp"
+#include <iostream>
+
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace security {
+namespace transform {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(Security)
+BOOST_AUTO_TEST_SUITE(Transform)
+BOOST_AUTO_TEST_SUITE(TestStripSpace)
+
+BOOST_AUTO_TEST_CASE(Basic)
+{
+ const char* input = R"STR(
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
+ incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
+ exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
+ irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
+ pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
+ deserunt mollit anim id est laborum.
+ )STR";
+ size_t inputLen = strlen(input);
+
+ OBufferStream os;
+ StepSource source;
+ source >> stripSpace() >> streamSink(os);
+
+ for (size_t offset = 0; offset < inputLen; offset += 40) {
+ source.write(reinterpret_cast<const uint8_t*>(input + offset),
+ std::min<size_t>(40, inputLen - offset));
+ }
+ source.end();
+
+ std::string expected(
+ "Loremipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemporincididuntutl"
+ "aboreetdoloremagnaaliqua.Utenimadminimveniam,quisnostrudexercitationullamcolabor"
+ "isnisiutaliquipexeacommodoconsequat.Duisauteiruredolorinreprehenderitinvoluptate"
+ "velitessecillumdoloreeufugiatnullapariatur.Excepteursintoccaecatcupidatatnonproi"
+ "dent,suntinculpaquiofficiadeseruntmollitanimidestlaborum.");
+ ConstBufferPtr buf = os.buf();
+ BOOST_CHECK_EQUAL(std::string(reinterpret_cast<const char*>(buf->get()), buf->size()), expected);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestStripSpace
+BOOST_AUTO_TEST_SUITE_END() // Transform
+BOOST_AUTO_TEST_SUITE_END() // Security
+
+} // namespace tests
+} // namespace transform
+} // namespace security
+} // namespace ndn
diff --git a/tests/unit-tests/util/io.t.cpp b/tests/unit-tests/util/io.t.cpp
index b789020..fc423a1 100644
--- a/tests/unit-tests/util/io.t.cpp
+++ b/tests/unit-tests/util/io.t.cpp
@@ -162,7 +162,46 @@
BOOST_AUTO_TEST_CASE(LoadBase64)
{
this->writeFile<std::string>("uwHu\n"); // printf '\xBB\x01\xEE' | base64
- shared_ptr<DecodableType> decoded = io::load<DecodableType>(filename, io::BASE_64);
+ shared_ptr<DecodableType> decoded = io::load<DecodableType>(filename, io::BASE64);
+ BOOST_CHECK(decoded != nullptr);
+}
+
+BOOST_AUTO_TEST_CASE(LoadBase64Newline64)
+{
+ this->writeFile<std::string>(
+ "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
+ "AAAAAAAAAAAA\n");
+ // printf '\x08\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
+ // \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
+ // \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
+ // \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' | base64
+ shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64);
+ BOOST_CHECK(decoded != nullptr);
+}
+
+BOOST_AUTO_TEST_CASE(LoadBase64Newline32)
+{
+ this->writeFile<std::string>(
+ "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n"
+ "AAAAAAAAAAAA\n");
+ shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64);
+ BOOST_CHECK(decoded != nullptr);
+}
+
+BOOST_AUTO_TEST_CASE(LoadBase64NewlineEnd)
+{
+ this->writeFile<std::string>(
+ "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n");
+ shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64);
+ BOOST_CHECK(decoded != nullptr);
+}
+
+BOOST_AUTO_TEST_CASE(LoadBase64NoNewline)
+{
+ this->writeFile<std::string>(
+ "CEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
+ shared_ptr<name::Component> decoded = io::load<name::Component>(filename, io::BASE64);
BOOST_CHECK(decoded != nullptr);
}
@@ -209,7 +248,7 @@
BOOST_AUTO_TEST_CASE(SaveBase64)
{
EncodableType encoded;
- BOOST_CHECK_NO_THROW(io::save(encoded, filename, io::BASE_64));
+ BOOST_CHECK_NO_THROW(io::save(encoded, filename, io::BASE64));
auto content = this->readFile<std::string>();
BOOST_CHECK_EQUAL(content, "qgHd\n"); // printf '\xAA\x01\xDD' | base64
}
@@ -254,7 +293,7 @@
io::save(*idCert, filename);
shared_ptr<security::v1::IdentityCertificate> readCert = io::load<security::v1::IdentityCertificate>(filename);
- BOOST_CHECK(readCert != nullptr);
+ BOOST_REQUIRE(readCert != nullptr);
BOOST_CHECK_EQUAL(idCert->getName(), readCert->getName());
}