blob: 90f2587d9e6af92c740dafd9e2ce4c2e286d6b51 [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"
Shock Jiang3016c982014-11-11 11:35:17 -080021
22namespace ndn {
23namespace ndns {
24namespace tests {
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080025
Shock Jiang3016c982014-11-11 11:35:17 -080026NDNS_LOG_INIT("TestFakeData")
27
28const boost::filesystem::path DbTestData::TEST_DATABASE = TEST_CONFIG_PATH "/" "test-ndns.db";
Shock Jiangcde28712014-10-19 21:17:20 -070029const Name DbTestData::TEST_IDENTITY_NAME("/test19");
Shock Jiang3016c982014-11-11 11:35:17 -080030const boost::filesystem::path DbTestData::TEST_CERT =
31 TEST_CONFIG_PATH "/" "anchors/root.cert";
32
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080033DbTestData::PreviousStateCleaner::PreviousStateCleaner()
34{
35 boost::filesystem::remove(TEST_DATABASE);
36 boost::filesystem::remove(TEST_CERT);
37}
38
Shock Jiang3016c982014-11-11 11:35:17 -080039DbTestData::DbTestData()
Shock Jiangcde28712014-10-19 21:17:20 -070040 : m_session(TEST_DATABASE.string())
Shock Jiang3016c982014-11-11 11:35:17 -080041{
42 NDNS_LOG_TRACE("start creating test data");
Shock Jiang3016c982014-11-11 11:35:17 -080043
44 ndns::Validator::VALIDATOR_CONF_FILE = TEST_CONFIG_PATH "/" "validator.conf";
45
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080046 m_keyChain.deleteIdentity(TEST_IDENTITY_NAME);
Shock Jiangcde28712014-10-19 21:17:20 -070047 m_certName = m_keyChain.createIdentity(TEST_IDENTITY_NAME);
Shock Jiang3016c982014-11-11 11:35:17 -080048
Shock Jiangcde28712014-10-19 21:17:20 -070049 ndn::io::save(*(m_keyChain.getCertificate(m_certName)), TEST_CERT.string());
50 NDNS_LOG_INFO("save test root cert " << m_certName << " to: " << TEST_CERT.string());
Shock Jiang3016c982014-11-11 11:35:17 -080051
52 BOOST_CHECK_GT(m_certName.size(), 0);
53 NDNS_LOG_TRACE("test certName: " << m_certName);
54
Shock Jiangcde28712014-10-19 21:17:20 -070055 m_root = Zone(TEST_IDENTITY_NAME);
56 Name name(TEST_IDENTITY_NAME);
57 name.append("net");
58 m_net = Zone(name);
59 name.append("ndnsim");
60 m_ndnsim =Zone(name);
Shock Jiang3016c982014-11-11 11:35:17 -080061
Shock Jiangcde28712014-10-19 21:17:20 -070062 m_session.insert(m_root);
63 BOOST_CHECK_GT(m_root.getId(), 0);
64 m_session.insert(m_net);
65 BOOST_CHECK_GT(m_net.getId(), 0);
66 m_session.insert(m_ndnsim);
67 BOOST_CHECK_GT(m_ndnsim.getId(), 0);
Shock Jiang3016c982014-11-11 11:35:17 -080068
Shock Jiangcde28712014-10-19 21:17:20 -070069 m_zones.push_back(m_root);
70 m_zones.push_back(m_net);
71 m_zones.push_back(m_ndnsim);
Shock Jiang3016c982014-11-11 11:35:17 -080072
73 int certificateIndex = 0;
74 function<void(const Name&,Zone&,const name::Component&)> addQueryRrset =
75 [this, &certificateIndex] (const Name& label, Zone& zone,
76 const name::Component& type) {
77 const time::seconds ttl(3000 + 100 * certificateIndex);
78 const name::Component version = name::Component::fromVersion(100 + 1000 * certificateIndex);
79 name::Component qType(label::NDNS_ITERATIVE_QUERY);
80 NdnsType ndnsType = NDNS_RESP;
81 if (type == label::CERT_RR_TYPE) {
82 ndnsType = NDNS_RAW;
83 qType = label::NDNS_CERT_QUERY;
84 }
85 std::ostringstream os;
86 os << "a fake content: " << (++certificateIndex) << "th";
87
88 addRrset(zone, label, type, ttl, version, qType, ndnsType, os.str());
89 };
Shock Jiang3016c982014-11-11 11:35:17 -080090
Shock Jiangcde28712014-10-19 21:17:20 -070091 addQueryRrset("/dsk-1", m_root, label::CERT_RR_TYPE);
92 addQueryRrset("/net/ksk-2", m_root, label::CERT_RR_TYPE);
93 addQueryRrset("/dsk-3", m_net, label::CERT_RR_TYPE);
94 addQueryRrset("/ndnsim/ksk-4", m_net, label::CERT_RR_TYPE);
95 addQueryRrset("/dsk-5", m_ndnsim, label::CERT_RR_TYPE);
96
97 addQueryRrset("net", m_root, label::NS_RR_TYPE);
98 addQueryRrset("ndnsim", m_net, label::NS_RR_TYPE);
99 addQueryRrset("www", m_ndnsim, label::TXT_RR_TYPE);
100 addQueryRrset("doc/www", m_ndnsim, label::TXT_RR_TYPE);
Shock Jiang3016c982014-11-11 11:35:17 -0800101
102
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
107 NDNS_LOG_INFO("insert testing data: OK");
108}
109
Shock Jiang3016c982014-11-11 11:35:17 -0800110void
111DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type,
112 const time::seconds& ttl, const name::Component& version,
113 const name::Component& qType, NdnsType ndnsType, const std::string& msg)
114{
115 Rrset rrset(&zone);
116 rrset.setLabel(label);
117 rrset.setType(type);
118 rrset.setTtl(ttl);
119 rrset.setVersion(version);
120
121 Response re;
122 re.setZone(zone.getName());
123 re.setQueryType(qType);
124 re.setRrLabel(label);
125 re.setRrType(type);
126 re.setVersion(version);
127 re.setNdnsType(ndnsType);
128 re.setFreshnessPeriod(ttl);
129
130 if (msg.size() > 0) {
131 if (type == label::CERT_RR_TYPE)
Junxiao Shi767f35c2016-07-23 01:54:42 +0000132 re.setAppContent(makeBinaryBlock(ndn::tlv::Content, msg.c_str(), msg.size()));
Shock Jiang3016c982014-11-11 11:35:17 -0800133 else
134 re.addRr(msg);
135 }
136 shared_ptr<Data> data = re.toData();
137 m_keyChain.sign(*data, m_certName); // now we ignore the certificate to sign the data
138 shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName);
139 BOOST_CHECK_EQUAL(Validator::verifySignature(*data, cert->getPublicKeyInfo()), true);
140 rrset.setData(data->wireEncode());
141
142 m_session.insert(rrset);
143
144 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