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/identity-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" |
| 25 | #include "tests/unit/security/pib/pib-data-fixture.hpp" |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 26 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 27 | namespace ndn::tests { |
| 28 | |
| 29 | using namespace ndn::security::pib; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(Security) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 32 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 33 | class IdentityImplFixture : public PibDataFixture |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 34 | { |
Davide Pesavento | 8618c1e | 2022-05-05 15:20:02 -0400 | [diff] [blame] | 35 | protected: |
| 36 | const shared_ptr<PibImpl> pibImpl = makePibWithIdentity(id1); |
| 37 | IdentityImpl identity1{id1, pibImpl}; |
| 38 | }; |
| 39 | |
| 40 | BOOST_FIXTURE_TEST_SUITE(TestIdentityImpl, IdentityImplFixture) |
| 41 | |
| 42 | BOOST_AUTO_TEST_CASE(Properties) |
| 43 | { |
Davide Pesavento | 07db073 | 2022-05-06 15:20:26 -0400 | [diff] [blame] | 44 | BOOST_TEST(identity1.getName() == id1); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 47 | BOOST_AUTO_TEST_CASE(KeyOperations) |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 48 | { |
Davide Pesavento | 8618c1e | 2022-05-05 15:20:02 -0400 | [diff] [blame] | 49 | // identity does not have any keys |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(identity1.getKeys().size(), 0); |
| 51 | |
| 52 | // get non-existing key, throw Pib::Error |
| 53 | BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error); |
| 54 | // get default key, throw Pib::Error |
| 55 | BOOST_CHECK_THROW(identity1.getDefaultKey(), Pib::Error); |
| 56 | // set non-existing key as default key, throw Pib::Error |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 57 | BOOST_CHECK_THROW(identity1.setDefaultKey(id1Key1Name), Pib::Error); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 58 | |
| 59 | // add key |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 60 | identity1.addKey(id1Key1, id1Key1Name); |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 61 | const auto& addedKey = identity1.getKey(id1Key1Name); |
| 62 | BOOST_CHECK_EQUAL(addedKey.getName(), id1Key1Name); |
| 63 | BOOST_TEST(addedKey.getPublicKey() == id1Key1, boost::test_tools::per_element()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 64 | |
| 65 | // new key becomes default key when there is no default key |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 66 | const auto& defaultKey0 = identity1.getDefaultKey(); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 67 | BOOST_CHECK_EQUAL(defaultKey0.getName(), id1Key1Name); |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 68 | BOOST_TEST(defaultKey0.getPublicKey() == id1Key1, boost::test_tools::per_element()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 69 | |
| 70 | // remove key |
| 71 | identity1.removeKey(id1Key1Name); |
| 72 | BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error); |
| 73 | BOOST_CHECK_THROW(identity1.getDefaultKey(), Pib::Error); |
| 74 | |
| 75 | // set default key directly |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 76 | BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1, id1Key1Name)); |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 77 | const auto& defaultKey1 = identity1.getDefaultKey(); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 78 | BOOST_CHECK_EQUAL(defaultKey1.getName(), id1Key1Name); |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 79 | BOOST_TEST(defaultKey1.getPublicKey() == id1Key1, boost::test_tools::per_element()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 80 | |
| 81 | // add another key |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 82 | identity1.addKey(id1Key2, id1Key2Name); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL(identity1.getKeys().size(), 2); |
| 84 | |
| 85 | // set default key through name |
| 86 | BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key2Name)); |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 87 | const auto& defaultKey2 = identity1.getDefaultKey(); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(defaultKey2.getName(), id1Key2Name); |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 89 | BOOST_TEST(defaultKey2.getPublicKey() == id1Key2, boost::test_tools::per_element()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 90 | |
| 91 | // remove key |
| 92 | identity1.removeKey(id1Key1Name); |
| 93 | BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error); |
| 94 | BOOST_CHECK_EQUAL(identity1.getKeys().size(), 1); |
| 95 | |
| 96 | // set default key directly again, change the default setting |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 97 | BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1, id1Key1Name)); |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 98 | const auto& defaultKey3 = identity1.getDefaultKey(); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(defaultKey3.getName(), id1Key1Name); |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 100 | BOOST_TEST(defaultKey3.getPublicKey() == id1Key1, boost::test_tools::per_element()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(identity1.getKeys().size(), 2); |
| 102 | |
| 103 | // remove all keys |
| 104 | identity1.removeKey(id1Key1Name); |
| 105 | BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error); |
| 106 | BOOST_CHECK_EQUAL(identity1.getKeys().size(), 1); |
| 107 | identity1.removeKey(id1Key2Name); |
| 108 | BOOST_CHECK_THROW(identity1.getKey(id1Key2Name), Pib::Error); |
| 109 | BOOST_CHECK_EQUAL(identity1.getKeys().size(), 0); |
| 110 | BOOST_CHECK_THROW(identity1.getDefaultKey(), Pib::Error); |
| 111 | } |
| 112 | |
Davide Pesavento | 8618c1e | 2022-05-05 15:20:02 -0400 | [diff] [blame] | 113 | BOOST_AUTO_TEST_CASE(ReplaceKey) |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 114 | { |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 115 | identity1.addKey(id1Key1, id1Key1Name); |
| 116 | auto k1 = identity1.getKey(id1Key1Name); |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 117 | BOOST_TEST(k1.getPublicKey() == id1Key1, boost::test_tools::per_element()); |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 118 | |
Davide Pesavento | 8618c1e | 2022-05-05 15:20:02 -0400 | [diff] [blame] | 119 | identity1.addKey(id1Key2, id1Key1Name); // overwrite key |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 120 | auto k2 = identity1.getKey(id1Key1Name); |
Davide Pesavento | 56cc0d7 | 2022-04-29 23:00:23 -0400 | [diff] [blame] | 121 | BOOST_TEST(k2.getPublicKey() == id1Key2, boost::test_tools::per_element()); |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 124 | BOOST_AUTO_TEST_CASE(Errors) |
| 125 | { |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 126 | BOOST_CHECK_THROW(identity1.addKey(id2Key1, id2Key1Name), std::invalid_argument); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 127 | BOOST_CHECK_THROW(identity1.removeKey(id2Key1Name), std::invalid_argument); |
| 128 | BOOST_CHECK_THROW(identity1.getKey(id2Key1Name), std::invalid_argument); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 129 | BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1, id2Key1Name), std::invalid_argument); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 130 | BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1Name), std::invalid_argument); |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_SUITE_END() // TestIdentityImpl |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 134 | BOOST_AUTO_TEST_SUITE_END() // Security |
| 135 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 136 | } // namespace ndn::tests |