blob: 390cee4260c18a2aa139adc565ed4e24595eeb0f [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"
Davide Pesavento4fb35d82019-10-31 19:33:10 -040023#include "ndn-cxx/security/pib/impl/pib-memory.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/security/pib/pib.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080025
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "tests/boost-test.hpp"
27#include "tests/unit/security/pib/pib-data-fixture.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080028
29namespace ndn {
30namespace security {
31namespace pib {
32namespace detail {
33namespace tests {
34
35BOOST_AUTO_TEST_SUITE(Security)
36BOOST_AUTO_TEST_SUITE(Pib)
Yingdi Yucbe72b02015-11-25 17:35:37 -080037BOOST_FIXTURE_TEST_SUITE(TestIdentityImpl, ndn::security::tests::PibDataFixture)
38
Davide Pesavento56cc0d72022-04-29 23:00:23 -040039using pib::Pib;
Yingdi Yucbe72b02015-11-25 17:35:37 -080040
41BOOST_AUTO_TEST_CASE(Basic)
42{
Davide Pesavento56cc0d72022-04-29 23:00:23 -040043 IdentityImpl identity1(id1, std::make_shared<pib::PibMemory>(), true);
Yingdi Yucbe72b02015-11-25 17:35:37 -080044 BOOST_CHECK_EQUAL(identity1.getName(), id1);
45}
46
Davide Pesavento56cc0d72022-04-29 23:00:23 -040047BOOST_AUTO_TEST_CASE(KeyOperations)
Yingdi Yucbe72b02015-11-25 17:35:37 -080048{
Davide Pesavento56cc0d72022-04-29 23:00:23 -040049 auto pibImpl = std::make_shared<pib::PibMemory>();
Yingdi Yucbe72b02015-11-25 17:35:37 -080050 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 Pesavento56cc0d72022-04-29 23:00:23 -040061 BOOST_CHECK_THROW(identity1.setDefaultKey(id1Key1Name), Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -080062
63 // add key
Davide Pesavento765abc92021-12-27 00:44:04 -050064 identity1.addKey(id1Key1, id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040065 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 Yucbe72b02015-11-25 17:35:37 -080068
69 // new key becomes default key when there is no default key
Davide Pesavento56cc0d72022-04-29 23:00:23 -040070 const auto& defaultKey0 = identity1.getDefaultKey();
Yingdi Yucbe72b02015-11-25 17:35:37 -080071 BOOST_CHECK_EQUAL(defaultKey0.getName(), id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040072 BOOST_TEST(defaultKey0.getPublicKey() == id1Key1, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -080073
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 Pesavento765abc92021-12-27 00:44:04 -050080 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1, id1Key1Name));
Davide Pesavento56cc0d72022-04-29 23:00:23 -040081 const auto& defaultKey1 = identity1.getDefaultKey();
Yingdi Yucbe72b02015-11-25 17:35:37 -080082 BOOST_CHECK_EQUAL(defaultKey1.getName(), id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040083 BOOST_TEST(defaultKey1.getPublicKey() == id1Key1, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -080084
85 // add another key
Davide Pesavento765abc92021-12-27 00:44:04 -050086 identity1.addKey(id1Key2, id1Key2Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -080087 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 2);
88
89 // set default key through name
90 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key2Name));
Davide Pesavento56cc0d72022-04-29 23:00:23 -040091 const auto& defaultKey2 = identity1.getDefaultKey();
Yingdi Yucbe72b02015-11-25 17:35:37 -080092 BOOST_CHECK_EQUAL(defaultKey2.getName(), id1Key2Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040093 BOOST_TEST(defaultKey2.getPublicKey() == id1Key2, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -080094
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 Pesavento765abc92021-12-27 00:44:04 -0500101 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1, id1Key1Name));
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400102 const auto& defaultKey3 = identity1.getDefaultKey();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800103 BOOST_CHECK_EQUAL(defaultKey3.getName(), id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400104 BOOST_TEST(defaultKey3.getPublicKey() == id1Key1, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -0800105 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 Afanasyeva10b2ff2017-01-30 12:44:15 -0800117BOOST_AUTO_TEST_CASE(Overwrite)
118{
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400119 IdentityImpl identity1(id1, std::make_shared<pib::PibMemory>(), true);
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800120
Davide Pesavento765abc92021-12-27 00:44:04 -0500121 identity1.addKey(id1Key1, id1Key1Name);
122 auto k1 = identity1.getKey(id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400123 BOOST_TEST(k1.getPublicKey() == id1Key1, boost::test_tools::per_element());
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800124
Davide Pesavento765abc92021-12-27 00:44:04 -0500125 identity1.addKey(id1Key2, id1Key1Name); // overwriting key should work
126 auto k2 = identity1.getKey(id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400127 BOOST_TEST(k2.getPublicKey() == id1Key2, boost::test_tools::per_element());
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800128}
129
Yingdi Yucbe72b02015-11-25 17:35:37 -0800130BOOST_AUTO_TEST_CASE(Errors)
131{
Davide Pesavento56cc0d72022-04-29 23:00:23 -0400132 auto pibImpl = std::make_shared<pib::PibMemory>();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800133
134 BOOST_CHECK_THROW(IdentityImpl(id1, pibImpl, false), Pib::Error);
135 IdentityImpl identity1(id1, pibImpl, true);
136
Davide Pesavento765abc92021-12-27 00:44:04 -0500137 identity1.addKey(id1Key1, id1Key1Name);
138 BOOST_CHECK_THROW(identity1.addKey(id2Key1, id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800139 BOOST_CHECK_THROW(identity1.removeKey(id2Key1Name), std::invalid_argument);
140 BOOST_CHECK_THROW(identity1.getKey(id2Key1Name), std::invalid_argument);
Davide Pesavento765abc92021-12-27 00:44:04 -0500141 BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1, id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800142 BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1Name), std::invalid_argument);
143}
144
145BOOST_AUTO_TEST_SUITE_END() // TestIdentityImpl
Yingdi Yucbe72b02015-11-25 17:35:37 -0800146BOOST_AUTO_TEST_SUITE_END() // Pib
147BOOST_AUTO_TEST_SUITE_END() // Security
148
149} // namespace tests
150} // namespace detail
151} // namespace pib
152} // namespace security
153} // namespace ndn