blob: b711ba6741f8c246ecb981166b72c9e5c75f3868 [file] [log] [blame]
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0f830802018-01-16 23:58:58 -05002/*
Davide Pesavento0c526032024-01-31 21:14:01 -05003 * Copyright (c) 2013-2024 Regents of the University of California.
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -07004 *
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 Afanasyev09236c22020-06-03 13:42:38 -040022#include "ndn-cxx/security/trust-anchor-container.hpp"
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050025#include "tests/key-chain-fixture.hpp"
26#include "tests/unit/clock-fixture.hpp"
Davide Pesavento0f830802018-01-16 23:58:58 -050027
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040028namespace ndn::tests {
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070029
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040030using namespace ndn::security;
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070031
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040032// This fixture creates a directory and prepares two certificates.
33// cert1 is written to a file under the directory, while cert2 is not.
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050034class TrustAnchorContainerFixture : public ClockFixture, public KeyChainFixture
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070035{
36public:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050037 TrustAnchorContainerFixture()
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070038 {
Davide Pesavento51974f62024-12-21 20:42:45 -050039 std::filesystem::create_directories(certDirPath);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070040
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050041 identity1 = m_keyChain.createIdentity("/TestAnchorContainer/First");
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070042 cert1 = identity1.getDefaultKey().getDefaultCertificate();
Davide Pesavento51974f62024-12-21 20:42:45 -050043 saveCert(cert1, certPath1);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070044
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050045 identity2 = m_keyChain.createIdentity("/TestAnchorContainer/Second");
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070046 cert2 = identity2.getDefaultKey().getDefaultCertificate();
Davide Pesavento51974f62024-12-21 20:42:45 -050047 saveCert(cert2, certPath2);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070048 }
49
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050050 ~TrustAnchorContainerFixture() override
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070051 {
Davide Pesavento51974f62024-12-21 20:42:45 -050052 std::filesystem::remove_all(certDirPath);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070053 }
54
Junxiao Shi9ee770b2022-04-25 23:33:33 +000055 void
Davide Pesavento0c526032024-01-31 21:14:01 -050056 checkFindByInterest(const Name& name, bool canBePrefix,
57 const std::optional<Certificate>& expected) const
Junxiao Shi9ee770b2022-04-25 23:33:33 +000058 {
59 Interest interest(name);
60 interest.setCanBePrefix(canBePrefix);
Davide Pesavento0c526032024-01-31 21:14:01 -050061 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 Shi9ee770b2022-04-25 23:33:33 +000070 }
71 }
72
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070073public:
Davide Pesavento51974f62024-12-21 20:42:45 -050074 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 Ding4caa0cc2015-10-23 20:31:27 -070077
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050078 TrustAnchorContainer anchorContainer;
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070079
80 Identity identity1;
81 Identity identity2;
82
83 Certificate cert1;
84 Certificate cert2;
85};
86
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050087BOOST_AUTO_TEST_SUITE(Security)
88BOOST_FIXTURE_TEST_SUITE(TestTrustAnchorContainer, TrustAnchorContainerFixture)
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070089
90// one static group and one dynamic group created from file
91BOOST_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 Pesavento51974f62024-12-21 20:42:45 -0500101 BOOST_CHECK_THROW(anchorContainer.insert("group1", certPath1, 1_s), TrustAnchorContainer::Error);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700102 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group1").size(), 1);
103 BOOST_CHECK_EQUAL(anchorContainer.size(), 1);
104
105 // From file
Davide Pesavento51974f62024-12-21 20:42:45 -0500106 anchorContainer.insert("group2", certPath2, 1_s);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700107 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 Pesavento51974f62024-12-21 20:42:45 -0500110 BOOST_CHECK_THROW(anchorContainer.insert("group2", certPath2, 1_s), TrustAnchorContainer::Error);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700111 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 1);
112 BOOST_CHECK_EQUAL(anchorContainer.size(), 2);
113
Davide Pesavento51974f62024-12-21 20:42:45 -0500114 std::filesystem::remove(certPath2);
Davide Pesavento0f830802018-01-16 23:58:58 -0500115 advanceClocks(1_s, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700116
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
133BOOST_AUTO_TEST_CASE(DynamicAnchorFromDir)
134{
Davide Pesavento51974f62024-12-21 20:42:45 -0500135 std::filesystem::remove(certPath2);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700136
Davide Pesavento51974f62024-12-21 20:42:45 -0500137 anchorContainer.insert("group", certDirPath, 1_s, /*isDir*/ true);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700138
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 Pesavento51974f62024-12-21 20:42:45 -0500143 saveCert(cert2, certPath2);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700144
Davide Pesavento0f830802018-01-16 23:58:58 -0500145 advanceClocks(100_ms, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700146
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 Pesavento51974f62024-12-21 20:42:45 -0500151 std::filesystem::remove_all(certDirPath);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700152
Davide Pesavento0f830802018-01-16 23:58:58 -0500153 advanceClocks(100_ms, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700154
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 Pesavento4c1ad4c2020-11-16 21:12:02 -0500160BOOST_AUTO_TEST_CASE(FindByInterest)
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700161{
Davide Pesavento51974f62024-12-21 20:42:45 -0500162 anchorContainer.insert("group1", certPath1, 1_s);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700163
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000164 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 Pesaventof6b45892023-03-13 15:00:51 -0400168 checkFindByInterest(Name(identity1.getName()).appendVersion(), true, std::nullopt);
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000169
170 auto makeIdentity1Cert = [=] (const std::string& issuerId) {
171 auto key = identity1.getDefaultKey();
172 MakeCertificateOptions opts;
Davide Pesavento8e047e12024-02-12 16:50:23 -0500173 opts.issuerId = name::Component::fromUri(issuerId);
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000174 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 Ding4caa0cc2015-10-23 20:31:27 -0700180
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 Shi9ee770b2022-04-25 23:33:33 +0000186 checkFindByInterest(cert3.getKeyName(), true, cert3);
187 checkFindByInterest(cert3.getName(), false, cert3);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700188}
189
190BOOST_AUTO_TEST_SUITE_END() // TestTrustAnchorContainer
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700191BOOST_AUTO_TEST_SUITE_END() // Security
192
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400193} // namespace ndn::tests