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 | /* |
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/key.hpp" |
| 23 | #include "security/pib/pib.hpp" |
| 24 | #include "security/pib/pib-memory.hpp" |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 25 | #include "security/pib/detail/key-impl.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 26 | |
| 27 | #include "boost-test.hpp" |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 28 | #include "pib-data-fixture.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 32 | namespace pib { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 33 | namespace tests { |
| 34 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 35 | using namespace ndn::security::tests; |
| 36 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Security) |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(Pib) |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 39 | BOOST_FIXTURE_TEST_SUITE(TestKey, PibDataFixture) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 40 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 41 | using pib::Pib; |
| 42 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 43 | BOOST_AUTO_TEST_CASE(ValidityChecking) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 44 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 45 | using security::pib::detail::KeyImpl; |
| 46 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 47 | Key key; |
| 48 | |
| 49 | BOOST_CHECK_EQUAL(static_cast<bool>(key), false); |
| 50 | BOOST_CHECK_EQUAL(!key, true); |
| 51 | |
| 52 | if (key) |
| 53 | BOOST_CHECK(false); |
| 54 | else |
| 55 | BOOST_CHECK(true); |
| 56 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 57 | auto keyImpl = make_shared<KeyImpl>(id1Key1Name, id1Key1.data(), id1Key1.size(), |
| 58 | make_shared<pib::PibMemory>()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 59 | key = Key(keyImpl); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 60 | |
| 61 | BOOST_CHECK_EQUAL(static_cast<bool>(key), true); |
| 62 | BOOST_CHECK_EQUAL(!key, false); |
| 63 | |
| 64 | if (key) |
| 65 | BOOST_CHECK(true); |
| 66 | else |
| 67 | BOOST_CHECK(false); |
| 68 | } |
| 69 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 70 | /** |
| 71 | * pib::Key is a wrapper of pib::detail::KeyImpl. Since the functionalities of KeyImpl |
| 72 | * have already been tested in detail/key-impl.t.cpp, we only test the shared property |
| 73 | * of pib::Key in this test case. |
| 74 | */ |
| 75 | |
| 76 | BOOST_AUTO_TEST_CASE(Share) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 77 | { |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 78 | using security::pib::detail::KeyImpl; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 79 | |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 80 | auto keyImpl = make_shared<KeyImpl>(id1Key1Name, id1Key1.data(), id1Key1.size(), |
| 81 | make_shared<pib::PibMemory>()); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 82 | Key key1(keyImpl); |
| 83 | Key key2(keyImpl); |
Junxiao Shi | 5759be3 | 2017-10-15 00:00:52 +0000 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(key1, key2); |
| 85 | BOOST_CHECK_NE(key1, Key()); |
| 86 | BOOST_CHECK_EQUAL(Key(), Key()); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 87 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 88 | key1.addCertificate(id1Key1Cert1); |
| 89 | BOOST_CHECK_NO_THROW(key2.getCertificate(id1Key1Cert1.getName())); |
| 90 | key2.removeCertificate(id1Key1Cert1.getName()); |
| 91 | BOOST_CHECK_THROW(key1.getCertificate(id1Key1Cert1.getName()), Pib::Error); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 92 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 93 | key1.setDefaultCertificate(id1Key1Cert1); |
| 94 | BOOST_CHECK_NO_THROW(key2.getDefaultCertificate()); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Alexander Afanasyev | 6fd26cf | 2017-08-31 17:43:09 -0400 | [diff] [blame] | 97 | BOOST_AUTO_TEST_CASE(Helpers) |
| 98 | { |
| 99 | BOOST_CHECK_EQUAL(v2::constructKeyName("/hello", name::Component("world")), "/hello/KEY/world"); |
| 100 | |
| 101 | BOOST_CHECK_EQUAL(v2::isValidKeyName("/hello"), false); |
| 102 | BOOST_CHECK_EQUAL(v2::isValidKeyName("/hello/KEY"), false); |
| 103 | BOOST_CHECK_EQUAL(v2::isValidKeyName("/hello/KEY/world"), true); |
| 104 | |
| 105 | BOOST_CHECK_EQUAL(v2::isValidKeyName("/KEY/hello"), true); |
| 106 | BOOST_CHECK_EQUAL(v2::isValidKeyName("/hello/world/KEY/!"), true); |
| 107 | |
| 108 | BOOST_CHECK_EQUAL(v2::extractIdentityFromKeyName("/KEY/hello"), "/"); |
| 109 | BOOST_CHECK_EQUAL(v2::extractIdentityFromKeyName("/hello/KEY/world"), "/hello"); |
| 110 | BOOST_CHECK_EQUAL(v2::extractIdentityFromKeyName("/hello/world/KEY/!"), "/hello/world"); |
| 111 | |
| 112 | BOOST_CHECK_THROW(v2::extractIdentityFromKeyName("/hello"), std::invalid_argument); |
| 113 | } |
| 114 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 115 | BOOST_AUTO_TEST_SUITE_END() // TestKey |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 116 | BOOST_AUTO_TEST_SUITE_END() // Pib |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 117 | BOOST_AUTO_TEST_SUITE_END() // Security |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 118 | |
| 119 | } // namespace tests |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 120 | } // namespace pib |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 121 | } // namespace security |
| 122 | } // namespace ndn |