blob: 544848fc7be87dd1b32d2d471e39eb63fd581155 [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 Pesaventof2cae612021-03-24 18:47:05 -04003 * Copyright (c) 2013-2021 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"
Davide Pesavento4fb35d82019-10-31 19:33:10 -040024#include "ndn-cxx/security/pib/impl/pib-memory.hpp"
25#include "ndn-cxx/security/pib/pib.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070026
Davide Pesavento7e780642018-11-24 15:51:34 -050027#include "tests/boost-test.hpp"
28#include "tests/unit/security/pib/pib-data-fixture.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070029
30namespace ndn {
31namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070032namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070033namespace tests {
34
Yingdi Yu6ee2d362015-07-16 21:48:05 -070035using namespace ndn::security::tests;
36
Davide Pesaventoeee3e822016-11-26 19:19:34 +010037BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yu6ee2d362015-07-16 21:48:05 -070038BOOST_AUTO_TEST_SUITE(Pib)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010039BOOST_FIXTURE_TEST_SUITE(TestKey, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070040
Davide Pesaventoeee3e822016-11-26 19:19:34 +010041BOOST_AUTO_TEST_CASE(ValidityChecking)
Yingdi Yub8f8b342015-04-27 11:06:42 -070042{
Yingdi Yub8f8b342015-04-27 11:06:42 -070043 Key key;
Davide Pesavento4fb35d82019-10-31 19:33:10 -040044 BOOST_CHECK(!key);
Yingdi Yub8f8b342015-04-27 11:06:42 -070045 BOOST_CHECK_EQUAL(static_cast<bool>(key), false);
Yingdi Yub8f8b342015-04-27 11:06:42 -070046
Davide Pesavento4fb35d82019-10-31 19:33:10 -040047 auto keyImpl = make_shared<detail::KeyImpl>(id1Key1Name, id1Key1.data(), id1Key1.size(),
48 make_shared<pib::PibMemory>());
Yingdi Yucbe72b02015-11-25 17:35:37 -080049 key = Key(keyImpl);
Davide Pesavento4fb35d82019-10-31 19:33:10 -040050 BOOST_CHECK(key);
Yingdi Yub8f8b342015-04-27 11:06:42 -070051 BOOST_CHECK_EQUAL(!key, false);
Yingdi Yub8f8b342015-04-27 11:06:42 -070052}
53
Davide Pesavento4fb35d82019-10-31 19:33:10 -040054// pib::Key is a wrapper of pib::detail::KeyImpl. Since the functionalities of KeyImpl
55// have already been tested in detail/key-impl.t.cpp, we only test the shared property
56// of pib::Key in this test case.
57BOOST_AUTO_TEST_CASE(SharedImpl)
Yingdi Yub8f8b342015-04-27 11:06:42 -070058{
Davide Pesavento4fb35d82019-10-31 19:33:10 -040059 auto keyImpl = make_shared<detail::KeyImpl>(id1Key1Name, id1Key1.data(), id1Key1.size(),
60 make_shared<pib::PibMemory>());
Yingdi Yucbe72b02015-11-25 17:35:37 -080061 Key key1(keyImpl);
62 Key key2(keyImpl);
Junxiao Shi5759be32017-10-15 00:00:52 +000063 BOOST_CHECK_EQUAL(key1, key2);
64 BOOST_CHECK_NE(key1, Key());
65 BOOST_CHECK_EQUAL(Key(), Key());
Yingdi Yub8f8b342015-04-27 11:06:42 -070066
Yingdi Yucbe72b02015-11-25 17:35:37 -080067 key1.addCertificate(id1Key1Cert1);
68 BOOST_CHECK_NO_THROW(key2.getCertificate(id1Key1Cert1.getName()));
69 key2.removeCertificate(id1Key1Cert1.getName());
Davide Pesavento4fb35d82019-10-31 19:33:10 -040070 BOOST_CHECK_THROW(key1.getCertificate(id1Key1Cert1.getName()), pib::Pib::Error);
Yingdi Yub8f8b342015-04-27 11:06:42 -070071
Yingdi Yucbe72b02015-11-25 17:35:37 -080072 key1.setDefaultCertificate(id1Key1Cert1);
73 BOOST_CHECK_NO_THROW(key2.getDefaultCertificate());
Yingdi Yub8f8b342015-04-27 11:06:42 -070074}
75
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040076BOOST_AUTO_TEST_CASE(Helpers)
77{
Davide Pesaventof2cae612021-03-24 18:47:05 -040078 BOOST_CHECK_EQUAL(constructKeyName("/hello", name::Component("world")), "/hello/KEY/world");
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040079
Davide Pesaventof2cae612021-03-24 18:47:05 -040080 BOOST_CHECK_EQUAL(isValidKeyName("/hello"), false);
81 BOOST_CHECK_EQUAL(isValidKeyName("/hello/KEY"), false);
82 BOOST_CHECK_EQUAL(isValidKeyName("/hello/KEY/world"), true);
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040083
Davide Pesaventof2cae612021-03-24 18:47:05 -040084 BOOST_CHECK_EQUAL(isValidKeyName("/KEY/hello"), true);
85 BOOST_CHECK_EQUAL(isValidKeyName("/hello/world/KEY/!"), true);
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040086
Davide Pesaventof2cae612021-03-24 18:47:05 -040087 BOOST_CHECK_EQUAL(extractIdentityFromKeyName("/KEY/hello"), "/");
88 BOOST_CHECK_EQUAL(extractIdentityFromKeyName("/hello/KEY/world"), "/hello");
89 BOOST_CHECK_EQUAL(extractIdentityFromKeyName("/hello/world/KEY/!"), "/hello/world");
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040090
Davide Pesaventof2cae612021-03-24 18:47:05 -040091 BOOST_CHECK_THROW(extractIdentityFromKeyName("/hello"), std::invalid_argument);
Alexander Afanasyev6fd26cf2017-08-31 17:43:09 -040092}
93
Davide Pesaventoeee3e822016-11-26 19:19:34 +010094BOOST_AUTO_TEST_SUITE_END() // TestKey
Yingdi Yu6ee2d362015-07-16 21:48:05 -070095BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +010096BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -070097
98} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -070099} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700100} // namespace security
101} // namespace ndn