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