blob: bee2cd92faef8b61f608ff4387f44cb7dd20fb90 [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";
Shock Jiangcde28712014-10-19 21:17:20 -070031const Name DbTestData::TEST_IDENTITY_NAME("/test19");
Shock Jiang3016c982014-11-11 11:35:17 -080032const boost::filesystem::path DbTestData::TEST_CERT =
33 TEST_CONFIG_PATH "/" "anchors/root.cert";
34
35DbTestData::DbTestData()
Shock Jiangcde28712014-10-19 21:17:20 -070036 : m_session(TEST_DATABASE.string())
Shock Jiang3016c982014-11-11 11:35:17 -080037{
38 NDNS_LOG_TRACE("start creating test data");
Shock Jiang3016c982014-11-11 11:35:17 -080039
40 ndns::Validator::VALIDATOR_CONF_FILE = TEST_CONFIG_PATH "/" "validator.conf";
41
Shock Jiangcde28712014-10-19 21:17:20 -070042 m_certName = m_keyChain.createIdentity(TEST_IDENTITY_NAME);
Shock Jiang3016c982014-11-11 11:35:17 -080043
Shock Jiangcde28712014-10-19 21:17:20 -070044 ndn::io::save(*(m_keyChain.getCertificate(m_certName)), TEST_CERT.string());
45 NDNS_LOG_INFO("save test root cert " << m_certName << " to: " << TEST_CERT.string());
Shock Jiang3016c982014-11-11 11:35:17 -080046
47 BOOST_CHECK_GT(m_certName.size(), 0);
48 NDNS_LOG_TRACE("test certName: " << m_certName);
49
Shock Jiangcde28712014-10-19 21:17:20 -070050 m_root = Zone(TEST_IDENTITY_NAME);
51 Name name(TEST_IDENTITY_NAME);
52 name.append("net");
53 m_net = Zone(name);
54 name.append("ndnsim");
55 m_ndnsim =Zone(name);
Shock Jiang3016c982014-11-11 11:35:17 -080056
Shock Jiangcde28712014-10-19 21:17:20 -070057 m_session.insert(m_root);
58 BOOST_CHECK_GT(m_root.getId(), 0);
59 m_session.insert(m_net);
60 BOOST_CHECK_GT(m_net.getId(), 0);
61 m_session.insert(m_ndnsim);
62 BOOST_CHECK_GT(m_ndnsim.getId(), 0);
Shock Jiang3016c982014-11-11 11:35:17 -080063
Shock Jiangcde28712014-10-19 21:17:20 -070064 m_zones.push_back(m_root);
65 m_zones.push_back(m_net);
66 m_zones.push_back(m_ndnsim);
Shock Jiang3016c982014-11-11 11:35:17 -080067
68 int certificateIndex = 0;
69 function<void(const Name&,Zone&,const name::Component&)> addQueryRrset =
70 [this, &certificateIndex] (const Name& label, Zone& zone,
71 const name::Component& type) {
72 const time::seconds ttl(3000 + 100 * certificateIndex);
73 const name::Component version = name::Component::fromVersion(100 + 1000 * certificateIndex);
74 name::Component qType(label::NDNS_ITERATIVE_QUERY);
75 NdnsType ndnsType = NDNS_RESP;
76 if (type == label::CERT_RR_TYPE) {
77 ndnsType = NDNS_RAW;
78 qType = label::NDNS_CERT_QUERY;
79 }
80 std::ostringstream os;
81 os << "a fake content: " << (++certificateIndex) << "th";
82
83 addRrset(zone, label, type, ttl, version, qType, ndnsType, os.str());
84 };
Shock Jiang3016c982014-11-11 11:35:17 -080085
Shock Jiangcde28712014-10-19 21:17:20 -070086 addQueryRrset("/dsk-1", m_root, label::CERT_RR_TYPE);
87 addQueryRrset("/net/ksk-2", m_root, label::CERT_RR_TYPE);
88 addQueryRrset("/dsk-3", m_net, label::CERT_RR_TYPE);
89 addQueryRrset("/ndnsim/ksk-4", m_net, label::CERT_RR_TYPE);
90 addQueryRrset("/dsk-5", m_ndnsim, label::CERT_RR_TYPE);
91
92 addQueryRrset("net", m_root, label::NS_RR_TYPE);
93 addQueryRrset("ndnsim", m_net, label::NS_RR_TYPE);
94 addQueryRrset("www", m_ndnsim, label::TXT_RR_TYPE);
95 addQueryRrset("doc/www", m_ndnsim, label::TXT_RR_TYPE);
Shock Jiang3016c982014-11-11 11:35:17 -080096
97
Shock Jiangcde28712014-10-19 21:17:20 -070098 addRrset(m_ndnsim, Name("doc"), label::NS_RR_TYPE , time::seconds(2000),
99 name::Component::fromVersion(1234), label::NDNS_ITERATIVE_QUERY, NDNS_AUTH,
100 std::string(""));
Shock Jiang3016c982014-11-11 11:35:17 -0800101
102 NDNS_LOG_INFO("insert testing data: OK");
103}
104
Shock Jiang3016c982014-11-11 11:35:17 -0800105void
106DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type,
107 const time::seconds& ttl, const name::Component& version,
108 const name::Component& qType, NdnsType ndnsType, const std::string& msg)
109{
110 Rrset rrset(&zone);
111 rrset.setLabel(label);
112 rrset.setType(type);
113 rrset.setTtl(ttl);
114 rrset.setVersion(version);
115
116 Response re;
117 re.setZone(zone.getName());
118 re.setQueryType(qType);
119 re.setRrLabel(label);
120 re.setRrType(type);
121 re.setVersion(version);
122 re.setNdnsType(ndnsType);
123 re.setFreshnessPeriod(ttl);
124
125 if (msg.size() > 0) {
126 if (type == label::CERT_RR_TYPE)
127 re.setAppContent(dataBlock(ndn::tlv::Content, msg.c_str(), msg.size()));
128 else
129 re.addRr(msg);
130 }
131 shared_ptr<Data> data = re.toData();
132 m_keyChain.sign(*data, m_certName); // now we ignore the certificate to sign the data
133 shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName);
134 BOOST_CHECK_EQUAL(Validator::verifySignature(*data, cert->getPublicKeyInfo()), true);
135 rrset.setData(data->wireEncode());
136
137 m_session.insert(rrset);
138
139 m_rrsets.push_back(rrset);
140}
141
142DbTestData::~DbTestData()
143{
144 for (auto& zone : m_zones)
145 m_session.remove(zone);
146
147 for (auto& rrset : m_rrsets)
148 m_session.remove(rrset);
149
150 m_session.close();
151
152 boost::filesystem::remove(TEST_DATABASE);
153 boost::filesystem::remove(TEST_CERT);
154
Shock Jiangcde28712014-10-19 21:17:20 -0700155 // m_keyChain.deleteIdentity(TEST_IDENTITY_NAME);
Shock Jiang3016c982014-11-11 11:35:17 -0800156
157 NDNS_LOG_INFO("remove database: " << TEST_DATABASE);
158}
159
160} // namespace tests
161} // namespace ndns
162} // namespace ndn