blob: b25805fcd95075d25f6b22e1d7ceea7ccb53c0f9 [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/*
Alexander Afanasyev09236c22020-06-03 13:42:38 -04003 * Copyright (c) 2013-2020 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"
Davide Pesavento7e780642018-11-24 15:51:34 -050023#include "ndn-cxx/util/io.hpp"
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
26#include "tests/unit/identity-management-time-fixture.hpp"
Davide Pesavento0f830802018-01-16 23:58:58 -050027
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070028#include <boost/filesystem.hpp>
29
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
37BOOST_AUTO_TEST_SUITE(Security)
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070038
39/**
40 * This fixture creates a directory and prepares two certificates.
41 * cert1 is written to a file under the directory, while cert2 is not.
42 */
43class AnchorContainerTestFixture : public IdentityManagementTimeFixture
44{
45public:
46 AnchorContainerTestFixture()
47 {
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040048 namespace fs = boost::filesystem;
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070049
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040050 fs::create_directory(fs::path(UNIT_TEST_CONFIG_PATH));
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070051
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040052 certDirPath = fs::path(UNIT_TEST_CONFIG_PATH) / "test-cert-dir";
53 fs::create_directory(certDirPath);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070054
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040055 certPath1 = fs::path(UNIT_TEST_CONFIG_PATH) / "test-cert-dir" / "trust-anchor-1.cert";
56 certPath2 = fs::path(UNIT_TEST_CONFIG_PATH) / "test-cert-dir" / "trust-anchor-2.cert";
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070057
58 identity1 = addIdentity("/TestAnchorContainer/First");
59 cert1 = identity1.getDefaultKey().getDefaultCertificate();
60 saveCertToFile(cert1, certPath1.string());
61
62 identity2 = addIdentity("/TestAnchorContainer/Second");
63 cert2 = identity2.getDefaultKey().getDefaultCertificate();
64 saveCertToFile(cert2, certPath2.string());
65 }
66
67 ~AnchorContainerTestFixture()
68 {
69 boost::filesystem::remove_all(UNIT_TEST_CONFIG_PATH);
70 }
71
72public:
73 TrustAnchorContainer anchorContainer;
74
75 boost::filesystem::path certDirPath;
76 boost::filesystem::path certPath1;
77 boost::filesystem::path certPath2;
78
79 Identity identity1;
80 Identity identity2;
81
82 Certificate cert1;
83 Certificate cert2;
84};
85
86BOOST_FIXTURE_TEST_SUITE(TestTrustAnchorContainer, AnchorContainerTestFixture)
87
88// one static group and one dynamic group created from file
89BOOST_AUTO_TEST_CASE(Insert)
90{
91 // Static
92 anchorContainer.insert("group1", Certificate(cert1));
93 BOOST_CHECK(anchorContainer.find(cert1.getName()) != nullptr);
94 BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr);
95 const Certificate* cert = anchorContainer.find(cert1.getName());
96 BOOST_CHECK_NO_THROW(anchorContainer.insert("group1", Certificate(cert1)));
97 BOOST_CHECK_EQUAL(cert, anchorContainer.find(cert1.getName())); // still the same instance of the certificate
98 // cannot add dynamic group when static already exists
Davide Pesavento0f830802018-01-16 23:58:58 -050099 BOOST_CHECK_THROW(anchorContainer.insert("group1", certPath1.string(), 1_s), TrustAnchorContainer::Error);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700100 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group1").size(), 1);
101 BOOST_CHECK_EQUAL(anchorContainer.size(), 1);
102
103 // From file
Davide Pesavento0f830802018-01-16 23:58:58 -0500104 anchorContainer.insert("group2", certPath2.string(), 1_s);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700105 BOOST_CHECK(anchorContainer.find(cert2.getName()) != nullptr);
106 BOOST_CHECK(anchorContainer.find(identity2.getName()) != nullptr);
107 BOOST_CHECK_THROW(anchorContainer.insert("group2", Certificate(cert2)), TrustAnchorContainer::Error);
Davide Pesavento0f830802018-01-16 23:58:58 -0500108 BOOST_CHECK_THROW(anchorContainer.insert("group2", certPath2.string(), 1_s), TrustAnchorContainer::Error);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700109 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 1);
110 BOOST_CHECK_EQUAL(anchorContainer.size(), 2);
111
112 boost::filesystem::remove(certPath2);
Davide Pesavento0f830802018-01-16 23:58:58 -0500113 advanceClocks(1_s, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700114
115 BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr);
116 BOOST_CHECK(anchorContainer.find(cert2.getName()) == nullptr);
117 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 0);
118 BOOST_CHECK_EQUAL(anchorContainer.size(), 1);
119
120 TrustAnchorGroup& group = anchorContainer.getGroup("group1");
121 auto staticGroup = dynamic_cast<StaticTrustAnchorGroup*>(&group);
122 BOOST_REQUIRE(staticGroup != nullptr);
123 BOOST_CHECK_EQUAL(staticGroup->size(), 1);
124 staticGroup->remove(cert1.getName());
125 BOOST_CHECK_EQUAL(staticGroup->size(), 0);
126 BOOST_CHECK_EQUAL(anchorContainer.size(), 0);
127
128 BOOST_CHECK_THROW(anchorContainer.getGroup("non-existing-group"), TrustAnchorContainer::Error);
129}
130
131BOOST_AUTO_TEST_CASE(DynamicAnchorFromDir)
132{
133 boost::filesystem::remove(certPath2);
134
Davide Pesavento0f830802018-01-16 23:58:58 -0500135 anchorContainer.insert("group", certDirPath.string(), 1_s, true /* isDir */);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700136
137 BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr);
138 BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr);
139 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 1);
140
141 saveCertToFile(cert2, certPath2.string());
142
Davide Pesavento0f830802018-01-16 23:58:58 -0500143 advanceClocks(100_ms, 11);
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(), 2);
148
149 boost::filesystem::remove_all(certDirPath);
150
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(), 0);
156}
157
158BOOST_FIXTURE_TEST_CASE(FindByInterest, AnchorContainerTestFixture)
159{
Davide Pesavento0f830802018-01-16 23:58:58 -0500160 anchorContainer.insert("group1", certPath1.string(), 1_s);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700161 Interest interest(identity1.getName());
162 BOOST_CHECK(anchorContainer.find(interest) != nullptr);
163 Interest interest1(identity1.getName().getPrefix(-1));
164 BOOST_CHECK(anchorContainer.find(interest1) != nullptr);
165 Interest interest2(Name(identity1.getName()).appendVersion());
166 BOOST_CHECK(anchorContainer.find(interest2) == nullptr);
167
168 Certificate cert3 = addCertificate(identity1.getDefaultKey(), "3");
169 Certificate cert4 = addCertificate(identity1.getDefaultKey(), "4");
170 Certificate cert5 = addCertificate(identity1.getDefaultKey(), "5");
171
172 Certificate cert3Copy = cert3;
173 anchorContainer.insert("group2", std::move(cert3Copy));
174 anchorContainer.insert("group3", std::move(cert4));
175 anchorContainer.insert("group4", std::move(cert5));
176
177 Interest interest3(cert3.getKeyName());
178 const Certificate* foundCert = anchorContainer.find(interest3);
179 BOOST_REQUIRE(foundCert != nullptr);
180 BOOST_CHECK(interest3.getName().isPrefixOf(foundCert->getName()));
181 BOOST_CHECK_EQUAL(foundCert->getName(), cert3.getName());
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700182}
183
184BOOST_AUTO_TEST_SUITE_END() // TestTrustAnchorContainer
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700185BOOST_AUTO_TEST_SUITE_END() // Security
186
187} // namespace tests
Alexander Afanasyev09236c22020-06-03 13:42:38 -0400188} // inline namespace v2
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700189} // namespace security
190} // namespace ndn