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