tests: use BOOST_TEST_CONTEXT and BOOST_DATA_TEST_CASE where possible

Fix the last two "CanBePrefix unset" warnings in unit tests

Change-Id: Ifd2420ae98e7c4741a1c20e7fa0d52f74d7178c8
diff --git a/ndn-cxx/encoding/buffer.cpp b/ndn-cxx/encoding/buffer.cpp
new file mode 100644
index 0000000..1084b22
--- /dev/null
+++ b/ndn-cxx/encoding/buffer.cpp
@@ -0,0 +1,34 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2020 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 "ndn-cxx/encoding/buffer.hpp"
+#include "ndn-cxx/util/string-helper.hpp"
+
+namespace ndn {
+
+std::ostream&
+boost_test_print_type(std::ostream& os, const Buffer& buf)
+{
+  printHex(os, buf, false);
+  return os;
+}
+
+} // namespace ndn
diff --git a/ndn-cxx/encoding/buffer.hpp b/ndn-cxx/encoding/buffer.hpp
index 8f4d3db..748bba7 100644
--- a/ndn-cxx/encoding/buffer.hpp
+++ b/ndn-cxx/encoding/buffer.hpp
@@ -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).
  *
@@ -26,6 +26,7 @@
 
 #include "ndn-cxx/detail/common.hpp"
 
+#include <initializer_list>
 #include <vector>
 
 namespace ndn {
@@ -91,6 +92,13 @@
   {
   }
 
+  /** @brief Creates a Buffer with the contents of an initializer list
+   */
+  Buffer(std::initializer_list<uint8_t> il)
+    : std::vector<uint8_t>(il)
+  {
+  }
+
   /** @return pointer to the first byte of the buffer, cast to the requested type T
    */
   template<class T>
@@ -122,6 +130,11 @@
 inline Buffer&
 Buffer::operator=(Buffer&&) noexcept = default;
 
+/** \cond */
+std::ostream&
+boost_test_print_type(std::ostream&, const Buffer&);
+/** \endcond */
+
 using BufferPtr = shared_ptr<Buffer>;
 using ConstBufferPtr = shared_ptr<const Buffer>;
 
diff --git a/tests/unit/encoding/block.t.cpp b/tests/unit/encoding/block.t.cpp
index fea4714..52ad9c1 100644
--- a/tests/unit/encoding/block.t.cpp
+++ b/tests/unit/encoding/block.t.cpp
@@ -25,7 +25,7 @@
 #include "tests/boost-test.hpp"
 
 #include <boost/lexical_cast.hpp>
-#include <boost/mpl/vector.hpp>
+#include <boost/test/data/test_case.hpp>
 
 #include <cstring>
 #include <sstream>
@@ -358,49 +358,26 @@
   BOOST_CHECK_EQUAL(*b.wire(),  0xfe);
 }
 
