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