Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017 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/detail/key-impl.hpp" |
| 23 | #include "security/pib/pib.hpp" |
| 24 | #include "security/pib/pib-memory.hpp" |
| 25 | #include "../pib-data-fixture.hpp" |
| 26 | |
| 27 | #include "boost-test.hpp" |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace security { |
| 31 | namespace pib { |
| 32 | namespace detail { |
| 33 | namespace tests { |
| 34 | |
| 35 | BOOST_AUTO_TEST_SUITE(Security) |
| 36 | BOOST_AUTO_TEST_SUITE(Pib) |
| 37 | BOOST_AUTO_TEST_SUITE(Detail) |
| 38 | BOOST_FIXTURE_TEST_SUITE(TestKeyImpl, ndn::security::tests::PibDataFixture) |
| 39 | |
| 40 | using security::Pib; |
| 41 | |
| 42 | BOOST_AUTO_TEST_CASE(Basic) |
| 43 | { |
| 44 | auto pibImpl = make_shared<pib::PibMemory>(); |
| 45 | KeyImpl key11(id1Key1Name, id1Key1.buf(), id1Key1.size(), pibImpl); |
| 46 | |
| 47 | BOOST_CHECK_EQUAL(key11.getName(), id1Key1Name); |
| 48 | BOOST_CHECK_EQUAL(key11.getIdentity(), id1); |
| 49 | BOOST_CHECK_EQUAL(key11.getKeyType(), KeyType::EC); |
| 50 | BOOST_CHECK(key11.getPublicKey() == id1Key1); |
| 51 | |
| 52 | KeyImpl key11Bak(id1Key1Name, pibImpl); |
| 53 | BOOST_CHECK_EQUAL(key11Bak.getName(), id1Key1Name); |
| 54 | BOOST_CHECK_EQUAL(key11Bak.getIdentity(), id1); |
| 55 | BOOST_CHECK_EQUAL(key11Bak.getKeyType(), KeyType::EC); |
| 56 | BOOST_CHECK(key11Bak.getPublicKey() == id1Key1); |
| 57 | } |
| 58 | |
| 59 | BOOST_AUTO_TEST_CASE(CertificateOperation) |
| 60 | { |
| 61 | auto pibImpl = make_shared<pib::PibMemory>(); |
| 62 | KeyImpl key11(id1Key1Name, id1Key1.buf(), id1Key1.size(), pibImpl); |
| 63 | BOOST_CHECK_NO_THROW(KeyImpl(id1Key1Name, pibImpl)); |
| 64 | |
| 65 | // key does not have any certificate |
| 66 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 0); |
| 67 | |
| 68 | // get non-existing certificate, throw Pib::Error |
| 69 | BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error); |
| 70 | // get default certificate, throw Pib::Error |
| 71 | BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error); |
| 72 | // set non-existing certificate as default certificate, throw Pib::Error |
| 73 | BOOST_REQUIRE_THROW(key11.setDefaultCertificate(id1Key1Cert1.getName()), Pib::Error); |
| 74 | |
| 75 | // add certificate |
| 76 | key11.addCertificate(id1Key1Cert1); |
| 77 | BOOST_CHECK_NO_THROW(key11.getCertificate(id1Key1Cert1.getName())); |
| 78 | |
| 79 | // new certificate becomes default certificate when there was no default certificate |
| 80 | BOOST_REQUIRE_NO_THROW(key11.getDefaultCertificate()); |
| 81 | const auto& defaultCert0 = key11.getDefaultCertificate(); |
| 82 | BOOST_CHECK_EQUAL(defaultCert0.getName(), id1Key1Cert1.getName()); |
| 83 | BOOST_CHECK_EQUAL(defaultCert0, id1Key1Cert1); |
| 84 | |
| 85 | // remove certificate |
| 86 | key11.removeCertificate(id1Key1Cert1.getName()); |
| 87 | BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error); |
| 88 | BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error); |
| 89 | |
| 90 | // set default certificate directly |
| 91 | BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert1)); |
| 92 | BOOST_REQUIRE_NO_THROW(key11.getDefaultCertificate()); |
| 93 | BOOST_CHECK_NO_THROW(key11.getCertificate(id1Key1Cert1.getName())); |
| 94 | |
| 95 | // check default cert |
| 96 | const auto& defaultCert1 = key11.getDefaultCertificate(); |
| 97 | BOOST_CHECK_EQUAL(defaultCert1.getName(), id1Key1Cert1.getName()); |
| 98 | BOOST_CHECK_EQUAL(defaultCert1, id1Key1Cert1); |
| 99 | |
| 100 | // add another certificate |
| 101 | key11.addCertificate(id1Key1Cert2); |
| 102 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 2); |
| 103 | |
| 104 | // set default certificate through name |
| 105 | BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert2.getName())); |
| 106 | BOOST_REQUIRE_NO_THROW(key11.getDefaultCertificate()); |
| 107 | const auto& defaultCert2 = key11.getDefaultCertificate(); |
| 108 | BOOST_CHECK_EQUAL(defaultCert2.getName(), id1Key1Cert2.getName()); |
| 109 | BOOST_CHECK_EQUAL(defaultCert2, id1Key1Cert2); |
| 110 | |
| 111 | // remove certificate |
| 112 | key11.removeCertificate(id1Key1Cert1.getName()); |
| 113 | BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error); |
| 114 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 1); |
| 115 | |
| 116 | // set default certificate directly again, change the default setting |
| 117 | BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert1)); |
| 118 | const auto& defaultCert3 = key11.getDefaultCertificate(); |
| 119 | BOOST_CHECK_EQUAL(defaultCert3.getName(), id1Key1Cert1.getName()); |
| 120 | BOOST_CHECK_EQUAL(defaultCert3, id1Key1Cert1); |
| 121 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 2); |
| 122 | |
| 123 | // remove all certificates |
| 124 | key11.removeCertificate(id1Key1Cert1.getName()); |
| 125 | BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error); |
| 126 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 1); |
| 127 | key11.removeCertificate(id1Key1Cert2.getName()); |
| 128 | BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert2.getName()), Pib::Error); |
| 129 | BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error); |
| 130 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 0); |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_CASE(Errors) |
| 134 | { |
| 135 | auto pibImpl = make_shared<pib::PibMemory>(); |
| 136 | |
| 137 | BOOST_CHECK_THROW(KeyImpl(id1Key1Name, pibImpl), Pib::Error); |
| 138 | KeyImpl key11(id1Key1Name, id1Key1.buf(), id1Key1.size(), pibImpl); |
| 139 | BOOST_CHECK_THROW(KeyImpl(id1Key1Name, id1Key1.buf(), id1Key1.size(), pibImpl), Pib::Error); |
| 140 | |
| 141 | BOOST_CHECK_THROW(KeyImpl(Name("/wrong"), pibImpl), std::invalid_argument); |
| 142 | BOOST_CHECK_THROW(KeyImpl(Name("/wrong"), id1Key1.buf(), id1Key1.size(), pibImpl), std::invalid_argument); |
| 143 | Buffer wrongKey; |
| 144 | BOOST_CHECK_THROW(KeyImpl(id1Key2Name, wrongKey.buf(), wrongKey.size(), pibImpl), std::invalid_argument); |
| 145 | |
| 146 | key11.addCertificate(id1Key1Cert1); |
| 147 | BOOST_CHECK_THROW(key11.addCertificate(id1Key1Cert1), Pib::Error); |
| 148 | BOOST_CHECK_THROW(key11.addCertificate(id1Key2Cert1), std::invalid_argument); |
| 149 | BOOST_CHECK_THROW(key11.removeCertificate(id1Key2Cert1.getName()), std::invalid_argument); |
| 150 | BOOST_CHECK_THROW(key11.getCertificate(id1Key2Cert1.getName()), std::invalid_argument); |
| 151 | BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key2Cert1), std::invalid_argument); |
| 152 | BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key2Cert1.getName()), std::invalid_argument); |
| 153 | } |
| 154 | |
| 155 | BOOST_AUTO_TEST_SUITE_END() // TestKeyImpl |
| 156 | BOOST_AUTO_TEST_SUITE_END() // Detail |
| 157 | BOOST_AUTO_TEST_SUITE_END() // Pib |
| 158 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 159 | |
| 160 | } // namespace tests |
| 161 | } // namespace detail |
| 162 | } // namespace pib |
| 163 | } // namespace security |
| 164 | } // namespace ndn |