blob: 21f6d27d6eca55cd8e28c50c391f2ed3c6e22426 [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/*
Alexander Afanasyev08d18742018-03-15 16:31:28 -04003 * Copyright (c) 2014-2018, 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
Shock Jiang0b165f42014-10-24 09:08:09 -070035BOOST_AUTO_TEST_SUITE(Validator)
36
Yumin Xiafa2bce72017-04-09 16:20:25 -070037class ValidatorTestFixture : public DbTestData
Shock Jiang0b165f42014-10-24 09:08:09 -070038{
39public:
Yumin Xiafa2bce72017-04-09 16:20:25 -070040 ValidatorTestFixture()
41 : m_forwarder(m_io, m_keyChain)
42 , m_face(m_forwarder.addFace())
43 , m_validator(NdnsValidatorBuilder::create(m_face, 500, 0, TEST_CONFIG_PATH "/" "validator.conf"))
Shock Jiang0b165f42014-10-24 09:08:09 -070044 {
Yumin Xiafa2bce72017-04-09 16:20:25 -070045 // generate a random cert
46 // check how does name-server test do
47 // initlize all servers
48 auto addServer = [&] (const Name& zoneName) {
49 Face& face = m_forwarder.addFace();
50 // validator is used only for check update signature
51 // no updates tested here, so validator will not be used
52 // passing m_validator is only for construct server
53 Name certName = CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain,
54 Name(zoneName).append("NDNS"));
55 auto server = make_shared<NameServer>(zoneName, certName, face,
56 m_session, m_keyChain, *m_validator);
57 m_servers.push_back(server);
58 };
59 addServer(m_testName);
60 addServer(m_netName);
61 addServer(m_ndnsimName);
62 m_ndnsimCert = CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain,
63 Name(m_ndnsimName).append("NDNS"));
64 m_randomCert = m_keyChain.createIdentity("/random/identity").getDefaultKey()
65 .getDefaultCertificate().getName();
66 advanceClocks(time::milliseconds(10), 1);
Shock Jiang0b165f42014-10-24 09:08:09 -070067 }
68
Yumin Xiafa2bce72017-04-09 16:20:25 -070069 ~ValidatorTestFixture()
Shock Jiang0b165f42014-10-24 09:08:09 -070070 {
Junxiao Shibbf7ac82016-07-14 14:45:15 +000071 m_face.getIoService().stop();
72 m_face.shutdown();
Shock Jiang0b165f42014-10-24 09:08:09 -070073 }
74
Shock Jiang0b165f42014-10-24 09:08:09 -070075public:
Yumin Xiafa2bce72017-04-09 16:20:25 -070076 DummyForwarder m_forwarder;
77 ndn::Face& m_face;
78 unique_ptr<security::v2::Validator> m_validator;
79 std::vector<shared_ptr<ndns::NameServer>> m_servers;
80 Name m_ndnsimCert;
81 Name m_randomCert;
Shock Jiang0b165f42014-10-24 09:08:09 -070082};
83
84
Yumin Xiafa2bce72017-04-09 16:20:25 -070085BOOST_FIXTURE_TEST_CASE(Basic, ValidatorTestFixture)
Shock Jiang0b165f42014-10-24 09:08:09 -070086{
Yumin Xiafa2bce72017-04-09 16:20:25 -070087 SignatureInfo info;
88 info.setValidityPeriod(security::ValidityPeriod(time::system_clock::TimePoint::min(),
89 time::system_clock::now() + time::days(10)));
Shock Jiang0b165f42014-10-24 09:08:09 -070090
Yumin Xia2c509c22017-02-09 14:37:36 -080091 // case1: record of testId3, signed by its dsk, should be successful validated.
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080092 Name dataName;
93 dataName
Yumin Xiafa2bce72017-04-09 16:20:25 -070094 .append(m_ndnsimName)
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080095 .append("NDNS")
Shock Jiang0b165f42014-10-24 09:08:09 -070096 .append("rrLabel")
97 .append("rrType")
98 .appendVersion();
99 shared_ptr<Data> data = make_shared<Data>(dataName);
Yumin Xiafa2bce72017-04-09 16:20:25 -0700100 m_keyChain.sign(*data, signingByCertificate(m_ndnsimCert).setSignatureInfo(info));
Shock Jiang0b165f42014-10-24 09:08:09 -0700101
102 bool hasValidated = false;
Yumin Xiafa2bce72017-04-09 16:20:25 -0700103 m_validator->validate(*data,
104 [&] (const Data& data) {
105 hasValidated = true;
106 BOOST_CHECK(true);
107 },
108 [&] (const Data& data, const security::v2::ValidationError& str) {
109 hasValidated = true;
110 BOOST_CHECK(false);
111 });
Shock Jiang0b165f42014-10-24 09:08:09 -0700112
Yumin Xiafa2bce72017-04-09 16:20:25 -0700113 advanceClocks(time::seconds(3), 100);
114 // m_io.run();
Shock Jiang0b165f42014-10-24 09:08:09 -0700115 BOOST_CHECK_EQUAL(hasValidated, true);
116
Yumin Xia2c509c22017-02-09 14:37:36 -0800117 // case2: signing testId2's data by testId3's key, which should failed in validation
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800118 dataName = Name();
119 dataName
Yumin Xiafa2bce72017-04-09 16:20:25 -0700120 .append(m_netName)
Yumin Xia2c509c22017-02-09 14:37:36 -0800121 .append("NDNS")
Shock Jiang0b165f42014-10-24 09:08:09 -0700122 .append("rrLabel")
Yumin Xia2c509c22017-02-09 14:37:36 -0800123 .append("CERT")
Shock Jiang0b165f42014-10-24 09:08:09 -0700124 .appendVersion();
125 data = make_shared<Data>(dataName);
Yumin Xiafa2bce72017-04-09 16:20:25 -0700126 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 -0700127
128 hasValidated = false;
Yumin Xiafa2bce72017-04-09 16:20:25 -0700129 m_validator->validate(*data,
130 [&] (const Data& data) {
131 hasValidated = true;
132 BOOST_CHECK(false);
133 },
134 [&] (const Data& data, const security::v2::ValidationError& str) {
135 hasValidated = true;
136 BOOST_CHECK(true);
137 });
Shock Jiang0b165f42014-10-24 09:08:09 -0700138
Yumin Xiafa2bce72017-04-09 16:20:25 -0700139 advanceClocks(time::seconds(3), 100);
Shock Jiang0b165f42014-10-24 09:08:09 -0700140 // cannot pass verification due to key's owner's name is longer than data owner's
141 BOOST_CHECK_EQUAL(hasValidated, true);
142
Yumin Xiafa2bce72017-04-09 16:20:25 -0700143 // case3: totally wrong key to sign
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800144 dataName = Name();
145 dataName
Yumin Xiafa2bce72017-04-09 16:20:25 -0700146 .append(m_ndnsimName)
Yumin Xia918343d2017-03-17 19:04:55 -0700147 .append("NDNS")
Shock Jiang0b165f42014-10-24 09:08:09 -0700148 .append("rrLabel")
Yumin Xia2c509c22017-02-09 14:37:36 -0800149 .append("CERT")
Shock Jiang0b165f42014-10-24 09:08:09 -0700150 .appendVersion();
151 data = make_shared<Data>(dataName);
Yumin Xiafa2bce72017-04-09 16:20:25 -0700152 m_keyChain.sign(*data, signingByCertificate(m_randomCert));
Shock Jiang0b165f42014-10-24 09:08:09 -0700153
154 hasValidated = false;
Yumin Xiafa2bce72017-04-09 16:20:25 -0700155 m_validator->validate(*data,
156 [&] (const Data& data) {
157 hasValidated = true;
158 BOOST_CHECK(false);
159 },
160 [&] (const Data& data, const security::v2::ValidationError& str) {
161 hasValidated = true;
162 BOOST_CHECK(true);
163 });
Shock Jiang0b165f42014-10-24 09:08:09 -0700164
Yumin Xiafa2bce72017-04-09 16:20:25 -0700165 advanceClocks(time::seconds(3), 100);
Shock Jiang0b165f42014-10-24 09:08:09 -0700166 // cannot pass due to a totally mismatched key
167 BOOST_CHECK_EQUAL(hasValidated, true);
168}
169
170BOOST_AUTO_TEST_SUITE_END()
171
172} // namespace tests
173} // namespace ndns
174} // namespace ndn