Add Client Conf
Change-Id: Ibb238b87e7076060386bbdbcc46a841268d6dc42
diff --git a/src/client-config.cpp b/src/client-config.cpp
new file mode 100644
index 0000000..c75615d
--- /dev/null
+++ b/src/client-config.cpp
@@ -0,0 +1,115 @@
+/* -*- 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 "client-config.hpp"
+
+namespace ndn {
+namespace ndncert {
+
+void
+ClientConfig::load(const std::string& fileName)
+{
+ try {
+ boost::property_tree::read_json(fileName, m_config);
+ }
+ catch (const boost::property_tree::info_parser_error& error) {
+ BOOST_THROW_EXCEPTION(Error("Failed to parse configuration file " + fileName +
+ " " + error.message() + " line " + std::to_string(error.line())));
+ }
+
+ if (m_config.begin() == m_config.end()) {
+ BOOST_THROW_EXCEPTION(Error("Error processing configuration file: " + fileName + " no data"));
+ }
+
+ parse();
+}
+
+void
+ClientConfig::parse()
+{
+ m_caItems.clear();
+ auto caList = m_config.get_child("ca-list");
+ auto it = caList.begin();
+ for (; it != caList.end(); it++) {
+ CaItem item;
+ item.m_caName = Name(it->second.get<std::string>("ca-prefix"));
+ item.m_caInfo = it->second.get<std::string>("ca-info");
+ item.m_probe = it->second.get("probe", "");
+
+ auto challengeList = it->second.get_child("supported-challenges");
+ item.m_supportedChallenges = parseChallengeList(challengeList);
+
+ m_caItems.push_back(item);
+ }
+}
+
+std::list<std::string>
+ClientConfig::parseChallengeList(const ConfigSection& section)
+{
+ std::list<std::string> result;
+ auto it = section.begin();
+ for (; it != section.end(); it++) {
+ result.push_back(it->second.get<std::string>("type"));
+ }
+ return result;
+}
+
+void
+ClientConfig::addNewCaItem(const CaItem& item)
+{
+ auto& caList = m_config.get_child("ca-list");
+
+ ConfigSection newCaItem;
+ ConfigSection newCaChallengeList;
+ newCaItem.put("ca-prefix", item.m_caName.toUri());
+ newCaItem.put("ca-info", item.m_caInfo);
+ if (item.m_probe != "") {
+ newCaItem.put("probe", item.m_probe);
+ }
+ for (const auto& challengeType : item.m_supportedChallenges) {
+ ConfigSection challengeSection;
+ challengeSection.put("type", challengeType);
+ newCaChallengeList.push_back(std::make_pair("", challengeSection));
+ }
+ newCaItem.add_child("supported-challenges", newCaChallengeList);
+ caList.push_back(std::make_pair("", newCaItem));
+
+ parse();
+}
+
+void
+ClientConfig::removeCaItem(const Name& caName)
+{
+ auto& caList = m_config.get_child("ca-list");
+ auto it = caList.begin();
+ while (it != caList.end()) {
+ if (it->second.get<std::string>("ca-prefix") == caName.toUri()) {
+ it = caList.erase(it);
+ break;
+ }
+ else {
+ it++;
+ }
+ }
+ parse();
+}
+
+} // namespace ndncert
+} // namespace ndn
diff --git a/src/client-config.hpp b/src/client-config.hpp
new file mode 100644
index 0000000..fbc71f3
--- /dev/null
+++ b/src/client-config.hpp
@@ -0,0 +1,75 @@
+/* -*- 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.
+ */
+
+#ifndef NDNCERT_CLIENT_CONFIG_HPP
+#define NDNCERT_CLIENT_CONFIG_HPP
+
+#include "ca-config.hpp"
+#include <boost/filesystem.hpp>
+
+namespace ndn {
+namespace ndncert {
+
+class CaItem
+{
+public:
+ Name m_caName;
+ std::string m_caInfo;
+ std::string m_probe;
+ std::list<std::string> m_supportedChallenges;
+};
+
+class ClientConfig
+{
+public:
+ class Error : public std::runtime_error
+ {
+ public:
+ using std::runtime_error::runtime_error;
+ };
+
+public:
+ void
+ load(const std::string& fileName);
+
+ void
+ addNewCaItem(const CaItem& item);
+
+ void
+ removeCaItem(const Name& caName);
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+ void
+ parse();
+
+ std::list<std::string>
+ parseChallengeList(const ConfigSection& section);
+
+public:
+ std::list<CaItem> m_caItems;
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+ ConfigSection m_config;
+};
+
+} // namespace ndncert
+} // namespace ndn
+
+#endif // NDNCERT_CLIENT_CONFIG_HPP
diff --git a/tests/unit-tests/client-config.t.cpp b/tests/unit-tests/client-config.t.cpp
new file mode 100644
index 0000000..f9c7530
--- /dev/null
+++ b/tests/unit-tests/client-config.t.cpp
@@ -0,0 +1,72 @@
+/* -*- 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 "boost-test.hpp"
+#include "client-config.hpp"
+
+namespace ndn {
+namespace ndncert {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(TestClientConfig)
+
+BOOST_AUTO_TEST_CASE(ReadConfigFile)
+{
+ ClientConfig config;
+ config.load("tests/unit-tests/client.conf.test");
+ BOOST_CHECK_EQUAL(config.m_caItems.size(), 2);
+
+ const auto& item = config.m_caItems.front();
+ BOOST_CHECK_EQUAL(item.m_caName.toUri(), "/ndn/edu/ucla/CA");
+ BOOST_CHECK_EQUAL(item.m_caInfo, "UCLA's ceritificate authority, located in BH4805.");
+ BOOST_CHECK_EQUAL(item.m_probe, "Please use your email address to apply a namespace first. UCLA email is preferred.");
+ BOOST_CHECK_EQUAL(item.m_supportedChallenges.size(), 2);
+ BOOST_CHECK_EQUAL(item.m_supportedChallenges.front(), "PIN");
+}
+
+BOOST_AUTO_TEST_CASE(AddAndRemoveCaItem)
+{
+ ClientConfig config;
+ config.load("tests/unit-tests/client.conf.test");
+
+ CaItem item;
+ item.m_caName = Name("/test");
+ item.m_caInfo = "test";
+ item.m_probe = "test";
+ std::list<std::string> list;
+ list.push_back("TEST");
+ item.m_supportedChallenges = list;
+
+ config.addNewCaItem(item);
+ BOOST_CHECK_EQUAL(config.m_caItems.size(), 3);
+ auto lastItem = config.m_caItems.back();
+ BOOST_CHECK_EQUAL(lastItem.m_caName.toUri(), "/test");
+
+ config.removeCaItem(Name("/test"));
+ BOOST_CHECK_EQUAL(config.m_caItems.size(), 2);
+ lastItem = config.m_caItems.back();
+ BOOST_CHECK_EQUAL(lastItem.m_caName.toUri(), "/ndn/edu/ucla/zhiyi/CA");
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestClientConfig
+
+} // namespace tests
+} // namespace ndncert
+} // namespace ndn
diff --git a/tests/unit-tests/client.conf.test b/tests/unit-tests/client.conf.test
new file mode 100644
index 0000000..745a43b
--- /dev/null
+++ b/tests/unit-tests/client.conf.test
@@ -0,0 +1,23 @@
+{
+ "ca-list":
+ [
+ {
+ "ca-prefix": "/ndn/edu/ucla/CA",
+ "ca-info": "UCLA's ceritificate authority, located in BH4805.",
+ "probe": "Please use your email address to apply a namespace first. UCLA email is preferred.",
+ "supported-challenges":
+ [
+ { "type": "PIN" },
+ { "type": "EMAIL" }
+ ]
+ },
+ {
+ "ca-prefix": "/ndn/edu/ucla/zhiyi/CA",
+ "ca-info": "Zhiyi's own ceritificate authority",
+ "supported-challenges":
+ [
+ { "type": "PIN" }
+ ]
+ }
+ ]
+}
\ No newline at end of file