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