Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/v2/trust-anchor-container.hpp" |
| 23 | #include "ndn-cxx/util/io.hpp" |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
| 26 | #include "tests/unit/identity-management-time-fixture.hpp" |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 27 | |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 28 | #include <boost/filesystem.hpp> |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace security { |
| 32 | namespace v2 { |
| 33 | namespace tests { |
| 34 | |
| 35 | using namespace ndn::tests; |
| 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(Security) |
| 38 | BOOST_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 | */ |
| 44 | class AnchorContainerTestFixture : public IdentityManagementTimeFixture |
| 45 | { |
| 46 | public: |
| 47 | AnchorContainerTestFixture() |
| 48 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 49 | namespace fs = boost::filesystem; |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 50 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 51 | fs::create_directory(fs::path(UNIT_TEST_CONFIG_PATH)); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 52 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 53 | certDirPath = fs::path(UNIT_TEST_CONFIG_PATH) / "test-cert-dir"; |
| 54 | fs::create_directory(certDirPath); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 55 | |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 56 | 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 Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 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 | |
| 73 | public: |
| 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 | |
| 87 | BOOST_FIXTURE_TEST_SUITE(TestTrustAnchorContainer, AnchorContainerTestFixture) |
| 88 | |
| 89 | // one static group and one dynamic group created from file |
| 90 | BOOST_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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 100 | BOOST_CHECK_THROW(anchorContainer.insert("group1", certPath1.string(), 1_s), TrustAnchorContainer::Error); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(anchorContainer.getGroup("group1").size(), 1); |
| 102 | BOOST_CHECK_EQUAL(anchorContainer.size(), 1); |
| 103 | |
| 104 | // From file |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 105 | anchorContainer.insert("group2", certPath2.string(), 1_s); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 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); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 109 | BOOST_CHECK_THROW(anchorContainer.insert("group2", certPath2.string(), 1_s), TrustAnchorContainer::Error); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(anchorContainer.getGroup("group2").size(), 1); |
| 111 | BOOST_CHECK_EQUAL(anchorContainer.size(), 2); |
| 112 | |
| 113 | boost::filesystem::remove(certPath2); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 114 | advanceClocks(1_s, 11); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 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 | |
| 132 | BOOST_AUTO_TEST_CASE(DynamicAnchorFromDir) |
| 133 | { |
| 134 | boost::filesystem::remove(certPath2); |
| 135 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 136 | anchorContainer.insert("group", certDirPath.string(), 1_s, true /* isDir */); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 144 | advanceClocks(100_ms, 11); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 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 | |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 152 | advanceClocks(100_ms, 11); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 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 | |
| 159 | BOOST_FIXTURE_TEST_CASE(FindByInterest, AnchorContainerTestFixture) |
| 160 | { |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 161 | anchorContainer.insert("group1", certPath1.string(), 1_s); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 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 | |
| 191 | BOOST_AUTO_TEST_SUITE_END() // TestTrustAnchorContainer |
| 192 | BOOST_AUTO_TEST_SUITE_END() // Detail |
| 193 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 194 | |
| 195 | } // namespace tests |
| 196 | } // namespace v2 |
| 197 | } // namespace security |
| 198 | } // namespace ndn |