Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Alexander Afanasyev | 09236c2 | 2020-06-03 13:42:38 -0400 | [diff] [blame] | 22 | #include "ndn-cxx/security/trust-anchor-container.hpp" |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 25 | #include "tests/key-chain-fixture.hpp" |
| 26 | #include "tests/unit/clock-fixture.hpp" |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 27 | |
Davide Pesavento | d8e0cad | 2021-05-26 21:43:47 -0400 | [diff] [blame] | 28 | #include <boost/filesystem/operations.hpp> |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 29 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 30 | namespace ndn::tests { |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 32 | using namespace ndn::security; |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 34 | // This fixture creates a directory and prepares two certificates. |
| 35 | // cert1 is written to a file under the directory, while cert2 is not. |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 36 | class TrustAnchorContainerFixture : public ClockFixture, public KeyChainFixture |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 37 | { |
| 38 | public: |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 39 | TrustAnchorContainerFixture() |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 40 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 41 | boost::filesystem::create_directories(certDirPath); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 42 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 43 | identity1 = m_keyChain.createIdentity("/TestAnchorContainer/First"); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 44 | cert1 = identity1.getDefaultKey().getDefaultCertificate(); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 45 | saveCert(cert1, certPath1.string()); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 46 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 47 | identity2 = m_keyChain.createIdentity("/TestAnchorContainer/Second"); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 48 | cert2 = identity2.getDefaultKey().getDefaultCertificate(); |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 49 | saveCert(cert2, certPath2.string()); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 52 | ~TrustAnchorContainerFixture() override |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 53 | { |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 54 | boost::filesystem::remove_all(certDirPath); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 57 | void |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 58 | checkFindByInterest(const Name& name, bool canBePrefix, std::optional<Certificate> expected) const |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 59 | { |
| 60 | Interest interest(name); |
| 61 | interest.setCanBePrefix(canBePrefix); |
| 62 | BOOST_TEST_CONTEXT(interest) { |
| 63 | auto found = anchorContainer.find(interest); |
| 64 | if (expected) { |
| 65 | BOOST_REQUIRE(found != nullptr); |
| 66 | BOOST_CHECK_EQUAL(found->getName(), expected->getName()); |
| 67 | } |
| 68 | else { |
| 69 | BOOST_CHECK(found == nullptr); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 74 | public: |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 75 | const boost::filesystem::path certDirPath{boost::filesystem::path(UNIT_TESTS_TMPDIR) / "test-cert-dir"}; |
| 76 | const boost::filesystem::path certPath1{certDirPath / "trust-anchor-1.cert"}; |
| 77 | const boost::filesystem::path certPath2{certDirPath / "trust-anchor-2.cert"}; |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 78 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 79 | TrustAnchorContainer anchorContainer; |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 80 | |
| 81 | Identity identity1; |
| 82 | Identity identity2; |
| 83 | |
| 84 | Certificate cert1; |
| 85 | Certificate cert2; |
| 86 | }; |
| 87 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 88 | BOOST_AUTO_TEST_SUITE(Security) |
| 89 | BOOST_FIXTURE_TEST_SUITE(TestTrustAnchorContainer, TrustAnchorContainerFixture) |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 90 | |
| 91 | // one static group and one dynamic group created from file |
| 92 | BOOST_AUTO_TEST_CASE(Insert) |
| 93 | { |
| 94 | // Static |
| 95 | anchorContainer.insert("group1", Certificate(cert1)); |
| 96 | BOOST_CHECK(anchorContainer.find(cert1.getName()) != nullptr); |
| 97 | BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr); |
| 98 | const Certificate* cert = anchorContainer.find(cert1.getName()); |
| 99 | BOOST_CHECK_NO_THROW(anchorContainer.insert("group1", Certificate(cert1))); |
| 100 | BOOST_CHECK_EQUAL(cert, anchorContainer.find(cert1.getName())); // still the same instance of the certificate |
| 101 | // cannot add dynamic group when static already exists |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 102 | BOOST_CHECK_THROW(anchorContainer.insert("group1", certPath1.string(), 1_s), TrustAnchorContainer::Error); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 103 | BOOST_CHECK_EQUAL(anchorContainer.getGroup("group1").size(), 1); |
| 104 | BOOST_CHECK_EQUAL(anchorContainer.size(), 1); |
| 105 | |
| 106 | // From file |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 107 | anchorContainer.insert("group2", certPath2.string(), 1_s); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 108 | BOOST_CHECK(anchorContainer.find(cert2.getName()) != nullptr); |
| 109 | BOOST_CHECK(anchorContainer.find(identity2.getName()) != nullptr); |
| 110 | BOOST_CHECK_THROW(anchorContainer.insert("group2", Certificate(cert2)), TrustAnchorContainer::Error); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 111 | BOOST_CHECK_THROW(anchorContainer.insert("group2", certPath2.string(), 1_s), TrustAnchorContainer::Error); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 112 | BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 1); |
| 113 | BOOST_CHECK_EQUAL(anchorContainer.size(), 2); |
| 114 | |
| 115 | boost::filesystem::remove(certPath2); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 116 | advanceClocks(1_s, 11); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 117 | |
| 118 | BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr); |
| 119 | BOOST_CHECK(anchorContainer.find(cert2.getName()) == nullptr); |
| 120 | BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 0); |
| 121 | BOOST_CHECK_EQUAL(anchorContainer.size(), 1); |
| 122 | |
| 123 | TrustAnchorGroup& group = anchorContainer.getGroup("group1"); |
| 124 | auto staticGroup = dynamic_cast<StaticTrustAnchorGroup*>(&group); |
| 125 | BOOST_REQUIRE(staticGroup != nullptr); |
| 126 | BOOST_CHECK_EQUAL(staticGroup->size(), 1); |
| 127 | staticGroup->remove(cert1.getName()); |
| 128 | BOOST_CHECK_EQUAL(staticGroup->size(), 0); |
| 129 | BOOST_CHECK_EQUAL(anchorContainer.size(), 0); |
| 130 | |
| 131 | BOOST_CHECK_THROW(anchorContainer.getGroup("non-existing-group"), TrustAnchorContainer::Error); |
| 132 | } |
| 133 | |
| 134 | BOOST_AUTO_TEST_CASE(DynamicAnchorFromDir) |
| 135 | { |
| 136 | boost::filesystem::remove(certPath2); |
| 137 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 138 | anchorContainer.insert("group", certDirPath.string(), 1_s, true /* isDir */); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 139 | |
| 140 | BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr); |
| 141 | BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr); |
| 142 | BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 1); |
| 143 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 144 | saveCert(cert2, certPath2.string()); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 145 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 146 | advanceClocks(100_ms, 11); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 147 | |
| 148 | BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr); |
| 149 | BOOST_CHECK(anchorContainer.find(identity2.getName()) != nullptr); |
| 150 | BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 2); |
| 151 | |
| 152 | boost::filesystem::remove_all(certDirPath); |
| 153 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 154 | advanceClocks(100_ms, 11); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 155 | |
| 156 | BOOST_CHECK(anchorContainer.find(identity1.getName()) == nullptr); |
| 157 | BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr); |
| 158 | BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 0); |
| 159 | } |
| 160 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 161 | BOOST_AUTO_TEST_CASE(FindByInterest) |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 162 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 163 | anchorContainer.insert("group1", certPath1.string(), 1_s); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 164 | |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 165 | checkFindByInterest(identity1.getName(), true, cert1); |
| 166 | checkFindByInterest(identity1.getName().getPrefix(-1), true, cert1); |
| 167 | checkFindByInterest(cert1.getKeyName(), true, cert1); |
| 168 | checkFindByInterest(cert1.getName(), false, cert1); |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 169 | checkFindByInterest(Name(identity1.getName()).appendVersion(), true, std::nullopt); |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 170 | |
| 171 | auto makeIdentity1Cert = [=] (const std::string& issuerId) { |
| 172 | auto key = identity1.getDefaultKey(); |
| 173 | MakeCertificateOptions opts; |
| 174 | opts.issuerId = name::Component::fromEscapedString(issuerId); |
| 175 | return m_keyChain.makeCertificate(key, signingByKey(key), opts); |
| 176 | }; |
| 177 | |
| 178 | auto cert3 = makeIdentity1Cert("3"); |
| 179 | auto cert4 = makeIdentity1Cert("4"); |
| 180 | auto cert5 = makeIdentity1Cert("5"); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 181 | |
| 182 | Certificate cert3Copy = cert3; |
| 183 | anchorContainer.insert("group2", std::move(cert3Copy)); |
| 184 | anchorContainer.insert("group3", std::move(cert4)); |
| 185 | anchorContainer.insert("group4", std::move(cert5)); |
| 186 | |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 187 | checkFindByInterest(cert3.getKeyName(), true, cert3); |
| 188 | checkFindByInterest(cert3.getName(), false, cert3); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | BOOST_AUTO_TEST_SUITE_END() // TestTrustAnchorContainer |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 192 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 193 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 194 | } // namespace ndn::tests |