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