blob: 0e76214836e1a3211b21874a97689bba15a45cf2 [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 Pesaventof6b45892023-03-13 15:00:51 -04003 * Copyright (c) 2013-2023 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 Pesaventof6b45892023-03-13 15:00:51 -040058 checkFindByInterest(const Name& name, bool canBePrefix, std::optional<Certificate> expected) const
Junxiao Shi9ee770b2022-04-25 23:33:33 +000059 {
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 Ding4caa0cc2015-10-23 20:31:27 -070074public:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050075 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 Ding4caa0cc2015-10-23 20:31:27 -070078
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050079 TrustAnchorContainer anchorContainer;
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070080
81 Identity identity1;
82 Identity identity2;
83
84 Certificate cert1;
85 Certificate cert2;
86};
87
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050088BOOST_AUTO_TEST_SUITE(Security)
89BOOST_FIXTURE_TEST_SUITE(TestTrustAnchorContainer, TrustAnchorContainerFixture)
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070090
91// one static group and one dynamic group created from file
92BOOST_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 Pesavento0f830802018-01-16 23:58:58 -0500102 BOOST_CHECK_THROW(anchorContainer.insert("group1", certPath1.string(), 1_s), TrustAnchorContainer::Error);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700103 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group1").size(), 1);
104 BOOST_CHECK_EQUAL(anchorContainer.size(), 1);
105
106 // From file
Davide Pesavento0f830802018-01-16 23:58:58 -0500107 anchorContainer.insert("group2", certPath2.string(), 1_s);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700108 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 Pesavento0f830802018-01-16 23:58:58 -0500111 BOOST_CHECK_THROW(anchorContainer.insert("group2", certPath2.string(), 1_s), TrustAnchorContainer::Error);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700112 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 1);
113 BOOST_CHECK_EQUAL(anchorContainer.size(), 2);
114
115 boost::filesystem::remove(certPath2);
Davide Pesavento0f830802018-01-16 23:58:58 -0500116 advanceClocks(1_s, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700117
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
134BOOST_AUTO_TEST_CASE(DynamicAnchorFromDir)
135{
136 boost::filesystem::remove(certPath2);
137
Davide Pesavento0f830802018-01-16 23:58:58 -0500138 anchorContainer.insert("group", certDirPath.string(), 1_s, true /* isDir */);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700139
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 Pesavento4c1ad4c2020-11-16 21:12:02 -0500144 saveCert(cert2, certPath2.string());
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700145
Davide Pesavento0f830802018-01-16 23:58:58 -0500146 advanceClocks(100_ms, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700147
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 Pesavento0f830802018-01-16 23:58:58 -0500154 advanceClocks(100_ms, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700155
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 Pesavento4c1ad4c2020-11-16 21:12:02 -0500161BOOST_AUTO_TEST_CASE(FindByInterest)
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700162{
Davide Pesavento0f830802018-01-16 23:58:58 -0500163 anchorContainer.insert("group1", certPath1.string(), 1_s);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700164
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000165 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 Pesaventof6b45892023-03-13 15:00:51 -0400169 checkFindByInterest(Name(identity1.getName()).appendVersion(), true, std::nullopt);
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000170
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 Ding4caa0cc2015-10-23 20:31:27 -0700181
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 Shi9ee770b2022-04-25 23:33:33 +0000187 checkFindByInterest(cert3.getKeyName(), true, cert3);
188 checkFindByInterest(cert3.getName(), false, cert3);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700189}
190
191BOOST_AUTO_TEST_SUITE_END() // TestTrustAnchorContainer
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700192BOOST_AUTO_TEST_SUITE_END() // Security
193
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400194} // namespace ndn::tests