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); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 81 | NdnsContentType contentType = NDNS_RESP; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 82 | if (type == label::CERT_RR_TYPE) { |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 83 | contentType = NDNS_KEY; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 84 | qType = label::NDNS_CERT_QUERY; |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 85 | } else if (type == label::NS_RR_TYPE) { |
| 86 | contentType = NDNS_LINK; |
| 87 | } else if (type == label::TXT_RR_TYPE) { |
| 88 | contentType = NDNS_RESP; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 89 | } |
| 90 | std::ostringstream os; |
| 91 | os << "a fake content: " << (++certificateIndex) << "th"; |
| 92 | |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 93 | addRrset(zone, label, type, ttl, version, qType, contentType, os.str()); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 94 | }; |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 95 | addQueryRrset("/dsk-1", m_root, label::CERT_RR_TYPE); |
| 96 | addQueryRrset("/net/ksk-2", m_root, label::CERT_RR_TYPE); |
| 97 | addQueryRrset("/dsk-3", m_net, label::CERT_RR_TYPE); |
| 98 | addQueryRrset("/ndnsim/ksk-4", m_net, label::CERT_RR_TYPE); |
| 99 | addQueryRrset("/dsk-5", m_ndnsim, label::CERT_RR_TYPE); |
| 100 | |
| 101 | addQueryRrset("net", m_root, label::NS_RR_TYPE); |
| 102 | addQueryRrset("ndnsim", m_net, label::NS_RR_TYPE); |
| 103 | addQueryRrset("www", m_ndnsim, label::TXT_RR_TYPE); |
| 104 | addQueryRrset("doc/www", m_ndnsim, label::TXT_RR_TYPE); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 105 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 106 | addRrset(m_ndnsim, Name("doc"), label::NS_RR_TYPE , time::seconds(2000), |
| 107 | name::Component::fromVersion(1234), label::NDNS_ITERATIVE_QUERY, NDNS_AUTH, |
| 108 | std::string("")); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 109 | |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 110 | // last link is the same as former one |
| 111 | BOOST_ASSERT(!m_links.empty()); |
| 112 | m_links.push_back(m_links.back()); |
| 113 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 114 | NDNS_LOG_INFO("insert testing data: OK"); |
| 115 | } |
| 116 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 117 | void |
| 118 | DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type, |
| 119 | const time::seconds& ttl, const name::Component& version, |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 120 | const name::Component& qType, NdnsContentType contentType, const std::string& msg) |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 121 | { |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 122 | Rrset rrset; |
| 123 | RrsetFactory rf(TEST_DATABASE.string(), zone.getName(), |
| 124 | m_keyChain, m_certName); |
| 125 | rf.onlyCheckZone(); |
| 126 | if (type == label::NS_RR_TYPE) { |
| 127 | ndn::Link::DelegationSet ds = {std::pair<uint32_t, Name>(1,"/xx")}; |
| 128 | rrset = rf.generateNsRrset(label, type, version.toVersion(), ttl, ds); |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 129 | if (contentType != NDNS_AUTH) { |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 130 | // do not add AUTH packet to link |
| 131 | m_links.push_back(Link(rrset.getData())); |
| 132 | } |
| 133 | } else if (type == label::TXT_RR_TYPE) { |
| 134 | rrset = rf.generateTxtRrset(label, type, version.toVersion(), ttl, |
| 135 | std::vector<std::string>()); |
| 136 | } else if (type == label::CERT_RR_TYPE) { |
| 137 | rrset = rf.generateCertRrset(label, type, version.toVersion(), ttl, |
| 138 | *m_keyChain.getCertificate(m_certName)); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 139 | } |
Yumin Xia | 4e56189 | 2016-10-21 10:48:01 -0700 | [diff] [blame] | 140 | |
| 141 | shared_ptr<Data> data = make_shared<Data>(rrset.getData()); |
| 142 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 143 | shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName); |
| 144 | BOOST_CHECK_EQUAL(Validator::verifySignature(*data, cert->getPublicKeyInfo()), true); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 145 | |
| 146 | m_session.insert(rrset); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 147 | m_rrsets.push_back(rrset); |
| 148 | } |
| 149 | |
| 150 | DbTestData::~DbTestData() |
| 151 | { |
| 152 | for (auto& zone : m_zones) |
| 153 | m_session.remove(zone); |
| 154 | |
| 155 | for (auto& rrset : m_rrsets) |
| 156 | m_session.remove(rrset); |
| 157 | |
| 158 | m_session.close(); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | } // namespace tests |
| 162 | } // namespace ndns |
| 163 | } // namespace ndn |