-template<typename T>
-struct MalformedInput
-{
-  static const std::vector<uint8_t> INPUT;
+static const Buffer MalformedInputs[] = {
+  {0x00, 0x00}, // invalid TLV type (zero)
+  {0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}, // TLV type too large
+  {0x01, 0xff, 0x42, 0x42}, // bad TLV length
+  {0x01, 0x02, 0x03}, // truncated TLV value
 };
 
-template<>
-const std::vector<uint8_t> MalformedInput<struct TlvTypeZero>::INPUT{
-  0x00, 0x00
-};
-template<>
-const std::vector<uint8_t> MalformedInput<struct TlvTypeTooLarge>::INPUT{
-  0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
-};
-template<>
-const std::vector<uint8_t> MalformedInput<struct BadTlvLength>::INPUT{
-  0x01, 0xff, 0x42, 0x42
-};
-template<>
-const std::vector<uint8_t> MalformedInput<struct TruncatedTlvValue>::INPUT{
-  0x01, 0x02, 0x03
-};
-
-using MalformedInputs = boost::mpl::vector<
-  MalformedInput<TlvTypeZero>,
-  MalformedInput<TlvTypeTooLarge>,
-  MalformedInput<BadTlvLength>,
-  MalformedInput<TruncatedTlvValue>
->;
-
-BOOST_AUTO_TEST_CASE_TEMPLATE(Malformed, T, MalformedInputs)
+BOOST_DATA_TEST_CASE(Malformed, MalformedInputs)
 {
   // constructor from raw buffer
-  BOOST_CHECK_THROW(Block(T::INPUT.data(), T::INPUT.size()), tlv::Error);
+  BOOST_CHECK_THROW(Block(sample.data(), sample.size()), tlv::Error);
 
   // fromStream()
   std::stringstream stream;
-  stream.write(reinterpret_cast<const char*>(T::INPUT.data()), T::INPUT.size());
+  stream.write(sample.template get<char>(), sample.size());
   stream.seekg(0);
   BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
 
   // fromBuffer(), ConstBufferPtr overload
-  auto buf = make_shared<Buffer>(T::INPUT.begin(), T::INPUT.end());
+  auto buf = make_shared<Buffer>(sample.begin(), sample.end());
   bool isOk;
   Block b;
   std::tie(isOk, b) = Block::fromBuffer(buf, 0);
@@ -408,7 +385,7 @@
   BOOST_CHECK(!b.isValid());
 
   // fromBuffer(), raw buffer overload
-  std::tie(isOk, b) = Block::fromBuffer(T::INPUT.data(), T::INPUT.size());
+  std::tie(isOk, b) = Block::fromBuffer(sample.data(), sample.size());
   BOOST_CHECK(!isOk);
   BOOST_CHECK(!b.isValid());
 }
diff --git a/tests/unit/encoding/nfd-constants.t.cpp b/tests/unit/encoding/nfd-constants.t.cpp
index e87f391..e1bc111 100644
--- a/tests/unit/encoding/nfd-constants.t.cpp
+++ b/tests/unit/encoding/nfd-constants.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).
  *
@@ -24,6 +24,7 @@
 #include "tests/boost-test.hpp"
 
 #include <boost/lexical_cast.hpp>
+#include <iomanip>
 #include <sstream>
 
 namespace ndn {
@@ -72,22 +73,25 @@
 BOOST_AUTO_TEST_CASE(ParseRouteOrigin)
 {
   auto expectSuccess = [] (const std::string& input, RouteOrigin expected) {
-    std::istringstream is(input);
-    RouteOrigin routeOrigin;
-    is >> routeOrigin;
+    BOOST_TEST_CONTEXT("input = " << std::quoted(input)) {
+      std::istringstream is(input);
+      RouteOrigin routeOrigin;
+      is >> routeOrigin;
 
-    BOOST_TEST_MESSAGE("parsing " << input);
-    BOOST_CHECK_EQUAL(routeOrigin, expected);
+      BOOST_CHECK(!is.fail());
+      BOOST_CHECK_EQUAL(routeOrigin, expected);
+    }
   };
 
   auto expectFail = [] (const std::string& input) {
-    std::istringstream is(input);
-    RouteOrigin routeOrigin;
-    is >> routeOrigin;
+    BOOST_TEST_CONTEXT("input = " << std::quoted(input)) {
+      std::istringstream is(input);
+      RouteOrigin routeOrigin;
+      is >> routeOrigin;
 
-    BOOST_TEST_MESSAGE("parsing " << input);
-    BOOST_CHECK(is.fail());
-    BOOST_CHECK_EQUAL(routeOrigin, ROUTE_ORIGIN_NONE);
+      BOOST_CHECK(is.fail());
+      BOOST_CHECK_EQUAL(routeOrigin, ROUTE_ORIGIN_NONE);
+    }
   };
 
   expectSuccess("none", ROUTE_ORIGIN_NONE);
diff --git a/tests/unit/interest.t.cpp b/tests/unit/interest.t.cpp
index e5c7d32..33c4eb2 100644
--- a/tests/unit/interest.t.cpp
+++ b/tests/unit/interest.t.cpp
@@ -1069,7 +1069,6 @@
   Interest i1;
   i1.setCanBePrefix(false);
   BOOST_CHECK_EXCEPTION(i1.extractSignedRanges(), tlv::Error, [] (const auto& e) {
-    BOOST_TEST_MESSAGE(e.what());
     return e.what() == "Name has zero name components"s;
   });
   i1.setName("/test/prefix");
diff --git a/tests/unit/name-component.t.cpp b/tests/unit/name-component.t.cpp
index 10663bf..87a3652 100644
--- a/tests/unit/name-component.t.cpp
+++ b/tests/unit/name-component.t.cpp
@@ -359,12 +359,12 @@
   ConventionTest<uint64_t>
   operator()() const
   {
-    return {bind(&Component::fromNumberWithMarker, 0xAA, _1),
-            bind(&Component::toNumberWithMarker, _1, 0xAA),
-            bind(&Name::appendNumberWithMarker, _1, 0xAA, _2),
+    return {[] (auto num) { return Component::fromNumberWithMarker(0xAA, num); },
+            [] (const Component& c) { return c.toNumberWithMarker(0xAA); },
+            [] (Name& name, auto num) -> Name& { return name.appendNumberWithMarker(0xAA, num); },
             Name("/%AA%03%E8"),
             1000,
-            bind(&Component::isNumberWithMarker, _1, 0xAA)};
+            [] (const Component& c) { return c.isNumberWithMarker(0xAA); }};
   }
 };
 
@@ -377,11 +377,11 @@
   operator()() const
   {
     return {&Component::fromSegment,
-            bind(&Component::toSegment, _1),
-            bind(&Name::appendSegment, _1, _2),
+            &Component::toSegment,
+            &Name::appendSegment,
             Name("/%00%27%10"),
             10000,
-            bind(&Component::isSegment, _1)};
+            &Component::isSegment};
   }
 };
 
