blob: 09825db204370b8ae40ad560a7fbab3f5c65f8f8 [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
30namespace ndn {
31namespace security {
Alexander Afanasyev09236c22020-06-03 13:42:38 -040032inline namespace v2 {
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070033namespace tests {
34
35using namespace ndn::tests;
36
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070037/**
38 * This fixture creates a directory and prepares two certificates.
39 * cert1 is written to a file under the directory, while cert2 is not.
40 */
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050041class TrustAnchorContainerFixture : public ClockFixture, public KeyChainFixture
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070042{
43public:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050044 TrustAnchorContainerFixture()
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070045 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050046 boost::filesystem::create_directories(certDirPath);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070047
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050048 identity1 = m_keyChain.createIdentity("/TestAnchorContainer/First");
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070049 cert1 = identity1.getDefaultKey().getDefaultCertificate();
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050050 saveCert(cert1, certPath1.string());
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070051
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050052 identity2 = m_keyChain.createIdentity("/TestAnchorContainer/Second");
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070053 cert2 = identity2.getDefaultKey().getDefaultCertificate();
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050054 saveCert(cert2, certPath2.string());
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070055 }
56
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050057 ~TrustAnchorContainerFixture() override
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070058 {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050059 boost::filesystem::remove_all(certDirPath);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070060 }
61
Junxiao Shi9ee770b2022-04-25 23:33:33 +000062 void
Davide Pesaventof6b45892023-03-13 15:00:51 -040063 checkFindByInterest(const Name& name, bool canBePrefix, std::optional<Certificate> expected) const
Junxiao Shi9ee770b2022-04-25 23:33:33 +000064 {
65 Interest interest(name);
66 interest.setCanBePrefix(canBePrefix);
67 BOOST_TEST_CONTEXT(interest) {
68 auto found = anchorContainer.find(interest);
69 if (expected) {
70 BOOST_REQUIRE(found != nullptr);
71 BOOST_CHECK_EQUAL(found->getName(), expected->getName());
72 }
73 else {
74 BOOST_CHECK(found == nullptr);
75 }
76 }
77 }
78
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070079public:
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050080 const boost::filesystem::path certDirPath{boost::filesystem::path(UNIT_TESTS_TMPDIR) / "test-cert-dir"};
81 const boost::filesystem::path certPath1{certDirPath / "trust-anchor-1.cert"};
82 const boost::filesystem::path certPath2{certDirPath / "trust-anchor-2.cert"};
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070083
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050084 TrustAnchorContainer anchorContainer;
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070085
86 Identity identity1;
87 Identity identity2;
88
89 Certificate cert1;
90 Certificate cert2;
91};
92
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050093BOOST_AUTO_TEST_SUITE(Security)
94BOOST_FIXTURE_TEST_SUITE(TestTrustAnchorContainer, TrustAnchorContainerFixture)
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070095
96// one static group and one dynamic group created from file
97BOOST_AUTO_TEST_CASE(Insert)
98{
99 // Static
100 anchorContainer.insert("group1", Certificate(cert1));
101 BOOST_CHECK(anchorContainer.find(cert1.getName()) != nullptr);
102 BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr);
103 const Certificate* cert = anchorContainer.find(cert1.getName());
104 BOOST_CHECK_NO_THROW(anchorContainer.insert("group1", Certificate(cert1)));
105 BOOST_CHECK_EQUAL(cert, anchorContainer.find(cert1.getName())); // still the same instance of the certificate
106 // cannot add dynamic group when static already exists
Davide Pesavento0f830802018-01-16 23:58:58 -0500107 BOOST_CHECK_THROW(anchorContainer.insert("group1", certPath1.string(), 1_s), TrustAnchorContainer::Error);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700108 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group1").size(), 1);
109 BOOST_CHECK_EQUAL(anchorContainer.size(), 1);
110
111 // From file
Davide Pesavento0f830802018-01-16 23:58:58 -0500112 anchorContainer.insert("group2", certPath2.string(), 1_s);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700113 BOOST_CHECK(anchorContainer.find(cert2.getName()) != nullptr);
114 BOOST_CHECK(anchorContainer.find(identity2.getName()) != nullptr);
115 BOOST_CHECK_THROW(anchorContainer.insert("group2", Certificate(cert2)), TrustAnchorContainer::Error);
Davide Pesavento0f830802018-01-16 23:58:58 -0500116 BOOST_CHECK_THROW(anchorContainer.insert("group2", certPath2.string(), 1_s), TrustAnchorContainer::Error);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700117 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 1);
118 BOOST_CHECK_EQUAL(anchorContainer.size(), 2);
119
120 boost::filesystem::remove(certPath2);
Davide Pesavento0f830802018-01-16 23:58:58 -0500121 advanceClocks(1_s, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700122
123 BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr);
124 BOOST_CHECK(anchorContainer.find(cert2.getName()) == nullptr);
125 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 0);
126 BOOST_CHECK_EQUAL(anchorContainer.size(), 1);
127
128 TrustAnchorGroup& group = anchorContainer.getGroup("group1");
129 auto staticGroup = dynamic_cast<StaticTrustAnchorGroup*>(&group);
130 BOOST_REQUIRE(staticGroup != nullptr);
131 BOOST_CHECK_EQUAL(staticGroup->size(), 1);
132 staticGroup->remove(cert1.getName());
133 BOOST_CHECK_EQUAL(staticGroup->size(), 0);
134 BOOST_CHECK_EQUAL(anchorContainer.size(), 0);
135
136 BOOST_CHECK_THROW(anchorContainer.getGroup("non-existing-group"), TrustAnchorContainer::Error);
137}
138
139BOOST_AUTO_TEST_CASE(DynamicAnchorFromDir)
140{
141 boost::filesystem::remove(certPath2);
142
Davide Pesavento0f830802018-01-16 23:58:58 -0500143 anchorContainer.insert("group", certDirPath.string(), 1_s, true /* isDir */);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700144
145 BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr);
146 BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr);
147 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 1);
148
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500149 saveCert(cert2, certPath2.string());
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700150
Davide Pesavento0f830802018-01-16 23:58:58 -0500151 advanceClocks(100_ms, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700152
153 BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr);
154 BOOST_CHECK(anchorContainer.find(identity2.getName()) != nullptr);
155 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 2);
156
157 boost::filesystem::remove_all(certDirPath);
158
Davide Pesavento0f830802018-01-16 23:58:58 -0500159 advanceClocks(100_ms, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700160
161 BOOST_CHECK(anchorContainer.find(identity1.getName()) == nullptr);
162 BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr);
163 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 0);
164}
165
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500166BOOST_AUTO_TEST_CASE(FindByInterest)
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700167{
Davide Pesavento0f830802018-01-16 23:58:58 -0500168 anchorContainer.insert("group1", certPath1.string(), 1_s);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700169
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000170 checkFindByInterest(identity1.getName(), true, cert1);
171 checkFindByInterest(identity1.getName().getPrefix(-1), true, cert1);
172 checkFindByInterest(cert1.getKeyName(), true, cert1);
173 checkFindByInterest(cert1.getName(), false, cert1);
Davide Pesaventof6b45892023-03-13 15:00:51 -0400174 checkFindByInterest(Name(identity1.getName()).appendVersion(), true, std::nullopt);
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000175
176 auto makeIdentity1Cert = [=] (const std::string& issuerId) {
177 auto key = identity1.getDefaultKey();
178 MakeCertificateOptions opts;
179 opts.issuerId = name::Component::fromEscapedString(issuerId);
180 return m_keyChain.makeCertificate(key, signingByKey(key), opts);
181 };
182
183 auto cert3 = makeIdentity1Cert("3");
184 auto cert4 = makeIdentity1Cert("4");
185 auto cert5 = makeIdentity1Cert("5");
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700186
187 Certificate cert3Copy = cert3;
188 anchorContainer.insert("group2", std::move(cert3Copy));
189 anchorContainer.insert("group3", std::move(cert4));
190 anchorContainer.insert("group4", std::move(cert5));
191
Junxiao Shi9ee770b2022-04-25 23:33:33 +0000192 checkFindByInterest(cert3.getKeyName(), true, cert3);
193 checkFindByInterest(cert3.getName(), false, cert3);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700194}
195
196BOOST_AUTO_TEST_SUITE_END() // TestTrustAnchorContainer
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700197BOOST_AUTO_TEST_SUITE_END() // Security
198
199} // namespace tests
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400200} // inline namespace v2
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700201} // namespace security
202} // namespace ndn