Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 Regents of the University of California. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [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 | |
Junxiao Shi | 24c5a00 | 2018-12-12 04:47:15 +0000 | [diff] [blame] | 22 | #include "ndn-cxx/security/pib/impl/key-impl.hpp" |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 25 | #include "tests/key-chain-fixture.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "tests/unit/security/pib/pib-data-fixture.hpp" |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace security { |
| 30 | namespace pib { |
| 31 | namespace detail { |
| 32 | namespace tests { |
| 33 | |
| 34 | BOOST_AUTO_TEST_SUITE(Security) |
| 35 | BOOST_AUTO_TEST_SUITE(Pib) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 36 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 37 | using pib::Pib; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 38 | |
Davide Pesavento | 8618c1e | 2022-05-05 15:20:02 -0400 | [diff] [blame] | 39 | class KeyImplFixture : public pib::tests::PibDataFixture |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 40 | { |
Davide Pesavento | 8618c1e | 2022-05-05 15:20:02 -0400 | [diff] [blame] | 41 | protected: |
| 42 | const shared_ptr<PibImpl> pibImpl = makePibWithKey(id1Key1Name, id1Key1); |
| 43 | KeyImpl key11{id1Key1Name, id1Key1, pibImpl}; |
| 44 | }; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 45 | |
Davide Pesavento | 8618c1e | 2022-05-05 15:20:02 -0400 | [diff] [blame] | 46 | BOOST_FIXTURE_TEST_SUITE(TestKeyImpl, KeyImplFixture) |
| 47 | |
| 48 | BOOST_AUTO_TEST_CASE(Properties) |
| 49 | { |
Davide Pesavento | 07db073 | 2022-05-06 15:20:26 -0400 | [diff] [blame] | 50 | BOOST_TEST(key11.getIdentity() == id1); |
| 51 | BOOST_TEST(key11.getName() == id1Key1Name); |
| 52 | BOOST_TEST(key11.getKeyType() == KeyType::EC); |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 53 | BOOST_TEST(key11.getPublicKey() == id1Key1, boost::test_tools::per_element()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 56 | BOOST_AUTO_TEST_CASE(CertificateOperations) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 57 | { |
Davide Pesavento | 8618c1e | 2022-05-05 15:20:02 -0400 | [diff] [blame] | 58 | // key does not have any certificates |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 59 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 0); |
| 60 | |
| 61 | // get non-existing certificate, throw Pib::Error |
| 62 | BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error); |
| 63 | // get default certificate, throw Pib::Error |
| 64 | BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error); |
| 65 | // set non-existing certificate as default certificate, throw Pib::Error |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 66 | BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key1Cert1.getName()), Pib::Error); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 67 | |
| 68 | // add certificate |
| 69 | key11.addCertificate(id1Key1Cert1); |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 70 | const auto& addedCert = key11.getCertificate(id1Key1Cert1.getName()); |
| 71 | BOOST_CHECK_EQUAL(addedCert, id1Key1Cert1); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 72 | |
| 73 | // new certificate becomes default certificate when there was no default certificate |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 74 | const auto& defaultCert0 = key11.getDefaultCertificate(); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 75 | BOOST_CHECK_EQUAL(defaultCert0, id1Key1Cert1); |
| 76 | |
| 77 | // remove certificate |
| 78 | key11.removeCertificate(id1Key1Cert1.getName()); |
| 79 | BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error); |
| 80 | BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error); |
| 81 | |
| 82 | // set default certificate directly |
| 83 | BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert1)); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 84 | const auto& defaultCert1 = key11.getDefaultCertificate(); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(defaultCert1, id1Key1Cert1); |
| 86 | |
| 87 | // add another certificate |
| 88 | key11.addCertificate(id1Key1Cert2); |
| 89 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 2); |
| 90 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 91 | // set default certificate through name and check return value |
| 92 | BOOST_CHECK_EQUAL(key11.setDefaultCertificate(id1Key1Cert2.getName()), id1Key1Cert2); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 93 | const auto& defaultCert2 = key11.getDefaultCertificate(); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 94 | BOOST_CHECK_EQUAL(defaultCert2, id1Key1Cert2); |
| 95 | |
| 96 | // remove certificate |
| 97 | key11.removeCertificate(id1Key1Cert1.getName()); |
| 98 | BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error); |
| 99 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 1); |
| 100 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 101 | // set removed certificate as default, certificate is implicitly added |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 102 | BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert1)); |
| 103 | const auto& defaultCert3 = key11.getDefaultCertificate(); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 104 | BOOST_CHECK_EQUAL(defaultCert3, id1Key1Cert1); |
| 105 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 2); |
| 106 | |
| 107 | // remove all certificates |
| 108 | key11.removeCertificate(id1Key1Cert1.getName()); |
| 109 | BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error); |
| 110 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 1); |
| 111 | key11.removeCertificate(id1Key1Cert2.getName()); |
| 112 | BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert2.getName()), Pib::Error); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 113 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 0); |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 114 | BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Davide Pesavento | 8618c1e | 2022-05-05 15:20:02 -0400 | [diff] [blame] | 117 | class ReplaceFixture : public ndn::tests::KeyChainFixture, |
| 118 | public KeyImplFixture |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 119 | { |
| 120 | }; |
| 121 | |
Davide Pesavento | 8618c1e | 2022-05-05 15:20:02 -0400 | [diff] [blame] | 122 | BOOST_FIXTURE_TEST_CASE(ReplaceCertificate, ReplaceFixture) |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 123 | { |
Davide Pesavento | 8618c1e | 2022-05-05 15:20:02 -0400 | [diff] [blame] | 124 | key11.addCertificate(id1Key1Cert1); |
| 125 | BOOST_CHECK_EQUAL(key11.getCertificate(id1Key1Cert1.getName()), id1Key1Cert1); |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 126 | |
| 127 | auto otherCert = id1Key1Cert1; |
| 128 | SignatureInfo info; |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 129 | info.setValidityPeriod(ValidityPeriod::makeRelative(-1_s, 10_s)); |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 130 | m_keyChain.sign(otherCert, SigningInfo().setSignatureInfo(info)); |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 131 | BOOST_TEST(otherCert.getName() == id1Key1Cert1.getName()); |
| 132 | BOOST_TEST(otherCert.getContent() == id1Key1Cert1.getContent()); |
| 133 | BOOST_TEST(otherCert != id1Key1Cert1); |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 134 | |
Davide Pesavento | 8618c1e | 2022-05-05 15:20:02 -0400 | [diff] [blame] | 135 | key11.addCertificate(otherCert); // overwrite cert |
| 136 | BOOST_TEST(key11.getCertificate(id1Key1Cert1.getName()) == otherCert); |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 139 | BOOST_AUTO_TEST_CASE(Errors) |
| 140 | { |
Davide Pesavento | 07db073 | 2022-05-06 15:20:26 -0400 | [diff] [blame] | 141 | // illegal key name |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 142 | BOOST_CHECK_THROW(KeyImpl(Name("/wrong"), id1Key1, pibImpl), std::invalid_argument); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 143 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 144 | BOOST_CHECK_THROW(key11.addCertificate(id1Key2Cert1), std::invalid_argument); |
| 145 | BOOST_CHECK_THROW(key11.removeCertificate(id1Key2Cert1.getName()), std::invalid_argument); |
| 146 | BOOST_CHECK_THROW(key11.getCertificate(id1Key2Cert1.getName()), std::invalid_argument); |
| 147 | BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key2Cert1), std::invalid_argument); |
| 148 | BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key2Cert1.getName()), std::invalid_argument); |
| 149 | } |
| 150 | |
Davide Pesavento | 07db073 | 2022-05-06 15:20:26 -0400 | [diff] [blame] | 151 | BOOST_AUTO_TEST_CASE(UnknownKeyType) |
| 152 | { |
| 153 | Name keyName = security::constructKeyName(id1, name::Component::fromEscapedString("foo")); |
| 154 | Buffer invalidKey{0x01, 0x02, 0x03, 0x04}; |
| 155 | pibImpl->addKey(id1, keyName, invalidKey); |
| 156 | |
| 157 | KeyImpl unknown(keyName, invalidKey, pibImpl); |
| 158 | BOOST_TEST(unknown.getIdentity() == id1); |
| 159 | BOOST_TEST(unknown.getName() == keyName); |
| 160 | BOOST_TEST(unknown.getKeyType() == KeyType::NONE); |
| 161 | BOOST_TEST(unknown.getPublicKey() == invalidKey, boost::test_tools::per_element()); |
| 162 | } |
| 163 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 164 | BOOST_AUTO_TEST_SUITE_END() // TestKeyImpl |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 165 | BOOST_AUTO_TEST_SUITE_END() // Pib |
| 166 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 167 | |
| 168 | } // namespace tests |
| 169 | } // namespace detail |
| 170 | } // namespace pib |
| 171 | } // namespace security |
| 172 | } // namespace ndn |