blob: efd6ca31e51a429b7d252f1e0aa211ad573a8c48 [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/*
3 * Copyright (c) 2013-2018 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
22#include "security/v2/trust-anchor-container.hpp"
Davide Pesavento0f830802018-01-16 23:58:58 -050023#include "util/io.hpp"
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070024
25#include "../../identity-management-time-fixture.hpp"
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070026#include "boost-test.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 {
32namespace v2 {
33namespace tests {
34
35using namespace ndn::tests;
36
37BOOST_AUTO_TEST_SUITE(Security)
38BOOST_AUTO_TEST_SUITE(V2)
39
40/**
41 * This fixture creates a directory and prepares two certificates.
42 * cert1 is written to a file under the directory, while cert2 is not.
43 */
44class AnchorContainerTestFixture : public IdentityManagementTimeFixture
45{
46public:
47 AnchorContainerTestFixture()
48 {
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040049 namespace fs = boost::filesystem;
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070050
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040051 fs::create_directory(fs::path(UNIT_TEST_CONFIG_PATH));
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070052
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040053 certDirPath = fs::path(UNIT_TEST_CONFIG_PATH) / "test-cert-dir";
54 fs::create_directory(certDirPath);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070055
Davide Pesaventodb4da5e2018-06-15 11:37:52 -040056 certPath1 = fs::path(UNIT_TEST_CONFIG_PATH) / "test-cert-dir" / "trust-anchor-1.cert";
57 certPath2 = fs::path(UNIT_TEST_CONFIG_PATH) / "test-cert-dir" / "trust-anchor-2.cert";
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070058
59 identity1 = addIdentity("/TestAnchorContainer/First");
60 cert1 = identity1.getDefaultKey().getDefaultCertificate();
61 saveCertToFile(cert1, certPath1.string());
62
63 identity2 = addIdentity("/TestAnchorContainer/Second");
64 cert2 = identity2.getDefaultKey().getDefaultCertificate();
65 saveCertToFile(cert2, certPath2.string());
66 }
67
68 ~AnchorContainerTestFixture()
69 {
70 boost::filesystem::remove_all(UNIT_TEST_CONFIG_PATH);
71 }
72
73public:
74 TrustAnchorContainer anchorContainer;
75
76 boost::filesystem::path certDirPath;
77 boost::filesystem::path certPath1;
78 boost::filesystem::path certPath2;
79
80 Identity identity1;
81 Identity identity2;
82
83 Certificate cert1;
84 Certificate cert2;
85};
86
87BOOST_FIXTURE_TEST_SUITE(TestTrustAnchorContainer, AnchorContainerTestFixture)
88
89// one static group and one dynamic group created from file
90BOOST_AUTO_TEST_CASE(Insert)
91{
92 // Static
93 anchorContainer.insert("group1", Certificate(cert1));
94 BOOST_CHECK(anchorContainer.find(cert1.getName()) != nullptr);
95 BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr);
96 const Certificate* cert = anchorContainer.find(cert1.getName());
97 BOOST_CHECK_NO_THROW(anchorContainer.insert("group1", Certificate(cert1)));
98 BOOST_CHECK_EQUAL(cert, anchorContainer.find(cert1.getName())); // still the same instance of the certificate
99 // cannot add dynamic group when static already exists
Davide Pesavento0f830802018-01-16 23:58:58 -0500100 BOOST_CHECK_THROW(anchorContainer.insert("group1", certPath1.string(), 1_s), TrustAnchorContainer::Error);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700101 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group1").size(), 1);
102 BOOST_CHECK_EQUAL(anchorContainer.size(), 1);
103
104 // From file
Davide Pesavento0f830802018-01-16 23:58:58 -0500105 anchorContainer.insert("group2", certPath2.string(), 1_s);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700106 BOOST_CHECK(anchorContainer.find(cert2.getName()) != nullptr);
107 BOOST_CHECK(anchorContainer.find(identity2.getName()) != nullptr);
108 BOOST_CHECK_THROW(anchorContainer.insert("group2", Certificate(cert2)), TrustAnchorContainer::Error);
Davide Pesavento0f830802018-01-16 23:58:58 -0500109 BOOST_CHECK_THROW(anchorContainer.insert("group2", certPath2.string(), 1_s), TrustAnchorContainer::Error);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700110 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 1);
111 BOOST_CHECK_EQUAL(anchorContainer.size(), 2);
112
113 boost::filesystem::remove(certPath2);
Davide Pesavento0f830802018-01-16 23:58:58 -0500114 advanceClocks(1_s, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700115
116 BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr);
117 BOOST_CHECK(anchorContainer.find(cert2.getName()) == nullptr);
118 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 0);
119 BOOST_CHECK_EQUAL(anchorContainer.size(), 1);
120
121 TrustAnchorGroup& group = anchorContainer.getGroup("group1");
122 auto staticGroup = dynamic_cast<StaticTrustAnchorGroup*>(&group);
123 BOOST_REQUIRE(staticGroup != nullptr);
124 BOOST_CHECK_EQUAL(staticGroup->size(), 1);
125 staticGroup->remove(cert1.getName());
126 BOOST_CHECK_EQUAL(staticGroup->size(), 0);
127 BOOST_CHECK_EQUAL(anchorContainer.size(), 0);
128
129 BOOST_CHECK_THROW(anchorContainer.getGroup("non-existing-group"), TrustAnchorContainer::Error);
130}
131
132BOOST_AUTO_TEST_CASE(DynamicAnchorFromDir)
133{
134 boost::filesystem::remove(certPath2);
135
Davide Pesavento0f830802018-01-16 23:58:58 -0500136 anchorContainer.insert("group", certDirPath.string(), 1_s, true /* isDir */);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700137
138 BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr);
139 BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr);
140 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 1);
141
142 saveCertToFile(cert2, certPath2.string());
143
Davide Pesavento0f830802018-01-16 23:58:58 -0500144 advanceClocks(100_ms, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700145
146 BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr);
147 BOOST_CHECK(anchorContainer.find(identity2.getName()) != nullptr);
148 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 2);
149
150 boost::filesystem::remove_all(certDirPath);
151
Davide Pesavento0f830802018-01-16 23:58:58 -0500152 advanceClocks(100_ms, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700153
154 BOOST_CHECK(anchorContainer.find(identity1.getName()) == nullptr);
155 BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr);
156 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 0);
157}
158
159BOOST_FIXTURE_TEST_CASE(FindByInterest, AnchorContainerTestFixture)
160{
Davide Pesavento0f830802018-01-16 23:58:58 -0500161 anchorContainer.insert("group1", certPath1.string(), 1_s);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700162 Interest interest(identity1.getName());
163 BOOST_CHECK(anchorContainer.find(interest) != nullptr);
164 Interest interest1(identity1.getName().getPrefix(-1));
165 BOOST_CHECK(anchorContainer.find(interest1) != nullptr);
166 Interest interest2(Name(identity1.getName()).appendVersion());
167 BOOST_CHECK(anchorContainer.find(interest2) == nullptr);
168
169 Certificate cert3 = addCertificate(identity1.getDefaultKey(), "3");
170 Certificate cert4 = addCertificate(identity1.getDefaultKey(), "4");
171 Certificate cert5 = addCertificate(identity1.getDefaultKey(), "5");
172
173 Certificate cert3Copy = cert3;
174 anchorContainer.insert("group2", std::move(cert3Copy));
175 anchorContainer.insert("group3", std::move(cert4));
176 anchorContainer.insert("group4", std::move(cert5));
177
178 Interest interest3(cert3.getKeyName());
179 const Certificate* foundCert = anchorContainer.find(interest3);
180 BOOST_REQUIRE(foundCert != nullptr);
181 BOOST_CHECK(interest3.getName().isPrefixOf(foundCert->getName()));
182 BOOST_CHECK_EQUAL(foundCert->getName(), cert3.getName());
183
184 interest3.setExclude(Exclude().excludeOne(cert3.getName().at(Certificate::ISSUER_ID_OFFSET)));
185 foundCert = anchorContainer.find(interest3);
186 BOOST_REQUIRE(foundCert != nullptr);
187 BOOST_CHECK(interest3.getName().isPrefixOf(foundCert->getName()));
188 BOOST_CHECK_NE(foundCert->getName(), cert3.getName());
189}
190
191BOOST_AUTO_TEST_SUITE_END() // TestTrustAnchorContainer
192BOOST_AUTO_TEST_SUITE_END() // Detail
193BOOST_AUTO_TEST_SUITE_END() // Security
194
195} // namespace tests
196} // namespace v2
197} // namespace security
198} // namespace ndn