@@ -394,11 +394,11 @@
   operator()() const
   {
     return {&Component::fromSegment,
-            bind(&Component::toSegment, _1),
-            bind(&Name::appendSegment, _1, _2),
+            &Component::toSegment,
+            &Name::appendSegment,
             Name("/33=%27%10"),
             10000,
-            bind(&Component::isSegment, _1)};
+            &Component::isSegment};
   }
 };
 
@@ -411,11 +411,11 @@
   operator()() const
   {
     return {&Component::fromByteOffset,
-            bind(&Component::toByteOffset, _1),
-            bind(&Name::appendByteOffset, _1, _2),
+            &Component::toByteOffset,
+            &Name::appendByteOffset,
             Name("/34=%00%01%86%A0"),
             100000,
-            bind(&Component::isByteOffset, _1)};
+            &Component::isByteOffset};
   }
 };
 
@@ -428,11 +428,11 @@
   operator()() const
   {
     return {&Component::fromVersion,
-            bind(&Component::toVersion, _1),
-            [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); },
+            &Component::toVersion,
+            [] (Name& name, auto version) -> Name& { return name.appendVersion(version); },
             Name("/%FD%00%0FB%40"),
             1000000,
-            bind(&Component::isVersion, _1)};
+            &Component::isVersion};
   }
 };
 
@@ -445,11 +445,11 @@
   operator()() const
   {
     return {&Component::fromVersion,
-            bind(&Component::toVersion, _1),
-            [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); },
+            &Component::toVersion,
+            [] (Name& name, auto version) -> Name& { return name.appendVersion(version); },
             Name("/35=%00%0FB%40"),
             1000000,
-            bind(&Component::isVersion, _1)};
+            &Component::isVersion};
   }
 };
 
@@ -462,11 +462,11 @@
   operator()() const
   {
     return {&Component::fromTimestamp,
-            bind(&Component::toTimestamp, _1),
-            [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); },
+            &Component::toTimestamp,
+            [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); },
             Name("/%FC%00%04%7BE%E3%1B%00%00"),
             time::getUnixEpoch() + 14600_days, // 40 years
-            bind(&Component::isTimestamp, _1)};
+            &Component::isTimestamp};
   }
 };
 
@@ -479,11 +479,11 @@
   operator()() const
   {
     return {&Component::fromTimestamp,
-            bind(&Component::toTimestamp, _1),
-            [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); },
+            &Component::toTimestamp,
+            [] (Name& name, auto tp) -> Name& { return name.appendTimestamp(tp); },
             Name("/36=%00%04%7BE%E3%1B%00%00"),
             time::getUnixEpoch() + 14600_days, // 40 years
-            bind(&Component::isTimestamp, _1)};
+            &Component::isTimestamp};
   }
 };
 
@@ -496,11 +496,11 @@
   operator()() const
   {
     return {&Component::fromSequenceNumber,
-            bind(&Component::toSequenceNumber, _1),
-            bind(&Name::appendSequenceNumber, _1, _2),
+            &Component::toSequenceNumber,
+            &Name::appendSequenceNumber,
             Name("/%FE%00%98%96%80"),
             10000000,
-            bind(&Component::isSequenceNumber, _1)};
+            &Component::isSequenceNumber};
   }
 };
 
