Convert to v2::security and adapt to ndn-cxx changes

Change-Id: I54f1b758cfb8f3f6cbc66a1aec5120ae1640b0ec
refs: #3964
diff --git a/tests/update/test-advertise-crash.cpp b/tests/update/test-advertise-crash.cpp
index 4f59c49..db9dfe6 100644
--- a/tests/update/test-advertise-crash.cpp
+++ b/tests/update/test-advertise-crash.cpp
@@ -30,14 +30,14 @@
 {
 public:
   AdvertiseCrashFixture()
-    : face(g_ioService, keyChain, {true, true})
-    , nlsr(g_ioService, g_scheduler, face, g_keyChain)
+    : face(m_ioService, m_keyChain, {true, true})
+    , nlsr(m_ioService, m_scheduler, face, m_keyChain)
     , namePrefixList(nlsr.getNamePrefixList())
     , updatePrefixUpdateProcessor(nlsr.getPrefixUpdateProcessor())
   {
     // Add an adjacency to nlsr
     Adjacent adj("/ndn/edu/test-site-2/%C1.Router/test",
-                 ndn::util::FaceUri("udp://1.0.0.2"), 10, Adjacent::STATUS_INACTIVE, 0, 0);
+                 ndn::FaceUri("udp://1.0.0.2"), 10, Adjacent::STATUS_INACTIVE, 0, 0);
     nlsr.getAdjacencyList().insert(adj);
 
     // Create a face dataset response with the face having the same uri as
@@ -50,6 +50,9 @@
 
     // Set the network so the LSA prefix is constructed
     nlsr.getConfParameter().setNetwork("/ndn");
+    nlsr.getConfParameter().setRouterName(ndn::Name("/This/router"));
+
+    addIdentity(ndn::Name("/ndn/This/router"));
 
     // So that NLSR starts listening on prefixes
     nlsr.initialize();
@@ -59,14 +62,13 @@
     // in fib.cpp if an operation is done on non-existent faceUri
     nlsr.processFaceDataset(faces);
 
-    this->advanceClocks(ndn::time::milliseconds(1));
+    this->advanceClocks(ndn::time::milliseconds(1), 10);
 
     face.sentInterests.clear();
   }
 
 public:
   ndn::util::DummyClientFace face;
-  ndn::KeyChain keyChain;
 
   Nlsr nlsr;
   NamePrefixList& namePrefixList;
@@ -89,7 +91,7 @@
   std::shared_ptr<ndn::Interest> advertiseInterest = std::make_shared<ndn::Interest>(advertiseCommand);
 
   face.receive(*advertiseInterest);
-  this->advanceClocks(ndn::time::milliseconds(10));
+  this->advanceClocks(ndn::time::milliseconds(10), 10);
 }
 
 } // namespace test
diff --git a/tests/update/test-nfd-rib-command-processor.cpp b/tests/update/test-nfd-rib-command-processor.cpp
index 53c77a1..2a78171 100644
--- a/tests/update/test-nfd-rib-command-processor.cpp
+++ b/tests/update/test-nfd-rib-command-processor.cpp
@@ -39,13 +39,16 @@
 {
 public:
   NfdRibCommandProcessorFixture()
-    : face(g_ioService, keyChain, {true, true})
-    , nlsr(g_ioService, g_scheduler, face, g_keyChain)
+    : face(m_ioService, m_keyChain, {true, true})
+    , nlsr(m_ioService, m_scheduler, face, m_keyChain)
     , namePrefixes(nlsr.getNamePrefixList())
     , processor(nlsr.getNfdRibCommandProcessor())
   {
     // Set the network so the LSA prefix is constructed
     nlsr.getConfParameter().setNetwork("/ndn");
+    nlsr.getConfParameter().setRouterName(ndn::Name("/This/router"));
+
+    addIdentity(ndn::Name("/ndn/This/router"));
 
     // Initialize NLSR so a sync socket is created
     nlsr.initialize();
@@ -54,7 +57,7 @@
     // be the same value as what ChronoSync uses in setting the sessionName
     sessionTime.appendNumber(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count());
 
-    this->advanceClocks(ndn::time::milliseconds(10));
+    this->advanceClocks(ndn::time::milliseconds(10), 10);
     face.sentInterests.clear();
 
     nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
@@ -74,7 +77,7 @@
     // Need to send an interest now since ChronoSync
     // no longer does face->put(*data) in publishData.
     // Instead it does it in onInterest
-    ndn::Name lsaInterestName("/localhop/ndn/NLSR/LSA");
+    ndn::Name lsaInterestName("/localhop/ndn/NLSR/LSA/This/router");
     lsaInterestName.append(std::to_string(Lsa::Type::NAME));
 
     // The part after LSA is Chronosync getSession
@@ -83,13 +86,14 @@
     shared_ptr<ndn::Interest> lsaInterest = make_shared<ndn::Interest>(lsaInterestName);
 
     face.receive(*lsaInterest);
-    this->advanceClocks(ndn::time::milliseconds(10));
+    this->advanceClocks(ndn::time::milliseconds(10), 10);
   }
 
   bool
   wasRoutingUpdatePublished()
   {
     sendInterestForPublishedData();
+
     const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix();
 
     const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
@@ -102,7 +106,6 @@
 
 public:
   ndn::util::DummyClientFace face;
-  ndn::KeyChain keyChain;
 
   Nlsr nlsr;
   NamePrefixList& namePrefixes;
@@ -148,7 +151,7 @@
     .wireEncode()), 0);
 
   face.receive(*command);
