Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -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/pib.hpp" |
Davide Pesavento | 4fb35d8 | 2019-10-31 19:33:10 -0400 | [diff] [blame] | 23 | #include "ndn-cxx/security/pib/impl/pib-memory.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 | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 32 | BOOST_AUTO_TEST_SUITE(Security) |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 33 | BOOST_FIXTURE_TEST_SUITE(TestPib, PibDataFixture) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 34 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 35 | BOOST_AUTO_TEST_CASE(TpmLocator) |
| 36 | { |
Davide Pesavento | 0e768ef | 2022-05-09 20:03:44 -0400 | [diff] [blame] | 37 | Pib pib("pib-memory:", make_shared<PibMemory>()); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 38 | |
| 39 | BOOST_CHECK_EQUAL(pib.getPibLocator(), "pib-memory:"); |
Davide Pesavento | 0e768ef | 2022-05-09 20:03:44 -0400 | [diff] [blame] | 40 | BOOST_CHECK_EQUAL(pib.getTpmLocator(), ""); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 41 | |
| 42 | pib.setTpmLocator("test-tpm-locator"); |
Davide Pesavento | 0e768ef | 2022-05-09 20:03:44 -0400 | [diff] [blame] | 43 | BOOST_CHECK_EQUAL(pib.getTpmLocator(), "test-tpm-locator"); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 44 | |
| 45 | BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error); |
| 46 | pib.addIdentity(id1); |
| 47 | BOOST_CHECK_NO_THROW(pib.getIdentity(id1)); |
| 48 | |
| 49 | pib.setTpmLocator("another-tpm-locator"); |
| 50 | BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error); |
Davide Pesavento | 0e768ef | 2022-05-09 20:03:44 -0400 | [diff] [blame] | 51 | BOOST_CHECK_EQUAL(pib.getTpmLocator(), "another-tpm-locator"); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 52 | |
| 53 | pib.addIdentity(id1); |
| 54 | BOOST_CHECK_NO_THROW(pib.getIdentity(id1)); |
| 55 | pib.reset(); |
| 56 | BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error); |
Davide Pesavento | 0e768ef | 2022-05-09 20:03:44 -0400 | [diff] [blame] | 57 | BOOST_CHECK_EQUAL(pib.getTpmLocator(), ""); |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 60 | BOOST_AUTO_TEST_CASE(IdentityOperations) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 61 | { |
Davide Pesavento | 0e768ef | 2022-05-09 20:03:44 -0400 | [diff] [blame] | 62 | Pib pib("pib-memory:", make_shared<PibMemory>()); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 63 | |
Davide Pesavento | 1c62884 | 2022-04-28 23:21:44 -0400 | [diff] [blame] | 64 | // PIB starts with no identities |
| 65 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 0); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 66 | BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 67 | // get default identity when it is not set yet, throw Pib::Error |
| 68 | BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error); |
| 69 | |
| 70 | // add identity |
| 71 | pib.addIdentity(id1); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 72 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1); |
Davide Pesavento | 1c62884 | 2022-04-28 23:21:44 -0400 | [diff] [blame] | 73 | BOOST_CHECK_EQUAL(pib.getIdentity(id1).getName(), id1); |
| 74 | // add another |
| 75 | pib.addIdentity(id2); |
| 76 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 2); |
| 77 | BOOST_CHECK_EQUAL(pib.getIdentity(id2).getName(), id2); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 78 | |
Davide Pesavento | 1c62884 | 2022-04-28 23:21:44 -0400 | [diff] [blame] | 79 | // first identity implicitly becomes the default when there was no default identity |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 80 | BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id1); |
| 81 | |
Davide Pesavento | 1c62884 | 2022-04-28 23:21:44 -0400 | [diff] [blame] | 82 | // remove both identities |
| 83 | pib.removeIdentity(id2); |
| 84 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1); |
| 85 | BOOST_CHECK_EQUAL(pib.getIdentity(id1).getName(), id1); |
| 86 | BOOST_CHECK_THROW(pib.getIdentity(id2), Pib::Error); |
| 87 | BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id1); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 88 | pib.removeIdentity(id1); |
Davide Pesavento | 1c62884 | 2022-04-28 23:21:44 -0400 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 0); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 90 | BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 91 | BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 92 | |
Davide Pesavento | 1c62884 | 2022-04-28 23:21:44 -0400 | [diff] [blame] | 93 | // set default identity (and implicitly create it) |
| 94 | pib.setDefaultIdentity(id1); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id1); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1); |
Davide Pesavento | 1c62884 | 2022-04-28 23:21:44 -0400 | [diff] [blame] | 97 | pib.setDefaultIdentity(id2); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id2); |
| 99 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 2); |
| 100 | |
| 101 | // remove default identity |
| 102 | pib.removeIdentity(id2); |
Davide Pesavento | 1c62884 | 2022-04-28 23:21:44 -0400 | [diff] [blame] | 103 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1); |
| 104 | BOOST_CHECK_EQUAL(pib.getIdentity(id1).getName(), id1); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 105 | BOOST_CHECK_THROW(pib.getIdentity(id2), Pib::Error); |
| 106 | BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error); |
Davide Pesavento | 1c62884 | 2022-04-28 23:21:44 -0400 | [diff] [blame] | 107 | |
| 108 | // adding an identity now makes it the default |
| 109 | pib.addIdentity(id2); |
| 110 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 2); |
| 111 | BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id2); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 114 | BOOST_AUTO_TEST_SUITE_END() // TestPib |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 115 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 116 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 117 | } // namespace ndn::tests |