blob: df43338e4cf5f7696433a99fdda7b6b0bf8f75a9 [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
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080035DbTestData::PreviousStateCleaner::PreviousStateCleaner()
36{
37 boost::filesystem::remove(TEST_DATABASE);
38 boost::filesystem::remove(TEST_CERT);
39}
40
Shock Jiang3016c982014-11-11 11:35:17 -080041DbTestData::DbTestData()
Shock Jiangcde28712014-10-19 21:17:20 -070042 : m_session(TEST_DATABASE.string())
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080043 , m_keyChain("sqlite3", "file")
Shock Jiang3016c982014-11-11 11:35:17 -080044{
45 NDNS_LOG_TRACE("start creating test data");
Shock Jiang3016c982014-11-11 11:35:17 -080046
47 ndns::Validator::VALIDATOR_CONF_FILE = TEST_CONFIG_PATH "/" "validator.conf";
48
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080049 m_keyChain.deleteIdentity(TEST_IDENTITY_NAME);
Shock Jiangcde28712014-10-19 21:17:20 -070050 m_certName = m_keyChain.createIdentity(TEST_IDENTITY_NAME);
Shock Jiang3016c982014-11-11 11:35:17 -080051
Shock Jiangcde28712014-10-19 21:17:20 -070052 ndn::io::save(*(m_keyChain.getCertificate(m_certName)), TEST_CERT.string());
53 NDNS_LOG_INFO("save test root cert " << m_certName << " to: " << TEST_CERT.string());
Shock Jiang3016c982014-11-11 11:35:17 -080054
55 BOOST_CHECK_GT(m_certName.size(), 0);
56 NDNS_LOG_TRACE("test certName: " << m_certName);
57
Shock Jiangcde28712014-10-19 21:17:20 -070058 m_root = Zone(TEST_IDENTITY_NAME);
59 Name name(TEST_IDENTITY_NAME);
60 name.append("net");
61 m_net = Zone(name);
62 name.append("ndnsim");
63 m_ndnsim =Zone(name);
Shock Jiang3016c982014-11-11 11:35:17 -080064
Shock Jiangcde28712014-10-19 21:17:20 -070065 m_session.insert(m_root);
66 BOOST_CHECK_GT(m_root.getId(), 0);
67 m_session.insert(m_net);
68 BOOST_CHECK_GT(m_net.getId(), 0);
69 m_session.insert(m_ndnsim);
70 BOOST_CHECK_GT(m_ndnsim.getId(), 0);
Shock Jiang3016c982014-11-11 11:35:17 -080071
Shock Jiangcde28712014-10-19 21:17:20 -070072 m_zones.push_back(m_root);
73 m_zones.push_back(m_net);
74 m_zones.push_back(m_ndnsim);
Shock Jiang3016c982014-11-11 11:35:17 -080075
76 int certificateIndex = 0;
77 function<void(const Name&,Zone&,const name::Component&)> addQueryRrset =
78 [this, &certificateIndex] (const Name& label, Zone& zone,
79 const name::Component& type) {
80 const time::seconds ttl(3000 + 100 * certificateIndex);
81 const name::Component version = name::Component::fromVersion(100 + 1000 * certificateIndex);
82 name::Component qType(label::NDNS_ITERATIVE_QUERY);
83 NdnsType ndnsType = NDNS_RESP;
84 if (type == label::CERT_RR_TYPE) {
85 ndnsType = NDNS_RAW;
86 qType = label::NDNS_CERT_QUERY;
87 }
88 std::ostringstream os;
89 os << "a fake content: " << (++certificateIndex) << "th";
90
91 addRrset(zone, label, type, ttl, version, qType, ndnsType, os.str());
92 };
Shock Jiang3016c982014-11-11 11:35:17 -080093
Shock Jiangcde28712014-10-19 21:17:20 -070094 addQueryRrset("/dsk-1", m_root, label::CERT_RR_TYPE);
95 addQueryRrset("/net/ksk-2", m_root, label::CERT_RR_TYPE);
96 addQueryRrset("/dsk-3", m_net, label::CERT_RR_TYPE);
97 addQueryRrset("/ndnsim/ksk-4", m_net, label::CERT_RR_TYPE);
98 addQueryRrset("/dsk-5", m_ndnsim, label::CERT_RR_TYPE);
99
100 addQueryRrset("net", m_root, label::NS_RR_TYPE);
101 addQueryRrset("ndnsim", m_net, label::NS_RR_TYPE);
102 addQueryRrset("www", m_ndnsim, label::TXT_RR_TYPE);
103 addQueryRrset("doc/www", m_ndnsim, label::TXT_RR_TYPE);
Shock Jiang3016c982014-11-11 11:35:17 -0800104
105
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
110 NDNS_LOG_INFO("insert testing data: OK");
111}
112
Shock Jiang3016c982014-11-11 11:35:17 -0800113void
114DbTestData::addRrset(Zone& zone, const Name& label, const name::Component& type,
115 const time::seconds& ttl, const name::Component& version,
116 const name::Component& qType, NdnsType ndnsType, const std::string& msg)
117{
118 Rrset rrset(&zone);
119 rrset.setLabel(label);
120 rrset.setType(type);
121 rrset.setTtl(ttl);
122 rrset.setVersion(version);
123
124 Response re;
125 re.setZone(zone.getName());
126 re.setQueryType(qType);
127 re.setRrLabel(label);
128 re.setRrType(type);
129 re.setVersion(version);
130 re.setNdnsType(ndnsType);
131 re.setFreshnessPeriod(ttl);
132
133 if (msg.size() > 0) {
134 if (type == label::CERT_RR_TYPE)
135 re.setAppContent(dataBlock(ndn::tlv::Content, msg.c_str(), msg.size()));
136 else
137 re.addRr(msg);
138 }
139 shared_ptr<Data> data = re.toData();
140 m_keyChain.sign(*data, m_certName); // now we ignore the certificate to sign the data
141 shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(m_certName);
142 BOOST_CHECK_EQUAL(Validator::verifySignature(*data, cert->getPublicKeyInfo()), true);
143 rrset.setData(data->wireEncode());
144
145 m_session.insert(rrset);
146
147 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