Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 767f35c | 2016-07-23 01:54:42 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California. |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "database-test-data.hpp" |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame^] | 21 | #include "daemon/rrset-factory.hpp" |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 22 | |
| 23 | namespace ndn { |
| 24 | namespace ndns { |
| 25 | namespace tests { |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 26 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 27 | NDNS_LOG_INIT("TestFakeData") |
| 28 | |
| 29 | const boost::filesystem::path DbTestData::TEST_DATABASE = TEST_CONFIG_PATH "/" "test-ndns.db"; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 30 | const Name DbTestData::TEST_IDENTITY_NAME("/test19"); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 31 | const boost::filesystem::path DbTestData::TEST_CERT = |
| 32 | TEST_CONFIG_PATH "/" "anchors/root.cert"; |
| 33 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 34 | DbTestData::PreviousStateCleaner::PreviousStateCleaner() |
| 35 | { |
| 36 | boost::filesystem::remove(TEST_DATABASE); |
| 37 | boost::filesystem::remove(TEST_CERT); |
| 38 | } |
| 39 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 40 | DbTestData::DbTestData() |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 41 | : m_session(TEST_DATABASE.string()) |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 42 | { |
| 43 | NDNS_LOG_TRACE("start creating test data"); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 44 | |
| 45 | ndns::Validator::VALIDATOR_CONF_FILE = TEST_CONFIG_PATH "/" "validator.conf"; |
| 46 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 47 | m_keyChain.deleteIdentity(TEST_IDENTITY_NAME); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 48 | m_certName = m_keyChain.createIdentity(TEST_IDENTITY_NAME); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 49 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 50 | ndn::io::save(*(m_keyChain.getCertificate(m_certName)), TEST_CERT.string()); |
| 51 | NDNS_LOG_INFO("save test root cert " << m_certName << " to: " << TEST_CERT.string()); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 52 | |
| 53 | BOOST_CHECK_GT(m_certName.size(), 0); |
| 54 | NDNS_LOG_TRACE("test certName: " << m_certName); |
| 55 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 56 | m_root = Zone(TEST_IDENTITY_NAME); |
| 57 | Name name(TEST_IDENTITY_NAME); |
| 58 | name.append("net"); |
| 59 | m_net = Zone(name); |
| 60 | name.append("ndnsim"); |
| 61 | m_ndnsim =Zone(name); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 62 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 63 | m_session.insert(m_root); |
| 64 | BOOST_CHECK_GT(m_root.getId(), 0); |
| 65 | m_session.insert(m_net); |
| 66 | BOOST_CHECK_GT(m_net.getId(), 0); |
| 67 | m_session.insert(m_ndnsim); |
| 68 | BOOST_CHECK_GT(m_ndnsim.getId(), 0); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 69 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 70 | m_zones.push_back(m_root); |
| 71 | m_zones.push_back(m_net); |
| 72 | m_zones.push_back(m_ndnsim); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 73 | |
| 74 | int certificateIndex = 0; |
| 75 | function<void(const Name&,Zone&,const name::Component&)> addQueryRrset = |
| 76 | [this, &certificateIndex] (const Name& label, Zone& zone, |
| 77 | const name::Component& type) { |
| 78 | const time::seconds ttl(3000 + 100 * certificateIndex); |
| 79 | const name::Component version = name::Component::fromVersion(100 + 1000 * certificateIndex); |
| 80 | name::Component qType(label::NDNS_ITERATIVE_QUERY); |
| 81 | NdnsType ndnsType = NDNS_RESP; |
| 82 | if (type == label::CERT_RR_TYPE) { |
| 83 | ndnsType = NDNS_RAW; |
| 84 | qType = label::NDNS_CERT_QUERY; |
| 85 | } |
| 86 | std::ostringstream os; |
| 87 | os << "a fake content: " << (++certificateIndex) << "th"; |
| 88 | |
| 89 | addRrset(zone, label, type, ttl, version, qType, ndnsType, os.str()); |
| 90 | }; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 91 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 92 | addQueryRrset("/dsk-1", m_root, label::CERT_RR_TYPE); |
| 93 | addQueryRrset("/net/ksk-2", m_root, label::CERT_RR_TYPE); |
| 94 | addQueryRrset("/dsk-3", m_net, label::CERT_RR_TYPE); |
| 95 | addQueryRrset("/ndnsim/ksk-4", m_net, label::CERT_RR_TYPE); |
| 96 | addQueryRrset("/dsk-5", m_ndnsim, label::CERT_RR_TYPE); |
| 97 | |
| 98 | addQueryRrset("net", m_root, label::NS_RR_TYPE); |
| 99 | addQueryRrset("ndnsim", m_net, label::NS_RR_TYPE); |
| 100 | addQueryRrset("www", m_ndnsim, label::TXT_RR_TYPE); |
| 101 | addQueryRrset("doc/www", m_ndnsim, label::TXT_RR_TYPE); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 102 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 103 | addRrset(m_ndnsim, Name("doc"), label::NS_RR_TYPE , time::seconds(2000), |
| 104 | name::Component::fromVersion(1234), label::NDNS_ITERATIVE_QUERY, NDNS_AUTH, |
| 105 | std::string("")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 106 | |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame^] | 107 | // last link is the same as former one |
| 108 | BOOST_ASSERT(!m_links.empty()); |
| 109 | m_links.push_back(m_links.back()); |
| 110 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 111 | NDNS_LOG_INFO("insert testing data: OK"); |
| 112 | } |
| 113 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 114 | void |
| 115 | DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type, |
| 116 | const time::seconds& ttl, const name::Component& version, |
| 117 | const name::Component& qType, NdnsType ndnsType, const std::string& msg) |
| 118 | { |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame^] | 119 | Rrset rrset; |
| 120 | RrsetFactory rf(TEST_DATABASE.string(), zone.getName(), |
| 121 | m_keyChain, m_certName); |
| 122 | rf.onlyCheckZone(); |
| 123 | if (type == label::NS_RR_TYPE) { |
| 124 | ndn::Link::DelegationSet ds = {std::pair<uint32_t, Name>(1,"/xx")}; |
| 125 | rrset = rf.generateNsRrset(label, type, version.toVersion(), ttl, ds); |
| 126 | if (ndnsType != NDNS_AUTH) { |
| 127 | // do not add AUTH packet to link |
| 128 | m_links.push_back(Link(rrset.getData())); |
| 129 | } |
| 130 | } else if (type == label::TXT_RR_TYPE) { |
| 131 | rrset = rf.generateTxtRrset(label, type, version.toVersion(), ttl, |
| 132 | std::vector<std::string>()); |
| 133 | } else if (type == label::CERT_RR_TYPE) { |
| 134 | rrset = rf.generateCertRrset(label, type, version.toVersion(), ttl, |
| 135 | *m_keyChain.getCertificate(m_certName)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 136 | } |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame^] | 137 | |
| 138 | shared_ptr<Data> data = make_shared<Data>(rrset.getData()); |
| 139 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 140 | shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName); |
| 141 | BOOST_CHECK_EQUAL(Validator::verifySignature(*data, cert->getPublicKeyInfo()), true); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 142 | |
| 143 | m_session.insert(rrset); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 144 | m_rrsets.push_back(rrset); |
| 145 | } |
| 146 | |
| 147 | DbTestData::~DbTestData() |
| 148 | { |
| 149 | for (auto& zone : m_zones) |
| 150 | m_session.remove(zone); |
| 151 | |
| 152 | for (auto& rrset : m_rrsets) |
| 153 | m_session.remove(rrset); |
| 154 | |
| 155 | m_session.close(); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | } // namespace tests |
| 159 | } // namespace ndns |
| 160 | } // namespace ndn |