blob: 9364811f131f518dd8fc94fd10de3132ca1e460d [file] [log] [blame]
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * @author Yingdi Yu <yingdi@cs.ucla.edu>
21 *
22 **/
23
24#include "validator.hpp"
25#include <ndn-cxx/util/scheduler.hpp>
26#include <ndn-cxx/security/key-chain.hpp>
27#include <ndn-cxx/security/certificate-cache-ttl.hpp>
28#include "boost-test.hpp"
Vince Lehman0a7da612014-10-29 14:39:29 -050029#include "common.hpp"
Yingdi Yu6a3a4dd2014-06-20 14:10:39 -070030
31namespace nlsr {
32
33namespace test {
34
35BOOST_AUTO_TEST_SUITE(TestValidator)
36
37struct ValidatorFixture
38{
39 ValidatorFixture()
40 : m_face2(m_face.getIoService())
41 , m_scheduler(m_face.getIoService())
42 , m_certificateCache(new ndn::CertificateCacheTtl(m_face.getIoService()))
43 , m_validator(m_face2, ndn::Name("/ndn/broadcast"), m_certificateCache)
44 , m_identity("/TestValidator/NLSR")
45 {
46 ndn::Name keyPrefix("/ndn/broadcast/KEYS");
47 m_face.setInterestFilter(keyPrefix,
48 ndn::bind(&ValidatorFixture::onKeyInterest, this, _1, _2),
49 ndn::bind(&ValidatorFixture::onKeyPrefixRegSuccess, this, _1),
50 ndn::bind(&ValidatorFixture::registrationFailed, this, _1, _2));
51
52 m_keyChain.createIdentity(m_identity);
53 ndn::Name certName = m_keyChain.getDefaultCertificateNameForIdentity(m_identity);
54 m_cert = m_keyChain.getCertificate(certName);
55 ndn::io::save(*m_cert, "trust-anchor.cert");
56
57 const std::string CONFIG =
58 "rule\n"
59 "{\n"
60 " id \"NSLR Hello Rule\"\n"
61 " for data\n"
62 " filter\n"
63 " {\n"
64 " type name\n"
65 " regex ^[^<NLSR><INFO>]*<NLSR><INFO><><>$\n"
66 " }\n"
67 " checker\n"
68 " {\n"
69 " type customized\n"
70 " sig-type rsa-sha256\n"
71 " key-locator\n"
72 " {\n"
73 " type name\n"
74 " hyper-relation\n"
75 " {\n"
76 " k-regex ^([^<KEY><NLSR>]*)<NLSR><KEY><ksk-.*><ID-CERT>$\n"
77 " k-expand \\\\1\n"
78 " h-relation equal\n"
79 " p-regex ^([^<NLSR><INFO>]*)<NLSR><INFO><><>$\n"
80 " p-expand \\\\1\n"
81 " }\n"
82 " }\n"
83 " }\n"
84 "}\n"
85 "rule\n"
86 "{\n"
87 " id \"Single Rule\"\n"
88 " for data\n"
89 " filter\n"
90 " {\n"
91 " type name\n"
92 " regex ^<TestValidator><NLSR><KEY><ksk-.*><><>$\n"
93 " }\n"
94 " checker\n"
95 " {\n"
96 " type fixed-signer\n"
97 " sig-type rsa-sha256\n"
98 " signer\n"
99 " {\n"
100 " type file\n"
101 " file-name \"trust-anchor.cert\"\n"
102 " }\n"
103 " }\n"
104 "}\n";
105
106 const boost::filesystem::path CONFIG_PATH =
107 (boost::filesystem::current_path() / std::string("unit-test.conf"));
108
109 m_validator.load(CONFIG, CONFIG_PATH.native());
110 }
111
112 ~ValidatorFixture()
113 {
114 m_keyChain.deleteIdentity(m_identity);
115
116 const boost::filesystem::path CERT_PATH =
117 (boost::filesystem::current_path() / std::string("trust-anchor.cert"));
118 boost::filesystem::remove(CERT_PATH);
119 }
120
121 void
122 onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
123 {
124 const ndn::Name& interestName = interest.getName();
125
126 ndn::Name certName = interestName.getSubName(name.size());
127
128 if (certName[-2].toUri() == "ID-CERT")
129 {
130 certName = certName.getPrefix(-1);
131 }
132 else if (certName[-1].toUri() != "ID-CERT")
133 return; //Wrong key interest.
134
135 if (certName != m_cert->getName().getPrefix(-1))
136 return; //No such a cert
137
138 ndn::Data data(interestName);
139 data.setContent(m_cert->wireEncode());
140 m_keyChain.signWithSha256(data);
141
142 m_face.put(data);
143 }
144
145 void
146 onKeyPrefixRegSuccess(const ndn::Name& name)
147 {
148 BOOST_REQUIRE(true);
149 }
150
151 void
152 registrationFailed(const ndn::Name& name, const std::string& msg)
153 {
154 std::cerr << "Failure Info: " << msg << std::endl;
155 BOOST_REQUIRE(false);
156 }
157
158 void
159 onValidated(const ndn::shared_ptr<const ndn::Data>& data)
160 {
161 BOOST_CHECK(true);
162 }
163
164 void
165 onValidationFailed(const ndn::shared_ptr<const ndn::Data>& data,
166 const std::string& failureInfo)
167 {
168 std::cerr << "Failure Info: " << failureInfo << std::endl;
169 BOOST_CHECK(false);
170 }
171
172 void
173 validate(const ndn::shared_ptr<const ndn::Data>& data)
174 {
175 m_validator.validate(*data,
176 bind(&ValidatorFixture::onValidated, this, _1),
177 bind(&ValidatorFixture::onValidationFailed, this, _1, _2));
178 }
179
180 void
181 terminate()
182 {
183 m_face.getIoService().stop();
184 }
185
186protected:
187 ndn::Face m_face;
188 ndn::Face m_face2;
189 ndn::Scheduler m_scheduler;
190 ndn::shared_ptr<ndn::CertificateCacheTtl> m_certificateCache;
191 nlsr::Validator m_validator;
192
193 ndn::KeyChain m_keyChain;
194 ndn::Name m_identity;
195 ndn::shared_ptr<ndn::IdentityCertificate> m_cert;
196};
197
198BOOST_FIXTURE_TEST_CASE(InfoCertFetch, ValidatorFixture)
199{
200 ndn::Name dataName = m_identity;
201 dataName.append("INFO").append("neighbor").append("version");
202 ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>(dataName);
203 m_keyChain.signByIdentity(*data, m_identity);
204
205 m_scheduler.scheduleEvent(ndn::time::milliseconds(200),
206 ndn::bind(&ValidatorFixture::validate, this, data));
207 m_scheduler.scheduleEvent(ndn::time::milliseconds(1000),
208 ndn::bind(&ValidatorFixture::terminate, this));
209 BOOST_REQUIRE_NO_THROW(m_face.processEvents());
210}
211
212BOOST_AUTO_TEST_SUITE_END()
213
214} // namespace test
215} // namespace nlsr