blob: 879ef408414a31a14b885068476894d597939fd1 [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 Pesaventod8e0cad2021-05-26 21:43:47 -040028#include <boost/filesystem/operations.hpp>
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070029
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040030namespace ndn::tests {
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070031
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040032using namespace ndn::security;
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070033
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040034// This fixture creates a directory and prepares two certificates.
35// cert1 is written to a file under the directory, while cert2 is not.
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050036class TrustAnchorContainerFixture : public ClockFixture, public KeyChainFixture
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070037{
38public:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050039 TrustAnchorContainerFixture()
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070040 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050041 boost::filesystem::create_directories(certDirPath);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070042
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050043 identity1 = m_keyChain.createIdentity("/TestAnchorContainer/First");
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070044 cert1 = identity1.getDefaultKey().getDefaultCertificate();
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050045 saveCert(cert1, certPath1.string());
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070046
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050047 identity2 = m_keyChain.createIdentity("/TestAnchorContainer/Second");
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070048 cert2 = identity2.getDefaultKey().getDefaultCertificate();
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050049 saveCert(cert2, certPath2.string());
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070050 }
51
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050052 ~TrustAnchorContainerFixture() override
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070053 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050054 boost::filesystem::remove_all(certDirPath);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070055 }
56
Junxiao Shi9ee770b2022-04-25 23:33:33 +000057 void
Davide Pesavento0c526032024-01-31 21:14:01 -050058 checkFindByInterest(const Name& name, bool canBePrefix,
59 const std::optional<Certificate>& expected) const
Junxiao Shi9ee770b2022-04-25 23:33:33 +000060 {
61 Interest interest(name);
62 interest.setCanBePrefix(canBePrefix);
Davide Pesavento0c526032024-01-31 21:14:01 -050063 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 Shi9ee770b2022-04-25 23:33:33 +000072 }
73 }
74
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070075public:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050076 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 Ding4caa0cc2015-10-23 20:31:27 -070079
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050080 TrustAnchorContainer anchorContainer;
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070081
82 Identity identity1;
83 Identity identity2;
84
85 Certificate cert1;
86 Certificate cert2;
87};
88
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050089BOOST_AUTO_TEST_SUITE(Security)
90BOOST_FIXTURE_TEST_SUITE(TestTrustAnchorContainer, TrustAnchorContainerFixture)
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070091
92// one static group and one dynamic group created from file
93BOOST_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 Pesavento0f830802018-01-16 23:58:58 -0500103 BOOST_CHECK_THROW(anchorContainer.insert("group1", certPath1.string(), 1_s), TrustAnchorContainer::Error);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700104 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group1").size(), 1);
105 BOOST_CHECK_EQUAL(anchorContainer.size(), 1);
106
107 // From file
Davide Pesavento0f830802018-01-16 23:58:58 -0500108 anchorContainer.insert("group2", certPath2.string(), 1_s);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700109 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 Pesavento0f830802018-01-16 23:58:58 -0500112 BOOST_CHECK_THROW(anchorContainer.insert("group2", certPath2.string(), 1_s), TrustAnchorContainer::Error);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700113 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 1);
114 BOOST_CHECK_EQUAL(anchorContainer.size(), 2);
115
116 boost::filesystem::remove(certPath2);
Davide Pesavento0f830802018-01-16 23:58:58 -0500117 advanceClocks(1_s, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700118
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
135BOOST_AUTO_TEST_CASE(DynamicAnchorFromDir)
136{
137 boost::filesystem::remove(certPath2);
138
Davide Pesavento0f830802018-01-16 23:58:58 -0500139 anchorContainer.insert("group", certDirPath.string(), 1_s, true /* isDir */);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700140
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 Pesavento4c1ad4c2020-11-16 21:12:02 -0500145 saveCert(cert2, certPath2.string());
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700146
Davide Pesavento0f830802018-01-16 23:58:58 -0500147 advanceClocks(100_ms, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700148
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 Pesavento0f830802018-01-16 23:58:58 -0500155 advanceClocks(100_ms, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700156
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 Pesavento4c1ad4c2020-11-16 21:12:02 -0500162BOOST_AUTO_TEST_CASE(FindByInterest)
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700163{
Davide Pesavento0f830802018-01-16 23:58:58 -0500164 anchorContainer.insert("group1", certPath1.string(), 1_s);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700165
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000166 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 Pesaventof6b45892023-03-13 15:00:51 -0400170 checkFindByInterest(Name(identity1.getName()).appendVersion(), true, std::nullopt);
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000171
172 auto makeIdentity1Cert = [=] (const std::string& issuerId) {
173 auto key = identity1.getDefaultKey();
174 MakeCertificateOptions opts;
175 opts.issuerId = name::Component::fromEscapedString(issuerId);
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 Ding4caa0cc2015-10-23 20:31:27 -0700182
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 Shi9ee770b2022-04-25 23:33:33 +0000188 checkFindByInterest(cert3.getKeyName(), true, cert3);
189 checkFindByInterest(cert3.getName(), false, cert3);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700190}
191
192BOOST_AUTO_TEST_SUITE_END() // TestTrustAnchorContainer
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700193BOOST_AUTO_TEST_SUITE_END() // Security
194
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400195} // namespace ndn::tests