blob: 4d1da0ce0c1f4551ce3f28523dc05a5436c70e3f [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 Pesavento78ca8ae2022-05-01 01:37:05 -04003 * Copyright (c) 2013-2022 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
28namespace ndn {
29namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070030namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070031namespace tests {
32
Davide Pesaventoeee3e822016-11-26 19:19:34 +010033BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yu6ee2d362015-07-16 21:48:05 -070034BOOST_AUTO_TEST_SUITE(Pib)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010035BOOST_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 Pesavento8618c1e2022-05-05 15:20:02 -040044 auto impl = std::make_shared<detail::KeyImpl>(id1Key1Name, id1Key1,
45 makePibWithKey(id1Key1Name, id1Key1));
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040046 key = Key(impl);
47 BOOST_TEST(key);
48 BOOST_TEST(key != Key());
Davide Pesavento07db0732022-05-06 15:20:26 -040049 BOOST_TEST(key.getName() == id1Key1Name);
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040050
51 impl.reset();
52 BOOST_TEST(!key);
Davide Pesavento07db0732022-05-06 15:20:26 -040053 BOOST_CHECK_THROW(key.getName(), std::domain_error);
Yingdi Yub8f8b342015-04-27 11:06:42 -070054}
55
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040056// pib::Key is a wrapper of pib::detail::KeyImpl. Since the functionality of KeyImpl is
57// already tested in key-impl.t.cpp, we only test the shared property of pib::Key in
58// this test case.
Davide Pesavento4fb35d82019-10-31 19:33:10 -040059BOOST_AUTO_TEST_CASE(SharedImpl)
Yingdi Yub8f8b342015-04-27 11:06:42 -070060{
Davide Pesavento765abc92021-12-27 00:44:04 -050061 auto keyImpl = std::make_shared<detail::KeyImpl>(id1Key1Name, id1Key1,
Davide Pesavento8618c1e2022-05-05 15:20:02 -040062 makePibWithKey(id1Key1Name, id1Key1));
Yingdi Yucbe72b02015-11-25 17:35:37 -080063 Key key1(keyImpl);
64 Key key2(keyImpl);
Yingdi Yub8f8b342015-04-27 11:06:42 -070065
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040066 BOOST_TEST(key1 == key2);
67 BOOST_TEST(key1 != Key());
68 BOOST_TEST(Key() != key2);
69 BOOST_TEST(Key() == Key());
70
71 BOOST_CHECK_THROW(key2.getCertificate(id1Key1Cert1.getName()), pib::Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -080072 key1.addCertificate(id1Key1Cert1);
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040073 BOOST_TEST(key2.getCertificate(id1Key1Cert1.getName()) == id1Key1Cert1);
74
Yingdi Yucbe72b02015-11-25 17:35:37 -080075 key2.removeCertificate(id1Key1Cert1.getName());
Davide Pesavento4fb35d82019-10-31 19:33:10 -040076 BOOST_CHECK_THROW(key1.getCertificate(id1Key1Cert1.getName()), pib::Pib::Error);
Yingdi Yub8f8b342015-04-27 11:06:42 -070077
Yingdi Yucbe72b02015-11-25 17:35:37 -080078 key1.setDefaultCertificate(id1Key1Cert1);
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040079 BOOST_TEST(key2.getDefaultCertificate() == id1Key1Cert1);
Yingdi Yub8f8b342015-04-27 11:06:42 -070080}
81
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040082BOOST_AUTO_TEST_CASE(Helpers)
83{
Davide Pesaventof2cae612021-03-24 18:47:05 -040084 BOOST_CHECK_EQUAL(constructKeyName("/hello", name::Component("world")), "/hello/KEY/world");
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040085
Davide Pesaventof2cae612021-03-24 18:47:05 -040086 BOOST_CHECK_EQUAL(isValidKeyName("/hello"), false);
87 BOOST_CHECK_EQUAL(isValidKeyName("/hello/KEY"), false);
88 BOOST_CHECK_EQUAL(isValidKeyName("/hello/KEY/world"), true);
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040089
Davide Pesaventof2cae612021-03-24 18:47:05 -040090 BOOST_CHECK_EQUAL(isValidKeyName("/KEY/hello"), true);
91 BOOST_CHECK_EQUAL(isValidKeyName("/hello/world/KEY/!"), true);
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040092
Davide Pesaventof2cae612021-03-24 18:47:05 -040093 BOOST_CHECK_EQUAL(extractIdentityFromKeyName("/KEY/hello"), "/");
94 BOOST_CHECK_EQUAL(extractIdentityFromKeyName("/hello/KEY/world"), "/hello");
95 BOOST_CHECK_EQUAL(extractIdentityFromKeyName("/hello/world/KEY/!"), "/hello/world");
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040096
Davide Pesaventof2cae612021-03-24 18:47:05 -040097 BOOST_CHECK_THROW(extractIdentityFromKeyName("/hello"), std::invalid_argument);
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040098}
99
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100100BOOST_AUTO_TEST_SUITE_END() // TestKey
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700101BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100102BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700103
104} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700105} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700106} // namespace security
107} // namespace ndn