blob: 6edcd1cfa8002718439c408758c7886a6a7846f6 [file] [log] [blame]
Shock Jiang3016c982014-11-11 11:35:17 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi767f35c2016-07-23 01:54:42 +00003 * Copyright (c) 2014-2016, 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"
Shock Jiang3016c982014-11-11 11:35:17 -080022
23namespace ndn {
24namespace ndns {
25namespace tests {
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080026
Shock Jiang3016c982014-11-11 11:35:17 -080027NDNS_LOG_INIT("TestFakeData")
28
29const boost::filesystem::path DbTestData::TEST_DATABASE = TEST_CONFIG_PATH "/" "test-ndns.db";
Shock Jiangcde28712014-10-19 21:17:20 -070030const Name DbTestData::TEST_IDENTITY_NAME("/test19");
Shock Jiang3016c982014-11-11 11:35:17 -080031const boost::filesystem::path DbTestData::TEST_CERT =
32 TEST_CONFIG_PATH "/" "anchors/root.cert";
33
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080034DbTestData::PreviousStateCleaner::PreviousStateCleaner()
35{
36 boost::filesystem::remove(TEST_DATABASE);
37 boost::filesystem::remove(TEST_CERT);
38}
39
Shock Jiang3016c982014-11-11 11:35:17 -080040DbTestData::DbTestData()
Shock Jiangcde28712014-10-19 21:17:20 -070041 : m_session(TEST_DATABASE.string())
Shock Jiang3016c982014-11-11 11:35:17 -080042{
43 NDNS_LOG_TRACE("start creating test data");
Shock Jiang3016c982014-11-11 11:35:17 -080044
45 ndns::Validator::VALIDATOR_CONF_FILE = TEST_CONFIG_PATH "/" "validator.conf";
46
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080047 m_keyChain.deleteIdentity(TEST_IDENTITY_NAME);
Shock Jiangcde28712014-10-19 21:17:20 -070048 m_certName = m_keyChain.createIdentity(TEST_IDENTITY_NAME);
Shock Jiang3016c982014-11-11 11:35:17 -080049
Shock Jiangcde28712014-10-19 21:17:20 -070050 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 Jiang3016c982014-11-11 11:35:17 -080052
53 BOOST_CHECK_GT(m_certName.size(), 0);
54 NDNS_LOG_TRACE("test certName: " << m_certName);
55
Shock Jiangcde28712014-10-19 21:17:20 -070056 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 Jiang3016c982014-11-11 11:35:17 -080062
Shock Jiangcde28712014-10-19 21:17:20 -070063 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 Jiang3016c982014-11-11 11:35:17 -080069
Shock Jiangcde28712014-10-19 21:17:20 -070070 m_zones.push_back(m_root);
71 m_zones.push_back(m_net);
72 m_zones.push_back(m_ndnsim);
Shock Jiang3016c982014-11-11 11:35:17 -080073
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);
81 NdnsType ndnsType = NDNS_RESP;
82 if (type == label::CERT_RR_TYPE) {
83 ndnsType = NDNS_RAW;
84 qType = label::NDNS_CERT_QUERY;
85 }
86 std::ostringstream os;
87 os << "a fake content: " << (++certificateIndex) << "th";
88
89 addRrset(zone, label, type, ttl, version, qType, ndnsType, os.str());
90 };
Shock Jiang3016c982014-11-11 11:35:17 -080091
Shock Jiangcde28712014-10-19 21:17:20 -070092 addQueryRrset("/dsk-1", m_root, label::CERT_RR_TYPE);
93 addQueryRrset("/net/ksk-2", m_root, label::CERT_RR_TYPE);
94 addQueryRrset("/dsk-3", m_net, label::CERT_RR_TYPE);
95 addQueryRrset("/ndnsim/ksk-4", m_net, label::CERT_RR_TYPE);
96 addQueryRrset("/dsk-5", m_ndnsim, label::CERT_RR_TYPE);
97
98 addQueryRrset("net", m_root, label::NS_RR_TYPE);
99 addQueryRrset("ndnsim", m_net, label::NS_RR_TYPE);
100 addQueryRrset("www", m_ndnsim, label::TXT_RR_TYPE);
101 addQueryRrset("doc/www", m_ndnsim, label::TXT_RR_TYPE);
Shock Jiang3016c982014-11-11 11:35:17 -0800102
Shock Jiangcde28712014-10-19 21:17:20 -0700103 addRrset(m_ndnsim, Name("doc"), label::NS_RR_TYPE , time::seconds(2000),
104 name::Component::fromVersion(1234), label::NDNS_ITERATIVE_QUERY, NDNS_AUTH,
105 std::string(""));
Shock Jiang3016c982014-11-11 11:35:17 -0800106
Yumin Xia4e561892016-10-21 10:48:01 -0700107 // last link is the same as former one
108 BOOST_ASSERT(!m_links.empty());
109 m_links.push_back(m_links.back());
110
Shock Jiang3016c982014-11-11 11:35:17 -0800111 NDNS_LOG_INFO("insert testing data: OK");
112}
113
Shock Jiang3016c982014-11-11 11:35:17 -0800114void
115DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type,
116 const time::seconds& ttl, const name::Component& version,
117 const name::Component& qType, NdnsType ndnsType, const std::string& msg)
118{
Yumin Xia4e561892016-10-21 10:48:01 -0700119 Rrset rrset;
120 RrsetFactory rf(TEST_DATABASE.string(), zone.getName(),
121 m_keyChain, m_certName);
122 rf.onlyCheckZone();
123 if (type == label::NS_RR_TYPE) {
124 ndn::Link::DelegationSet ds = {std::pair<uint32_t, Name>(1,"/xx")};
125 rrset = rf.generateNsRrset(label, type, version.toVersion(), ttl, ds);
126 if (ndnsType != NDNS_AUTH) {
127 // do not add AUTH packet to link
128 m_links.push_back(Link(rrset.getData()));
129 }
130 } else if (type == label::TXT_RR_TYPE) {
131 rrset = rf.generateTxtRrset(label, type, version.toVersion(), ttl,
132 std::vector<std::string>());
133 } else if (type == label::CERT_RR_TYPE) {
134 rrset = rf.generateCertRrset(label, type, version.toVersion(), ttl,
135 *m_keyChain.getCertificate(m_certName));
Shock Jiang3016c982014-11-11 11:35:17 -0800136 }
Yumin Xia4e561892016-10-21 10:48:01 -0700137
138 shared_ptr<Data> data = make_shared<Data>(rrset.getData());
139
Shock Jiang3016c982014-11-11 11:35:17 -0800140 shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName);
141 BOOST_CHECK_EQUAL(Validator::verifySignature(*data, cert->getPublicKeyInfo()), true);
Shock Jiang3016c982014-11-11 11:35:17 -0800142
143 m_session.insert(rrset);
Shock Jiang3016c982014-11-11 11:35:17 -0800144 m_rrsets.push_back(rrset);
145}
146
147DbTestData::~DbTestData()
148{
149 for (auto& zone : m_zones)
150 m_session.remove(zone);
151
152 for (auto& rrset : m_rrsets)
153 m_session.remove(rrset);
154
155 m_session.close();
Shock Jiang3016c982014-11-11 11:35:17 -0800156}
157
158} // namespace tests
159} // namespace ndns
160} // namespace ndn