blob: a3c180619e1cbaef72cf6392cac2da58972c4719 [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -04002/*
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/key.hpp"
Junxiao Shi24c5a002018-12-12 04:47:15 +000023#include "ndn-cxx/security/pib/impl/key-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<Key>));
33
Davide Pesaventoeee3e822016-11-26 19:19:34 +010034BOOST_AUTO_TEST_SUITE(Security)
35BOOST_FIXTURE_TEST_SUITE(TestKey, 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 Key key;
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040040 BOOST_TEST(!key);
41 BOOST_TEST(key == Key());
Davide Pesavento07db0732022-05-06 15:20:26 -040042 BOOST_CHECK_THROW(key.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<KeyImpl>(id1Key1Name, id1Key1, makePibWithKey(id1Key1Name, id1Key1));
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040045 key = Key(impl);
46 BOOST_TEST(key);
47 BOOST_TEST(key != Key());
Davide Pesavento07db0732022-05-06 15:20:26 -040048 BOOST_TEST(key.getName() == id1Key1Name);
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040049
50 impl.reset();
51 BOOST_TEST(!key);
Davide Pesavento07db0732022-05-06 15:20:26 -040052 BOOST_CHECK_THROW(key.getName(), std::domain_error);
Yingdi Yub8f8b342015-04-27 11:06:42 -070053}
54
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040055// pib::Key is a wrapper of pib::KeyImpl. Since the functionality of KeyImpl is
56// already tested in key-impl.t.cpp, we only test the shared property of pib::Key
57// 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 keyImpl = std::make_shared<KeyImpl>(id1Key1Name, id1Key1, makePibWithKey(id1Key1Name, id1Key1));
Yingdi Yucbe72b02015-11-25 17:35:37 -080061 Key key1(keyImpl);
62 Key key2(keyImpl);
Yingdi Yub8f8b342015-04-27 11:06:42 -070063
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040064 BOOST_TEST(key1 == key2);
65 BOOST_TEST(key1 != Key());
66 BOOST_TEST(Key() != key2);
67 BOOST_TEST(Key() == Key());
68
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040069 BOOST_CHECK_THROW(key2.getCertificate(id1Key1Cert1.getName()), Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -080070 key1.addCertificate(id1Key1Cert1);
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040071 BOOST_TEST(key2.getCertificate(id1Key1Cert1.getName()) == id1Key1Cert1);
72
Yingdi Yucbe72b02015-11-25 17:35:37 -080073 key2.removeCertificate(id1Key1Cert1.getName());
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040074 BOOST_CHECK_THROW(key1.getCertificate(id1Key1Cert1.getName()), Pib::Error);
Yingdi Yub8f8b342015-04-27 11:06:42 -070075
Yingdi Yucbe72b02015-11-25 17:35:37 -080076 key1.setDefaultCertificate(id1Key1Cert1);
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040077 BOOST_TEST(key2.getDefaultCertificate() == id1Key1Cert1);
Yingdi Yub8f8b342015-04-27 11:06:42 -070078}
79
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040080BOOST_AUTO_TEST_CASE(Helpers)
81{
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040082 using ndn::security::constructKeyName;
83 using ndn::security::isValidKeyName;
84 using ndn::security::extractIdentityFromKeyName;
85
Davide Pesaventof2cae612021-03-24 18:47:05 -040086 BOOST_CHECK_EQUAL(constructKeyName("/hello", name::Component("world")), "/hello/KEY/world");
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040087
Davide Pesaventof2cae612021-03-24 18:47:05 -040088 BOOST_CHECK_EQUAL(isValidKeyName("/hello"), false);
89 BOOST_CHECK_EQUAL(isValidKeyName("/hello/KEY"), false);
90 BOOST_CHECK_EQUAL(isValidKeyName("/hello/KEY/world"), true);
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040091
Davide Pesaventof2cae612021-03-24 18:47:05 -040092 BOOST_CHECK_EQUAL(isValidKeyName("/KEY/hello"), true);
93 BOOST_CHECK_EQUAL(isValidKeyName("/hello/world/KEY/!"), true);
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040094
Davide Pesaventof2cae612021-03-24 18:47:05 -040095 BOOST_CHECK_EQUAL(extractIdentityFromKeyName("/KEY/hello"), "/");
96 BOOST_CHECK_EQUAL(extractIdentityFromKeyName("/hello/KEY/world"), "/hello");
97 BOOST_CHECK_EQUAL(extractIdentityFromKeyName("/hello/world/KEY/!"), "/hello/world");
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040098
Davide Pesaventof2cae612021-03-24 18:47:05 -040099 BOOST_CHECK_THROW(extractIdentityFromKeyName("/hello"), std::invalid_argument);
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -0400100}
101
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100102BOOST_AUTO_TEST_SUITE_END() // TestKey
103BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700104
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400105} // namespace ndn::tests