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