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" |
Davide Pesavento | 4fb35d8 | 2019-10-31 19:33:10 -0400 | [diff] [blame] | 23 | #include "ndn-cxx/security/pib/impl/pib-memory.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/security/pib/pib.hpp" |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 25 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "tests/boost-test.hpp" |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 27 | #include "tests/key-chain-fixture.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 28 | #include "tests/unit/security/pib/pib-data-fixture.hpp" |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace security { |
| 32 | namespace pib { |
| 33 | namespace detail { |
| 34 | namespace tests { |
| 35 | |
| 36 | BOOST_AUTO_TEST_SUITE(Security) |
| 37 | BOOST_AUTO_TEST_SUITE(Pib) |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 38 | BOOST_FIXTURE_TEST_SUITE(TestKeyImpl, security::tests::PibDataFixture) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 39 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 40 | using pib::Pib; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 41 | |
| 42 | BOOST_AUTO_TEST_CASE(Basic) |
| 43 | { |
| 44 | auto pibImpl = make_shared<pib::PibMemory>(); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 45 | KeyImpl key11(id1Key1Name, id1Key1, pibImpl); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 46 | |
| 47 | BOOST_CHECK_EQUAL(key11.getName(), id1Key1Name); |
| 48 | BOOST_CHECK_EQUAL(key11.getIdentity(), id1); |
| 49 | BOOST_CHECK_EQUAL(key11.getKeyType(), KeyType::EC); |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 50 | BOOST_TEST(key11.getPublicKey() == id1Key1, boost::test_tools::per_element()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 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); |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 56 | BOOST_TEST(key11Bak.getPublicKey() == id1Key1, boost::test_tools::per_element()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 59 | BOOST_AUTO_TEST_CASE(CertificateOperations) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 60 | { |
| 61 | auto pibImpl = make_shared<pib::PibMemory>(); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 62 | KeyImpl key11(id1Key1Name, id1Key1, pibImpl); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 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 |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 73 | BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key1Cert1.getName()), Pib::Error); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 74 | |
| 75 | // add certificate |
| 76 | key11.addCertificate(id1Key1Cert1); |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 77 | const auto& addedCert = key11.getCertificate(id1Key1Cert1.getName()); |
| 78 | BOOST_CHECK_EQUAL(addedCert, id1Key1Cert1); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 79 | |
| 80 | // new certificate becomes default certificate when there was no default certificate |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 81 | const auto& defaultCert0 = key11.getDefaultCertificate(); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 82 | BOOST_CHECK_EQUAL(defaultCert0, id1Key1Cert1); |
| 83 | |
| 84 | // remove certificate |
| 85 | key11.removeCertificate(id1Key1Cert1.getName()); |
| 86 | BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error); |
| 87 | BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error); |
| 88 | |
| 89 | // set default certificate directly |
| 90 | BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert1)); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 91 | const auto& defaultCert1 = key11.getDefaultCertificate(); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 92 | BOOST_CHECK_EQUAL(defaultCert1, id1Key1Cert1); |
| 93 | |
| 94 | // add another certificate |
| 95 | key11.addCertificate(id1Key1Cert2); |
| 96 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 2); |
| 97 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 98 | // set default certificate through name and check return value |
| 99 | BOOST_CHECK_EQUAL(key11.setDefaultCertificate(id1Key1Cert2.getName()), id1Key1Cert2); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 100 | const auto& defaultCert2 = key11.getDefaultCertificate(); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(defaultCert2, id1Key1Cert2); |
| 102 | |
| 103 | // remove certificate |
| 104 | key11.removeCertificate(id1Key1Cert1.getName()); |
| 105 | BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error); |
| 106 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 1); |
| 107 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 108 | // set removed certificate as default, certificate is implicitly added |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 109 | BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert1)); |
| 110 | const auto& defaultCert3 = key11.getDefaultCertificate(); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 111 | BOOST_CHECK_EQUAL(defaultCert3, id1Key1Cert1); |
| 112 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 2); |
| 113 | |
| 114 | // remove all certificates |
| 115 | key11.removeCertificate(id1Key1Cert1.getName()); |
| 116 | BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error); |
| 117 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 1); |
| 118 | key11.removeCertificate(id1Key1Cert2.getName()); |
| 119 | BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert2.getName()), Pib::Error); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(key11.getCertificates().size(), 0); |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 121 | BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 124 | class OverwriteFixture : public ndn::security::tests::PibDataFixture, |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 125 | public ndn::tests::KeyChainFixture |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 126 | { |
| 127 | }; |
| 128 | |
| 129 | BOOST_FIXTURE_TEST_CASE(Overwrite, OverwriteFixture) |
| 130 | { |
| 131 | auto pibImpl = make_shared<pib::PibMemory>(); |
| 132 | |
| 133 | BOOST_CHECK_THROW(KeyImpl(id1Key1Name, pibImpl), Pib::Error); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 134 | KeyImpl(id1Key1Name, id1Key1, pibImpl); |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 135 | KeyImpl key1(id1Key1Name, pibImpl); |
| 136 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 137 | KeyImpl(id1Key1Name, id1Key2, pibImpl); // overwriting of the key should work |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 138 | KeyImpl key2(id1Key1Name, pibImpl); |
| 139 | |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 140 | Buffer key1buf(key1.getPublicKey().begin(), key1.getPublicKey().end()); |
| 141 | Buffer key2buf(key2.getPublicKey().begin(), key2.getPublicKey().end()); |
| 142 | BOOST_CHECK(key1buf != key2buf); // key1 cached the original public key |
| 143 | BOOST_CHECK(key2buf == id1Key2); |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 144 | |
| 145 | key1.addCertificate(id1Key1Cert1); |
| 146 | BOOST_CHECK_EQUAL(key1.getCertificate(id1Key1Cert1.getName()), id1Key1Cert1); |
| 147 | |
| 148 | auto otherCert = id1Key1Cert1; |
| 149 | SignatureInfo info; |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 150 | info.setValidityPeriod(ValidityPeriod::makeRelative(-1_s, 10_s)); |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 151 | m_keyChain.sign(otherCert, SigningInfo().setSignatureInfo(info)); |
| 152 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 153 | BOOST_TEST(otherCert.getName() == id1Key1Cert1.getName()); |
| 154 | BOOST_TEST(otherCert.getContent() == id1Key1Cert1.getContent()); |
| 155 | BOOST_TEST(otherCert != id1Key1Cert1); |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 156 | |
| 157 | key1.addCertificate(otherCert); |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 158 | BOOST_TEST(key1.getCertificate(id1Key1Cert1.getName()) == otherCert); |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 161 | BOOST_AUTO_TEST_CASE(Errors) |
| 162 | { |
| 163 | auto pibImpl = make_shared<pib::PibMemory>(); |
| 164 | |
| 165 | BOOST_CHECK_THROW(KeyImpl(id1Key1Name, pibImpl), Pib::Error); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 166 | KeyImpl key11(id1Key1Name, id1Key1, pibImpl); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 167 | |
| 168 | BOOST_CHECK_THROW(KeyImpl(Name("/wrong"), pibImpl), std::invalid_argument); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 169 | BOOST_CHECK_THROW(KeyImpl(Name("/wrong"), id1Key1, pibImpl), std::invalid_argument); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 170 | Buffer wrongKey; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 171 | BOOST_CHECK_THROW(KeyImpl(id1Key2Name, wrongKey, pibImpl), std::invalid_argument); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 172 | |
| 173 | key11.addCertificate(id1Key1Cert1); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 174 | BOOST_CHECK_THROW(key11.addCertificate(id1Key2Cert1), std::invalid_argument); |
| 175 | BOOST_CHECK_THROW(key11.removeCertificate(id1Key2Cert1.getName()), std::invalid_argument); |
| 176 | BOOST_CHECK_THROW(key11.getCertificate(id1Key2Cert1.getName()), std::invalid_argument); |
| 177 | BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key2Cert1), std::invalid_argument); |
| 178 | BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key2Cert1.getName()), std::invalid_argument); |
| 179 | } |
| 180 | |
| 181 | BOOST_AUTO_TEST_SUITE_END() // TestKeyImpl |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 182 | BOOST_AUTO_TEST_SUITE_END() // Pib |
| 183 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 184 | |
| 185 | } // namespace tests |
| 186 | } // namespace detail |
| 187 | } // namespace pib |
| 188 | } // namespace security |
| 189 | } // namespace ndn |