-  this->advanceClocks(ndn::time::milliseconds(10));
+  this->advanceClocks(ndn::time::milliseconds(10), 10);
 
   BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 1);
   std::list<ndn::Name> names = namePrefixes.getNames();
@@ -173,7 +176,7 @@
     .wireEncode()), 0);
 
   face.receive(ndn::Interest(name));
-  this->advanceClocks(ndn::time::milliseconds(10));
+  this->advanceClocks(ndn::time::milliseconds(10), 10);
 
   BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
   BOOST_CHECK(wasRoutingUpdatePublished());
@@ -190,7 +193,7 @@
     .wireEncode()), 0);
 
   face.receive(ndn::Interest(name));
-  this->advanceClocks(ndn::time::milliseconds(10));
+  this->advanceClocks(ndn::time::milliseconds(10), 10);
 
   BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0);
 
diff --git a/tests/update/test-prefix-update-processor.cpp b/tests/update/test-prefix-update-processor.cpp
index 62ec8f6..5bb61f1 100644
--- a/tests/update/test-prefix-update-processor.cpp
+++ b/tests/update/test-prefix-update-processor.cpp
@@ -29,7 +29,11 @@
 #include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
 #include <ndn-cxx/mgmt/nfd/control-response.hpp>
 #include <ndn-cxx/security/key-chain.hpp>
+#include <ndn-cxx/security/pib/identity.hpp>
+#include <ndn-cxx/security/signing-helpers.hpp>
+#include <ndn-cxx/security/command-interest-signer.hpp>
 #include <ndn-cxx/util/dummy-client-face.hpp>
+#include <ndn-cxx/lp/tags.hpp>
 
 #include <boost/filesystem.hpp>
 
