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 | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 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 | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 28 | namespace ndn::tests { |
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 | using namespace ndn::security; |
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 | // This fixture creates a directory and prepares two certificates. |
| 33 | // 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] | 34 | class TrustAnchorContainerFixture : public ClockFixture, public KeyChainFixture |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 35 | { |
| 36 | public: |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 37 | TrustAnchorContainerFixture() |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 38 | { |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 39 | std::filesystem::create_directories(certDirPath); |
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 | identity1 = m_keyChain.createIdentity("/TestAnchorContainer/First"); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 42 | cert1 = identity1.getDefaultKey().getDefaultCertificate(); |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 43 | saveCert(cert1, certPath1); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 44 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 45 | identity2 = m_keyChain.createIdentity("/TestAnchorContainer/Second"); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 46 | cert2 = identity2.getDefaultKey().getDefaultCertificate(); |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 47 | saveCert(cert2, certPath2); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 50 | ~TrustAnchorContainerFixture() override |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 51 | { |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 52 | std::filesystem::remove_all(certDirPath); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 55 | void |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 56 | checkFindByInterest(const Name& name, bool canBePrefix, |
| 57 | const std::optional<Certificate>& expected) const |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 58 | { |
| 59 | Interest interest(name); |
| 60 | interest.setCanBePrefix(canBePrefix); |
Davide Pesavento | 0c52603 | 2024-01-31 21:14:01 -0500 | [diff] [blame] | 61 | BOOST_TEST_INFO_SCOPE("Interest = " << interest); |
| 62 | |
| 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); |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 73 | public: |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 74 | const std::filesystem::path certDirPath{std::filesystem::path(UNIT_TESTS_TMPDIR) / "test-cert-dir"}; |
| 75 | const std::filesystem::path certPath1{certDirPath / "trust-anchor-1.cert"}; |
| 76 | const std::filesystem::path certPath2{certDirPath / "trust-anchor-2.cert"}; |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 77 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 78 | TrustAnchorContainer anchorContainer; |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 79 | |
| 80 | Identity identity1; |
| 81 | Identity identity2; |
| 82 | |
| 83 | Certificate cert1; |
| 84 | Certificate cert2; |
| 85 | }; |
| 86 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 87 | BOOST_AUTO_TEST_SUITE(Security) |
| 88 | BOOST_FIXTURE_TEST_SUITE(TestTrustAnchorContainer, TrustAnchorContainerFixture) |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 89 | |
| 90 | // one static group and one dynamic group created from file |
| 91 | BOOST_AUTO_TEST_CASE(Insert) |
| 92 | { |
| 93 | // Static |
| 94 | anchorContainer.insert("group1", Certificate(cert1)); |
| 95 | BOOST_CHECK(anchorContainer.find(cert1.getName()) != nullptr); |
| 96 | BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr); |
| 97 | const Certificate* cert = anchorContainer.find(cert1.getName()); |
| 98 | BOOST_CHECK_NO_THROW(anchorContainer.insert("group1", Certificate(cert1))); |
| 99 | BOOST_CHECK_EQUAL(cert, anchorContainer.find(cert1.getName())); // still the same instance of the certificate |
| 100 | // cannot add dynamic group when static already exists |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 101 | BOOST_CHECK_THROW(anchorContainer.insert("group1", certPath1, 1_s), TrustAnchorContainer::Error); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(anchorContainer.getGroup("group1").size(), 1); |
| 103 | BOOST_CHECK_EQUAL(anchorContainer.size(), 1); |
| 104 | |
| 105 | // From file |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 106 | anchorContainer.insert("group2", certPath2, 1_s); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 107 | BOOST_CHECK(anchorContainer.find(cert2.getName()) != nullptr); |
| 108 | BOOST_CHECK(anchorContainer.find(identity2.getName()) != nullptr); |
| 109 | BOOST_CHECK_THROW(anchorContainer.insert("group2", Certificate(cert2)), TrustAnchorContainer::Error); |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 110 | BOOST_CHECK_THROW(anchorContainer.insert("group2", certPath2, 1_s), TrustAnchorContainer::Error); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 111 | BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 1); |
| 112 | BOOST_CHECK_EQUAL(anchorContainer.size(), 2); |
| 113 | |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 114 | std::filesystem::remove(certPath2); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 115 | advanceClocks(1_s, 11); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 116 | |
| 117 | BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr); |
| 118 | BOOST_CHECK(anchorContainer.find(cert2.getName()) == nullptr); |
| 119 | BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 0); |
| 120 | BOOST_CHECK_EQUAL(anchorContainer.size(), 1); |
| 121 | |
| 122 | TrustAnchorGroup& group = anchorContainer.getGroup("group1"); |
| 123 | auto staticGroup = dynamic_cast<StaticTrustAnchorGroup*>(&group); |
| 124 | BOOST_REQUIRE(staticGroup != nullptr); |
| 125 | BOOST_CHECK_EQUAL(staticGroup->size(), 1); |
| 126 | staticGroup->remove(cert1.getName()); |
| 127 | BOOST_CHECK_EQUAL(staticGroup->size(), 0); |
| 128 | BOOST_CHECK_EQUAL(anchorContainer.size(), 0); |
| 129 | |
| 130 | BOOST_CHECK_THROW(anchorContainer.getGroup("non-existing-group"), TrustAnchorContainer::Error); |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_CASE(DynamicAnchorFromDir) |
| 134 | { |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 135 | std::filesystem::remove(certPath2); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 136 | |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 137 | anchorContainer.insert("group", certDirPath, 1_s, /*isDir*/ true); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 138 | |
| 139 | BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr); |
| 140 | BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr); |
| 141 | BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 1); |
| 142 | |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 143 | saveCert(cert2, certPath2); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 144 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 145 | advanceClocks(100_ms, 11); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 146 | |
| 147 | BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr); |
| 148 | BOOST_CHECK(anchorContainer.find(identity2.getName()) != nullptr); |
| 149 | BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 2); |
| 150 | |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 151 | std::filesystem::remove_all(certDirPath); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 152 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 153 | advanceClocks(100_ms, 11); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 154 | |
| 155 | BOOST_CHECK(anchorContainer.find(identity1.getName()) == nullptr); |
| 156 | BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr); |
| 157 | BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 0); |
| 158 | } |
| 159 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 160 | BOOST_AUTO_TEST_CASE(FindByInterest) |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 161 | { |
Davide Pesavento | 51974f6 | 2024-12-21 20:42:45 -0500 | [diff] [blame^] | 162 | anchorContainer.insert("group1", certPath1, 1_s); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 163 | |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 164 | checkFindByInterest(identity1.getName(), true, cert1); |
| 165 | checkFindByInterest(identity1.getName().getPrefix(-1), true, cert1); |
| 166 | checkFindByInterest(cert1.getKeyName(), true, cert1); |
| 167 | checkFindByInterest(cert1.getName(), false, cert1); |
Davide Pesavento | f6b4589 | 2023-03-13 15:00:51 -0400 | [diff] [blame] | 168 | checkFindByInterest(Name(identity1.getName()).appendVersion(), true, std::nullopt); |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 169 | |
| 170 | auto makeIdentity1Cert = [=] (const std::string& issuerId) { |
| 171 | auto key = identity1.getDefaultKey(); |
| 172 | MakeCertificateOptions opts; |
Davide Pesavento | 8e047e1 | 2024-02-12 16:50:23 -0500 | [diff] [blame] | 173 | opts.issuerId = name::Component::fromUri(issuerId); |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 174 | return m_keyChain.makeCertificate(key, signingByKey(key), opts); |
| 175 | }; |
| 176 | |
| 177 | auto cert3 = makeIdentity1Cert("3"); |
| 178 | auto cert4 = makeIdentity1Cert("4"); |
| 179 | auto cert5 = makeIdentity1Cert("5"); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 180 | |
| 181 | Certificate cert3Copy = cert3; |
| 182 | anchorContainer.insert("group2", std::move(cert3Copy)); |
| 183 | anchorContainer.insert("group3", std::move(cert4)); |
| 184 | anchorContainer.insert("group4", std::move(cert5)); |
| 185 | |
Junxiao Shi | 9ee770b | 2022-04-25 23:33:33 +0000 | [diff] [blame] | 186 | checkFindByInterest(cert3.getKeyName(), true, cert3); |
| 187 | checkFindByInterest(cert3.getName(), false, cert3); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | BOOST_AUTO_TEST_SUITE_END() // TestTrustAnchorContainer |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 191 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 192 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 193 | } // namespace ndn::tests |