Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Davide Pesavento | 7702c0f | 2023-08-10 20:40:03 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California. |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 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 | |
Yumin Xia | 99c821a | 2017-04-07 11:01:08 -0700 | [diff] [blame] | 20 | #include "validator/validator.hpp" |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 21 | #include "ndns-label.hpp" |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 22 | #include "daemon/name-server.hpp" |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 23 | #include "util/cert-helper.hpp" |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 24 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 25 | #include "boost-test.hpp" |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 26 | #include "unit/database-test-data.hpp" |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 27 | |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 28 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 29 | |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace ndns { |
| 32 | namespace tests { |
Alexander Afanasyev | c7c9900 | 2015-10-09 17:27:30 -0700 | [diff] [blame] | 33 | |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Validator) |
| 35 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 36 | class ValidatorTestFixture : public DbTestData |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 37 | { |
| 38 | public: |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 39 | ValidatorTestFixture() |
Davide Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 40 | : m_validatorFace(m_io, m_keyChain, {true, true}) |
| 41 | , m_validator(NdnsValidatorBuilder::create(m_validatorFace, 500, 0, |
Davide Pesavento | bdd88c1 | 2020-11-26 00:35:08 -0500 | [diff] [blame] | 42 | UNIT_TESTS_TMPDIR "/validator.conf")) |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 43 | { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 44 | // generate a random cert |
| 45 | // check how does name-server test do |
Davide Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 46 | // initialize all servers |
| 47 | auto addServer = [this] (const Name& zoneName) { |
Davide Pesavento | 7702c0f | 2023-08-10 20:40:03 -0400 | [diff] [blame] | 48 | m_serverFaces.push_back(make_unique<DummyClientFace>(m_io, m_keyChain, |
| 49 | DummyClientFace::Options{true, true})); |
Davide Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 50 | m_serverFaces.back()->linkTo(m_validatorFace); |
| 51 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 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")); |
Davide Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 57 | auto server = make_shared<NameServer>(zoneName, certName, *m_serverFaces.back(), |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 58 | m_session, m_keyChain, *m_validator); |
Davide Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 59 | m_servers.push_back(std::move(server)); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 60 | }; |
| 61 | addServer(m_testName); |
| 62 | addServer(m_netName); |
| 63 | addServer(m_ndnsimName); |
Davide Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 64 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 65 | m_ndnsimCert = CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain, |
Davide Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 66 | Name(m_ndnsimName).append("NDNS")); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 67 | m_randomCert = m_keyChain.createIdentity("/random/identity").getDefaultKey() |
Davide Pesavento | 2a3bb84 | 2019-03-22 17:39:29 -0400 | [diff] [blame] | 68 | .getDefaultCertificate().getName(); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 69 | advanceClocks(time::milliseconds(10), 1); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 72 | public: |
Davide Pesavento | 7702c0f | 2023-08-10 20:40:03 -0400 | [diff] [blame] | 73 | DummyClientFace m_validatorFace; |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 74 | unique_ptr<security::Validator> m_validator; |
Davide Pesavento | 7702c0f | 2023-08-10 20:40:03 -0400 | [diff] [blame] | 75 | std::vector<unique_ptr<DummyClientFace>> m_serverFaces; |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 76 | std::vector<shared_ptr<ndns::NameServer>> m_servers; |
| 77 | Name m_ndnsimCert; |
| 78 | Name m_randomCert; |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 81 | BOOST_FIXTURE_TEST_CASE(Basic, ValidatorTestFixture) |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 82 | { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 83 | SignatureInfo info; |
Davide Pesavento | 7702c0f | 2023-08-10 20:40:03 -0400 | [diff] [blame] | 84 | info.setValidityPeriod(security::ValidityPeriod(time::system_clock::time_point::min(), |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 85 | time::system_clock::now() + time::days(10))); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 86 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 87 | // case1: record of testId3, signed by its dsk, should be successful validated. |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 88 | Name dataName; |
| 89 | dataName |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 90 | .append(m_ndnsimName) |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 91 | .append("NDNS") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 92 | .append("rrLabel") |
| 93 | .append("rrType") |
| 94 | .appendVersion(); |
| 95 | shared_ptr<Data> data = make_shared<Data>(dataName); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 96 | m_keyChain.sign(*data, signingByCertificate(m_ndnsimCert).setSignatureInfo(info)); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 97 | |
| 98 | bool hasValidated = false; |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 99 | m_validator->validate(*data, |
| 100 | [&] (const Data& data) { |
| 101 | hasValidated = true; |
| 102 | BOOST_CHECK(true); |
| 103 | }, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 104 | [&] (const Data& data, const security::ValidationError& str) { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 105 | hasValidated = true; |
| 106 | BOOST_CHECK(false); |
| 107 | }); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 108 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 109 | advanceClocks(time::seconds(3), 100); |
| 110 | // m_io.run(); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 111 | BOOST_CHECK_EQUAL(hasValidated, true); |
| 112 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 113 | // case2: signing testId2's data by testId3's key, which should failed in validation |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 114 | dataName = Name(); |
| 115 | dataName |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 116 | .append(m_netName) |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 117 | .append("NDNS") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 118 | .append("rrLabel") |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 119 | .append("CERT") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 120 | .appendVersion(); |
| 121 | data = make_shared<Data>(dataName); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 122 | m_keyChain.sign(*data, signingByCertificate(m_ndnsimCert)); // key's owner's name is longer than data owner's |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 123 | |
| 124 | hasValidated = false; |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 125 | m_validator->validate(*data, |
| 126 | [&] (const Data& data) { |
| 127 | hasValidated = true; |
| 128 | BOOST_CHECK(false); |
| 129 | }, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 130 | [&] (const Data& data, const security::ValidationError& str) { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 131 | hasValidated = true; |
| 132 | BOOST_CHECK(true); |
| 133 | }); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 134 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 135 | advanceClocks(time::seconds(3), 100); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 136 | // cannot pass verification due to key's owner's name is longer than data owner's |
| 137 | BOOST_CHECK_EQUAL(hasValidated, true); |
| 138 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 139 | // case3: totally wrong key to sign |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 140 | dataName = Name(); |
| 141 | dataName |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 142 | .append(m_ndnsimName) |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 143 | .append("NDNS") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 144 | .append("rrLabel") |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 145 | .append("CERT") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 146 | .appendVersion(); |
| 147 | data = make_shared<Data>(dataName); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 148 | m_keyChain.sign(*data, signingByCertificate(m_randomCert)); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 149 | |
| 150 | hasValidated = false; |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 151 | m_validator->validate(*data, |
| 152 | [&] (const Data& data) { |
| 153 | hasValidated = true; |
| 154 | BOOST_CHECK(false); |
| 155 | }, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 156 | [&] (const Data& data, const security::ValidationError& str) { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 157 | hasValidated = true; |
| 158 | BOOST_CHECK(true); |
| 159 | }); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 160 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 161 | advanceClocks(time::seconds(3), 100); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 162 | // cannot pass due to a totally mismatched key |
| 163 | BOOST_CHECK_EQUAL(hasValidated, true); |
| 164 | } |
| 165 | |
| 166 | BOOST_AUTO_TEST_SUITE_END() |
| 167 | |
| 168 | } // namespace tests |
| 169 | } // namespace ndns |
| 170 | } // namespace ndn |