Add list function

Change-Id: I3923d4f12e2134250b5a61419d53582e5450bdde
diff --git a/tests/unit-tests/ca-module.t.cpp b/tests/unit-tests/ca-module.t.cpp
index 27cb9a7..5d23a9f 100644
--- a/tests/unit-tests/ca-module.t.cpp
+++ b/tests/unit-tests/ca-module.t.cpp
@@ -23,6 +23,7 @@
 #include "database-fixture.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>
@@ -48,8 +49,8 @@
   BOOST_CHECK_EQUAL(ca.getCaStorage()->getCertificate("111").getIdentity(), Name("/ndn/site2"));
 
   advanceClocks(time::milliseconds(20), 60);
-  BOOST_CHECK_EQUAL(ca.m_registeredPrefixIds.size(), 3);
-  BOOST_CHECK_EQUAL(ca.m_interestFilterIds.size(), 17);
+  BOOST_CHECK_EQUAL(ca.m_registeredPrefixIds.size(), 4);
+  BOOST_CHECK_EQUAL(ca.m_interestFilterIds.size(), 18);
 }
 
 BOOST_AUTO_TEST_CASE(HandleProbe)
@@ -160,6 +161,95 @@
   BOOST_CHECK_EQUAL(nClientInterest, 1);
 }
 
+BOOST_AUTO_TEST_CASE(HandleLocalhostList)
+{
+  auto identity0 = addIdentity(Name("/ndn"));
+  auto identity1 = addIdentity(Name("/ndn/edu/ucla/cs/zhiyi"));
+  auto identity2 = addIdentity(Name("/ndn/site1"));
+  m_keyChain.setDefaultIdentity(identity0);
+
+  util::DummyClientFace face(m_io, {true, true});
+  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
+
+  advanceClocks(time::milliseconds(20), 60);
+  Interest interest(Name("/localhost/CA/_LIST"));
+
+  int count = 0;
+  face.onSendData.connect([&] (const Data& response) {
+      count++;
+      JsonSection contentJson = ClientModule::getJsonFromData(response);
+      ClientConfig clientConf;
+      clientConf.load(contentJson);
+      BOOST_CHECK_EQUAL(clientConf.m_caItems.size(), 3);
+    });
+  face.receive(interest);
+
+  advanceClocks(time::milliseconds(20), 60);
+  BOOST_CHECK_EQUAL(count, 1);
+}
+
+BOOST_AUTO_TEST_CASE(HandleList)
+{
+  auto identity0 = addIdentity(Name("/ndn"));
+  util::DummyClientFace face(m_io, {true, true});
+  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
+
+  advanceClocks(time::milliseconds(20), 60);
+  Interest interest(Name("/ndn/CA/_LIST"));
+
+  int count = 0;
+  face.onSendData.connect([&] (const Data& response) {
+      count++;
+      JsonSection contentJson = ClientModule::getJsonFromData(response);
+      BOOST_CHECK_EQUAL(contentJson.get_child("ca-list").size(), 2);
+      std::string schemaDataName = contentJson.get<std::string>("trust-schema");
+      BOOST_CHECK_EQUAL(schemaDataName, "TODO: add trust schema");
+    });
+  face.receive(interest);
+
+  advanceClocks(time::milliseconds(20), 60);
+  BOOST_CHECK_EQUAL(count, 1);
+}
+
+BOOST_AUTO_TEST_CASE(HandleTargetList)
+{
+  auto identity0 = addIdentity(Name("/ndn"));
+  util::DummyClientFace face(m_io, {true, true});
+  CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test");
+  ca.setRecommendCaHandler(Name("/ndn"),
+    [] (const std::string& input, const std::list<Name>& list) -> std::tuple<Name, std::string> {
+      Name recommendedCa;
+      std::string identity;
+      for (auto caName : list) {
+        std::string univName = readString(caName.get(-1));
+        if (input.find(univName) != std::string::npos) {
+          recommendedCa = caName;
+          identity = input.substr(0, input.find("@"));
+        }
+      }
+      return std::make_tuple(recommendedCa, identity);
+    });
+
+  advanceClocks(time::milliseconds(20), 60);
+  Interest interest(Name("/ndn/CA/_LIST/example@memphis.edu"));
+
+  int count = 0;
+  face.onSendData.connect([&] (const Data& response) {
+      count++;
+      JsonSection contentJson = ClientModule::getJsonFromData(response);
+      std::string recommendedCA = contentJson.get<std::string>("recommended-ca");
+      std::string recommendedIdentity = contentJson.get<std::string>("recommended-identity");
+      std::string schemaDataName = contentJson.get<std::string>("trust-schema");
+      BOOST_CHECK_EQUAL(recommendedCA, "/ndn/edu/memphis");
+      BOOST_CHECK_EQUAL(recommendedIdentity, "example");
+      BOOST_CHECK_EQUAL(schemaDataName, "TODO: add trust schema");
+    });
+  face.receive(interest);
+
+  advanceClocks(time::milliseconds(20), 60);
+  BOOST_CHECK_EQUAL(count, 1);
+}
+
 BOOST_AUTO_TEST_SUITE_END() // TestCaModule
 
 } // namespace tests