blob: 0efecdce585ad638526d47ceb8ea0336a4b283a8 [file] [log] [blame]
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2017 Regents of the University of California.
4 *
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"
23
24#include "../../identity-management-time-fixture.hpp"
25#include "util/io.hpp"
26#include "boost-test.hpp"
27#include <boost/filesystem.hpp>
28
29namespace ndn {
30namespace security {
31namespace v2 {
32namespace tests {
33
34using namespace ndn::tests;
35
36BOOST_AUTO_TEST_SUITE(Security)
37BOOST_AUTO_TEST_SUITE(V2)
38
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 {
48 boost::filesystem::create_directory(boost::filesystem::path(UNIT_TEST_CONFIG_PATH));
49
50 certDirPath = boost::filesystem::path(UNIT_TEST_CONFIG_PATH) / std::string("test-cert-dir");
51 boost::filesystem::create_directory(certDirPath);
52
53 certPath1 = boost::filesystem::path(UNIT_TEST_CONFIG_PATH) /
54 std::string("test-cert-dir") / std::string("trust-anchor-1.cert");
55
56 certPath2 = boost::filesystem::path(UNIT_TEST_CONFIG_PATH) /
57 std::string("test-cert-dir") / std::string("trust-anchor-2.cert");
58
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
100 BOOST_CHECK_THROW(anchorContainer.insert("group1", certPath1.string(), time::seconds(1)), TrustAnchorContainer::Error);
101 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group1").size(), 1);
102 BOOST_CHECK_EQUAL(anchorContainer.size(), 1);
103
104 // From file
105 anchorContainer.insert("group2", certPath2.string(), time::seconds(1));
106 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);
109 BOOST_CHECK_THROW(anchorContainer.insert("group2", certPath2.string(), time::seconds(1)), TrustAnchorContainer::Error);
110 BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 1);
111 BOOST_CHECK_EQUAL(anchorContainer.size(), 2);
112
113 boost::filesystem::remove(certPath2);
114 advanceClocks(time::seconds(1), 11);
115
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
136 anchorContainer.insert("group", certDirPath.string(), time::seconds(1), true /* isDir */);
137
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
144 advanceClocks(time::milliseconds(100), 11);
145
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
152 advanceClocks(time::milliseconds(100), 11);
153
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{
161 anchorContainer.insert("group1", certPath1.string(), time::seconds(1));
162 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