blob: 3434916c0f539e915941c308822b8941f64ac014 [file] [log] [blame]
Shock Jiang0b165f42014-10-24 09:08:09 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Yumin Xiaacd21332016-11-28 22:54:48 -08003 * Copyright (c) 2014-2017, Regents of the University of California.
Shock Jiang0b165f42014-10-24 09:08:09 -07004 *
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
Yumin Xia99c821a2017-04-07 11:01:08 -070020#include "validator/validator.hpp"
Yumin Xiafa2bce72017-04-09 16:20:25 -070021#include "ndns-label.hpp"
22#include "util/cert-helper.hpp"
23#include "daemon/name-server.hpp"
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080024
25#include "test-common.hpp"
Yumin Xiafa2bce72017-04-09 16:20:25 -070026#include "dummy-forwarder.hpp"
27#include "unit/database-test-data.hpp"
Yumin Xia2c509c22017-02-09 14:37:36 -080028
29#include <ndn-cxx/util/io.hpp>
Shock Jiang0b165f42014-10-24 09:08:09 -070030
31namespace ndn {
32namespace ndns {
33namespace tests {
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070034
35NDNS_LOG_INIT("ValidatorTest")
Shock Jiang0b165f42014-10-24 09:08:09 -070036
37BOOST_AUTO_TEST_SUITE(Validator)
38
Yumin Xiafa2bce72017-04-09 16:20:25 -070039class ValidatorTestFixture : public DbTestData
Shock Jiang0b165f42014-10-24 09:08:09 -070040{
41public:
Yumin Xiafa2bce72017-04-09 16:20:25 -070042 ValidatorTestFixture()
43 : m_forwarder(m_io, m_keyChain)
44 , m_face(m_forwarder.addFace())
45 , m_validator(NdnsValidatorBuilder::create(m_face, 500, 0, TEST_CONFIG_PATH "/" "validator.conf"))
Shock Jiang0b165f42014-10-24 09:08:09 -070046 {
Yumin Xiafa2bce72017-04-09 16:20:25 -070047 // generate a random cert
48 // check how does name-server test do
49 // initlize all servers
50 auto addServer = [&] (const Name& zoneName) {
51 Face& face = m_forwarder.addFace();
52 // validator is used only for check update signature
53 // no updates tested here, so validator will not be used
54 // passing m_validator is only for construct server
55 Name certName = CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain,
56 Name(zoneName).append("NDNS"));
57 auto server = make_shared<NameServer>(zoneName, certName, face,
58 m_session, m_keyChain, *m_validator);
59 m_servers.push_back(server);
60 };
61 addServer(m_testName);
62 addServer(m_netName);
63 addServer(m_ndnsimName);
64 m_ndnsimCert = CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain,
65 Name(m_ndnsimName).append("NDNS"));
66 m_randomCert = m_keyChain.createIdentity("/random/identity").getDefaultKey()
67 .getDefaultCertificate().getName();
68 advanceClocks(time::milliseconds(10), 1);
Shock Jiang0b165f42014-10-24 09:08:09 -070069 }
70
Yumin Xiafa2bce72017-04-09 16:20:25 -070071 ~ValidatorTestFixture()
Shock Jiang0b165f42014-10-24 09:08:09 -070072 {
Junxiao Shibbf7ac82016-07-14 14:45:15 +000073 m_face.getIoService().stop();
74 m_face.shutdown();
Shock Jiang0b165f42014-10-24 09:08:09 -070075 }
76
Shock Jiang0b165f42014-10-24 09:08:09 -070077public:
Yumin Xiafa2bce72017-04-09 16:20:25 -070078 DummyForwarder m_forwarder;
79 ndn::Face& m_face;
80 unique_ptr<security::v2::Validator> m_validator;
81 std::vector<shared_ptr<ndns::NameServer>> m_servers;
82 Name m_ndnsimCert;
83 Name m_randomCert;
Shock Jiang0b165f42014-10-24 09:08:09 -070084};
85
86
Yumin Xiafa2bce72017-04-09 16:20:25 -070087BOOST_FIXTURE_TEST_CASE(Basic, ValidatorTestFixture)
Shock Jiang0b165f42014-10-24 09:08:09 -070088{
Yumin Xiafa2bce72017-04-09 16:20:25 -070089 SignatureInfo info;
90 info.setValidityPeriod(security::ValidityPeriod(time::system_clock::TimePoint::min(),
91 time::system_clock::now() + time::days(10)));
Shock Jiang0b165f42014-10-24 09:08:09 -070092
Yumin Xia2c509c22017-02-09 14:37:36 -080093 // case1: record of testId3, signed by its dsk, should be successful validated.
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080094 Name dataName;
95 dataName
Yumin Xiafa2bce72017-04-09 16:20:25 -070096 .append(m_ndnsimName)
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080097 .append("NDNS")
Shock Jiang0b165f42014-10-24 09:08:09 -070098 .append("rrLabel")
99 .append("rrType")
100 .appendVersion();
101 shared_ptr<Data> data = make_shared<Data>(dataName);
Yumin Xiafa2bce72017-04-09 16:20:25 -0700102 m_keyChain.sign(*data, signingByCertificate(m_ndnsimCert).setSignatureInfo(info));
Shock Jiang0b165f42014-10-24 09:08:09 -0700103
104 bool hasValidated = false;
Yumin Xiafa2bce72017-04-09 16:20:25 -0700105 m_validator->validate(*data,
106 [&] (const Data& data) {
107 hasValidated = true;
108 BOOST_CHECK(true);
109 },
110 [&] (const Data& data, const security::v2::ValidationError& str) {
111 hasValidated = true;
112 BOOST_CHECK(false);
113 });
Shock Jiang0b165f42014-10-24 09:08:09 -0700114
Yumin Xiafa2bce72017-04-09 16:20:25 -0700115 advanceClocks(time::seconds(3), 100);
116 // m_io.run();
Shock Jiang0b165f42014-10-24 09:08:09 -0700117 BOOST_CHECK_EQUAL(hasValidated, true);
118
Yumin Xia2c509c22017-02-09 14:37:36 -0800119 // case2: signing testId2's data by testId3's key, which should failed in validation
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800120 dataName = Name();
121 dataName
Yumin Xiafa2bce72017-04-09 16:20:25 -0700122 .append(m_netName)
Yumin Xia2c509c22017-02-09 14:37:36 -0800123 .append("NDNS")
Shock Jiang0b165f42014-10-24 09:08:09 -0700124 .append("rrLabel")
Yumin Xia2c509c22017-02-09 14:37:36 -0800125 .append("CERT")
Shock Jiang0b165f42014-10-24 09:08:09 -0700126 .appendVersion();
127 data = make_shared<Data>(dataName);
Yumin Xiafa2bce72017-04-09 16:20:25 -0700128 m_keyChain.sign(*data, signingByCertificate(m_ndnsimCert)); // key's owner's name is longer than data owner's
Shock Jiang0b165f42014-10-24 09:08:09 -0700129
130 hasValidated = false;
Yumin Xiafa2bce72017-04-09 16:20:25 -0700131 m_validator->validate(*data,
132 [&] (const Data& data) {
133 hasValidated = true;
134 BOOST_CHECK(false);
135 },
136 [&] (const Data& data, const security::v2::ValidationError& str) {
137 hasValidated = true;
138 BOOST_CHECK(true);
139 });
Shock Jiang0b165f42014-10-24 09:08:09 -0700140
Yumin Xiafa2bce72017-04-09 16:20:25 -0700141 advanceClocks(time::seconds(3), 100);
Shock Jiang0b165f42014-10-24 09:08:09 -0700142 // cannot pass verification due to key's owner's name is longer than data owner's
143 BOOST_CHECK_EQUAL(hasValidated, true);
144
Yumin Xiafa2bce72017-04-09 16:20:25 -0700145 // case3: totally wrong key to sign
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800146 dataName = Name();
147 dataName
Yumin Xiafa2bce72017-04-09 16:20:25 -0700148 .append(m_ndnsimName)
Yumin Xia918343d2017-03-17 19:04:55 -0700149 .append("NDNS")
Shock Jiang0b165f42014-10-24 09:08:09 -0700150 .append("rrLabel")
Yumin Xia2c509c22017-02-09 14:37:36 -0800151 .append("CERT")
Shock Jiang0b165f42014-10-24 09:08:09 -0700152 .appendVersion();
153 data = make_shared<Data>(dataName);
Yumin Xiafa2bce72017-04-09 16:20:25 -0700154 m_keyChain.sign(*data, signingByCertificate(m_randomCert));
Shock Jiang0b165f42014-10-24 09:08:09 -0700155
156 hasValidated = false;
Yumin Xiafa2bce72017-04-09 16:20:25 -0700157 m_validator->validate(*data,
158 [&] (const Data& data) {
159 hasValidated = true;
160 BOOST_CHECK(false);
161 },
162 [&] (const Data& data, const security::v2::ValidationError& str) {
163 hasValidated = true;
164 BOOST_CHECK(true);
165 });
Shock Jiang0b165f42014-10-24 09:08:09 -0700166
Yumin Xiafa2bce72017-04-09 16:20:25 -0700167 advanceClocks(time::seconds(3), 100);
Shock Jiang0b165f42014-10-24 09:08:09 -0700168 // cannot pass due to a totally mismatched key
169 BOOST_CHECK_EQUAL(hasValidated, true);
170}
171
172BOOST_AUTO_TEST_SUITE_END()
173
174} // namespace tests
175} // namespace ndns
176} // namespace ndn