@@ -43,74 +47,78 @@
 {
 public:
   PrefixUpdateFixture()
-    : face(g_ioService, keyChain, {true, true})
-    , siteIdentity(ndn::Name("/ndn/edu/test-site").appendVersion())
-    , opIdentity(ndn::Name(siteIdentity).append(ndn::Name("%C1.Operator")).appendVersion())
-    , nlsr(g_ioService, g_scheduler, face, g_keyChain)
-    , keyPrefix(("/ndn/broadcast"))
+    : face(m_ioService, m_keyChain, {true, true})
+    , siteIdentityName(ndn::Name("/edu/test-site"))
+    , opIdentityName(ndn::Name("/edu/test-site").append(ndn::Name("%C1.Operator")))
+    , nlsr(m_ioService, m_scheduler, face, m_keyChain)
     , namePrefixList(nlsr.getNamePrefixList())
     , updatePrefixUpdateProcessor(nlsr.getPrefixUpdateProcessor())
     , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert"))
   {
-    createSiteCert();
-    BOOST_REQUIRE(siteCert != nullptr);
+    // Site cert
+    siteIdentity = addIdentity(siteIdentityName);
+    saveCertificate(siteIdentity, SITE_CERT_PATH.string());
 
-    createOperatorCert();
-    BOOST_REQUIRE(opCert != nullptr);
+    // Operator cert
+    opIdentity = addSubCertificate(opIdentityName, siteIdentity);
 
-    const std::string CONFIG =
-      "rule\n"
-      "{\n"
-      "  id \"NLSR ControlCommand Rule\"\n"
-      "  for interest\n"
-      "  filter\n"
-      "  {\n"
-      "    type name\n"
-      "    regex ^<localhost><nlsr><prefix-update>[<advertise><withdraw>]<>$\n"
-      "  }\n"
-      "  checker\n"
-      "  {\n"
-      "    type customized\n"
-      "    sig-type rsa-sha256\n"
-      "    key-locator\n"
-      "    {\n"
-      "      type name\n"
-      "      regex ^([^<KEY><%C1.Operator>]*)<%C1.Operator>[^<KEY>]*<KEY><ksk-.*><ID-CERT>$\n"
-      "    }\n"
-      "  }\n"
-      "}\n"
-      "rule\n"
-      "{\n"
-      "  id \"NLSR Hierarchy Rule\"\n"
-      "  for data\n"
-      "  filter\n"
-      "  {\n"
-      "    type name\n"
-      "    regex ^[^<KEY>]*<KEY><ksk-.*><ID-CERT><>$\n"
-      "  }\n"
-      "  checker\n"
-      "  {\n"
-      "    type hierarchical\n"
-      "    sig-type rsa-sha256\n"
-      "  }\n"
-      "}\n"
-      "trust-anchor\n"
-      "{\n"
-      " type file\n"
-      " file-name \"site.cert\"\n"
-      "}\n";
+    const std::string CONFIG = R"CONF(
+        rule
+        {
+          id "NLSR ControlCommand Rule"
+          for interest
+          filter
+          {
+            type name
+            regex ^(<localhost><nlsr>)<prefix-update>[<advertise><withdraw>]<><><>$
+          }
+          checker
+          {
+            type customized
+            sig-type rsa-sha256
+            key-locator
+            {
+              type name
+              regex ^<>*<KEY><>$
+            }
+          }
+        }
+        rule
+        {
+          id "NLSR Hierarchy Rule"
+          for data
+          filter
+          {
+            type name
+            regex ^[^<KEY>]*<KEY><><><>$
+          }
+          checker
+          {
+            type hierarchical
+            sig-type rsa-sha256
+          }
+        }
+        trust-anchor
+        {
+         type file
+         file-name "site.cert"
+        }
+    )CONF";
 
     const boost::filesystem::path CONFIG_PATH =
       (boost::filesystem::current_path() / std::string("unit-test.conf"));
 
     updatePrefixUpdateProcessor.getValidator().load(CONFIG, CONFIG_PATH.native());
 
-    // Insert certs after the validator is loaded since ValidatorConfig::load() clears
-    // the certificate cache
-    nlsr.addCertificateToCache(opCert);
+    nlsr.loadCertToPublish(opIdentity.getDefaultKey().getDefaultCertificate());
 
     // Set the network so the LSA prefix is constructed
     nlsr.getConfParameter().setNetwork("/ndn");
+    nlsr.getConfParameter().setSiteName("/edu/test-site");
+    nlsr.getConfParameter().setRouterName("/%C1.Router/this-router");
+    nlsr.getConfParameter().buildRouterPrefix();
+
+    addIdentity(ndn::Name("/ndn/edu/test-site/%C1.Router/this-router"));
 
     // Initialize NLSR so a sync socket is created
     nlsr.initialize();
@@ -124,45 +132,14 @@
     face.sentInterests.clear();
   }
 
-  void
-  createSiteCert()
+  void sendInterestForPublishedData()
   {
-    // Site cert
-    keyChain.createIdentity(siteIdentity);
-    siteCertName = keyChain.getDefaultCertificateNameForIdentity(siteIdentity);
-    siteCert = keyChain.getCertificate(siteCertName);
-
-    ndn::io::save(*siteCert, SITE_CERT_PATH.string());
-  }
-
-  void
-  createOperatorCert()
-  {
-    // Operator cert
-    ndn::Name keyName = keyChain.generateRsaKeyPairAsDefault(opIdentity, true);
-
-    opCert = std::make_shared<ndn::IdentityCertificate>();
-    std::shared_ptr<ndn::PublicKey> pubKey = keyChain.getPublicKey(keyName);
-    opCertName = keyName.getPrefix(-1);
-    opCertName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
-    opCert->setName(opCertName);
-    opCert->setNotBefore(time::system_clock::now() - time::days(1));
-    opCert->setNotAfter(time::system_clock::now() + time::days(1));
-    opCert->setPublicKeyInfo(*pubKey);
-    opCert->addSubjectDescription(ndn::security::v1::CertificateSubjectDescription(ndn::oid::ATTRIBUTE_NAME,
-                                                                                   keyName.toUri()));
-    opCert->encode();
-
-    keyChain.signByIdentity(*opCert, siteIdentity);
-
-    keyChain.addCertificateAsIdentityDefault(*opCert);
-  }
-
-  void sendInterestForPublishedData() {
     // Need to send an interest now since ChronoSync
     // no longer does face->put(*data) in publishData.
     // Instead it does it in onInterest
-    ndn::Name lsaInterestName("/localhop/ndn/NLSR/LSA");
+    ndn::Name lsaInterestName = nlsr.getConfParameter().getLsaPrefix();
+    lsaInterestName.append(nlsr.getConfParameter().getSiteName());
+    lsaInterestName.append(nlsr.getConfParameter().getRouterName());
     lsaInterestName.append(std::to_string(Lsa::Type::NAME));
 
     // The part after LSA is Chronosync getSession
@@ -172,7 +149,7 @@
     std::shared_ptr<Interest> lsaInterest = std::make_shared<Interest>(lsaInterestName);
 
     face.receive(*lsaInterest);
-    this->advanceClocks(ndn::time::milliseconds(10));
+    this->advanceClocks(ndn::time::milliseconds(100));
   }
 
   bool
