blob: 3f38e0e7744aacdb7d8e91012ac63a0f4aec6e2e [file] [log] [blame]
Yingdi Yucbe72b02015-11-25 17:35:37 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento5d0b0102017-10-07 13:43:16 -04002/*
Davide Pesavento56cc0d72022-04-29 23:00:23 -04003 * Copyright (c) 2013-2022 Regents of the University of California.
Yingdi Yucbe72b02015-11-25 17:35:37 -08004 *
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 Shi24c5a002018-12-12 04:47:15 +000022#include "ndn-cxx/security/pib/impl/identity-impl.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
25#include "tests/unit/security/pib/pib-data-fixture.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080026
27namespace ndn {
28namespace security {
29namespace pib {
30namespace detail {
31namespace tests {
32
33BOOST_AUTO_TEST_SUITE(Security)
34BOOST_AUTO_TEST_SUITE(Pib)
Yingdi Yucbe72b02015-11-25 17:35:37 -080035
Davide Pesavento56cc0d72022-04-29 23:00:23 -040036using pib::Pib;
Yingdi Yucbe72b02015-11-25 17:35:37 -080037
Davide Pesavento8618c1e2022-05-05 15:20:02 -040038class IdentityImplFixture : public pib::tests::PibDataFixture
Yingdi Yucbe72b02015-11-25 17:35:37 -080039{
Davide Pesavento8618c1e2022-05-05 15:20:02 -040040protected:
41 const shared_ptr<PibImpl> pibImpl = makePibWithIdentity(id1);
42 IdentityImpl identity1{id1, pibImpl};
43};
44
45BOOST_FIXTURE_TEST_SUITE(TestIdentityImpl, IdentityImplFixture)
46
47BOOST_AUTO_TEST_CASE(Properties)
48{
Davide Pesavento07db0732022-05-06 15:20:26 -040049 BOOST_TEST(identity1.getName() == id1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080050}
51
Davide Pesavento56cc0d72022-04-29 23:00:23 -040052BOOST_AUTO_TEST_CASE(KeyOperations)
Yingdi Yucbe72b02015-11-25 17:35:37 -080053{
Davide Pesavento8618c1e2022-05-05 15:20:02 -040054 // identity does not have any keys
Yingdi Yucbe72b02015-11-25 17:35:37 -080055 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 Pesavento56cc0d72022-04-29 23:00:23 -040062 BOOST_CHECK_THROW(identity1.setDefaultKey(id1Key1Name), Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -080063
64 // add key
Davide Pesavento765abc92021-12-27 00:44:04 -050065 identity1.addKey(id1Key1, id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040066 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 Yucbe72b02015-11-25 17:35:37 -080069
70 // new key becomes default key when there is no default key
Davide Pesavento56cc0d72022-04-29 23:00:23 -040071 const auto& defaultKey0 = identity1.getDefaultKey();
Yingdi Yucbe72b02015-11-25 17:35:37 -080072 BOOST_CHECK_EQUAL(defaultKey0.getName(), id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040073 BOOST_TEST(defaultKey0.getPublicKey() == id1Key1, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -080074
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 Pesavento765abc92021-12-27 00:44:04 -050081 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1, id1Key1Name));
Davide Pesavento56cc0d72022-04-29 23:00:23 -040082 const auto& defaultKey1 = identity1.getDefaultKey();
Yingdi Yucbe72b02015-11-25 17:35:37 -080083 BOOST_CHECK_EQUAL(defaultKey1.getName(), id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040084 BOOST_TEST(defaultKey1.getPublicKey() == id1Key1, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -080085
86 // add another key
Davide Pesavento765abc92021-12-27 00:44:04 -050087 identity1.addKey(id1Key2, id1Key2Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -080088 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 2);
89
90 // set default key through name
91 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key2Name));
Davide Pesavento56cc0d72022-04-29 23:00:23 -040092 const auto& defaultKey2 = identity1.getDefaultKey();
Yingdi Yucbe72b02015-11-25 17:35:37 -080093 BOOST_CHECK_EQUAL(defaultKey2.getName(), id1Key2Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040094 BOOST_TEST(defaultKey2.getPublicKey() == id1Key2, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -080095
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 Pesavento765abc92021-12-27 00:44:04 -0500102 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1, id1Key1Name));
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400103 const auto& defaultKey3 = identity1.getDefaultKey();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800104 BOOST_CHECK_EQUAL(defaultKey3.getName(), id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400105 BOOST_TEST(defaultKey3.getPublicKey() == id1Key1, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -0800106 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 Pesavento8618c1e2022-05-05 15:20:02 -0400118BOOST_AUTO_TEST_CASE(ReplaceKey)
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800119{
Davide Pesavento765abc92021-12-27 00:44:04 -0500120 identity1.addKey(id1Key1, id1Key1Name);
121 auto k1 = identity1.getKey(id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400122 BOOST_TEST(k1.getPublicKey() == id1Key1, boost::test_tools::per_element());
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800123
Davide Pesavento8618c1e2022-05-05 15:20:02 -0400124 identity1.addKey(id1Key2, id1Key1Name); // overwrite key
Davide Pesavento765abc92021-12-27 00:44:04 -0500125 auto k2 = identity1.getKey(id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400126 BOOST_TEST(k2.getPublicKey() == id1Key2, boost::test_tools::per_element());
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800127}
128
Yingdi Yucbe72b02015-11-25 17:35:37 -0800129BOOST_AUTO_TEST_CASE(Errors)
130{
Davide Pesavento765abc92021-12-27 00:44:04 -0500131 BOOST_CHECK_THROW(identity1.addKey(id2Key1, id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800132 BOOST_CHECK_THROW(identity1.removeKey(id2Key1Name), std::invalid_argument);
133 BOOST_CHECK_THROW(identity1.getKey(id2Key1Name), std::invalid_argument);
Davide Pesavento765abc92021-12-27 00:44:04 -0500134 BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1, id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800135 BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1Name), std::invalid_argument);
136}
137
138BOOST_AUTO_TEST_SUITE_END() // TestIdentityImpl
Yingdi Yucbe72b02015-11-25 17:35:37 -0800139BOOST_AUTO_TEST_SUITE_END() // Pib
140BOOST_AUTO_TEST_SUITE_END() // Security
141
142} // namespace tests
143} // namespace detail
144} // namespace pib
145} // namespace security
146} // namespace ndn