blob: c3f6e07b2d648ee999bf0a9a4571d60255c9b0c8 [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);
Yumin Xiaa484ba72016-11-10 20:40:12 -080081 NdnsContentType contentType = NDNS_RESP;
Shock Jiang3016c982014-11-11 11:35:17 -080082 if (type == label::CERT_RR_TYPE) {
Yumin Xia3c6b1fd2016-12-11 19:08:47 -080083 contentType = NDNS_KEY;
Shock Jiang3016c982014-11-11 11:35:17 -080084 qType = label::NDNS_CERT_QUERY;
Yumin Xiaa484ba72016-11-10 20:40:12 -080085 } else if (type == label::NS_RR_TYPE) {
86 contentType = NDNS_LINK;
87 } else if (type == label::TXT_RR_TYPE) {
88 contentType = NDNS_RESP;
Shock Jiang3016c982014-11-11 11:35:17 -080089 }
90 std::ostringstream os;
91 os << "a fake content: " << (++certificateIndex) << "th";
92
Yumin Xiaa484ba72016-11-10 20:40:12 -080093 addRrset(zone, label, type, ttl, version, qType, contentType, os.str());
Shock Jiang3016c982014-11-11 11:35:17 -080094 };
Shock Jiangcde28712014-10-19 21:17:20 -070095 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 Jiang3016c982014-11-11 11:35:17 -0800105
Shock Jiangcde28712014-10-19 21:17:20 -0700106 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 Jiang3016c982014-11-11 11:35:17 -0800109
Yumin Xia4e561892016-10-21 10:48:01 -0700110 // last link is the same as former one
111 BOOST_ASSERT(!m_links.empty());
112 m_links.push_back(m_links.back());
113
Shock Jiang3016c982014-11-11 11:35:17 -0800114 NDNS_LOG_INFO("insert testing data: OK");
115}
116
Shock Jiang3016c982014-11-11 11:35:17 -0800117void
118DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type,
119 const time::seconds& ttl, const name::Component& version,
Yumin Xiaa484ba72016-11-10 20:40:12 -0800120 const name::Component& qType, NdnsContentType contentType, const std::string& msg)
Shock Jiang3016c982014-11-11 11:35:17 -0800121{
Yumin Xia4e561892016-10-21 10:48:01 -0700122 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 Xiaa484ba72016-11-10 20:40:12 -0800129 if (contentType != NDNS_AUTH) {
Yumin Xia4e561892016-10-21 10:48:01 -0700130 // 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 Jiang3016c982014-11-11 11:35:17 -0800139 }
Yumin Xia4e561892016-10-21 10:48:01 -0700140
141 shared_ptr<Data> data = make_shared<Data>(rrset.getData());
142
Shock Jiang3016c982014-11-11 11:35:17 -0800143 shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName);
144 BOOST_CHECK_EQUAL(Validator::verifySignature(*data, cert->getPublicKeyInfo()), true);
Shock Jiang3016c982014-11-11 11:35:17 -0800145
146 m_session.insert(rrset);
Shock Jiang3016c982014-11-11 11:35:17 -0800147 m_rrsets.push_back(rrset);
148}
149
150DbTestData::~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 Jiang3016c982014-11-11 11:35:17 -0800159}
160
161} // namespace tests
162} // namespace ndns
163} // namespace ndn