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 | /* |
Alexander Afanasyev | 08d1874 | 2018-03-15 16:31:28 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2018, 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" |
| 22 | #include "util/cert-helper.hpp" |
| 23 | #include "daemon/name-server.hpp" |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 24 | |
| 25 | #include "test-common.hpp" |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 26 | #include "dummy-forwarder.hpp" |
| 27 | #include "unit/database-test-data.hpp" |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 28 | |
| 29 | #include <ndn-cxx/util/io.hpp> |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
| 32 | namespace ndns { |
| 33 | namespace tests { |
Alexander Afanasyev | c7c9900 | 2015-10-09 17:27:30 -0700 | [diff] [blame] | 34 | |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(Validator) |
| 36 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 37 | class ValidatorTestFixture : public DbTestData |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 38 | { |
| 39 | public: |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 40 | 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 Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 44 | { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 45 | // 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 Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 69 | ~ValidatorTestFixture() |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 70 | { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 71 | m_face.getIoService().stop(); |
| 72 | m_face.shutdown(); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 75 | public: |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 76 | 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 Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 85 | BOOST_FIXTURE_TEST_CASE(Basic, ValidatorTestFixture) |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 86 | { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 87 | SignatureInfo info; |
| 88 | info.setValidityPeriod(security::ValidityPeriod(time::system_clock::TimePoint::min(), |
| 89 | time::system_clock::now() + time::days(10))); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 90 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 91 | // case1: record of testId3, signed by its dsk, should be successful validated. |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 92 | Name dataName; |
| 93 | dataName |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 94 | .append(m_ndnsimName) |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 95 | .append("NDNS") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 96 | .append("rrLabel") |
| 97 | .append("rrType") |
| 98 | .appendVersion(); |
| 99 | shared_ptr<Data> data = make_shared<Data>(dataName); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 100 | m_keyChain.sign(*data, signingByCertificate(m_ndnsimCert).setSignatureInfo(info)); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 101 | |
| 102 | bool hasValidated = false; |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 103 | 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 Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 112 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 113 | advanceClocks(time::seconds(3), 100); |
| 114 | // m_io.run(); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(hasValidated, true); |
| 116 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 117 | // 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] | 118 | dataName = Name(); |
| 119 | dataName |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 120 | .append(m_netName) |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 121 | .append("NDNS") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 122 | .append("rrLabel") |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 123 | .append("CERT") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 124 | .appendVersion(); |
| 125 | data = make_shared<Data>(dataName); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 126 | 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] | 127 | |
| 128 | hasValidated = false; |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 129 | 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 Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 138 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 139 | advanceClocks(time::seconds(3), 100); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 140 | // cannot pass verification due to key's owner's name is longer than data owner's |
| 141 | BOOST_CHECK_EQUAL(hasValidated, true); |
| 142 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 143 | // case3: totally wrong key to sign |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 144 | dataName = Name(); |
| 145 | dataName |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 146 | .append(m_ndnsimName) |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 147 | .append("NDNS") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 148 | .append("rrLabel") |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 149 | .append("CERT") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 150 | .appendVersion(); |
| 151 | data = make_shared<Data>(dataName); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 152 | m_keyChain.sign(*data, signingByCertificate(m_randomCert)); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 153 | |
| 154 | hasValidated = false; |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 155 | 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 Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 164 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame] | 165 | advanceClocks(time::seconds(3), 100); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 166 | // cannot pass due to a totally mismatched key |
| 167 | BOOST_CHECK_EQUAL(hasValidated, true); |
| 168 | } |
| 169 | |
| 170 | BOOST_AUTO_TEST_SUITE_END() |
| 171 | |
| 172 | } // namespace tests |
| 173 | } // namespace ndns |
| 174 | } // namespace ndn |