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