blob: 92865d4c2bd1584fe46640c7cf8714a7a18d5b72 [file] [log] [blame]
Shock Jiang3016c982014-11-11 11:35:17 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
3 * Copyright (c) 2014-2017, Regents of the University of California.
Shock Jiang3016c982014-11-11 11:35:17 -08004 *
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 Xia4e561892016-10-21 10:48:01 -070021#include "daemon/rrset-factory.hpp"
Yumin Xia2c509c22017-02-09 14:37:36 -080022#include "util/cert-helper.hpp"
23#include "mgmt/management-tool.hpp"
24#include <ndn-cxx/security/verification-helpers.hpp>
Shock Jiang3016c982014-11-11 11:35:17 -080025
26namespace ndn {
27namespace ndns {
28namespace tests {
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080029
Shock Jiang3016c982014-11-11 11:35:17 -080030NDNS_LOG_INIT("TestFakeData")
31
32const boost::filesystem::path DbTestData::TEST_DATABASE = TEST_CONFIG_PATH "/" "test-ndns.db";
Shock Jiangcde28712014-10-19 21:17:20 -070033const Name DbTestData::TEST_IDENTITY_NAME("/test19");
Shock Jiang3016c982014-11-11 11:35:17 -080034const boost::filesystem::path DbTestData::TEST_CERT =
35 TEST_CONFIG_PATH "/" "anchors/root.cert";
Yumin Xia2c509c22017-02-09 14:37:36 -080036const boost::filesystem::path DbTestData::TEST_DKEY_CERT =
37 TEST_CONFIG_PATH "/" "dkey.cert";
Shock Jiang3016c982014-11-11 11:35:17 -080038
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080039DbTestData::PreviousStateCleaner::PreviousStateCleaner()
40{
41 boost::filesystem::remove(TEST_DATABASE);
42 boost::filesystem::remove(TEST_CERT);
43}
44
Shock Jiang3016c982014-11-11 11:35:17 -080045DbTestData::DbTestData()
Yumin Xiafa2bce72017-04-09 16:20:25 -070046 : m_session(TEST_DATABASE.string()),
47 m_testName("/test19"),
48 m_netName("/test19/net"),
49 m_ndnsimName("/test19/net/ndnsim")
Shock Jiang3016c982014-11-11 11:35:17 -080050{
51 NDNS_LOG_TRACE("start creating test data");
Shock Jiang3016c982014-11-11 11:35:17 -080052
Yumin Xia2c509c22017-02-09 14:37:36 -080053 ndns::NdnsValidatorBuilder::VALIDATOR_CONF_FILE = TEST_CONFIG_PATH "/" "validator.conf";
Shock Jiang3016c982014-11-11 11:35:17 -080054
Yumin Xia2c509c22017-02-09 14:37:36 -080055 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 Jiang3016c982014-11-11 11:35:17 -080068
Yumin Xiafa2bce72017-04-09 16:20:25 -070069 Name testName(m_testName);
Yumin Xia2c509c22017-02-09 14:37:36 -080070 m_test = tool.createZone(testName, ROOT_ZONE);
71 // m_test's DKEY is not added to parent zone
Yumin Xiafa2bce72017-04-09 16:20:25 -070072 Name netName(m_netName);
Yumin Xia2c509c22017-02-09 14:37:36 -080073 m_net = tool.createZone(netName, testName);
74 addDkeyCertToParent(m_net, m_test);
Yumin Xiafa2bce72017-04-09 16:20:25 -070075 Name ndnsimName(m_ndnsimName);
Yumin Xia2c509c22017-02-09 14:37:36 -080076 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 Jiangcde28712014-10-19 21:17:20 -070089 NDNS_LOG_INFO("save test root cert " << m_certName << " to: " << TEST_CERT.string());
Shock Jiang3016c982014-11-11 11:35:17 -080090
91 BOOST_CHECK_GT(m_certName.size(), 0);
92 NDNS_LOG_TRACE("test certName: " << m_certName);
93
Shock Jiang3016c982014-11-11 11:35:17 -080094 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 Xiaa484ba72016-11-10 20:40:12 -0800101 NdnsContentType contentType = NDNS_RESP;
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700102 if (type == label::APPCERT_RR_TYPE) {
Yumin Xia3c6b1fd2016-12-11 19:08:47 -0800103 contentType = NDNS_KEY;
Yumin Xia2c509c22017-02-09 14:37:36 -0800104 }
105 else if (type == label::NS_RR_TYPE) {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800106 contentType = NDNS_LINK;
Yumin Xia2c509c22017-02-09 14:37:36 -0800107 }
108 else if (type == label::TXT_RR_TYPE) {
Yumin Xiaa484ba72016-11-10 20:40:12 -0800109 contentType = NDNS_RESP;
Shock Jiang3016c982014-11-11 11:35:17 -0800110 }
111 std::ostringstream os;
112 os << "a fake content: " << (++certificateIndex) << "th";
113
Yumin Xiaa484ba72016-11-10 20:40:12 -0800114 addRrset(zone, label, type, ttl, version, qType, contentType, os.str());
Shock Jiang3016c982014-11-11 11:35:17 -0800115 };
Shock Jiangcde28712014-10-19 21:17:20 -0700116
Yumin Xia2c509c22017-02-09 14:37:36 -0800117 addQueryRrset("net", m_test, label::NS_RR_TYPE);
Shock Jiangcde28712014-10-19 21:17:20 -0700118 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 Jiang3016c982014-11-11 11:35:17 -0800121
Shock Jiangcde28712014-10-19 21:17:20 -0700122 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 Jiang3016c982014-11-11 11:35:17 -0800125
Yumin Xia4e561892016-10-21 10:48:01 -0700126 // last link is the same as former one
127 BOOST_ASSERT(!m_links.empty());
128 m_links.push_back(m_links.back());
129
Shock Jiang3016c982014-11-11 11:35:17 -0800130 NDNS_LOG_INFO("insert testing data: OK");
131}
132
Shock Jiang3016c982014-11-11 11:35:17 -0800133void
134DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type,
135 const time::seconds& ttl, const name::Component& version,
Yumin Xiaa484ba72016-11-10 20:40:12 -0800136 const name::Component& qType, NdnsContentType contentType, const std::string& msg)
Shock Jiang3016c982014-11-11 11:35:17 -0800137{
Yumin Xia4e561892016-10-21 10:48:01 -0700138 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 Xia2c509c22017-02-09 14:37:36 -0800143 ndn::DelegationList ds;
144 ds.insert(1, "xx");
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700145 rrset = rf.generateNsRrset(label, version.toVersion(), ttl, ds);
Yumin Xiaa484ba72016-11-10 20:40:12 -0800146 if (contentType != NDNS_AUTH) {
Yumin Xia4e561892016-10-21 10:48:01 -0700147 // do not add AUTH packet to link
148 m_links.push_back(Link(rrset.getData()));
149 }
Yumin Xia2c509c22017-02-09 14:37:36 -0800150 }
151 else if (type == label::TXT_RR_TYPE) {
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700152 rrset = rf.generateTxtRrset(label, version.toVersion(), ttl,
Yumin Xia2c509c22017-02-09 14:37:36 -0800153 std::vector<std::string>());
154 }
Yumin Xiad4e8ce52017-03-17 19:56:52 -0700155 else if (type == label::APPCERT_RR_TYPE) {
156 rrset = rf.generateCertRrset(label, version.toVersion(), ttl,
Yumin Xia2c509c22017-02-09 14:37:36 -0800157 m_cert);
Shock Jiang3016c982014-11-11 11:35:17 -0800158 }
Yumin Xia4e561892016-10-21 10:48:01 -0700159
160 shared_ptr<Data> data = make_shared<Data>(rrset.getData());
161
Yumin Xia2c509c22017-02-09 14:37:36 -0800162 security::verifySignature(*data, m_cert);
Shock Jiang3016c982014-11-11 11:35:17 -0800163
164 m_session.insert(rrset);
Shock Jiang3016c982014-11-11 11:35:17 -0800165 m_rrsets.push_back(rrset);
166}
167
168DbTestData::~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 Jiang3016c982014-11-11 11:35:17 -0800177}
178
179} // namespace tests
180} // namespace ndns
181} // namespace ndn