add DbMgr again & testing data
Change-Id: I3e2153fe1d7bf68a9880b8ff533d9fe27d1e15ba
diff --git a/tests/unit/daemon/db-mgr.cpp b/tests/unit/daemon/db-mgr.cpp
new file mode 100644
index 0000000..fb621d2
--- /dev/null
+++ b/tests/unit/daemon/db-mgr.cpp
@@ -0,0 +1,221 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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 a copy of the GNU General Public License along with
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.hpp"
+#include "daemon/db-mgr.hpp"
+#include "logger.hpp"
+
+#include "../../boost-test.hpp"
+#include <boost/filesystem.hpp>
+
+#include <algorithm> // std::sort
+
+namespace ndn {
+namespace ndns {
+namespace tests {
+
+NDNS_LOG_INIT("DbMgrTest")
+
+BOOST_AUTO_TEST_SUITE(DbMgr)
+
+static const boost::filesystem::path TEST_DATABASE2 = TEST_CONFIG_PATH "/" "test-ndns.db";
+
+class DbMgrFixture
+{
+public:
+ DbMgrFixture()
+ : session(TEST_DATABASE2.string())
+ {
+ }
+
+ ~DbMgrFixture()
+ {
+ session.close();
+ boost::filesystem::remove(TEST_DATABASE2);
+ NDNS_LOG_INFO("remove database " << TEST_DATABASE2);
+ }
+
+public:
+ ndns::DbMgr session;
+};
+
+
+
+BOOST_FIXTURE_TEST_CASE(Zones, DbMgrFixture)
+{
+ Zone zone1;
+ zone1.setName("/net");
+ zone1.setTtl(time::seconds(4600));
+ BOOST_CHECK_NO_THROW(session.insert(zone1));
+ BOOST_CHECK_GT(zone1.getId(), 0);
+
+ Zone zone2;
+ zone2.setName("/net");
+ session.find(zone2);
+ BOOST_CHECK_EQUAL(zone2.getId(), zone1.getId());
+ BOOST_CHECK_EQUAL(zone2.getTtl(), zone1.getTtl());
+
+ BOOST_CHECK_NO_THROW(session.insert(zone2)); // zone2 already has id. Nothing to execute
+
+ zone2.setId(0);
+ BOOST_CHECK_THROW(session.insert(zone2), ndns::DbMgr::ExecuteError);
+
+ BOOST_CHECK_NO_THROW(session.remove(zone1));
+ BOOST_CHECK_EQUAL(zone1.getId(), 0);
+
+ // record shouldn't exist at this point
+ BOOST_CHECK_NO_THROW(session.find(zone2));
+ BOOST_CHECK_EQUAL(zone2.getId(), 0);
+}
+
+BOOST_FIXTURE_TEST_CASE(Rrsets, DbMgrFixture)
+{
+ Zone zone("/net");
+ Rrset rrset1(&zone);
+
+ // Add
+
+ rrset1.setLabel("/net/ksk-123");
+ rrset1.setType(name::Component("ID-CERT"));
+ rrset1.setVersion(name::Component::fromVersion(567));
+ rrset1.setTtl(time::seconds(4600));
+
+ static const std::string DATA1 = "SOME DATA";
+ rrset1.setData(dataBlock(ndn::tlv::Content, DATA1.c_str(), DATA1.size()));
+
+ BOOST_CHECK_EQUAL(rrset1.getId(), 0);
+ BOOST_CHECK_NO_THROW(session.insert(rrset1));
+ BOOST_CHECK_GT(rrset1.getId(), 0);
+ BOOST_CHECK_GT(rrset1.getZone()->getId(), 0);
+
+ // Lookup
+
+ Rrset rrset2(&zone);
+ rrset2.setLabel("/net/ksk-123");
+ rrset2.setType(name::Component("ID-CERT"));
+
+ bool isFound = false;
+ BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
+ BOOST_CHECK_EQUAL(isFound, true);
+
+ BOOST_CHECK_EQUAL(rrset2.getId(), rrset1.getId());
+ BOOST_CHECK_EQUAL(rrset2.getLabel(), rrset1.getLabel());
+ BOOST_CHECK_EQUAL(rrset2.getType(), rrset1.getType());
+ BOOST_CHECK_EQUAL(rrset2.getVersion(), rrset1.getVersion());
+ BOOST_CHECK_EQUAL(rrset2.getTtl(), rrset1.getTtl());
+ BOOST_CHECK(rrset2.getData() == rrset1.getData());
+
+ // Replace
+
+ rrset1.setVersion(name::Component::fromVersion(890));
+ static const std::string DATA2 = "ANOTHER DATA";
+ rrset1.setData(dataBlock(ndn::tlv::Content, DATA2.c_str(), DATA2.size()));
+
+ BOOST_CHECK_NO_THROW(session.update(rrset1));
+
+ rrset2 = Rrset(&zone);
+ rrset2.setLabel("/net/ksk-123");
+ rrset2.setType(name::Component("ID-CERT"));
+
+ isFound = false;
+ BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
+ BOOST_CHECK_EQUAL(isFound, true);
+
+ BOOST_CHECK_EQUAL(rrset2.getId(), rrset1.getId());
+ BOOST_CHECK_EQUAL(rrset2.getLabel(), rrset1.getLabel());
+ BOOST_CHECK_EQUAL(rrset2.getType(), rrset1.getType());
+ BOOST_CHECK_EQUAL(rrset2.getVersion(), rrset1.getVersion());
+ BOOST_CHECK_EQUAL(rrset2.getTtl(), rrset1.getTtl());
+ BOOST_CHECK(rrset2.getData() == rrset1.getData());
+
+ // Remove
+
+ BOOST_CHECK_NO_THROW(session.remove(rrset1));
+
+ rrset2 = Rrset(&zone);
+ rrset2.setLabel("/net/ksk-123");
+ rrset2.setType(name::Component("ID-CERT"));
+
+ isFound = false;
+ BOOST_CHECK_NO_THROW(isFound = session.find(rrset2));
+ BOOST_CHECK_EQUAL(isFound, false);
+
+ // Check error handling
+
+ rrset1 = Rrset();
+ BOOST_CHECK_THROW(session.insert(rrset1), ndns::DbMgr::RrsetError);
+ BOOST_CHECK_THROW(session.find(rrset1), ndns::DbMgr::RrsetError);
+
+ rrset1.setId(1);
+ BOOST_CHECK_THROW(session.update(rrset1), ndns::DbMgr::RrsetError);
+
+ rrset1.setId(0);
+ rrset1.setZone(&zone);
+ BOOST_CHECK_THROW(session.update(rrset1), ndns::DbMgr::RrsetError);
+
+ BOOST_CHECK_THROW(session.remove(rrset1), ndns::DbMgr::RrsetError);
+
+ rrset1.setId(1);
+ BOOST_CHECK_NO_THROW(session.remove(rrset1));
+
+ rrset1.setZone(0);
+ rrset1.setId(1);
+ BOOST_CHECK_NO_THROW(session.remove(rrset1));
+}
+
+
+BOOST_FIXTURE_TEST_CASE(FindRrsets, DbMgrFixture)
+{
+ Zone zone("/");
+ Rrset rrset1(&zone);
+ rrset1.setLabel("/net/ksk-123");
+ rrset1.setType(name::Component("ID-CERT"));
+ rrset1.setVersion(name::Component::fromVersion(567));
+ rrset1.setTtl(time::seconds(4600));
+
+ static const std::string DATA1 = "SOME DATA";
+ rrset1.setData(dataBlock(ndn::tlv::Content, DATA1.data(), DATA1.size()));
+ session.insert(rrset1);
+
+ Rrset rrset2(&zone);
+ rrset2.setLabel("/net");
+ rrset2.setType(name::Component("NS"));
+ rrset2.setVersion(name::Component::fromVersion(232));
+ rrset2.setTtl(time::seconds(2100));
+ std::string data2 = "host1.net";
+ rrset2.setData(dataBlock(ndn::tlv::Content, data2.c_str(), data2.size()));
+ session.insert(rrset2);
+
+ std::vector<Rrset> vec = session.findRrsets(zone);
+ BOOST_CHECK_EQUAL(vec.size(), 2);
+
+ std::sort(vec.begin(),
+ vec.end(),
+ [] (const Rrset& n1, const Rrset& n2) {
+ return n1.getLabel().size() < n2.getLabel().size();
+ });
+ BOOST_CHECK_EQUAL(vec[0].getLabel(), "/net");
+ BOOST_CHECK_EQUAL(vec[1].getLabel(), "/net/ksk-123");
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace ndns
+} // namespace ndn
diff --git a/tests/unit/database-test-data.cpp b/tests/unit/database-test-data.cpp
new file mode 100644
index 0000000..3db2335
--- /dev/null
+++ b/tests/unit/database-test-data.cpp
@@ -0,0 +1,180 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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 a copy of the GNU General Public License along with
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "database-test-data.hpp"
+#include "logger.hpp"
+
+#include <boost/filesystem.hpp>
+
+namespace ndn {
+namespace ndns {
+namespace tests {
+NDNS_LOG_INIT("TestFakeData")
+
+const boost::filesystem::path DbTestData::TEST_DATABASE = TEST_CONFIG_PATH "/" "test-ndns.db";
+const Name DbTestData::TEST_IDENTITY_NAME("/");
+const boost::filesystem::path DbTestData::TEST_CERT =
+ TEST_CONFIG_PATH "/" "anchors/root.cert";
+
+DbTestData::DbTestData()
+ : doesTestIdentityExist(false)
+ , m_session(TEST_DATABASE.string())
+{
+ NDNS_LOG_TRACE("start creating test data");
+ // m_session.clearAllData();
+
+ ndns::Validator::VALIDATOR_CONF_FILE = TEST_CONFIG_PATH "/" "validator.conf";
+
+ if (!m_keyChain.doesIdentityExist(TEST_IDENTITY_NAME)) {
+ m_keyChain.createIdentity(TEST_IDENTITY_NAME);
+ }
+ else {
+ doesTestIdentityExist = true;
+ }
+
+ m_keyName = m_keyChain.generateRsaKeyPair(TEST_IDENTITY_NAME, false);
+
+ shared_ptr<IdentityCertificate> scert = m_keyChain.selfSign(m_keyName);
+ m_keyChain.addCertificate(*scert);
+ m_certName = scert->getName();
+
+ ndn::io::save(*scert, TEST_CERT.string());
+ NDNS_LOG_TRACE("test key: " << m_keyName);
+ NDNS_LOG_TRACE("save test root cert " << m_certName << " to: " << TEST_CERT.string());
+
+ BOOST_CHECK_GT(m_certName.size(), 0);
+ NDNS_LOG_TRACE("test certName: " << m_certName);
+
+ Zone root("/");
+ Zone net("/net");
+ Zone ndnsim("/net/ndnsim");
+
+ m_session.insert(root);
+ BOOST_CHECK_GT(root.getId(), 0);
+ m_session.insert(net);
+ BOOST_CHECK_GT(net.getId(), 0);
+ m_session.insert(ndnsim);
+ BOOST_CHECK_GT(ndnsim.getId(), 0);
+
+ m_zones.push_back(root);
+ m_zones.push_back(net);
+ m_zones.push_back(ndnsim);
+
+ int certificateIndex = 0;
+ function<void(const Name&,Zone&,const name::Component&)> addQueryRrset =
+ [this, &certificateIndex] (const Name& label, Zone& zone,
+ const name::Component& type) {
+ const time::seconds ttl(3000 + 100 * certificateIndex);
+ const name::Component version = name::Component::fromVersion(100 + 1000 * certificateIndex);
+ name::Component qType(label::NDNS_ITERATIVE_QUERY);
+ NdnsType ndnsType = NDNS_RESP;
+ if (type == label::CERT_RR_TYPE) {
+ ndnsType = NDNS_RAW;
+ qType = label::NDNS_CERT_QUERY;
+ }
+ std::ostringstream os;
+ os << "a fake content: " << (++certificateIndex) << "th";
+
+ addRrset(zone, label, type, ttl, version, qType, ndnsType, os.str());
+ };
+ addQueryRrset("/dsk-1", root, label::CERT_RR_TYPE);
+ addQueryRrset("/net/ksk-2", root, label::CERT_RR_TYPE);
+ addQueryRrset("/dsk-3", net, label::CERT_RR_TYPE);
+ addQueryRrset("/ndnsim/ksk-4", net, label::CERT_RR_TYPE);
+ addQueryRrset("/dsk-5", ndnsim, label::CERT_RR_TYPE);
+
+ addQueryRrset("net", root, label::NS_RR_TYPE);
+ addQueryRrset("ndnsim", net, label::NS_RR_TYPE);
+ addQueryRrset("www", ndnsim, label::TXT_RR_TYPE);
+ addQueryRrset("doc/www", ndnsim, label::TXT_RR_TYPE);
+
+
+ addRrset(ndnsim, Name("doc"), label::NS_RR_TYPE , time::seconds(2000),
+ name::Component("1234"), label::NDNS_ITERATIVE_QUERY, NDNS_AUTH, std::string(""));
+
+ NDNS_LOG_INFO("insert testing data: OK");
+}
+
+
+
+void
+DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type,
+ const time::seconds& ttl, const name::Component& version,
+ const name::Component& qType, NdnsType ndnsType, const std::string& msg)
+{
+ Rrset rrset(&zone);
+ rrset.setLabel(label);
+ rrset.setType(type);
+ rrset.setTtl(ttl);
+ rrset.setVersion(version);
+
+ Response re;
+ re.setZone(zone.getName());
+ re.setQueryType(qType);
+ re.setRrLabel(label);
+ re.setRrType(type);
+ re.setVersion(version);
+ re.setNdnsType(ndnsType);
+ re.setFreshnessPeriod(ttl);
+
+ if (msg.size() > 0) {
+ if (type == label::CERT_RR_TYPE)
+ re.setAppContent(dataBlock(ndn::tlv::Content, msg.c_str(), msg.size()));
+ else
+ re.addRr(msg);
+ }
+ shared_ptr<Data> data = re.toData();
+ m_keyChain.sign(*data, m_certName); // now we ignore the certificate to sign the data
+ shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName);
+ BOOST_CHECK_EQUAL(Validator::verifySignature(*data, cert->getPublicKeyInfo()), true);
+ rrset.setData(data->wireEncode());
+
+ m_session.insert(rrset);
+
+ m_rrsets.push_back(rrset);
+}
+
+DbTestData::~DbTestData()
+{
+ for (auto& zone : m_zones)
+ m_session.remove(zone);
+
+ for (auto& rrset : m_rrsets)
+ m_session.remove(rrset);
+
+ m_session.close();
+
+ boost::filesystem::remove(TEST_DATABASE);
+ boost::filesystem::remove(TEST_CERT);
+
+ if (doesTestIdentityExist) {
+ m_keyChain.deleteCertificate(m_certName);
+ m_keyChain.deleteKey(m_keyName);
+ NDNS_LOG_TRACE("delete key: " << m_keyName << " and certificate: " << m_certName);
+ }
+ else{
+ m_keyChain.deleteIdentity(TEST_IDENTITY_NAME);
+ }
+
+ NDNS_LOG_INFO("remove database: " << TEST_DATABASE);
+}
+
+} // namespace tests
+} // namespace ndns
+} // namespace ndn
diff --git a/tests/unit/database-test-data.hpp b/tests/unit/database-test-data.hpp
new file mode 100644
index 0000000..38b1d43
--- /dev/null
+++ b/tests/unit/database-test-data.hpp
@@ -0,0 +1,68 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014, Regents of the University of California.
+ *
+ * This file is part of NDNS (Named Data Networking Domain Name Service).
+ * See AUTHORS.md for complete list of NDNS authors and contributors.
+ *
+ * NDNS 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.
+ *
+ * NDNS 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 a copy of the GNU General Public License along with
+ * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NDNS_TESTS_UNIT_DATABASE_TEST_DATA_HPP
+#define NDNS_TESTS_UNIT_DATABASE_TEST_DATA_HPP
+
+#include "daemon/db-mgr.hpp"
+#include "clients/response.hpp"
+#include "clients/query.hpp"
+#include "validator.hpp"
+
+#include "../boost-test.hpp"
+
+#include <ndn-cxx/security/key-chain.hpp>
+#include <boost/filesystem.hpp>
+
+namespace ndn {
+namespace ndns {
+namespace tests {
+
+class DbTestData
+{
+public:
+ static const boost::filesystem::path TEST_DATABASE;
+ static const Name TEST_IDENTITY_NAME;
+ static const boost::filesystem::path TEST_CERT;
+
+ DbTestData();
+
+ ~DbTestData();
+
+private:
+ void
+ addRrset(Zone& zone, const Name& label, const name::Component& type,
+ const time::seconds& ttl, const name::Component& version,
+ const name::Component& qType, NdnsType ndnsType, const std::string& msg);
+public:
+ Name m_certName;
+ Name m_keyName;
+ std::vector<Zone> m_zones;
+ std::vector<Rrset> m_rrsets;
+
+ bool doesTestIdentityExist;
+ DbMgr m_session;
+ KeyChain m_keyChain;
+};
+
+} // namespace tests
+} // namespace ndns
+} // namespace ndn
+
+#endif // NDNS_TESTS_UNIT_DATABASE_TEST_DATA_HPP
diff --git a/tests/unit/logger.cpp b/tests/unit/logger.cpp
index 100aa5b..c2491ad 100644
--- a/tests/unit/logger.cpp
+++ b/tests/unit/logger.cpp
@@ -19,6 +19,7 @@
#include "logger.hpp"
#include "../boost-test.hpp"
+#include "config.hpp"
#include <fstream>