Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame^] | 2 | /* |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -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 | |
Alexander Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 22 | #include "security/pib/pib-memory.hpp" |
| 23 | #include "security/pib/pib-sqlite3.hpp" |
| 24 | #include "security/pib/pib.hpp" |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 25 | #include "security/security-common.hpp" |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 26 | |
| 27 | #include "boost-test.hpp" |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 28 | #include "pib-data-fixture.hpp" |
| 29 | |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 30 | #include <boost/filesystem.hpp> |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 31 | #include <boost/mpl/list.hpp> |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 32 | |
| 33 | namespace ndn { |
| 34 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 35 | namespace pib { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 36 | namespace tests { |
| 37 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 38 | using namespace ndn::security::tests; |
| 39 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 40 | BOOST_AUTO_TEST_SUITE(Security) |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 41 | BOOST_AUTO_TEST_SUITE(Pib) |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 42 | BOOST_AUTO_TEST_SUITE(TestPibImpl) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 43 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 44 | using pib::Pib; |
| 45 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 46 | class PibMemoryFixture : public PibDataFixture |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 47 | { |
| 48 | public: |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 49 | PibMemory pib; |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 52 | class PibSqlite3Fixture : public PibDataFixture |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 53 | { |
| 54 | public: |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 55 | PibSqlite3Fixture() |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 56 | : tmpPath(boost::filesystem::path(UNIT_TEST_CONFIG_PATH) / "DbTest") |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 57 | , pib(tmpPath.c_str()) |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 58 | { |
| 59 | } |
| 60 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 61 | ~PibSqlite3Fixture() |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 62 | { |
| 63 | boost::filesystem::remove_all(tmpPath); |
| 64 | } |
| 65 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 66 | public: |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 67 | boost::filesystem::path tmpPath; |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 68 | PibSqlite3 pib; |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 71 | typedef boost::mpl::list<PibMemoryFixture, |
| 72 | PibSqlite3Fixture> PibImpls; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 73 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 74 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(TpmLocator, T, PibImpls, T) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 75 | { |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 76 | // Basic getting and setting |
| 77 | BOOST_CHECK_NO_THROW(this->pib.getTpmLocator()); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 78 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 79 | BOOST_CHECK_NO_THROW(this->pib.setTpmLocator("tpmLocator")); |
| 80 | BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator"); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 81 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 82 | // Add cert, and do not change TPM locator |
| 83 | this->pib.addCertificate(this->id1Key1Cert1); |
| 84 | BOOST_CHECK(this->pib.hasIdentity(this->id1)); |
| 85 | BOOST_CHECK(this->pib.hasKey(this->id1Key1Name)); |
| 86 | BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName())); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 87 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 88 | // Set TPM locator to the same value, nothing should change |
| 89 | this->pib.setTpmLocator("tpmLocator"); |
| 90 | BOOST_CHECK(this->pib.hasIdentity(this->id1)); |
| 91 | BOOST_CHECK(this->pib.hasKey(this->id1Key1Name)); |
| 92 | BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName())); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 93 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 94 | // Change TPM locator (contents of PIB should not change) |
| 95 | this->pib.setTpmLocator("newTpmLocator"); |
| 96 | BOOST_CHECK(this->pib.hasIdentity(this->id1)); |
| 97 | BOOST_CHECK(this->pib.hasKey(this->id1Key1Name)); |
| 98 | BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName())); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 101 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, T) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 102 | { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 103 | // no default setting, throw Error |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 104 | BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error); |
| 105 | |
| 106 | // check id1, which should not exist |
| 107 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false); |
| 108 | |
| 109 | // add id1, should be default |
| 110 | this->pib.addIdentity(this->id1); |
| 111 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true); |
| 112 | BOOST_CHECK_NO_THROW(this->pib.getDefaultIdentity()); |
| 113 | BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1); |
| 114 | |
| 115 | // add id2, should not be default |
| 116 | this->pib.addIdentity(this->id2); |
| 117 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id2), true); |
| 118 | BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1); |
| 119 | |
| 120 | // set id2 explicitly as default |
| 121 | this->pib.setDefaultIdentity(this->id2); |
| 122 | BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2); |
| 123 | |
| 124 | // remove id2, should not have default identity |
| 125 | this->pib.removeIdentity(this->id2); |
| 126 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id2), false); |
| 127 | BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error); |
| 128 | |
| 129 | // add id2 again, should be default |
| 130 | this->pib.addIdentity(this->id2); |
| 131 | BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2); |
| 132 | |
| 133 | // get all identities, should contain id1 and id2 |
| 134 | std::set<Name> idNames = this->pib.getIdentities(); |
| 135 | BOOST_CHECK_EQUAL(idNames.size(), 2); |
| 136 | BOOST_CHECK_EQUAL(idNames.count(this->id1), 1); |
| 137 | BOOST_CHECK_EQUAL(idNames.count(this->id2), 1); |
| 138 | } |
| 139 | |
| 140 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(ClearIdentities, T, PibImpls, T) |
| 141 | { |
| 142 | this->pib.setTpmLocator("tpmLocator"); |
| 143 | |
| 144 | // Add id, key, and cert |
| 145 | this->pib.addCertificate(this->id1Key1Cert1); |
| 146 | BOOST_CHECK(this->pib.hasIdentity(this->id1)); |
| 147 | BOOST_CHECK(this->pib.hasKey(this->id1Key1Name)); |
| 148 | BOOST_CHECK(this->pib.hasCertificate(this->id1Key1Cert1.getName())); |
| 149 | |
| 150 | // Clear identities |
| 151 | this->pib.clearIdentities(); |
| 152 | BOOST_CHECK_EQUAL(this->pib.getIdentities().size(), 0); |
| 153 | BOOST_CHECK_EQUAL(this->pib.getKeysOfIdentity(this->id1).size(), 0); |
| 154 | BOOST_CHECK_EQUAL(this->pib.getCertificatesOfKey(this->id1Key1Name).size(), 0); |
| 155 | BOOST_CHECK_EQUAL(this->pib.getTpmLocator(), "tpmLocator"); |
| 156 | } |
| 157 | |
| 158 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(KeyManagement, T, PibImpls, T) |
| 159 | { |
| 160 | // no default setting, throw Error |
| 161 | BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id1), Pib::Error); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 162 | |
| 163 | // check id1Key1, should not exist, neither should id1. |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 164 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false); |
| 165 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 166 | |
| 167 | // add id1Key1, should be default, id1 should be added implicitly |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame^] | 168 | this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1.data(), this->id1Key1.size()); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 169 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true); |
| 170 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true); |
| 171 | const Buffer& keyBits = this->pib.getKeyBits(this->id1Key1Name); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 172 | BOOST_CHECK(keyBits == this->id1Key1); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 173 | BOOST_CHECK_NO_THROW(this->pib.getDefaultKeyOfIdentity(this->id1)); |
| 174 | BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 175 | |
| 176 | // add id1Key2, should not be default |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame^] | 177 | this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2.data(), this->id1Key2.size()); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 178 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key2Name), true); |
| 179 | BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key1Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 180 | |
| 181 | // set id1Key2 explicitly as default |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 182 | this->pib.setDefaultKeyOfIdentity(this->id1, this->id1Key2Name); |
| 183 | BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 184 | |
| 185 | // set a non-existing key as default, throw Error |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 186 | BOOST_CHECK_THROW(this->pib.setDefaultKeyOfIdentity(this->id1, Name("/non-existing")), |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 187 | Pib::Error); |
| 188 | |
| 189 | // remove id1Key2, should not have default key |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 190 | this->pib.removeKey(this->id1Key2Name); |
| 191 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key2Name), false); |
| 192 | BOOST_CHECK_THROW(this->pib.getKeyBits(this->id1Key2Name), Pib::Error); |
| 193 | BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id1), Pib::Error); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 194 | |
| 195 | // add id1Key2 back, should be default |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame^] | 196 | this->pib.addKey(this->id1, this->id1Key2Name, this->id1Key2.data(), this->id1Key2.size()); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 197 | BOOST_CHECK_NO_THROW(this->pib.getKeyBits(this->id1Key2Name)); |
| 198 | BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id1), this->id1Key2Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 199 | |
| 200 | // get all the keys: id1Key1 and id1Key2 |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 201 | std::set<Name> keyNames = this->pib.getKeysOfIdentity(this->id1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 202 | BOOST_CHECK_EQUAL(keyNames.size(), 2); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 203 | BOOST_CHECK_EQUAL(keyNames.count(this->id1Key1Name), 1); |
| 204 | BOOST_CHECK_EQUAL(keyNames.count(this->id1Key2Name), 1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 205 | |
| 206 | // remove id1, should remove all the keys |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 207 | this->pib.removeIdentity(this->id1); |
| 208 | keyNames = this->pib.getKeysOfIdentity(this->id1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 209 | BOOST_CHECK_EQUAL(keyNames.size(), 0); |
| 210 | } |
| 211 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 212 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, T) |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 213 | { |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 214 | // no default setting, throw Error |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 215 | BOOST_CHECK_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), Pib::Error); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 216 | |
| 217 | // check id1Key1Cert1, should not exist, neither should id1 and id1Key1 |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 218 | BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), false); |
| 219 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), false); |
| 220 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 221 | |
| 222 | // add id1Key1Cert1, should be default, id1 and id1Key1 should be added implicitly |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 223 | this->pib.addCertificate(this->id1Key1Cert1); |
| 224 | BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), true); |
| 225 | BOOST_CHECK_EQUAL(this->pib.hasIdentity(this->id1), true); |
| 226 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 227 | BOOST_CHECK(this->pib.getCertificate(this->id1Key1Cert1.getName()).wireEncode() == this->id1Key1Cert1.wireEncode()); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 228 | BOOST_CHECK_NO_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name)); |
| 229 | BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 230 | |
| 231 | // add id1Key1Cert2, should not be default |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 232 | this->pib.addCertificate(this->id1Key1Cert2); |
| 233 | BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert2.getName()), true); |
| 234 | BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 235 | |
| 236 | // set id1Key1Cert2 explicitly as default |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 237 | this->pib.setDefaultCertificateOfKey(this->id1Key1Name, this->id1Key1Cert2.getName()); |
| 238 | BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert2); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 239 | |
| 240 | // set a non-existing cert as default, throw Error |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 241 | BOOST_CHECK_THROW(this->pib.setDefaultCertificateOfKey(this->id1Key1Name, Name("/non-existing")), |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 242 | Pib::Error); |
| 243 | |
| 244 | // remove id1Key1Cert2, should not have default cert |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 245 | this->pib.removeCertificate(this->id1Key1Cert2.getName()); |
| 246 | BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert2.getName()), false); |
| 247 | BOOST_CHECK_THROW(this->pib.getCertificate(this->id1Key1Cert2.getName()), Pib::Error); |
| 248 | BOOST_CHECK_THROW(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), Pib::Error); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 249 | |
| 250 | // add id1Key1Cert2, should be default |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 251 | this->pib.addCertificate(this->id1Key1Cert2); |
| 252 | BOOST_CHECK_NO_THROW(this->pib.getCertificate(this->id1Key1Cert1.getName())); |
| 253 | BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id1Key1Name), this->id1Key1Cert2); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 254 | |
| 255 | // get all certificates: id1Key1Cert1 and id1Key1Cert2 |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 256 | std::set<Name> certNames = this->pib.getCertificatesOfKey(this->id1Key1Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 257 | BOOST_CHECK_EQUAL(certNames.size(), 2); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 258 | BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert1.getName()), 1); |
| 259 | BOOST_CHECK_EQUAL(certNames.count(this->id1Key1Cert2.getName()), 1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 260 | |
| 261 | // remove id1Key1, should remove all the certs |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 262 | this->pib.removeKey(this->id1Key1Name); |
| 263 | certNames = this->pib.getCertificatesOfKey(this->id1Key1Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 264 | BOOST_CHECK_EQUAL(certNames.size(), 0); |
| 265 | } |
| 266 | |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 267 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(DefaultsManagement, T, PibImpls, T) |
| 268 | { |
| 269 | this->pib.addIdentity(this->id1); |
| 270 | BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1); |
| 271 | |
| 272 | this->pib.addIdentity(this->id2); |
| 273 | BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id1); |
| 274 | |
| 275 | this->pib.removeIdentity(this->id1); |
| 276 | BOOST_CHECK_THROW(this->pib.getDefaultIdentity(), Pib::Error); |
| 277 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame^] | 278 | this->pib.addKey(this->id2, this->id2Key1Name, this->id2Key1.data(), this->id2Key1.size()); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 279 | BOOST_CHECK_EQUAL(this->pib.getDefaultIdentity(), this->id2); |
| 280 | BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key1Name); |
| 281 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame^] | 282 | this->pib.addKey(this->id2, this->id2Key2Name, this->id2Key2.data(), this->id2Key2.size()); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 283 | BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key1Name); |
| 284 | |
| 285 | this->pib.removeKey(this->id2Key1Name); |
| 286 | BOOST_CHECK_THROW(this->pib.getDefaultKeyOfIdentity(this->id2), Pib::Error); |
| 287 | |
| 288 | this->pib.addCertificate(this->id2Key2Cert1); |
| 289 | BOOST_CHECK_EQUAL(this->pib.getDefaultKeyOfIdentity(this->id2), this->id2Key2Name); |
| 290 | BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(), this->id2Key2Cert1.getName()); |
| 291 | |
| 292 | this->pib.addCertificate(this->id2Key2Cert2); |
| 293 | BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(), this->id2Key2Cert1.getName()); |
| 294 | |
| 295 | this->pib.removeCertificate(this->id2Key2Cert2.getName()); |
| 296 | BOOST_CHECK_EQUAL(this->pib.getDefaultCertificateOfKey(this->id2Key2Name).getName(), this->id2Key2Cert1.getName()); |
| 297 | } |
| 298 | |
| 299 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Overwrite, T, PibImpls, T) |
| 300 | { |
| 301 | // check id1Key1, should not exist |
| 302 | this->pib.removeIdentity(this->id1); |
| 303 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), false); |
| 304 | |
| 305 | // add id1Key1 |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame^] | 306 | this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1.data(), this->id1Key1.size()); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 307 | BOOST_CHECK_EQUAL(this->pib.hasKey(this->id1Key1Name), true); |
| 308 | const Buffer& keyBits = this->pib.getKeyBits(this->id1Key1Name); |
| 309 | BOOST_CHECK(keyBits == this->id1Key1); |
| 310 | |
| 311 | // check overwrite, add a key with the same name. |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame^] | 312 | this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key2.data(), this->id1Key2.size()); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 313 | const Buffer& keyBits2 = this->pib.getKeyBits(this->id1Key1Name); |
| 314 | BOOST_CHECK(keyBits2 == this->id1Key2); |
| 315 | |
| 316 | // check id1Key1Cert1, should not exist |
| 317 | this->pib.removeIdentity(this->id1); |
| 318 | BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), false); |
| 319 | |
| 320 | // add id1Key1Cert1 |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame^] | 321 | this->pib.addKey(this->id1, this->id1Key1Name, this->id1Key1.data(), this->id1Key1.size()); |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 322 | this->pib.addCertificate(this->id1Key1Cert1); |
| 323 | BOOST_CHECK_EQUAL(this->pib.hasCertificate(this->id1Key1Cert1.getName()), true); |
| 324 | |
| 325 | auto cert = this->pib.getCertificate(this->id1Key1Cert1.getName()); |
| 326 | BOOST_CHECK(cert.wireEncode() == this->id1Key1Cert1.wireEncode()); |
| 327 | |
| 328 | // Create a fake cert with the same name |
| 329 | auto cert2 = this->id1Key2Cert1; |
| 330 | cert2.setName(this->id1Key1Cert1.getName()); |
| 331 | cert2.setSignature(this->id1Key2Cert1.getSignature()); |
| 332 | this->pib.addCertificate(cert2); |
| 333 | |
| 334 | auto cert3 = this->pib.getCertificate(this->id1Key1Cert1.getName()); |
| 335 | BOOST_CHECK(cert3.wireEncode() == cert2.wireEncode()); |
| 336 | |
| 337 | // both key and certificate are overwritten |
| 338 | Buffer keyBits3 = this->pib.getKeyBits(this->id1Key1Name); |
| 339 | BOOST_CHECK(keyBits3 == this->id1Key2); |
| 340 | } |
| 341 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 342 | BOOST_AUTO_TEST_SUITE_END() // TestPibImpl |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 343 | BOOST_AUTO_TEST_SUITE_END() // Pib |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 344 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 345 | |
| 346 | } // namespace tests |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 347 | } // namespace pib |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 348 | } // namespace security |
| 349 | } // namespace ndn |