Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2017, 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" |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 22 | #include "util/cert-helper.hpp" |
| 23 | #include "mgmt/management-tool.hpp" |
| 24 | #include <ndn-cxx/security/verification-helpers.hpp> |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace ndns { |
| 28 | namespace tests { |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 29 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 30 | NDNS_LOG_INIT("TestFakeData") |
| 31 | |
| 32 | 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] | 33 | const Name DbTestData::TEST_IDENTITY_NAME("/test19"); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 34 | const boost::filesystem::path DbTestData::TEST_CERT = |
| 35 | TEST_CONFIG_PATH "/" "anchors/root.cert"; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 36 | const boost::filesystem::path DbTestData::TEST_DKEY_CERT = |
| 37 | TEST_CONFIG_PATH "/" "dkey.cert"; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame] | 39 | DbTestData::PreviousStateCleaner::PreviousStateCleaner() |
| 40 | { |
| 41 | boost::filesystem::remove(TEST_DATABASE); |
| 42 | boost::filesystem::remove(TEST_CERT); |
| 43 | } |
| 44 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 45 | DbTestData::DbTestData() |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 46 | : m_session(TEST_DATABASE.string()) |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 47 | { |
| 48 | NDNS_LOG_TRACE("start creating test data"); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 49 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 50 | ndns::NdnsValidatorBuilder::VALIDATOR_CONF_FILE = TEST_CONFIG_PATH "/" "validator.conf"; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 51 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 52 | ManagementTool tool(TEST_DATABASE.string(), m_keyChain); |
| 53 | // this is how DKEY is added to parent zone in real world. |
| 54 | auto addDkeyCertToParent = [&tool](Zone& dkeyFrom, Zone& dkeyTo)->void{ |
| 55 | Certificate dkeyCert; |
| 56 | dkeyCert = tool.getZoneDkey(dkeyFrom); |
| 57 | ndn::io::save(dkeyCert, TEST_DKEY_CERT.string()); |
| 58 | tool.addRrsetFromFile(dkeyTo.getName(), |
| 59 | TEST_DKEY_CERT.string(), |
| 60 | DEFAULT_RR_TTL, |
| 61 | DEFAULT_CERT, |
| 62 | ndn::io::BASE64, |
| 63 | true); |
| 64 | }; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 65 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 66 | Name testName(TEST_IDENTITY_NAME); |
| 67 | m_test = tool.createZone(testName, ROOT_ZONE); |
| 68 | // m_test's DKEY is not added to parent zone |
| 69 | Name netName = Name(testName).append("net"); |
| 70 | m_net = tool.createZone(netName, testName); |
| 71 | addDkeyCertToParent(m_net, m_test); |
| 72 | Name ndnsimName = Name(netName).append("ndnsim"); |
| 73 | m_ndnsim = tool.createZone(ndnsimName, netName); |
| 74 | addDkeyCertToParent(m_ndnsim, m_net); |
| 75 | |
| 76 | m_zones.push_back(m_test); |
| 77 | m_zones.push_back(m_net); |
| 78 | m_zones.push_back(m_ndnsim); |
| 79 | |
| 80 | Name identityName = Name(testName).append("NDNS"); |
| 81 | m_identity = CertHelper::getIdentity(m_keyChain, identityName); |
| 82 | m_certName = CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain, identityName); |
| 83 | m_cert = CertHelper::getCertificate(m_keyChain, identityName, m_certName); |
| 84 | |
| 85 | ndn::io::save(m_cert, TEST_CERT.string()); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 86 | 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] | 87 | |
| 88 | BOOST_CHECK_GT(m_certName.size(), 0); |
| 89 | NDNS_LOG_TRACE("test certName: " << m_certName); |
| 90 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 91 | int certificateIndex = 0; |
| 92 | function<void(const Name&,Zone&,const name::Component&)> addQueryRrset = |
| 93 | [this, &certificateIndex] (const Name& label, Zone& zone, |
| 94 | const name::Component& type) { |
| 95 | const time::seconds ttl(3000 + 100 * certificateIndex); |
| 96 | const name::Component version = name::Component::fromVersion(100 + 1000 * certificateIndex); |
| 97 | name::Component qType(label::NDNS_ITERATIVE_QUERY); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 98 | NdnsContentType contentType = NDNS_RESP; |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 99 | if (type == label::APPCERT_RR_TYPE) { |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 100 | contentType = NDNS_KEY; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 101 | } |
| 102 | else if (type == label::NS_RR_TYPE) { |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 103 | contentType = NDNS_LINK; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 104 | } |
| 105 | else if (type == label::TXT_RR_TYPE) { |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 106 | contentType = NDNS_RESP; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 107 | } |
| 108 | std::ostringstream os; |
| 109 | os << "a fake content: " << (++certificateIndex) << "th"; |
| 110 | |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 111 | addRrset(zone, label, type, ttl, version, qType, contentType, os.str()); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 112 | }; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 113 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 114 | addQueryRrset("net", m_test, label::NS_RR_TYPE); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 115 | addQueryRrset("ndnsim", m_net, label::NS_RR_TYPE); |
| 116 | addQueryRrset("www", m_ndnsim, label::TXT_RR_TYPE); |
| 117 | addQueryRrset("doc/www", m_ndnsim, label::TXT_RR_TYPE); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 118 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 119 | addRrset(m_ndnsim, Name("doc"), label::NS_RR_TYPE , time::seconds(2000), |
| 120 | name::Component::fromVersion(1234), label::NDNS_ITERATIVE_QUERY, NDNS_AUTH, |
| 121 | std::string("")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 122 | |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 123 | // last link is the same as former one |
| 124 | BOOST_ASSERT(!m_links.empty()); |
| 125 | m_links.push_back(m_links.back()); |
| 126 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 127 | NDNS_LOG_INFO("insert testing data: OK"); |
| 128 | } |
| 129 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 130 | void |
| 131 | DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type, |
| 132 | const time::seconds& ttl, const name::Component& version, |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 133 | const name::Component& qType, NdnsContentType contentType, const std::string& msg) |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 134 | { |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 135 | Rrset rrset; |
| 136 | RrsetFactory rf(TEST_DATABASE.string(), zone.getName(), |
| 137 | m_keyChain, m_certName); |
| 138 | rf.onlyCheckZone(); |
| 139 | if (type == label::NS_RR_TYPE) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 140 | ndn::DelegationList ds; |
| 141 | ds.insert(1, "xx"); |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 142 | rrset = rf.generateNsRrset(label, version.toVersion(), ttl, ds); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 143 | if (contentType != NDNS_AUTH) { |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 144 | // do not add AUTH packet to link |
| 145 | m_links.push_back(Link(rrset.getData())); |
| 146 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 147 | } |
| 148 | else if (type == label::TXT_RR_TYPE) { |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 149 | rrset = rf.generateTxtRrset(label, version.toVersion(), ttl, |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 150 | std::vector<std::string>()); |
| 151 | } |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 152 | else if (type == label::APPCERT_RR_TYPE) { |
| 153 | rrset = rf.generateCertRrset(label, version.toVersion(), ttl, |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 154 | m_cert); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 155 | } |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 156 | |
| 157 | shared_ptr<Data> data = make_shared<Data>(rrset.getData()); |
| 158 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 159 | security::verifySignature(*data, m_cert); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 160 | |
| 161 | m_session.insert(rrset); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 162 | m_rrsets.push_back(rrset); |
| 163 | } |
| 164 | |
| 165 | DbTestData::~DbTestData() |
| 166 | { |
| 167 | for (auto& zone : m_zones) |
| 168 | m_session.remove(zone); |
| 169 | |
| 170 | for (auto& rrset : m_rrsets) |
| 171 | m_session.remove(rrset); |
| 172 | |
| 173 | m_session.close(); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | } // namespace tests |
| 177 | } // namespace ndns |
| 178 | } // namespace ndn |