Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 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 | |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 46 | class PibMemoryWrapper |
| 47 | { |
| 48 | public: |
| 49 | PibMemory impl; |
| 50 | }; |
| 51 | |
| 52 | class PibSqlite3Wrapper |
| 53 | { |
| 54 | public: |
| 55 | PibSqlite3Wrapper() |
| 56 | : tmpPath(boost::filesystem::path(UNIT_TEST_CONFIG_PATH) / "DbTest") |
| 57 | , impl(tmpPath.c_str()) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | ~PibSqlite3Wrapper() |
| 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; |
| 68 | PibSqlite3 impl; |
| 69 | }; |
| 70 | |
| 71 | typedef boost::mpl::list<PibMemoryWrapper, |
| 72 | PibSqlite3Wrapper> PibImpls; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 73 | |
| 74 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, PibDataFixture) |
| 75 | { |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 76 | T wrapper; |
| 77 | PibImpl& pibImpl = wrapper.impl; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 78 | |
| 79 | // no default setting, throw Error |
| 80 | BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error); |
| 81 | |
| 82 | // check id1, which should not exist |
| 83 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false); |
| 84 | |
| 85 | // add id1, should be default |
| 86 | pibImpl.addIdentity(id1); |
| 87 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true); |
| 88 | BOOST_CHECK_NO_THROW(pibImpl.getDefaultIdentity()); |
| 89 | BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id1); |
| 90 | |
| 91 | // add id2, should not be default |
| 92 | pibImpl.addIdentity(id2); |
| 93 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id2), true); |
| 94 | BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id1); |
| 95 | |
| 96 | // set id2 explicitly as default |
| 97 | pibImpl.setDefaultIdentity(id2); |
| 98 | BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id2); |
| 99 | |
| 100 | // remove id2, should not have default identity |
| 101 | pibImpl.removeIdentity(id2); |
| 102 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id2), false); |
| 103 | BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error); |
| 104 | |
| 105 | // add id2 again, should be default |
| 106 | pibImpl.addIdentity(id2); |
| 107 | BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id2); |
| 108 | |
| 109 | // get all identities, should contain id1 and id2 |
| 110 | std::set<Name> idNames = pibImpl.getIdentities(); |
| 111 | BOOST_CHECK_EQUAL(idNames.size(), 2); |
| 112 | BOOST_CHECK_EQUAL(idNames.count(id1), 1); |
| 113 | BOOST_CHECK_EQUAL(idNames.count(id2), 1); |
| 114 | } |
| 115 | |
| 116 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(KeyManagement, T, PibImpls, PibDataFixture) |
| 117 | { |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 118 | T wrapper; |
| 119 | PibImpl& pibImpl = wrapper.impl; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 120 | |
| 121 | // no default setting, throw Error |
| 122 | BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error); |
| 123 | |
| 124 | // check id1Key1, should not exist, neither should id1. |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 125 | BOOST_CHECK_EQUAL(pibImpl.hasKey(id1Key1Name), false); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false); |
| 127 | |
| 128 | // add id1Key1, should be default, id1 should be added implicitly |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 129 | pibImpl.addKey(id1, id1Key1Name, id1Key1.buf(), id1Key1.size()); |
| 130 | BOOST_CHECK_EQUAL(pibImpl.hasKey(id1Key1Name), true); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 131 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 132 | const Buffer& keyBits = pibImpl.getKeyBits(id1Key1Name); |
| 133 | BOOST_CHECK_EQUAL_COLLECTIONS(keyBits.begin(), keyBits.end(), id1Key1.begin(), id1Key1.end()); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 134 | BOOST_CHECK_NO_THROW(pibImpl.getDefaultKeyOfIdentity(id1)); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 135 | BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 136 | |
| 137 | // add id1Key2, should not be default |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 138 | pibImpl.addKey(id1, id1Key2Name, id1Key2.buf(), id1Key2.size()); |
| 139 | BOOST_CHECK_EQUAL(pibImpl.hasKey(id1Key2Name), true); |
| 140 | BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 141 | |
| 142 | // set id1Key2 explicitly as default |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 143 | pibImpl.setDefaultKeyOfIdentity(id1, id1Key2Name); |
| 144 | BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key2Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 145 | |
| 146 | // set a non-existing key as default, throw Error |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 147 | BOOST_CHECK_THROW(pibImpl.setDefaultKeyOfIdentity(id1, Name("/non-existing")), |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 148 | Pib::Error); |
| 149 | |
| 150 | // remove id1Key2, should not have default key |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 151 | pibImpl.removeKey(id1Key2Name); |
| 152 | BOOST_CHECK_EQUAL(pibImpl.hasKey(id1Key2Name), false); |
| 153 | BOOST_CHECK_THROW(pibImpl.getKeyBits(id1Key2Name), Pib::Error); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 154 | BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error); |
| 155 | |
| 156 | // add id1Key2 back, should be default |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 157 | pibImpl.addKey(id1, id1Key2Name, id1Key2.buf(), id1Key2.size()); |
| 158 | BOOST_CHECK_NO_THROW(pibImpl.getKeyBits(id1Key2Name)); |
| 159 | BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key2Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 160 | |
| 161 | // get all the keys: id1Key1 and id1Key2 |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 162 | std::set<Name> keyNames = pibImpl.getKeysOfIdentity(id1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 163 | BOOST_CHECK_EQUAL(keyNames.size(), 2); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 164 | BOOST_CHECK_EQUAL(keyNames.count(id1Key1Name), 1); |
| 165 | BOOST_CHECK_EQUAL(keyNames.count(id1Key2Name), 1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 166 | |
| 167 | // remove id1, should remove all the keys |
| 168 | pibImpl.removeIdentity(id1); |
| 169 | keyNames = pibImpl.getKeysOfIdentity(id1); |
| 170 | BOOST_CHECK_EQUAL(keyNames.size(), 0); |
| 171 | } |
| 172 | |
| 173 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, PibDataFixture) |
| 174 | { |
Mickey Sweatt | 11314b7 | 2015-06-10 17:20:19 -0700 | [diff] [blame] | 175 | T wrapper; |
| 176 | PibImpl& pibImpl = wrapper.impl; |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 177 | |
| 178 | // no default setting, throw Error |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 179 | BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1Key1Name), Pib::Error); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 180 | |
| 181 | // check id1Key1Cert1, should not exist, neither should id1 and id1Key1 |
| 182 | BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), false); |
| 183 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 184 | BOOST_CHECK_EQUAL(pibImpl.hasKey(id1Key1Name), false); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 185 | |
| 186 | // add id1Key1Cert1, should be default, id1 and id1Key1 should be added implicitly |
| 187 | pibImpl.addCertificate(id1Key1Cert1); |
| 188 | BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), true); |
| 189 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 190 | BOOST_CHECK_EQUAL(pibImpl.hasKey(id1Key1Name), true); |
| 191 | const auto& cert = pibImpl.getCertificate(id1Key1Cert1.getName()); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 192 | BOOST_CHECK_EQUAL_COLLECTIONS(cert.wireEncode().wire(), |
| 193 | cert.wireEncode().wire() + cert.wireEncode().size(), |
| 194 | id1Key1Cert1.wireEncode().wire(), |
| 195 | id1Key1Cert1.wireEncode().wire() + id1Key1Cert1.wireEncode().size()); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 196 | BOOST_CHECK_NO_THROW(pibImpl.getDefaultCertificateOfKey(id1Key1Name)); |
| 197 | BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1Key1Name), id1Key1Cert1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 198 | |
| 199 | // add id1Key1Cert2, should not be default |
| 200 | pibImpl.addCertificate(id1Key1Cert2); |
| 201 | BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert2.getName()), true); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 202 | BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1Key1Name), id1Key1Cert1); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 203 | |
| 204 | // set id1Key1Cert2 explicitly as default |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 205 | pibImpl.setDefaultCertificateOfKey(id1Key1Name, id1Key1Cert2.getName()); |
| 206 | BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1Key1Name), id1Key1Cert2); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 207 | |
| 208 | // set a non-existing cert as default, throw Error |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 209 | BOOST_CHECK_THROW(pibImpl.setDefaultCertificateOfKey(id1Key1Name, Name("/non-existing")), |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 210 | Pib::Error); |
| 211 | |
| 212 | // remove id1Key1Cert2, should not have default cert |
| 213 | pibImpl.removeCertificate(id1Key1Cert2.getName()); |
| 214 | BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert2.getName()), false); |
| 215 | BOOST_CHECK_THROW(pibImpl.getCertificate(id1Key1Cert2.getName()), Pib::Error); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 216 | BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1Key1Name), Pib::Error); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 217 | |
| 218 | // add id1Key1Cert2, should be default |
| 219 | pibImpl.addCertificate(id1Key1Cert2); |
| 220 | BOOST_CHECK_NO_THROW(pibImpl.getCertificate(id1Key1Cert1.getName())); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 221 | BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1Key1Name), id1Key1Cert2); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 222 | |
| 223 | // get all certificates: id1Key1Cert1 and id1Key1Cert2 |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 224 | std::set<Name> certNames = pibImpl.getCertificatesOfKey(id1Key1Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 225 | BOOST_CHECK_EQUAL(certNames.size(), 2); |
| 226 | BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert1.getName()), 1); |
| 227 | BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert2.getName()), 1); |
| 228 | |
| 229 | // remove id1Key1, should remove all the certs |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 230 | pibImpl.removeKey(id1Key1Name); |
| 231 | certNames = pibImpl.getCertificatesOfKey(id1Key1Name); |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 232 | BOOST_CHECK_EQUAL(certNames.size(), 0); |
| 233 | } |
| 234 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 235 | BOOST_AUTO_TEST_SUITE_END() // TestPibImpl |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 236 | BOOST_AUTO_TEST_SUITE_END() // Pib |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 237 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 238 | |
| 239 | } // namespace tests |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 240 | } // namespace pib |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 241 | } // namespace security |
| 242 | } // namespace ndn |