Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
| 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" |
| 21 | #include "logger.hpp" |
| 22 | |
| 23 | #include <boost/filesystem.hpp> |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ndns { |
| 27 | namespace tests { |
| 28 | NDNS_LOG_INIT("TestFakeData") |
| 29 | |
| 30 | 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] | 31 | const Name DbTestData::TEST_IDENTITY_NAME("/test19"); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 32 | const boost::filesystem::path DbTestData::TEST_CERT = |
| 33 | TEST_CONFIG_PATH "/" "anchors/root.cert"; |
| 34 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame^] | 35 | DbTestData::PreviousStateCleaner::PreviousStateCleaner() |
| 36 | { |
| 37 | boost::filesystem::remove(TEST_DATABASE); |
| 38 | boost::filesystem::remove(TEST_CERT); |
| 39 | } |
| 40 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 41 | DbTestData::DbTestData() |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 42 | : m_session(TEST_DATABASE.string()) |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame^] | 43 | , m_keyChain("sqlite3", "file") |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 44 | { |
| 45 | NDNS_LOG_TRACE("start creating test data"); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 46 | |
| 47 | ndns::Validator::VALIDATOR_CONF_FILE = TEST_CONFIG_PATH "/" "validator.conf"; |
| 48 | |
Alexander Afanasyev | d6b3bda | 2014-11-25 17:33:58 -0800 | [diff] [blame^] | 49 | m_keyChain.deleteIdentity(TEST_IDENTITY_NAME); |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 50 | m_certName = m_keyChain.createIdentity(TEST_IDENTITY_NAME); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 51 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 52 | ndn::io::save(*(m_keyChain.getCertificate(m_certName)), TEST_CERT.string()); |
| 53 | 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] | 54 | |
| 55 | BOOST_CHECK_GT(m_certName.size(), 0); |
| 56 | NDNS_LOG_TRACE("test certName: " << m_certName); |
| 57 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 58 | m_root = Zone(TEST_IDENTITY_NAME); |
| 59 | Name name(TEST_IDENTITY_NAME); |
| 60 | name.append("net"); |
| 61 | m_net = Zone(name); |
| 62 | name.append("ndnsim"); |
| 63 | m_ndnsim =Zone(name); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 64 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 65 | m_session.insert(m_root); |
| 66 | BOOST_CHECK_GT(m_root.getId(), 0); |
| 67 | m_session.insert(m_net); |
| 68 | BOOST_CHECK_GT(m_net.getId(), 0); |
| 69 | m_session.insert(m_ndnsim); |
| 70 | BOOST_CHECK_GT(m_ndnsim.getId(), 0); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 71 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 72 | m_zones.push_back(m_root); |
| 73 | m_zones.push_back(m_net); |
| 74 | m_zones.push_back(m_ndnsim); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 75 | |
| 76 | int certificateIndex = 0; |
| 77 | function<void(const Name&,Zone&,const name::Component&)> addQueryRrset = |
| 78 | [this, &certificateIndex] (const Name& label, Zone& zone, |
| 79 | const name::Component& type) { |
| 80 | const time::seconds ttl(3000 + 100 * certificateIndex); |
| 81 | const name::Component version = name::Component::fromVersion(100 + 1000 * certificateIndex); |
| 82 | name::Component qType(label::NDNS_ITERATIVE_QUERY); |
| 83 | NdnsType ndnsType = NDNS_RESP; |
| 84 | if (type == label::CERT_RR_TYPE) { |
| 85 | ndnsType = NDNS_RAW; |
| 86 | qType = label::NDNS_CERT_QUERY; |
| 87 | } |
| 88 | std::ostringstream os; |
| 89 | os << "a fake content: " << (++certificateIndex) << "th"; |
| 90 | |
| 91 | addRrset(zone, label, type, ttl, version, qType, ndnsType, os.str()); |
| 92 | }; |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 93 | |
Shock Jiang | cde2871 | 2014-10-19 21:17:20 -0700 | [diff] [blame] | 94 | addQueryRrset("/dsk-1", m_root, label::CERT_RR_TYPE); |
| 95 | addQueryRrset("/net/ksk-2", m_root, label::CERT_RR_TYPE); |
| 96 | addQueryRrset("/dsk-3", m_net, label::CERT_RR_TYPE); |
| 97 | addQueryRrset("/ndnsim/ksk-4", m_net, label::CERT_RR_TYPE); |
| 98 | addQueryRrset("/dsk-5", m_ndnsim, label::CERT_RR_TYPE); |
| 99 | |
| 100 | addQueryRrset("net", m_root, label::NS_RR_TYPE); |
| 101 | addQueryRrset("ndnsim", m_net, label::NS_RR_TYPE); |
| 102 | addQueryRrset("www", m_ndnsim, label::TXT_RR_TYPE); |
| 103 | addQueryRrset("doc/www", m_ndnsim, label::TXT_RR_TYPE); |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 104 | |
| 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 | |
| 110 | NDNS_LOG_INFO("insert testing data: OK"); |
| 111 | } |
| 112 | |
Shock Jiang | 3016c98 | 2014-11-11 11:35:17 -0800 | [diff] [blame] | 113 | void |
| 114 | DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type, |
| 115 | const time::seconds& ttl, const name::Component& version, |
| 116 | const name::Component& qType, NdnsType ndnsType, const std::string& msg) |
| 117 | { |
| 118 | Rrset rrset(&zone); |
| 119 | rrset.setLabel(label); |
| 120 | rrset.setType(type); |
| 121 | rrset.setTtl(ttl); |
| 122 | rrset.setVersion(version); |
| 123 | |
| 124 | Response re; |
| 125 | re.setZone(zone.getName()); |
| 126 | re.setQueryType(qType); |
| 127 | re.setRrLabel(label); |
| 128 | re.setRrType(type); |
| 129 | re.setVersion(version); |
| 130 | re.setNdnsType(ndnsType); |
| 131 | re.setFreshnessPeriod(ttl); |
| 132 | |
| 133 | if (msg.size() > 0) { |
| 134 | if (type == label::CERT_RR_TYPE) |
| 135 | re.setAppContent(dataBlock(ndn::tlv::Content, msg.c_str(), msg.size())); |
| 136 | else |
| 137 | re.addRr(msg); |
| 138 | } |
| 139 | shared_ptr<Data> data = re.toData(); |
| 140 | m_keyChain.sign(*data, m_certName); // now we ignore the certificate to sign the data |
| 141 | shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName); |
| 142 | BOOST_CHECK_EQUAL(Validator::verifySignature(*data, cert->getPublicKeyInfo()), true); |
| 143 | rrset.setData(data->wireEncode()); |
| 144 | |
| 145 | m_session.insert(rrset); |
| 146 | |
| 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 |