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