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 | /* |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 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 | |
Alexander Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 22 | #include "security/pib/pib.hpp" |
| 23 | #include "security/pib/pib-memory.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 24 | |
| 25 | #include "boost-test.hpp" |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 26 | #include "pib-data-fixture.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 30 | namespace pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 31 | namespace tests { |
| 32 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 33 | using namespace ndn::security::tests; |
| 34 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(Security) |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Pib) |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 37 | BOOST_FIXTURE_TEST_SUITE(TestPib, PibDataFixture) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 38 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 39 | using pib::Pib; |
| 40 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 41 | BOOST_AUTO_TEST_CASE(ValidityChecking) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 42 | { |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 43 | Pib pib("pib-memory", "", make_shared<PibMemory>()); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 44 | |
| 45 | Identity id = pib.addIdentity(id1); |
| 46 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(static_cast<bool>(id), true); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(!id, false); |
| 49 | |
| 50 | if (id) |
| 51 | BOOST_CHECK(true); |
| 52 | else |
| 53 | BOOST_CHECK(false); |
| 54 | |
| 55 | // key |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame^] | 56 | Key key = id.addKey(id1Key1.data(), id1Key1.size(), id1Key1Name); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 57 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(static_cast<bool>(key), true); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 59 | BOOST_CHECK_EQUAL(!key, false); |
| 60 | |
| 61 | if (key) |
| 62 | BOOST_CHECK(true); |
| 63 | else |
| 64 | BOOST_CHECK(false); |
| 65 | } |
| 66 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 67 | BOOST_AUTO_TEST_CASE(TpmLocator) |
| 68 | { |
| 69 | Pib pib("pib-memory", "", make_shared<PibMemory>()); |
| 70 | |
| 71 | BOOST_CHECK_EQUAL(pib.getPibLocator(), "pib-memory:"); |
| 72 | BOOST_CHECK_THROW(pib.getTpmLocator(), Pib::Error); |
| 73 | |
| 74 | pib.setTpmLocator("test-tpm-locator"); |
| 75 | BOOST_CHECK_NO_THROW(pib.getTpmLocator()); |
| 76 | |
| 77 | BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error); |
| 78 | pib.addIdentity(id1); |
| 79 | BOOST_CHECK_NO_THROW(pib.getIdentity(id1)); |
| 80 | |
| 81 | pib.setTpmLocator("another-tpm-locator"); |
| 82 | BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error); |
| 83 | |
| 84 | pib.addIdentity(id1); |
| 85 | BOOST_CHECK_NO_THROW(pib.getIdentity(id1)); |
| 86 | pib.reset(); |
| 87 | BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error); |
| 88 | BOOST_CHECK_THROW(pib.getTpmLocator(), Pib::Error); |
| 89 | } |
| 90 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 91 | BOOST_AUTO_TEST_CASE(IdentityOperations) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 92 | { |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 93 | Pib pib("pib-memory", "", make_shared<PibMemory>()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 94 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 0); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 95 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 96 | // get non-existing identity, throw Pib::Error |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 97 | BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 98 | // get default identity when it is not set yet, throw Pib::Error |
| 99 | BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error); |
| 100 | |
| 101 | // add identity |
| 102 | pib.addIdentity(id1); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 103 | BOOST_CHECK_NO_THROW(pib.getIdentity(id1)); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 104 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1); |
| 105 | |
| 106 | // new key becomes default key when there was no default key |
| 107 | BOOST_REQUIRE_NO_THROW(pib.getDefaultIdentity()); |
| 108 | BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id1); |
| 109 | |
| 110 | // remove identity |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 111 | pib.removeIdentity(id1); |
| 112 | BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 113 | BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 114 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 0); |
| 115 | |
| 116 | // set default identity |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 117 | BOOST_REQUIRE_NO_THROW(pib.setDefaultIdentity(id1)); |
| 118 | BOOST_REQUIRE_NO_THROW(pib.getDefaultIdentity()); |
| 119 | BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id1); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1); |
| 121 | BOOST_REQUIRE_NO_THROW(pib.setDefaultIdentity(id2)); |
| 122 | BOOST_REQUIRE_NO_THROW(pib.getDefaultIdentity()); |
| 123 | BOOST_CHECK_EQUAL(pib.getDefaultIdentity().getName(), id2); |
| 124 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 2); |
| 125 | |
| 126 | // remove default identity |
| 127 | pib.removeIdentity(id2); |
| 128 | BOOST_CHECK_THROW(pib.getIdentity(id2), Pib::Error); |
| 129 | BOOST_CHECK_THROW(pib.getDefaultIdentity(), Pib::Error); |
| 130 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 1); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 131 | pib.removeIdentity(id1); |
| 132 | BOOST_CHECK_THROW(pib.getIdentity(id1), Pib::Error); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 133 | BOOST_CHECK_EQUAL(pib.getIdentities().size(), 0); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 136 | BOOST_AUTO_TEST_SUITE_END() // TestPib |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 137 | BOOST_AUTO_TEST_SUITE_END() // Pib |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 138 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 139 | |
| 140 | } // namespace tests |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 141 | } // namespace pib |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 142 | } // namespace security |
| 143 | } // namespace ndn |