@@ -185,7 +162,8 @@
     const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
       [lsaPrefix] (const ndn::Data& data) {
         return lsaPrefix.isPrefixOf(data.getName());
-      });
+      }
+    );
 
     return (it != face.sentData.end());
   }
@@ -204,28 +182,16 @@
     BOOST_CHECK_EQUAL(response.getCode(), expectedCode);
   }
 
-  ~PrefixUpdateFixture()
-  {
-    keyChain.deleteIdentity(siteIdentity);
-    keyChain.deleteIdentity(opIdentity);
-
-    boost::filesystem::remove(SITE_CERT_PATH);
-  }
-
 public:
   ndn::util::DummyClientFace face;
-  ndn::KeyChain keyChain;
 
-  ndn::Name siteIdentity;
-  ndn::Name siteCertName;
-  std::shared_ptr<IdentityCertificate> siteCert;
+  ndn::Name siteIdentityName;
+  ndn::security::pib::Identity siteIdentity;
 
-  ndn::Name opIdentity;
-  ndn::Name opCertName;
-  std::shared_ptr<IdentityCertificate> opCert;
+  ndn::Name opIdentityName;
+  ndn::security::pib::Identity opIdentity;
 
   Nlsr nlsr;
-  ndn::Name keyPrefix;
   NamePrefixList& namePrefixList;
   PrefixUpdateProcessor& updatePrefixUpdateProcessor;
 
@@ -238,16 +204,29 @@
 BOOST_AUTO_TEST_CASE(Basic)
 {
   uint64_t nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
-  // Advertise
+
   ndn::nfd::ControlParameters parameters;
   parameters.setName("/prefix/to/advertise/");
+
+  // Control Command format: /<prefix>/<management-module>/<command-verb>/<control-parameters>
+  // /<timestamp>/<random-value>/<signed-interests-components>
+
+  // Advertise
   ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
+
+  // append /<control-parameters>
   advertiseCommand.append(parameters.wireEncode());
 
-  std::shared_ptr<Interest> advertiseInterest = std::make_shared<Interest>(advertiseCommand);
-  keyChain.signByIdentity(*advertiseInterest, opIdentity);
+  ndn::security::CommandInterestSigner cis(m_keyChain);
 
-  face.receive(*advertiseInterest);
+  // CommandInterestSigner::makeCommandInterest() will append the last
+  // three components: (<timestamp>/<random-value>/<signed-interests-components>)
+  ndn::Interest advertiseInterest =
+    cis.makeCommandInterest(advertiseCommand,
+                            ndn::security::signingByIdentity(opIdentity));
+
+  face.receive(advertiseInterest);
+
   this->advanceClocks(ndn::time::milliseconds(10));
 
   NamePrefixList& namePrefixList = nlsr.getNamePrefixList();
@@ -261,14 +240,15 @@
   face.sentData.clear();
   nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq();
 
-  // Withdraw
+  //Withdraw
   ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
   withdrawCommand.append(parameters.wireEncode());
 
-  std::shared_ptr<Interest> withdrawInterest = std::make_shared<Interest>(withdrawCommand);
-  keyChain.signByIdentity(*withdrawInterest, opIdentity);
+  ndn::Interest withdrawInterest
+    = cis.makeCommandInterest(withdrawCommand,
+                              ndn::security::signingByIdentity(opIdentity));
 
-  face.receive(*withdrawInterest);
+  face.receive(withdrawInterest);
   this->advanceClocks(ndn::time::milliseconds(10));
 
   BOOST_CHECK_EQUAL(namePrefixList.size(), 0);