@@ -513,11 +513,11 @@
   operator()() const
   {
     return {&Component::fromSequenceNumber,
-            bind(&Component::toSequenceNumber, _1),
-            bind(&Name::appendSequenceNumber, _1, _2),
+            &Component::toSequenceNumber,
+            &Name::appendSequenceNumber,
             Name("/37=%00%98%96%80"),
             10000000,
-            bind(&Component::isSequenceNumber, _1)};
+            &Component::isSequenceNumber};
   }
 };
 
@@ -540,9 +540,7 @@
   Component invalidComponent2("1234567890");
 
   auto test = T()();
-
   const Name& expected = test.expected;
-  BOOST_TEST_MESSAGE("Check " << expected[0]);
 
   Component actualComponent = test.makeComponent(test.value);
   BOOST_CHECK_EQUAL(actualComponent, expected[0]);
diff --git a/tests/unit/security/certificate-fetcher-offline.t.cpp b/tests/unit/security/certificate-fetcher-offline.t.cpp
index ac1e9ee..f674993 100644
--- a/tests/unit/security/certificate-fetcher-offline.t.cpp
+++ b/tests/unit/security/certificate-fetcher-offline.t.cpp
@@ -21,7 +21,6 @@
 
 #include "ndn-cxx/security/certificate-fetcher-offline.hpp"
 #include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp"
-#include "ndn-cxx/util/scope.hpp"
 
 #include "tests/boost-test.hpp"
 #include "tests/unit/security/validator-fixture.hpp"
