blob: 30749acd8da4433035a204f002905aef7ce91153 [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 {
49 boost::filesystem::create_directory(boost::filesystem::path(UNIT_TEST_CONFIG_PATH));
50
51 certDirPath = boost::filesystem::path(UNIT_TEST_CONFIG_PATH) / std::string("test-cert-dir");
52 boost::filesystem::create_directory(certDirPath);
53
54 certPath1 = boost::filesystem::path(UNIT_TEST_CONFIG_PATH) /
55 std::string("test-cert-dir") / std::string("trust-anchor-1.cert");
56
57 certPath2 = boost::filesystem::path(UNIT_TEST_CONFIG_PATH) /
58 std::string("test-cert-dir") / std::string("trust-anchor-2.cert");
59
60 identity1 = addIdentity("/TestAnchorContainer/First");
61 cert1 = identity1.getDefaultKey().getDefaultCertificate();
62 saveCertToFile(cert1, certPath1.string());
63
64 identity2 = addIdentity("/TestAnchorContainer/Second");
65 cert2 = identity2.getDefaultKey().getDefaultCertificate();
66 saveCertToFile(cert2, certPath2.string());
67 }
68
69 ~AnchorContainerTestFixture()
70 {
71 boost::filesystem::remove_all(UNIT_TEST_CONFIG_PATH);
72 }
73
74public:
75 TrustAnchorContainer anchorContainer;
76
77 boost::filesystem::path certDirPath;
78 boost::filesystem::path certPath1;
79 boost::filesystem::path certPath2;
80
81 Identity identity1;
82 Identity identity2;
83
84 Certificate cert1;
85 Certificate cert2;
86};
87
88BOOST_FIXTURE_TEST_SUITE(TestTrustAnchorContainer, AnchorContainerTestFixture)
89
90// one static group and one dynamic group created from file
91BOOST_AUTO_TEST_CASE(Insert)
92{
93 // Static
94 anchorContainer.insert("group1", Certificate(cert1));
95 BOOST_CHECK(anchorContainer.find(cert1.getName()) != nullptr);
96 BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr);
97 const Certificate* cert = anchorContainer.find(cert1.getName());
98 BOOST_CHECK_NO_THROW(anchorContainer.insert("group1", Certificate(cert1)));
99 BOOST_CHECK_EQUAL(cert, anchorContainer.find(cert1.getName())); // still the same instance of the certificate
100 // cannot add dynamic group when static already exists
Davide Pesavento0f830802018-01-16 23:58:58 -0500101 BOOST_CHECK_THROW(anchorContainer.insert("group1", certPath1.string(), 1_s), TrustAnchorContainer::Error);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700102 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group1").size(), 1);
103 BOOST_CHECK_EQUAL(anchorContainer.size(), 1);
104
105 // From file
Davide Pesavento0f830802018-01-16 23:58:58 -0500106 anchorContainer.insert("group2", certPath2.string(), 1_s);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700107 BOOST_CHECK(anchorContainer.find(cert2.getName()) != nullptr);
108 BOOST_CHECK(anchorContainer.find(identity2.getName()) != nullptr);
109 BOOST_CHECK_THROW(anchorContainer.insert("group2", Certificate(cert2)), TrustAnchorContainer::Error);
Davide Pesavento0f830802018-01-16 23:58:58 -0500110 BOOST_CHECK_THROW(anchorContainer.insert("group2", certPath2.string(), 1_s), TrustAnchorContainer::Error);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700111 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 1);
112 BOOST_CHECK_EQUAL(anchorContainer.size(), 2);
113
114 boost::filesystem::remove(certPath2);
Davide Pesavento0f830802018-01-16 23:58:58 -0500115 advanceClocks(1_s, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700116
117 BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr);
118 BOOST_CHECK(anchorContainer.find(cert2.getName()) == nullptr);
119 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 0);
120 BOOST_CHECK_EQUAL(anchorContainer.size(), 1);
121
122 TrustAnchorGroup& group = anchorContainer.getGroup("group1");
123 auto staticGroup = dynamic_cast<StaticTrustAnchorGroup*>(&group);
124 BOOST_REQUIRE(staticGroup != nullptr);
125 BOOST_CHECK_EQUAL(staticGroup->size(), 1);
126 staticGroup->remove(cert1.getName());
127 BOOST_CHECK_EQUAL(staticGroup->size(), 0);
128 BOOST_CHECK_EQUAL(anchorContainer.size(), 0);
129
130 BOOST_CHECK_THROW(anchorContainer.getGroup("non-existing-group"), TrustAnchorContainer::Error);
131}
132
133BOOST_AUTO_TEST_CASE(DynamicAnchorFromDir)
134{
135 boost::filesystem::remove(certPath2);
136
Davide Pesavento0f830802018-01-16 23:58:58 -0500137 anchorContainer.insert("group", certDirPath.string(), 1_s, true /* isDir */);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700138
139 BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr);
140 BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr);
141 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 1);
142
143 saveCertToFile(cert2, certPath2.string());
144
Davide Pesavento0f830802018-01-16 23:58:58 -0500145 advanceClocks(100_ms, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700146
147 BOOST_CHECK(anchorContainer.find(identity1.getName()) != nullptr);
148 BOOST_CHECK(anchorContainer.find(identity2.getName()) != nullptr);
149 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 2);
150
151 boost::filesystem::remove_all(certDirPath);
152
Davide Pesavento0f830802018-01-16 23:58:58 -0500153 advanceClocks(100_ms, 11);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700154
155 BOOST_CHECK(anchorContainer.find(identity1.getName()) == nullptr);
156 BOOST_CHECK(anchorContainer.find(identity2.getName()) == nullptr);
157 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group").size(), 0);
158}
159
160BOOST_FIXTURE_TEST_CASE(FindByInterest, AnchorContainerTestFixture)
161{
Davide Pesavento0f830802018-01-16 23:58:58 -0500162 anchorContainer.insert("group1", certPath1.string(), 1_s);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700163 Interest interest(identity1.getName());
164 BOOST_CHECK(anchorContainer.find(interest) != nullptr);
165 Interest interest1(identity1.getName().getPrefix(-1));
166 BOOST_CHECK(anchorContainer.find(interest1) != nullptr);
167 Interest interest2(Name(identity1.getName()).appendVersion());
168 BOOST_CHECK(anchorContainer.find(interest2) == nullptr);
169
170 Certificate cert3 = addCertificate(identity1.getDefaultKey(), "3");
171 Certificate cert4 = addCertificate(identity1.getDefaultKey(), "4");
172 Certificate cert5 = addCertificate(identity1.getDefaultKey(), "5");
173
174 Certificate cert3Copy = cert3;
175 anchorContainer.insert("group2", std::move(cert3Copy));
176 anchorContainer.insert("group3", std::move(cert4));
177 anchorContainer.insert("group4", std::move(cert5));
178
179 Interest interest3(cert3.getKeyName());
180 const Certificate* foundCert = anchorContainer.find(interest3);
181 BOOST_REQUIRE(foundCert != nullptr);
182 BOOST_CHECK(interest3.getName().isPrefixOf(foundCert->getName()));
183 BOOST_CHECK_EQUAL(foundCert->getName(), cert3.getName());
184
185 interest3.setExclude(Exclude().excludeOne(cert3.getName().at(Certificate::ISSUER_ID_OFFSET)));
186 foundCert = anchorContainer.find(interest3);
187 BOOST_REQUIRE(foundCert != nullptr);
188 BOOST_CHECK(interest3.getName().isPrefixOf(foundCert->getName()));
189 BOOST_CHECK_NE(foundCert->getName(), cert3.getName());
190}
191
192BOOST_AUTO_TEST_SUITE_END() // TestTrustAnchorContainer
193BOOST_AUTO_TEST_SUITE_END() // Detail
194BOOST_AUTO_TEST_SUITE_END() // Security
195
196} // namespace tests
197} // namespace v2
198} // namespace security
199} // namespace ndn