blob: 42a991671d619d5fad71b01c9fac75fa5fa94e4d [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5759be32017-10-15 00:00:52 +00002/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -07004 *
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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/pib/identity.hpp"
Junxiao Shi24c5a002018-12-12 04:47:15 +000023#include "ndn-cxx/security/pib/impl/identity-impl.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
26#include "tests/unit/security/pib/pib-data-fixture.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070027
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040028namespace ndn::tests {
29
30using namespace ndn::security::pib;
Yingdi Yub8f8b342015-04-27 11:06:42 -070031
Davide Pesaventofffdd622023-08-28 22:50:43 -040032BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Identity>));
33
Davide Pesaventoeee3e822016-11-26 19:19:34 +010034BOOST_AUTO_TEST_SUITE(Security)
35BOOST_FIXTURE_TEST_SUITE(TestIdentity, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070036
Davide Pesaventoeee3e822016-11-26 19:19:34 +010037BOOST_AUTO_TEST_CASE(ValidityChecking)
Yingdi Yub8f8b342015-04-27 11:06:42 -070038{
Yingdi Yub8f8b342015-04-27 11:06:42 -070039 Identity id;
Davide Pesavento56cc0d72022-04-29 23:00:23 -040040 BOOST_TEST(!id);
41 BOOST_TEST(id == Identity());
Davide Pesavento07db0732022-05-06 15:20:26 -040042 BOOST_CHECK_THROW(id.getName(), std::domain_error);
Yingdi Yub8f8b342015-04-27 11:06:42 -070043
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040044 auto impl = std::make_shared<IdentityImpl>(id1, makePibWithIdentity(id1));
Davide Pesavento56cc0d72022-04-29 23:00:23 -040045 id = Identity(impl);
46 BOOST_TEST(id);
47 BOOST_TEST(id != Identity());
Davide Pesavento07db0732022-05-06 15:20:26 -040048 BOOST_TEST(id.getName() == id1);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040049
50 impl.reset();
51 BOOST_TEST(!id);
Davide Pesavento07db0732022-05-06 15:20:26 -040052 BOOST_CHECK_THROW(id.getName(), std::domain_error);
Yingdi Yub8f8b342015-04-27 11:06:42 -070053}
54
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040055// pib::Identity is a wrapper of pib::IdentityImpl. Since the functionality
56// of IdentityImpl is already tested in identity-impl.t.cpp, we only test
57// the shared property of pib::Identity in this test case.
Davide Pesavento4fb35d82019-10-31 19:33:10 -040058BOOST_AUTO_TEST_CASE(SharedImpl)
Yingdi Yub8f8b342015-04-27 11:06:42 -070059{
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040060 auto impl = std::make_shared<IdentityImpl>(id1, makePibWithIdentity(id1));
Davide Pesavento56cc0d72022-04-29 23:00:23 -040061 Identity identity1(impl);
62 Identity identity2(impl);
Yingdi Yucbe72b02015-11-25 17:35:37 -080063
Davide Pesavento56cc0d72022-04-29 23:00:23 -040064 BOOST_TEST(identity1 == identity2);
65 BOOST_TEST(identity1 != Identity());
66 BOOST_TEST(Identity() != identity2);
67 BOOST_TEST(Identity() == Identity());
68
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040069 BOOST_CHECK_THROW(identity2.getKey(id1Key1Name), Pib::Error);
Davide Pesavento35c63792022-01-17 02:06:03 -050070 identity1.addKey(id1Key1, id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040071 BOOST_TEST(identity2.getKey(id1Key1Name));
72
Yingdi Yucbe72b02015-11-25 17:35:37 -080073 identity2.removeKey(id1Key1Name);
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040074 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
Yingdi Yub8f8b342015-04-27 11:06:42 -070075
Davide Pesavento35c63792022-01-17 02:06:03 -050076 identity1.setDefaultKey(id1Key1, id1Key1Name);
Davide Pesavento56cc0d72022-04-29 23:00:23 -040077 BOOST_TEST(identity2.getDefaultKey().getName() == id1Key1Name);
Yingdi Yub8f8b342015-04-27 11:06:42 -070078}
79
Davide Pesaventoeee3e822016-11-26 19:19:34 +010080BOOST_AUTO_TEST_SUITE_END() // TestIdentity
81BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -070082
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040083} // namespace ndn::tests