@@ -31,8 +30,6 @@
 inline namespace v2 {
 namespace tests {
 
-using namespace ndn::tests;
-
 BOOST_AUTO_TEST_SUITE(Security)
 
 class CertificateFetcherOfflineWrapper : public CertificateFetcherOffline
@@ -48,23 +45,18 @@
 
 BOOST_FIXTURE_TEST_SUITE(TestCertificateFetcherOffline, CertificateFetcherOfflineFixture)
 
-using Packets = boost::mpl::vector<Interest, Data>;
+using Packets = boost::mpl::vector<InterestV03Pkt, DataPkt>;
 
 BOOST_AUTO_TEST_CASE_TEMPLATE(Validate, Packet, Packets)
 {
-  // Can't set CanBePrefix on Interests in this test case because of template
-  // TODO: Remove in #4582
-  auto guard = make_scope_exit([] { Interest::s_errorIfCanBePrefixUnset = true; });
-  Interest::s_errorIfCanBePrefixUnset = false;
+  const Name name = "/Security/ValidatorFixture/Sub1/Packet";
 
-  Packet unsignedPacket("/Security/ValidatorFixture/Sub1/Packet");
-
-  Packet packet = unsignedPacket;
+  auto packet = Packet::makePacket(name);
   m_keyChain.sign(packet, signingByIdentity(subIdentity));
   VALIDATE_FAILURE(packet, "Should fail, as no cert should be requested");
   BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 0);
 
-  packet = unsignedPacket;
+  packet = Packet::makePacket(name);
   m_keyChain.sign(packet, signingByIdentity(identity));
   VALIDATE_SUCCESS(packet, "Should succeed, as signed by trust anchor");
   BOOST_CHECK_EQUAL(this->face.sentInterests.size(), 0);
diff --git a/tests/unit/security/validation-policy-simple-hierarchy.t.cpp b/tests/unit/security/validation-policy-simple-hierarchy.t.cpp
index a6b6fdc..c0729f4 100644
--- a/tests/unit/security/validation-policy-simple-hierarchy.t.cpp
+++ b/tests/unit/security/validation-policy-simple-hierarchy.t.cpp
@@ -20,7 +20,6 @@
  */
 
 #include "ndn-cxx/security/validation-policy-simple-hierarchy.hpp"
-#include "ndn-cxx/util/scope.hpp"
 
 #include "tests/boost-test.hpp"
 #include "tests/unit/security/validator-fixture.hpp"
@@ -32,43 +31,36 @@
 inline namespace v2 {
 namespace tests {
 
-using namespace ndn::tests;
-
 BOOST_AUTO_TEST_SUITE(Security)
 BOOST_FIXTURE_TEST_SUITE(TestValidationPolicySimpleHierarchy,
                          HierarchicalValidatorFixture<ValidationPolicySimpleHierarchy>)
 
-using Packets = boost::mpl::vector<Interest, Data>;
+using Packets = boost::mpl::vector<InterestV03Pkt, DataPkt>;
 
 BOOST_AUTO_TEST_CASE_TEMPLATE(Validate, Packet, Packets)
 {
-  // Can't set CanBePrefix on Interests in this test case because of template
-  // TODO: Remove in #4582
-  auto guard = make_scope_exit([] { Interest::s_errorIfCanBePrefixUnset = true; });
-  Interest::s_errorIfCanBePrefixUnset = false;
+  const Name name = "/Security/ValidatorFixture/Sub1/Sub2/Packet";
 
-  Packet unsignedPacket("/Security/ValidatorFixture/Sub1/Sub2/Packet");
-
-  Packet packet = unsignedPacket;
+  auto packet = Packet::makePacket(name);
   VALIDATE_FAILURE(packet, "Unsigned");
 
-  packet = unsignedPacket;
+  packet = Packet::makePacket(name);
   m_keyChain.sign(packet, signingWithSha256());
   VALIDATE_FAILURE(packet, "Policy doesn't accept Sha256Digest signature");
 
-  packet = unsignedPacket;
+  packet = Packet::makePacket(name);
   m_keyChain.sign(packet, signingByIdentity(identity));
   VALIDATE_SUCCESS(packet, "Should get accepted, as signed by the anchor");
 
-  packet = unsignedPacket;
+  packet = Packet::makePacket(name);
   m_keyChain.sign(packet, signingByIdentity(subIdentity));
   VALIDATE_SUCCESS(packet, "Should get accepted, as signed by the policy-compliant cert");
 
-  packet = unsignedPacket;
+  packet = Packet::makePacket(name);
   m_keyChain.sign(packet, signingByIdentity(otherIdentity));
   VALIDATE_FAILURE(packet, "Should fail, as signed by the policy-violating cert");
 
-  packet = unsignedPacket;
+  packet = Packet::makePacket(name);
   m_keyChain.sign(packet, signingByIdentity(subSelfSignedIdentity));
   VALIDATE_FAILURE(packet, "Should fail, because subSelfSignedIdentity is not a trust anchor");
 
diff --git a/tests/unit/security/validator-fixture.hpp b/tests/unit/security/validator-fixture.hpp
index 8da34f0..343e363 100644
--- a/tests/unit/security/validator-fixture.hpp
+++ b/tests/unit/security/validator-fixture.hpp
@@ -185,6 +185,12 @@
     return name;
   }
 
+  static Data
+  makePacket(const Name& name)
+  {
+    return Data(name);
+  }
+
   static shared_ptr<ValidationState>
   makeState()
   {
@@ -203,6 +209,12 @@
   static Name
   makeName(Name name, KeyChain& keyChain);
 
+  static Interest
+  makePacket(const Name& name)
+  {
+    return Interest(name).setCanBePrefix(false);
+  }
+
   static shared_ptr<ValidationState>
   makeState()
   {
@@ -223,6 +235,12 @@
   static Name
   makeName(Name name, KeyChain& keyChain);
 
+  static Interest
+  makePacket(const Name& name)
+  {
+    return Interest(name).setCanBePrefix(false);
+  }
+
   static shared_ptr<ValidationState>
   makeState()
   {
diff --git a/tests/unit/unit-tests-pch.hpp b/tests/unit/unit-tests-pch.hpp
index dd67e02..f3cfdf2 100644
--- a/tests/unit/unit-tests-pch.hpp
+++ b/tests/unit/unit-tests-pch.hpp
@@ -32,4 +32,6 @@
 
 #include "tests/key-chain-fixture.hpp"
 
+#include <boost/test/data/test_case.hpp>
+
 #endif // NDN_CXX_TESTS_UNIT_UNIT_TESTS_PCH_HPP
diff --git a/tests/unit/util/scheduler.t.cpp b/tests/unit/util/scheduler.t.cpp
index 1bd3860..12f018a 100644
--- a/tests/unit/util/scheduler.t.cpp
+++ b/tests/unit/util/scheduler.t.cpp
@@ -298,7 +298,6 @@
   BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(eid), nullString);
 
   eid = scheduler.schedule(10_ms, []{});
-  BOOST_TEST_MESSAGE("eid=" << eid);
   BOOST_CHECK_NE(boost::lexical_cast<std::string>(eid), nullString);
 }