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