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" |
| 23 | #include "security/pib.hpp" |
| 24 | #include "pib-data-fixture.hpp" |
| 25 | |
| 26 | #include <boost/test/test_case_template.hpp> |
| 27 | #include <boost/mpl/list.hpp> |
| 28 | #include "boost-test.hpp" |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace security { |
| 32 | namespace tests { |
| 33 | |
| 34 | BOOST_AUTO_TEST_SUITE(SecurityPibImpl) |
| 35 | |
| 36 | typedef boost::mpl::list<PibMemory> PibImpls; |
| 37 | |
| 38 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, PibDataFixture) |
| 39 | { |
| 40 | T pibImpl; |
| 41 | |
| 42 | // no default setting, throw Error |
| 43 | BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error); |
| 44 | |
| 45 | // check id1, which should not exist |
| 46 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false); |
| 47 | |
| 48 | // add id1, should be default |
| 49 | pibImpl.addIdentity(id1); |
| 50 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true); |
| 51 | BOOST_CHECK_NO_THROW(pibImpl.getDefaultIdentity()); |
| 52 | BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id1); |
| 53 | |
| 54 | // add id2, should not be default |
| 55 | pibImpl.addIdentity(id2); |
| 56 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id2), true); |
| 57 | BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id1); |
| 58 | |
| 59 | // set id2 explicitly as default |
| 60 | pibImpl.setDefaultIdentity(id2); |
| 61 | BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id2); |
| 62 | |
| 63 | // remove id2, should not have default identity |
| 64 | pibImpl.removeIdentity(id2); |
| 65 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id2), false); |
| 66 | BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error); |
| 67 | |
| 68 | // add id2 again, should be default |
| 69 | pibImpl.addIdentity(id2); |
| 70 | BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id2); |
| 71 | |
| 72 | // get all identities, should contain id1 and id2 |
| 73 | std::set<Name> idNames = pibImpl.getIdentities(); |
| 74 | BOOST_CHECK_EQUAL(idNames.size(), 2); |
| 75 | BOOST_CHECK_EQUAL(idNames.count(id1), 1); |
| 76 | BOOST_CHECK_EQUAL(idNames.count(id2), 1); |
| 77 | } |
| 78 | |
| 79 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(KeyManagement, T, PibImpls, PibDataFixture) |
| 80 | { |
| 81 | T pibImpl; |
| 82 | |
| 83 | // no default setting, throw Error |
| 84 | BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error); |
| 85 | |
| 86 | // check id1Key1, should not exist, neither should id1. |
| 87 | BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), false); |
| 88 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false); |
| 89 | |
| 90 | // add id1Key1, should be default, id1 should be added implicitly |
| 91 | pibImpl.addKey(id1, id1Key1Name.get(-1), id1Key1); |
| 92 | BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), true); |
| 93 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true); |
| 94 | const PublicKey& keyBits = pibImpl.getKeyBits(id1, id1Key1Name.get(-1)); |
| 95 | BOOST_CHECK_EQUAL_COLLECTIONS(keyBits.get().buf(), keyBits.get().buf() + keyBits.get().size(), |
| 96 | id1Key1.get().buf(), id1Key1.get().buf() + id1Key1.get().size()); |
| 97 | BOOST_CHECK_NO_THROW(pibImpl.getDefaultKeyOfIdentity(id1)); |
| 98 | BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name.get(-1)); |
| 99 | |
| 100 | // add id1Key2, should not be default |
| 101 | pibImpl.addKey(id1, id1Key2Name.get(-1), id1Key2); |
| 102 | BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key2Name.get(-1)), true); |
| 103 | BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name.get(-1)); |
| 104 | |
| 105 | // set id1Key2 explicitly as default |
| 106 | pibImpl.setDefaultKeyOfIdentity(id1, id1Key2Name.get(-1)); |
| 107 | BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key2Name.get(-1)); |
| 108 | |
| 109 | // set a non-existing key as default, throw Error |
| 110 | BOOST_CHECK_THROW(pibImpl.setDefaultKeyOfIdentity(id1, name::Component("non-existing")), |
| 111 | Pib::Error); |
| 112 | |
| 113 | // remove id1Key2, should not have default key |
| 114 | pibImpl.removeKey(id1, id1Key2Name.get(-1)); |
| 115 | BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key2Name.get(-1)), false); |
| 116 | BOOST_CHECK_THROW(pibImpl.getKeyBits(id1, id1Key2Name.get(-1)), Pib::Error); |
| 117 | BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error); |
| 118 | |
| 119 | // add id1Key2 back, should be default |
| 120 | pibImpl.addKey(id1, id1Key2Name.get(-1), id1Key2); |
| 121 | BOOST_CHECK_NO_THROW(pibImpl.getKeyBits(id1, id1Key2Name.get(-1))); |
| 122 | BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key2Name.get(-1)); |
| 123 | |
| 124 | // get all the keys: id1Key1 and id1Key2 |
| 125 | std::set<name::Component> keyNames = pibImpl.getKeysOfIdentity(id1); |
| 126 | BOOST_CHECK_EQUAL(keyNames.size(), 2); |
| 127 | BOOST_CHECK_EQUAL(keyNames.count(id1Key1Name.get(-1)), 1); |
| 128 | BOOST_CHECK_EQUAL(keyNames.count(id1Key2Name.get(-1)), 1); |
| 129 | |
| 130 | // remove id1, should remove all the keys |
| 131 | pibImpl.removeIdentity(id1); |
| 132 | keyNames = pibImpl.getKeysOfIdentity(id1); |
| 133 | BOOST_CHECK_EQUAL(keyNames.size(), 0); |
| 134 | } |
| 135 | |
| 136 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, PibDataFixture) |
| 137 | { |
| 138 | T pibImpl; |
| 139 | |
| 140 | // no default setting, throw Error |
| 141 | BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), Pib::Error); |
| 142 | |
| 143 | // check id1Key1Cert1, should not exist, neither should id1 and id1Key1 |
| 144 | BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), false); |
| 145 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false); |
| 146 | BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), false); |
| 147 | |
| 148 | // add id1Key1Cert1, should be default, id1 and id1Key1 should be added implicitly |
| 149 | pibImpl.addCertificate(id1Key1Cert1); |
| 150 | BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), true); |
| 151 | BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true); |
| 152 | BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), true); |
| 153 | const IdentityCertificate& cert = pibImpl.getCertificate(id1Key1Cert1.getName()); |
| 154 | BOOST_CHECK_EQUAL_COLLECTIONS(cert.wireEncode().wire(), |
| 155 | cert.wireEncode().wire() + cert.wireEncode().size(), |
| 156 | id1Key1Cert1.wireEncode().wire(), |
| 157 | id1Key1Cert1.wireEncode().wire() + id1Key1Cert1.wireEncode().size()); |
| 158 | BOOST_CHECK_NO_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1))); |
| 159 | BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert1); |
| 160 | |
| 161 | // add id1Key1Cert2, should not be default |
| 162 | pibImpl.addCertificate(id1Key1Cert2); |
| 163 | BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert2.getName()), true); |
| 164 | BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert1); |
| 165 | |
| 166 | // set id1Key1Cert2 explicitly as default |
| 167 | pibImpl.setDefaultCertificateOfKey(id1, id1Key1Name.get(-1), id1Key1Cert2.getName()); |
| 168 | BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert2); |
| 169 | |
| 170 | // set a non-existing cert as default, throw Error |
| 171 | BOOST_CHECK_THROW(pibImpl.setDefaultCertificateOfKey(id1, id1Key1Name.get(-1), Name("/non-existing")), |
| 172 | Pib::Error); |
| 173 | |
| 174 | // remove id1Key1Cert2, should not have default cert |
| 175 | pibImpl.removeCertificate(id1Key1Cert2.getName()); |
| 176 | BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert2.getName()), false); |
| 177 | BOOST_CHECK_THROW(pibImpl.getCertificate(id1Key1Cert2.getName()), Pib::Error); |
| 178 | BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), Pib::Error); |
| 179 | |
| 180 | // add id1Key1Cert2, should be default |
| 181 | pibImpl.addCertificate(id1Key1Cert2); |
| 182 | BOOST_CHECK_NO_THROW(pibImpl.getCertificate(id1Key1Cert1.getName())); |
| 183 | BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert2); |
| 184 | |
| 185 | // get all certificates: id1Key1Cert1 and id1Key1Cert2 |
| 186 | std::set<Name> certNames = pibImpl.getCertificatesOfKey(id1, id1Key1Name.get(-1)); |
| 187 | BOOST_CHECK_EQUAL(certNames.size(), 2); |
| 188 | BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert1.getName()), 1); |
| 189 | BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert2.getName()), 1); |
| 190 | |
| 191 | // remove id1Key1, should remove all the certs |
| 192 | pibImpl.removeKey(id1, id1Key1Name.get(-1)); |
| 193 | certNames = pibImpl.getCertificatesOfKey(id1, id1Key1Name.get(-1)); |
| 194 | BOOST_CHECK_EQUAL(certNames.size(), 0); |
| 195 | } |
| 196 | |
| 197 | BOOST_AUTO_TEST_SUITE_END() |
| 198 | |
| 199 | } // namespace tests |
| 200 | } // namespace security |
| 201 | } // namespace ndn |