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 | /* |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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 | |
| 35 | NDNS_LOG_INIT("ValidatorTest") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(Validator) |
| 38 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 39 | class ValidatorTestFixture : public DbTestData |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 40 | { |
| 41 | public: |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 42 | 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 Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 46 | { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 47 | // 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 Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 71 | ~ValidatorTestFixture() |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 72 | { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 73 | m_face.getIoService().stop(); |
| 74 | m_face.shutdown(); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 77 | public: |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 78 | 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 Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 87 | BOOST_FIXTURE_TEST_CASE(Basic, ValidatorTestFixture) |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 88 | { |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 89 | SignatureInfo info; |
| 90 | info.setValidityPeriod(security::ValidityPeriod(time::system_clock::TimePoint::min(), |
| 91 | time::system_clock::now() + time::days(10))); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 92 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 93 | // case1: record of testId3, signed by its dsk, should be successful validated. |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 94 | Name dataName; |
| 95 | dataName |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 96 | .append(m_ndnsimName) |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 97 | .append("NDNS") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 98 | .append("rrLabel") |
| 99 | .append("rrType") |
| 100 | .appendVersion(); |
| 101 | shared_ptr<Data> data = make_shared<Data>(dataName); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 102 | m_keyChain.sign(*data, signingByCertificate(m_ndnsimCert).setSignatureInfo(info)); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 103 | |
| 104 | bool hasValidated = false; |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 105 | 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 Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 114 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 115 | advanceClocks(time::seconds(3), 100); |
| 116 | // m_io.run(); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(hasValidated, true); |
| 118 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 119 | // 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] | 120 | dataName = Name(); |
| 121 | dataName |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 122 | .append(m_netName) |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 123 | .append("NDNS") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 124 | .append("rrLabel") |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 125 | .append("CERT") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 126 | .appendVersion(); |
| 127 | data = make_shared<Data>(dataName); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 128 | 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] | 129 | |
| 130 | hasValidated = false; |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 131 | 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 Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 140 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 141 | advanceClocks(time::seconds(3), 100); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 142 | // cannot pass verification due to key's owner's name is longer than data owner's |
| 143 | BOOST_CHECK_EQUAL(hasValidated, true); |
| 144 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 145 | // case3: totally wrong key to sign |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 146 | dataName = Name(); |
| 147 | dataName |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 148 | .append(m_ndnsimName) |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 149 | .append("NDNS") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 150 | .append("rrLabel") |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 151 | .append("CERT") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 152 | .appendVersion(); |
| 153 | data = make_shared<Data>(dataName); |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 154 | m_keyChain.sign(*data, signingByCertificate(m_randomCert)); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 155 | |
| 156 | hasValidated = false; |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 157 | 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 Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 166 | |
Yumin Xia | fa2bce7 | 2017-04-09 16:20:25 -0700 | [diff] [blame^] | 167 | advanceClocks(time::seconds(3), 100); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 168 | // cannot pass due to a totally mismatched key |
| 169 | BOOST_CHECK_EQUAL(hasValidated, true); |
| 170 | } |
| 171 | |
| 172 | BOOST_AUTO_TEST_SUITE_END() |
| 173 | |
| 174 | } // namespace tests |
| 175 | } // namespace ndns |
| 176 | } // namespace ndn |