Add CA module

Change-Id: Ic7a7291911c56fce907f1f201fb6dce16a444a44
diff --git a/tests/unit-tests/ca-module.t.cpp b/tests/unit-tests/ca-module.t.cpp
new file mode 100644
index 0000000..33c81ee
--- /dev/null
+++ b/tests/unit-tests/ca-module.t.cpp
@@ -0,0 +1,143 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert 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 General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "identity-management-fixture.hpp"
+#include "ca-module.hpp"
+#include "client-module.hpp"
+#include "challenge-module.hpp"
+#include <ndn-cxx/util/dummy-client-face.hpp>
+#include <ndn-cxx/security/signing-helpers.hpp>
+#include <ndn-cxx/security/transform/public-key.hpp>
+#include <ndn-cxx/security/verification-helpers.hpp>
+
+namespace ndn {
+namespace ndncert {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(TestCaModule, IdentityManagementV2TimeFixture)
+
+BOOST_AUTO_TEST_CASE(Initialization)
+{
+  util::DummyClientFace face(m_io, {true, true});
+  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
+  BOOST_CHECK_EQUAL(ca.getCaConf().m_caItems.front().m_caName.toUri(), "/ndn/edu/ucla/cs/zhiyi");
+  BOOST_CHECK_EQUAL(ca.getCaConf().m_caItems.back().m_caName.toUri(), "/ndn/site1");
+
+  auto identity = addIdentity(Name("/ndn/site2"));
+  auto key = identity.getDefaultKey();
+  auto cert = key.getDefaultCertificate();
+  ca.getCaStorage()->addCertificate("111", cert);
+  BOOST_CHECK_EQUAL(ca.getCaStorage()->getCertificate("111").getIdentity(), Name("/ndn/site2"));
+
+  advanceClocks(time::milliseconds(20), 60);
+  BOOST_CHECK_EQUAL(ca.m_registeredPrefixIds.size(), 2);
+  BOOST_CHECK_EQUAL(ca.m_interestFilterIds.size(), 12);
+}
+
+BOOST_AUTO_TEST_CASE(HandleProbe)
+{
+  auto identity = addIdentity(Name("/ndn/site1"));
+  auto key = identity.getDefaultKey();
+  auto cert = key.getDefaultCertificate();
+
+  util::DummyClientFace face(m_io, {true, true});
+  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
+  ca.setProbeHandler([&] (const std::string& probeInfo) {
+      return probeInfo;
+    });
+  ca.getCaConf().m_caItems.back().m_anchor = cert.getName();
+
+  advanceClocks(time::milliseconds(20), 60);
+
+  Name interestName("/ndn/site1/CA");
+  interestName.append("_PROBE").append("zhiyi");
+  Interest interest(interestName);
+
+  int count = 0;
+  face.onSendData.connect([&] (const Data& response) {
+      count++;
+      BOOST_CHECK(security::verifySignature(response, cert));
+      JsonSection contentJson = ClientModule::getJsonFromData(response);
+      BOOST_CHECK_EQUAL(contentJson.get(JSON_IDNENTIFIER, ""), "/ndn/site1/zhiyi");
+    });
+  face.receive(interest);
+
+  advanceClocks(time::milliseconds(20), 60);
+  BOOST_CHECK_EQUAL(count, 1);
+}
+
+BOOST_AUTO_TEST_CASE(HandleNew)
+{
+  auto identity = addIdentity(Name("/ndn/site1"));
+  auto key = identity.getDefaultKey();
+  auto cert = key.getDefaultCertificate();
+
+  util::DummyClientFace face(m_io, {true, true});
+  util::DummyClientFace face2(m_io, {true, true});
+
+  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
+  ca.setProbeHandler([&] (const std::string& probeInfo) {
+      return probeInfo;
+    });
+  ca.getCaConf().m_caItems.back().m_anchor = cert.getName();
+  advanceClocks(time::milliseconds(20), 60);
+
+  Name identityName("/ndn/site1");
+  identityName.append("zhiyi");
+  ClientModule client(face2, m_keyChain);
+  ClientCaItem item;
+  item.m_caName = Name("/ndn/site1/CA");
+  item.m_anchor = cert;
+  client.getClientConf().m_caItems.push_back(item);
+
+  int nClientInterest = 0;
+  int nCaData = 0;
+  int nClientCallback = 0;
+
+  face.onSendData.connect([&] (const Data& data) {
+      nCaData++;
+      JsonSection contentJson = ClientModule::getJsonFromData(data);
+      BOOST_CHECK(!contentJson.get(JSON_REQUEST_ID, "").empty());
+      face2.receive(data);
+    });
+  face2.onSendInterest.connect([&] (const Interest& interest) {
+      nClientInterest++;
+      face.receive(interest);
+    });
+
+  client.sendNew(item, identityName,
+                 [&] (const shared_ptr<RequestState> state) {
+                   nClientCallback++;
+                   BOOST_CHECK(state->m_requestId != "");
+                 },
+                 [] (const std::string& s) { BOOST_CHECK(false); });
+
+  advanceClocks(time::milliseconds(20), 60);
+
+  BOOST_CHECK_EQUAL(nClientCallback, 1);
+  BOOST_CHECK_EQUAL(nCaData, 1);
+  BOOST_CHECK_EQUAL(nClientInterest, 1);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestCaModule
+
+} // namespace tests
+} // namespace ndncert
+} // namespace ndn