blob: 3db2335f607d6ce43157cc724e8b56d7a25b7499 [file] [log] [blame]
Shock Jiang3016c982014-11-11 11:35:17 -08001/* -*- 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
25namespace ndn {
26namespace ndns {
27namespace tests {
28NDNS_LOG_INIT("TestFakeData")
29
30const boost::filesystem::path DbTestData::TEST_DATABASE = TEST_CONFIG_PATH "/" "test-ndns.db";
31const Name DbTestData::TEST_IDENTITY_NAME("/");
32const boost::filesystem::path DbTestData::TEST_CERT =
33 TEST_CONFIG_PATH "/" "anchors/root.cert";
34
35DbTestData::DbTestData()
36 : doesTestIdentityExist(false)
37 , m_session(TEST_DATABASE.string())
38{
39 NDNS_LOG_TRACE("start creating test data");
40 // m_session.clearAllData();
41
42 ndns::Validator::VALIDATOR_CONF_FILE = TEST_CONFIG_PATH "/" "validator.conf";
43
44 if (!m_keyChain.doesIdentityExist(TEST_IDENTITY_NAME)) {
45 m_keyChain.createIdentity(TEST_IDENTITY_NAME);
46 }
47 else {
48 doesTestIdentityExist = true;
49 }
50
51 m_keyName = m_keyChain.generateRsaKeyPair(TEST_IDENTITY_NAME, false);
52
53 shared_ptr<IdentityCertificate> scert = m_keyChain.selfSign(m_keyName);
54 m_keyChain.addCertificate(*scert);
55 m_certName = scert->getName();
56
57 ndn::io::save(*scert, TEST_CERT.string());
58 NDNS_LOG_TRACE("test key: " << m_keyName);
59 NDNS_LOG_TRACE("save test root cert " << m_certName << " to: " << TEST_CERT.string());
60
61 BOOST_CHECK_GT(m_certName.size(), 0);
62 NDNS_LOG_TRACE("test certName: " << m_certName);
63
64 Zone root("/");
65 Zone net("/net");
66 Zone ndnsim("/net/ndnsim");
67
68 m_session.insert(root);
69 BOOST_CHECK_GT(root.getId(), 0);
70 m_session.insert(net);
71 BOOST_CHECK_GT(net.getId(), 0);
72 m_session.insert(ndnsim);
73 BOOST_CHECK_GT(ndnsim.getId(), 0);
74
75 m_zones.push_back(root);
76 m_zones.push_back(net);
77 m_zones.push_back(ndnsim);
78
79 int certificateIndex = 0;
80 function<void(const Name&,Zone&,const name::Component&)> addQueryRrset =
81 [this, &certificateIndex] (const Name& label, Zone& zone,
82 const name::Component& type) {
83 const time::seconds ttl(3000 + 100 * certificateIndex);
84 const name::Component version = name::Component::fromVersion(100 + 1000 * certificateIndex);
85 name::Component qType(label::NDNS_ITERATIVE_QUERY);
86 NdnsType ndnsType = NDNS_RESP;
87 if (type == label::CERT_RR_TYPE) {
88 ndnsType = NDNS_RAW;
89 qType = label::NDNS_CERT_QUERY;
90 }
91 std::ostringstream os;
92 os << "a fake content: " << (++certificateIndex) << "th";
93
94 addRrset(zone, label, type, ttl, version, qType, ndnsType, os.str());
95 };
96 addQueryRrset("/dsk-1", root, label::CERT_RR_TYPE);
97 addQueryRrset("/net/ksk-2", root, label::CERT_RR_TYPE);
98 addQueryRrset("/dsk-3", net, label::CERT_RR_TYPE);
99 addQueryRrset("/ndnsim/ksk-4", net, label::CERT_RR_TYPE);
100 addQueryRrset("/dsk-5", ndnsim, label::CERT_RR_TYPE);
101
102 addQueryRrset("net", root, label::NS_RR_TYPE);
103 addQueryRrset("ndnsim", net, label::NS_RR_TYPE);
104 addQueryRrset("www", ndnsim, label::TXT_RR_TYPE);
105 addQueryRrset("doc/www", ndnsim, label::TXT_RR_TYPE);
106
107
108 addRrset(ndnsim, Name("doc"), label::NS_RR_TYPE , time::seconds(2000),
109 name::Component("1234"), label::NDNS_ITERATIVE_QUERY, NDNS_AUTH, std::string(""));
110
111 NDNS_LOG_INFO("insert testing data: OK");
112}
113
114
115
116void
117DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type,
118 const time::seconds& ttl, const name::Component& version,
119 const name::Component& qType, NdnsType ndnsType, const std::string& msg)
120{
121 Rrset rrset(&zone);
122 rrset.setLabel(label);
123 rrset.setType(type);
124 rrset.setTtl(ttl);
125 rrset.setVersion(version);
126
127 Response re;
128 re.setZone(zone.getName());
129 re.setQueryType(qType);
130 re.setRrLabel(label);
131 re.setRrType(type);
132 re.setVersion(version);
133 re.setNdnsType(ndnsType);
134 re.setFreshnessPeriod(ttl);
135
136 if (msg.size() > 0) {
137 if (type == label::CERT_RR_TYPE)
138 re.setAppContent(dataBlock(ndn::tlv::Content, msg.c_str(), msg.size()));
139 else
140 re.addRr(msg);
141 }
142 shared_ptr<Data> data = re.toData();
143 m_keyChain.sign(*data, m_certName); // now we ignore the certificate to sign the data
144 shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName);
145 BOOST_CHECK_EQUAL(Validator::verifySignature(*data, cert->getPublicKeyInfo()), true);
146 rrset.setData(data->wireEncode());
147
148 m_session.insert(rrset);
149
150 m_rrsets.push_back(rrset);
151}
152
153DbTestData::~DbTestData()
154{
155 for (auto& zone : m_zones)
156 m_session.remove(zone);
157
158 for (auto& rrset : m_rrsets)
159 m_session.remove(rrset);
160
161 m_session.close();
162
163 boost::filesystem::remove(TEST_DATABASE);
164 boost::filesystem::remove(TEST_CERT);
165
166 if (doesTestIdentityExist) {
167 m_keyChain.deleteCertificate(m_certName);
168 m_keyChain.deleteKey(m_keyName);
169 NDNS_LOG_TRACE("delete key: " << m_keyName << " and certificate: " << m_certName);
170 }
171 else{
172 m_keyChain.deleteIdentity(TEST_IDENTITY_NAME);
173 }
174
175 NDNS_LOG_INFO("remove database: " << TEST_DATABASE);
176}
177
178} // namespace tests
179} // namespace ndns
180} // namespace ndn