ping+tests: switch to ndn-cxx KeyChain v2

refs #4089

Change-Id: I21b3b09acdea9c1de61a14e2652b4fa763f2a7a0
diff --git a/tests/chunks/producer.t.cpp b/tests/chunks/producer.t.cpp
index dc5346f..d3c339f 100644
--- a/tests/chunks/producer.t.cpp
+++ b/tests/chunks/producer.t.cpp
@@ -1,8 +1,8 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2016,  Regents of the University of California,
- *                      Colorado State University,
- *                      University Pierre & Marie Curie, Sorbonne University.
+ * Copyright (c) 2016-2017,  Regents of the University of California,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University.
  *
  * This file is part of ndn-tools (Named Data Networking Essential Tools).
  * See AUTHORS.md for complete list of ndn-tools authors and contributors.
@@ -25,28 +25,52 @@
 
 #include "tools/chunks/putchunks/producer.hpp"
 
-#include "tests/test-common.hpp"
+#include <ndn-cxx/security/pib/identity.hpp>
+#include <ndn-cxx/security/pib/key.hpp>
 #include <ndn-cxx/util/dummy-client-face.hpp>
-#include <ndn-cxx/security/validator-null.hpp>
-
 #include <cmath>
 
+#include "tests/test-common.hpp"
+#include "tests/identity-management-fixture.hpp"
+
 namespace ndn {
 namespace chunks {
 namespace tests {
 
 using namespace ndn::tests;
 
+class ProducerFixture : public IdentityManagementFixture
+{
+protected:
+  ProducerFixture()
+    : face(io, {true, true})
+    , prefix("/ndn/chunks/test")
+    , testString(std::string(
+        "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget "
+        "dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, "
+        "nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, "
+        "sem. Nulla consequat massa Donec pede justo,"))
+    ,keyLocatorName(m_keyChain.createIdentity("/ProducerFixture").getDefaultKey().getName())
+  {
+  }
+
+protected:
+  boost::asio::io_service io;
+  util::DummyClientFace face;
+  security::SigningInfo signingInfo;
+  Name prefix;
+  time::milliseconds freshnessPeriod = time::seconds(10);
+  size_t maxSegmentSize = 40;
+  std::istringstream testString;
+  uint64_t version = 1449227841747;
+  Name keyLocatorName;
+};
+
 BOOST_AUTO_TEST_SUITE(Chunks)
-BOOST_AUTO_TEST_SUITE(TestProducer)
+BOOST_FIXTURE_TEST_SUITE(TestProducer, ProducerFixture)
 
 BOOST_AUTO_TEST_CASE(InputData)
 {
-  util::DummyClientFace face;
-  KeyChain keyChain;
-  security::SigningInfo signingInfo;
-  Name prefix("/ndn/chunks/test");
-  int maxSegmentSize = 40;
   std::vector<std::string> testStrings {
       "",
 
@@ -63,7 +87,7 @@
 
   for (size_t i = 0; i <  testStrings.size(); ++i) {
     std::istringstream str(testStrings[i]);
-    Producer prod(prefix, face, keyChain, signingInfo, time::seconds(4), maxSegmentSize, false,
+    Producer prod(prefix, face, m_keyChain, signingInfo, time::seconds(4), maxSegmentSize, false,
                   false, str);
 
     size_t expectedSize = std::ceil(static_cast<double>(testStrings[i].size()) / maxSegmentSize);
@@ -76,22 +100,7 @@
 
 BOOST_AUTO_TEST_CASE(RequestSegmentUnspecifiedVersion)
 {
-  boost::asio::io_service io;
-  util::DummyClientFace face(io, {true, true});
-  KeyChain keyChain;
-  security::SigningInfo signingInfo;
-  Name prefix("/ndn/chunks/test");
-  time::milliseconds freshnessPeriod(time::seconds(10));
-  size_t maxSegmentSize(40);
-  std::istringstream testString(std::string(
-          "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget "
-          "dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, "
-          "nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, "
-          "sem. Nulla consequat massa Donec pede justo,"));
-
-  Name keyLocatorName = keyChain.getDefaultCertificateName().getPrefix(-1);
-
-  Producer producer(prefix, face, keyChain, signingInfo, freshnessPeriod, maxSegmentSize,
+  Producer producer(prefix, face, m_keyChain, signingInfo, freshnessPeriod, maxSegmentSize,
                     false, false, testString);
   io.poll();
 
@@ -128,23 +137,7 @@
 
 BOOST_AUTO_TEST_CASE(RequestSegmentSpecifiedVersion)
 {
-  boost::asio::io_service io;
-  util::DummyClientFace face(io, {true, true});
-  KeyChain keyChain;
-  security::SigningInfo signingInfo;
-  Name prefix("/ndn/chunks/test");
-  time::milliseconds freshnessPeriod(time::seconds(10));
-  size_t maxSegmentSize(40);
-  std::istringstream testString(std::string(
-          "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget "
-          "dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, "
-          "nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, "
-          "sem. Nulla consequat massa Donec pede justo,"));
-
-  uint64_t version = 1449227841747;
-  Name keyLocatorName = keyChain.getDefaultCertificateName().getPrefix(-1);
-
-  Producer producer(prefix.appendVersion(version), face, keyChain, signingInfo, freshnessPeriod,
+  Producer producer(prefix.appendVersion(version), face, m_keyChain, signingInfo, freshnessPeriod,
                     maxSegmentSize, false, false, testString);
   io.poll();
 
@@ -182,22 +175,7 @@
 
 BOOST_AUTO_TEST_CASE(RequestNotExistingSegment)
 {
-  boost::asio::io_service io;
-  util::DummyClientFace face(io, {true, true});
-  KeyChain keyChain;
-  security::SigningInfo signingInfo;
-  Name prefix("/ndn/chunks/test");
-  time::milliseconds freshnessPeriod(time::seconds(10));
-  size_t maxSegmentSize(40);
-  std::istringstream testString(std::string(
-          "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget "
-          "dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, "
-          "nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, "
-          "sem. Nulla consequat massa Donec pede justo,"));
-
-  Name keyLocatorName = keyChain.getDefaultCertificateName().getPrefix(-1);
-
-  Producer producer(prefix, face, keyChain, signingInfo, freshnessPeriod, maxSegmentSize,
+  Producer producer(prefix, face, m_keyChain, signingInfo, freshnessPeriod, maxSegmentSize,
                     false, false, testString);
   io.poll();
 
diff --git a/tests/dump/ndndump.t.cpp b/tests/dump/ndndump.t.cpp
index d4b8396..b360bd9 100644
--- a/tests/dump/ndndump.t.cpp
+++ b/tests/dump/ndndump.t.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2016,  University of Memphis.
+ * Copyright (c) 2014-2017,  University of Memphis.
  *
  * This file is part of ndn-tools (Named Data Networking Essential Tools).
  * See AUTHORS.md for complete list of ndn-tools authors and contributors.
@@ -19,14 +19,14 @@
 
 #include "tools/dump/ndndump.hpp"
 
-#include "tests/test-common.hpp"
-
-#include <boost/test/output_test_stream.hpp>
-
 #include <ndn-cxx/lp/packet.hpp>
 #include <ndn-cxx/security/key-chain.hpp>
 #include <ndn-cxx/util/ethernet.hpp>
 
+#include "tests/test-common.hpp"
+#include "tests/identity-management-fixture.hpp"
+#include <boost/test/output_test_stream.hpp>
+
 namespace ndn {
 namespace dump {
 namespace tests {
@@ -52,7 +52,7 @@
   std::streambuf* originalBuffer;
 };
 
-class NdnDumpFixture
+class NdnDumpFixture : public IdentityManagementFixture
 {
 protected:
   NdnDumpFixture()
@@ -60,23 +60,23 @@
     dump.m_dataLinkType = DLT_EN10MB;
   }
 
-  template <typename Packet>
+  template<typename Packet>
   void
   receive(const Packet& packet)
   {
-    ndn::EncodingBuffer buffer(packet.wireEncode());
+    EncodingBuffer buffer(packet.wireEncode());
     receive(buffer);
   }
 
   void
-  receive(ndn::EncodingBuffer& buffer)
+  receive(EncodingBuffer& buffer)
   {
-    ndn::util::ethernet::Address host;
+    util::ethernet::Address host;
 
     // Ethernet header
-    uint16_t frameType = htons(ndn::util::ethernet::ETHERTYPE_NDN);
+    uint16_t frameType = htons(util::ethernet::ETHERTYPE_NDN);
     buffer.prependByteArray(reinterpret_cast<const uint8_t*>(&frameType),
-                            ndn::util::ethernet::TYPE_LEN);
+                            util::ethernet::TYPE_LEN);
     buffer.prependByteArray(host.data(), host.size());
     buffer.prependByteArray(host.data(), host.size());
 
@@ -98,7 +98,7 @@
 
 BOOST_AUTO_TEST_CASE(CaptureInterest)
 {
-  ndn::Interest interest("/test");
+  Interest interest("/test");
   interest.setNonce(0);
 
   this->receive(interest);
@@ -111,10 +111,8 @@
 
 BOOST_AUTO_TEST_CASE(CaptureData)
 {
-  ndn::security::KeyChain keyChain;
-
-  ndn::Data data("/test");
-  keyChain.sign(data);
+  Data data("/test");
+  m_keyChain.sign(data);
 
   this->receive(data);
 
@@ -125,11 +123,11 @@
 
 BOOST_AUTO_TEST_CASE(CaptureNack)
 {
-  ndn::Interest interest("/test");
+  Interest interest("/test");
   interest.setNonce(0);
 
-  ndn::lp::Nack nack(interest);
-  nack.setReason(ndn::lp::NackReason::DUPLICATE);
+  lp::Nack nack(interest);
+  nack.setReason(lp::NackReason::DUPLICATE);
 
   lp::Packet lpPacket(interest.wireEncode());
   lpPacket.add<lp::NackField>(nack.getHeader());
@@ -149,7 +147,7 @@
         0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
   };
 
-  ndn::Buffer buffer(data, 4);
+  Buffer buffer(data, 4);
 
   lp::Packet lpPacket;
   lpPacket.add<lp::FragmentField>(std::make_pair(buffer.begin(), buffer.end()));
@@ -201,7 +199,7 @@
 
 BOOST_AUTO_TEST_CASE(CaptureUnknownNetworkPacket)
 {
-  EncodingBuffer buffer(ndn::encoding::makeEmptyBlock(tlv::Name));
+  EncodingBuffer buffer(encoding::makeEmptyBlock(tlv::Name));
 
   this->receive(buffer);
 
diff --git a/tests/identity-management-fixture.cpp b/tests/identity-management-fixture.cpp
index 01754ba..171216f 100644
--- a/tests/identity-management-fixture.cpp
+++ b/tests/identity-management-fixture.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2016,  Regents of the University of California.
+ * Copyright (c) 2014-2017,  Regents of the University of California.
  *
  * This file is part of ndn-tools (Named Data Networking Essential Tools).
  * See AUTHORS.md for complete list of ndn-tools authors and contributors.
@@ -18,6 +18,10 @@
  */
 
 #include "identity-management-fixture.hpp"
+#include <ndn-cxx/security/pib/identity.hpp>
+#include <ndn-cxx/security/pib/key.hpp>
+#include <ndn-cxx/security/pib/pib.hpp>
+#include <ndn-cxx/security/v2/certificate.hpp>
 #include <ndn-cxx/util/io.hpp>
 #include <boost/filesystem.hpp>
 
@@ -25,21 +29,13 @@
 namespace tests {
 
 IdentityManagementFixture::IdentityManagementFixture()
-  : m_keyChainPath((boost::filesystem::path(TMP_TESTS_PATH) / "IdentityManagementFixture").string())
-  , m_keyChain("pib-sqlite3:" + m_keyChainPath, "tpm-file:" + m_keyChainPath)
+  : m_keyChain("pib-memory:", "tpm-memory:")
 {
 }
 
 IdentityManagementFixture::~IdentityManagementFixture()
 {
-  for (const auto& id : m_identities) {
-    m_keyChain.deleteIdentity(id);
-  }
-
-  boost::system::error_code ec; // ignore error
-
-  boost::filesystem::remove_all(m_keyChainPath, ec);
-
+  boost::system::error_code ec;
   for (const auto& certFile : m_certFiles) {
     boost::filesystem::remove(certFile, ec);
   }
@@ -50,7 +46,6 @@
 {
   try {
     m_keyChain.createIdentity(identity, params);
-    m_identities.push_back(identity);
     return true;
   }
   catch (const std::runtime_error&) {
@@ -61,11 +56,11 @@
 bool
 IdentityManagementFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd)
 {
-  shared_ptr<IdentityCertificate> cert;
+  security::v2::Certificate cert;
   try {
-    cert = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(identity));
+    cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate();
   }
-  catch (const SecPublicInfo::Error&) {
+  catch (const security::Pib::Error&) {
     if (wantAdd && this->addIdentity(identity)) {
       return this->saveIdentityCertificate(identity, filename, false);
     }
@@ -74,7 +69,7 @@
 
   m_certFiles.push_back(filename);
   try {
-    io::save(*cert, filename);
+    io::save(cert, filename);
     return true;
   }
   catch (const io::Error&) {
diff --git a/tests/identity-management-fixture.hpp b/tests/identity-management-fixture.hpp
index 2d84fae..fbe5251 100644
--- a/tests/identity-management-fixture.hpp
+++ b/tests/identity-management-fixture.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2016,  Regents of the University of California.
+ * Copyright (c) 2014-2017,  Regents of the University of California.
  *
  * This file is part of ndn-tools (Named Data Networking Essential Tools).
  * See AUTHORS.md for complete list of ndn-tools authors and contributors.
@@ -27,14 +27,14 @@
 namespace ndn {
 namespace tests {
 
-/** \brief a fixture that cleans up KeyChain identities and certificate files upon destruction
+/** \brief a fixture providing an in-memory KeyChain
  */
 class IdentityManagementFixture
 {
 public:
   IdentityManagementFixture();
 
-  /** \brief deletes created identities and saved certificate files
+  /** \brief deletes saved certificate files
    */
   ~IdentityManagementFixture();
 
@@ -42,7 +42,7 @@
    *  \return whether successful
    */
   bool
-  addIdentity(const Name& identity, const KeyParams& params = KeyChain::DEFAULT_KEY_PARAMS);
+  addIdentity(const Name& identity, const KeyParams& params = KeyChain::getDefaultKeyParams());
 
   /** \brief save identity certificate to a file
    *  \param identity identity name
@@ -53,14 +53,10 @@
   bool
   saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd = false);
 
-private:
-  std::string m_keyChainPath;
-
 protected:
   KeyChain m_keyChain;
 
 private:
-  std::vector<Name> m_identities;
   std::vector<std::string> m_certFiles;
 };
 
diff --git a/tools/ping/client/ping.cpp b/tools/ping/client/ping.cpp
index 966d29e..c6290b0 100644
--- a/tools/ping/client/ping.cpp
+++ b/tools/ping/client/ping.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014-2016,  Arizona Board of Regents.
+ * Copyright (c) 2014-2017,  Arizona Board of Regents.
  *
  * This file is part of ndn-tools (Named Data Networking Essential Tools).
  * See AUTHORS.md for complete list of ndn-tools authors and contributors.
@@ -22,6 +22,7 @@
  */
 
 #include "ping.hpp"
+#include <ndn-cxx/util/random.hpp>
 
 namespace ndn {
 namespace ping {