blob: 6b449931d17157c1c5446de6a00a2e6bdaa9c8dc [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 Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040027namespace ndn::tests {
28
29using namespace ndn::security::pib;
Yingdi Yucbe72b02015-11-25 17:35:37 -080030
31BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yucbe72b02015-11-25 17:35:37 -080032
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040033class IdentityImplFixture : public PibDataFixture
Yingdi Yucbe72b02015-11-25 17:35:37 -080034{
Davide Pesavento8618c1e2022-05-05 15:20:02 -040035protected:
36 const shared_ptr<PibImpl> pibImpl = makePibWithIdentity(id1);
37 IdentityImpl identity1{id1, pibImpl};
38};
39
40BOOST_FIXTURE_TEST_SUITE(TestIdentityImpl, IdentityImplFixture)
41
42BOOST_AUTO_TEST_CASE(Properties)
43{
Davide Pesavento07db0732022-05-06 15:20:26 -040044 BOOST_TEST(identity1.getName() == id1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080045}
46
Davide Pesavento56cc0d72022-04-29 23:00:23 -040047BOOST_AUTO_TEST_CASE(KeyOperations)
Yingdi Yucbe72b02015-11-25 17:35:37 -080048{
Davide Pesavento8618c1e2022-05-05 15:20:02 -040049 // identity does not have any keys
Yingdi Yucbe72b02015-11-25 17:35:37 -080050 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 0);
51
52 // get non-existing key, throw Pib::Error
53 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
54 // get default key, throw Pib::Error
55 BOOST_CHECK_THROW(identity1.getDefaultKey(), Pib::Error);
56 // set non-existing key as default key, throw Pib::Error
Davide Pesavento56cc0d72022-04-29 23:00:23 -040057 BOOST_CHECK_THROW(identity1.setDefaultKey(id1Key1Name), Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -080058
59 // add key
Davide Pesavento765abc92021-12-27 00:44:04 -050060 identity1.addKey(id1Key1, id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040061 const auto& addedKey = identity1.getKey(id1Key1Name);
62 BOOST_CHECK_EQUAL(addedKey.getName(), id1Key1Name);
63 BOOST_TEST(addedKey.getPublicKey() == id1Key1, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -080064
65 // new key becomes default key when there is no default key
Davide Pesavento56cc0d72022-04-29 23:00:23 -040066 const auto& defaultKey0 = identity1.getDefaultKey();
Yingdi Yucbe72b02015-11-25 17:35:37 -080067 BOOST_CHECK_EQUAL(defaultKey0.getName(), id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040068 BOOST_TEST(defaultKey0.getPublicKey() == id1Key1, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -080069
70 // remove key
71 identity1.removeKey(id1Key1Name);
72 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
73 BOOST_CHECK_THROW(identity1.getDefaultKey(), Pib::Error);
74
75 // set default key directly
Davide Pesavento765abc92021-12-27 00:44:04 -050076 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1, id1Key1Name));
Davide Pesavento56cc0d72022-04-29 23:00:23 -040077 const auto& defaultKey1 = identity1.getDefaultKey();
Yingdi Yucbe72b02015-11-25 17:35:37 -080078 BOOST_CHECK_EQUAL(defaultKey1.getName(), id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040079 BOOST_TEST(defaultKey1.getPublicKey() == id1Key1, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -080080
81 // add another key
Davide Pesavento765abc92021-12-27 00:44:04 -050082 identity1.addKey(id1Key2, id1Key2Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -080083 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 2);
84
85 // set default key through name
86 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key2Name));
Davide Pesavento56cc0d72022-04-29 23:00:23 -040087 const auto& defaultKey2 = identity1.getDefaultKey();
Yingdi Yucbe72b02015-11-25 17:35:37 -080088 BOOST_CHECK_EQUAL(defaultKey2.getName(), id1Key2Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040089 BOOST_TEST(defaultKey2.getPublicKey() == id1Key2, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -080090
91 // remove key
92 identity1.removeKey(id1Key1Name);
93 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
94 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 1);
95
96 // set default key directly again, change the default setting
Davide Pesavento765abc92021-12-27 00:44:04 -050097 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1, id1Key1Name));
Davide Pesavento56cc0d72022-04-29 23:00:23 -040098 const auto& defaultKey3 = identity1.getDefaultKey();
Yingdi Yucbe72b02015-11-25 17:35:37 -080099 BOOST_CHECK_EQUAL(defaultKey3.getName(), id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400100 BOOST_TEST(defaultKey3.getPublicKey() == id1Key1, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -0800101 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 2);
102
103 // remove all keys
104 identity1.removeKey(id1Key1Name);
105 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
106 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 1);
107 identity1.removeKey(id1Key2Name);
108 BOOST_CHECK_THROW(identity1.getKey(id1Key2Name), Pib::Error);
109 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 0);
110 BOOST_CHECK_THROW(identity1.getDefaultKey(), Pib::Error);
111}
112
Davide Pesavento8618c1e2022-05-05 15:20:02 -0400113BOOST_AUTO_TEST_CASE(ReplaceKey)
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800114{
Davide Pesavento765abc92021-12-27 00:44:04 -0500115 identity1.addKey(id1Key1, id1Key1Name);
116 auto k1 = identity1.getKey(id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400117 BOOST_TEST(k1.getPublicKey() == id1Key1, boost::test_tools::per_element());
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800118
Davide Pesavento8618c1e2022-05-05 15:20:02 -0400119 identity1.addKey(id1Key2, id1Key1Name); // overwrite key
Davide Pesavento765abc92021-12-27 00:44:04 -0500120 auto k2 = identity1.getKey(id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400121 BOOST_TEST(k2.getPublicKey() == id1Key2, boost::test_tools::per_element());
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800122}
123
Yingdi Yucbe72b02015-11-25 17:35:37 -0800124BOOST_AUTO_TEST_CASE(Errors)
125{
Davide Pesavento765abc92021-12-27 00:44:04 -0500126 BOOST_CHECK_THROW(identity1.addKey(id2Key1, id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800127 BOOST_CHECK_THROW(identity1.removeKey(id2Key1Name), std::invalid_argument);
128 BOOST_CHECK_THROW(identity1.getKey(id2Key1Name), std::invalid_argument);
Davide Pesavento765abc92021-12-27 00:44:04 -0500129 BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1, id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800130 BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1Name), std::invalid_argument);
131}
132
133BOOST_AUTO_TEST_SUITE_END() // TestIdentityImpl
Yingdi Yucbe72b02015-11-25 17:35:37 -0800134BOOST_AUTO_TEST_SUITE_END() // Security
135
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400136} // namespace ndn::tests