blob: 2f883a56bbe129a1dafd7652bf14ee8c3f190c29 [file] [log] [blame]
Yingdi Yucbe72b02015-11-25 17:35:37 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento5d0b0102017-10-07 13:43:16 -04002/*
Davide Pesavento78ca8ae2022-05-01 01:37:05 -04003 * Copyright (c) 2013-2022 Regents of the University of California.
Yingdi Yucbe72b02015-11-25 17:35:37 -08004 *
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
Junxiao Shi24c5a002018-12-12 04:47:15 +000022#include "ndn-cxx/security/pib/impl/key-impl.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050025#include "tests/key-chain-fixture.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "tests/unit/security/pib/pib-data-fixture.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080027
28namespace ndn {
29namespace security {
30namespace pib {
31namespace detail {
32namespace tests {
33
34BOOST_AUTO_TEST_SUITE(Security)
35BOOST_AUTO_TEST_SUITE(Pib)
Yingdi Yucbe72b02015-11-25 17:35:37 -080036
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040037using pib::Pib;
Yingdi Yucbe72b02015-11-25 17:35:37 -080038
Davide Pesavento8618c1e2022-05-05 15:20:02 -040039class KeyImplFixture : public pib::tests::PibDataFixture
Yingdi Yucbe72b02015-11-25 17:35:37 -080040{
Davide Pesavento8618c1e2022-05-05 15:20:02 -040041protected:
42 const shared_ptr<PibImpl> pibImpl = makePibWithKey(id1Key1Name, id1Key1);
43 KeyImpl key11{id1Key1Name, id1Key1, pibImpl};
44};
Yingdi Yucbe72b02015-11-25 17:35:37 -080045
Davide Pesavento8618c1e2022-05-05 15:20:02 -040046BOOST_FIXTURE_TEST_SUITE(TestKeyImpl, KeyImplFixture)
47
48BOOST_AUTO_TEST_CASE(Properties)
49{
Yingdi Yucbe72b02015-11-25 17:35:37 -080050 BOOST_CHECK_EQUAL(key11.getIdentity(), id1);
Davide Pesavento8618c1e2022-05-05 15:20:02 -040051 BOOST_CHECK_EQUAL(key11.getName(), id1Key1Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -080052 BOOST_CHECK_EQUAL(key11.getKeyType(), KeyType::EC);
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040053 BOOST_TEST(key11.getPublicKey() == id1Key1, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -080054}
55
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040056BOOST_AUTO_TEST_CASE(CertificateOperations)
Yingdi Yucbe72b02015-11-25 17:35:37 -080057{
Davide Pesavento8618c1e2022-05-05 15:20:02 -040058 // key does not have any certificates
Yingdi Yucbe72b02015-11-25 17:35:37 -080059 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 0);
60
61 // get non-existing certificate, throw Pib::Error
62 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
63 // get default certificate, throw Pib::Error
64 BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error);
65 // set non-existing certificate as default certificate, throw Pib::Error
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040066 BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key1Cert1.getName()), Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -080067
68 // add certificate
69 key11.addCertificate(id1Key1Cert1);
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040070 const auto& addedCert = key11.getCertificate(id1Key1Cert1.getName());
71 BOOST_CHECK_EQUAL(addedCert, id1Key1Cert1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080072
73 // new certificate becomes default certificate when there was no default certificate
Yingdi Yucbe72b02015-11-25 17:35:37 -080074 const auto& defaultCert0 = key11.getDefaultCertificate();
Yingdi Yucbe72b02015-11-25 17:35:37 -080075 BOOST_CHECK_EQUAL(defaultCert0, id1Key1Cert1);
76
77 // remove certificate
78 key11.removeCertificate(id1Key1Cert1.getName());
79 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
80 BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error);
81
82 // set default certificate directly
83 BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert1));
Yingdi Yucbe72b02015-11-25 17:35:37 -080084 const auto& defaultCert1 = key11.getDefaultCertificate();
Yingdi Yucbe72b02015-11-25 17:35:37 -080085 BOOST_CHECK_EQUAL(defaultCert1, id1Key1Cert1);
86
87 // add another certificate
88 key11.addCertificate(id1Key1Cert2);
89 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 2);
90
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040091 // set default certificate through name and check return value
92 BOOST_CHECK_EQUAL(key11.setDefaultCertificate(id1Key1Cert2.getName()), id1Key1Cert2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080093 const auto& defaultCert2 = key11.getDefaultCertificate();
Yingdi Yucbe72b02015-11-25 17:35:37 -080094 BOOST_CHECK_EQUAL(defaultCert2, id1Key1Cert2);
95
96 // remove certificate
97 key11.removeCertificate(id1Key1Cert1.getName());
98 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
99 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 1);
100
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400101 // set removed certificate as default, certificate is implicitly added
Yingdi Yucbe72b02015-11-25 17:35:37 -0800102 BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert1));
103 const auto& defaultCert3 = key11.getDefaultCertificate();
Yingdi Yucbe72b02015-11-25 17:35:37 -0800104 BOOST_CHECK_EQUAL(defaultCert3, id1Key1Cert1);
105 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 2);
106
107 // remove all certificates
108 key11.removeCertificate(id1Key1Cert1.getName());
109 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
110 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 1);
111 key11.removeCertificate(id1Key1Cert2.getName());
112 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert2.getName()), Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800113 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 0);
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400114 BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800115}
116
Davide Pesavento8618c1e2022-05-05 15:20:02 -0400117class ReplaceFixture : public ndn::tests::KeyChainFixture,
118 public KeyImplFixture
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800119{
120};
121
Davide Pesavento8618c1e2022-05-05 15:20:02 -0400122BOOST_FIXTURE_TEST_CASE(ReplaceCertificate, ReplaceFixture)
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800123{
Davide Pesavento8618c1e2022-05-05 15:20:02 -0400124 key11.addCertificate(id1Key1Cert1);
125 BOOST_CHECK_EQUAL(key11.getCertificate(id1Key1Cert1.getName()), id1Key1Cert1);
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800126
127 auto otherCert = id1Key1Cert1;
128 SignatureInfo info;
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400129 info.setValidityPeriod(ValidityPeriod::makeRelative(-1_s, 10_s));
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800130 m_keyChain.sign(otherCert, SigningInfo().setSignatureInfo(info));
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400131 BOOST_TEST(otherCert.getName() == id1Key1Cert1.getName());
132 BOOST_TEST(otherCert.getContent() == id1Key1Cert1.getContent());
133 BOOST_TEST(otherCert != id1Key1Cert1);
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800134
Davide Pesavento8618c1e2022-05-05 15:20:02 -0400135 key11.addCertificate(otherCert); // overwrite cert
136 BOOST_TEST(key11.getCertificate(id1Key1Cert1.getName()) == otherCert);
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800137}
138
Yingdi Yucbe72b02015-11-25 17:35:37 -0800139BOOST_AUTO_TEST_CASE(Errors)
140{
Davide Pesavento765abc92021-12-27 00:44:04 -0500141 BOOST_CHECK_THROW(KeyImpl(Name("/wrong"), id1Key1, pibImpl), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800142
Davide Pesavento8618c1e2022-05-05 15:20:02 -0400143 Buffer invalidKey;
144 BOOST_CHECK_THROW(KeyImpl(id1Key1Name, invalidKey, pibImpl), std::invalid_argument);
145
Yingdi Yucbe72b02015-11-25 17:35:37 -0800146 BOOST_CHECK_THROW(key11.addCertificate(id1Key2Cert1), std::invalid_argument);
147 BOOST_CHECK_THROW(key11.removeCertificate(id1Key2Cert1.getName()), std::invalid_argument);
148 BOOST_CHECK_THROW(key11.getCertificate(id1Key2Cert1.getName()), std::invalid_argument);
149 BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key2Cert1), std::invalid_argument);
150 BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key2Cert1.getName()), std::invalid_argument);
151}
152
153BOOST_AUTO_TEST_SUITE_END() // TestKeyImpl
Yingdi Yucbe72b02015-11-25 17:35:37 -0800154BOOST_AUTO_TEST_SUITE_END() // Pib
155BOOST_AUTO_TEST_SUITE_END() // Security
156
157} // namespace tests
158} // namespace detail
159} // namespace pib
160} // namespace security
161} // namespace ndn