Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 6fd26cf | 2017-08-31 17:43:09 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 4 | * |
| 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 Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/security/pib/key.hpp" |
Junxiao Shi | 24c5a00 | 2018-12-12 04:47:15 +0000 | [diff] [blame] | 23 | #include "ndn-cxx/security/pib/impl/key-impl.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
| 26 | #include "tests/unit/security/pib/pib-data-fixture.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 27 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 28 | namespace ndn::tests { |
| 29 | |
| 30 | using namespace ndn::security::pib; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 31 | |
Davide Pesavento | fffdd62 | 2023-08-28 22:50:43 -0400 | [diff] [blame] | 32 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Key>)); |
| 33 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Security) |
| 35 | BOOST_FIXTURE_TEST_SUITE(TestKey, PibDataFixture) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 37 | BOOST_AUTO_TEST_CASE(ValidityChecking) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 38 | { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 39 | Key key; |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 40 | BOOST_TEST(!key); |
| 41 | BOOST_TEST(key == Key()); |
Davide Pesavento | 07db073 | 2022-05-06 15:20:26 -0400 | [diff] [blame] | 42 | BOOST_CHECK_THROW(key.getName(), std::domain_error); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 43 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 44 | auto impl = std::make_shared<KeyImpl>(id1Key1Name, id1Key1, makePibWithKey(id1Key1Name, id1Key1)); |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 45 | key = Key(impl); |
| 46 | BOOST_TEST(key); |
| 47 | BOOST_TEST(key != Key()); |
Davide Pesavento | 07db073 | 2022-05-06 15:20:26 -0400 | [diff] [blame] | 48 | BOOST_TEST(key.getName() == id1Key1Name); |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 49 | |
| 50 | impl.reset(); |
| 51 | BOOST_TEST(!key); |
Davide Pesavento | 07db073 | 2022-05-06 15:20:26 -0400 | [diff] [blame] | 52 | BOOST_CHECK_THROW(key.getName(), std::domain_error); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 55 | // 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 Pesavento | 4fb35d8 | 2019-10-31 19:33:10 -0400 | [diff] [blame] | 58 | BOOST_AUTO_TEST_CASE(SharedImpl) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 59 | { |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 60 | auto keyImpl = std::make_shared<KeyImpl>(id1Key1Name, id1Key1, makePibWithKey(id1Key1Name, id1Key1)); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 61 | Key key1(keyImpl); |
| 62 | Key key2(keyImpl); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 63 | |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 64 | BOOST_TEST(key1 == key2); |
| 65 | BOOST_TEST(key1 != Key()); |
| 66 | BOOST_TEST(Key() != key2); |
| 67 | BOOST_TEST(Key() == Key()); |
| 68 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 69 | BOOST_CHECK_THROW(key2.getCertificate(id1Key1Cert1.getName()), Pib::Error); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 70 | key1.addCertificate(id1Key1Cert1); |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 71 | BOOST_TEST(key2.getCertificate(id1Key1Cert1.getName()) == id1Key1Cert1); |
| 72 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 73 | key2.removeCertificate(id1Key1Cert1.getName()); |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 74 | BOOST_CHECK_THROW(key1.getCertificate(id1Key1Cert1.getName()), Pib::Error); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 75 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 76 | key1.setDefaultCertificate(id1Key1Cert1); |
Davide Pesavento | 78ca8ae | 2022-05-01 01:37:05 -0400 | [diff] [blame] | 77 | BOOST_TEST(key2.getDefaultCertificate() == id1Key1Cert1); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Alexander Afanasyev | 6fd26cf | 2017-08-31 17:43:09 -0400 | [diff] [blame] | 80 | BOOST_AUTO_TEST_CASE(Helpers) |
| 81 | { |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 82 | using ndn::security::constructKeyName; |
| 83 | using ndn::security::isValidKeyName; |
| 84 | using ndn::security::extractIdentityFromKeyName; |
| 85 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 86 | BOOST_CHECK_EQUAL(constructKeyName("/hello", name::Component("world")), "/hello/KEY/world"); |
Alexander Afanasyev | 6fd26cf | 2017-08-31 17:43:09 -0400 | [diff] [blame] | 87 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(isValidKeyName("/hello"), false); |
| 89 | BOOST_CHECK_EQUAL(isValidKeyName("/hello/KEY"), false); |
| 90 | BOOST_CHECK_EQUAL(isValidKeyName("/hello/KEY/world"), true); |
Alexander Afanasyev | 6fd26cf | 2017-08-31 17:43:09 -0400 | [diff] [blame] | 91 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 92 | BOOST_CHECK_EQUAL(isValidKeyName("/KEY/hello"), true); |
| 93 | BOOST_CHECK_EQUAL(isValidKeyName("/hello/world/KEY/!"), true); |
Alexander Afanasyev | 6fd26cf | 2017-08-31 17:43:09 -0400 | [diff] [blame] | 94 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 95 | 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 Afanasyev | 6fd26cf | 2017-08-31 17:43:09 -0400 | [diff] [blame] | 98 | |
Davide Pesavento | f2cae61 | 2021-03-24 18:47:05 -0400 | [diff] [blame] | 99 | BOOST_CHECK_THROW(extractIdentityFromKeyName("/hello"), std::invalid_argument); |
Alexander Afanasyev | 6fd26cf | 2017-08-31 17:43:09 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 102 | BOOST_AUTO_TEST_SUITE_END() // TestKey |
| 103 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 104 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 105 | } // namespace ndn::tests |