blob: 311cfe0a36725d8376c5d25fb470bf94dabacccd [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 Pesavento8e047e12024-02-12 16:50:23 -05003 * Copyright (c) 2013-2024 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040028namespace ndn::tests {
29
30using namespace ndn::security::pib;
Yingdi Yucbe72b02015-11-25 17:35:37 -080031
32BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yucbe72b02015-11-25 17:35:37 -080033
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040034class KeyImplFixture : public PibDataFixture
Yingdi Yucbe72b02015-11-25 17:35:37 -080035{
Davide Pesavento8618c1e2022-05-05 15:20:02 -040036protected:
37 const shared_ptr<PibImpl> pibImpl = makePibWithKey(id1Key1Name, id1Key1);
38 KeyImpl key11{id1Key1Name, id1Key1, pibImpl};
39};
Yingdi Yucbe72b02015-11-25 17:35:37 -080040
Davide Pesavento8618c1e2022-05-05 15:20:02 -040041BOOST_FIXTURE_TEST_SUITE(TestKeyImpl, KeyImplFixture)
42
43BOOST_AUTO_TEST_CASE(Properties)
44{
Davide Pesavento07db0732022-05-06 15:20:26 -040045 BOOST_TEST(key11.getIdentity() == id1);
46 BOOST_TEST(key11.getName() == id1Key1Name);
47 BOOST_TEST(key11.getKeyType() == KeyType::EC);
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040048 BOOST_TEST(key11.getPublicKey() == id1Key1, boost::test_tools::per_element());
Yingdi Yucbe72b02015-11-25 17:35:37 -080049}
50
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040051BOOST_AUTO_TEST_CASE(CertificateOperations)
Yingdi Yucbe72b02015-11-25 17:35:37 -080052{
Davide Pesavento8618c1e2022-05-05 15:20:02 -040053 // key does not have any certificates
Yingdi Yucbe72b02015-11-25 17:35:37 -080054 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 0);
55
56 // get non-existing certificate, throw Pib::Error
57 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
58 // get default certificate, throw Pib::Error
59 BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error);
60 // set non-existing certificate as default certificate, throw Pib::Error
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040061 BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key1Cert1.getName()), Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -080062
63 // add certificate
64 key11.addCertificate(id1Key1Cert1);
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040065 const auto& addedCert = key11.getCertificate(id1Key1Cert1.getName());
66 BOOST_CHECK_EQUAL(addedCert, id1Key1Cert1);
Yingdi Yucbe72b02015-11-25 17:35:37 -080067
68 // new certificate becomes default certificate when there was no default certificate
Yingdi Yucbe72b02015-11-25 17:35:37 -080069 const auto& defaultCert0 = key11.getDefaultCertificate();
Yingdi Yucbe72b02015-11-25 17:35:37 -080070 BOOST_CHECK_EQUAL(defaultCert0, id1Key1Cert1);
71
72 // remove certificate
73 key11.removeCertificate(id1Key1Cert1.getName());
74 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
75 BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error);
76
77 // set default certificate directly
78 BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert1));
Yingdi Yucbe72b02015-11-25 17:35:37 -080079 const auto& defaultCert1 = key11.getDefaultCertificate();
Yingdi Yucbe72b02015-11-25 17:35:37 -080080 BOOST_CHECK_EQUAL(defaultCert1, id1Key1Cert1);
81
82 // add another certificate
83 key11.addCertificate(id1Key1Cert2);
84 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 2);
85
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040086 // set default certificate through name and check return value
87 BOOST_CHECK_EQUAL(key11.setDefaultCertificate(id1Key1Cert2.getName()), id1Key1Cert2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080088 const auto& defaultCert2 = key11.getDefaultCertificate();
Yingdi Yucbe72b02015-11-25 17:35:37 -080089 BOOST_CHECK_EQUAL(defaultCert2, id1Key1Cert2);
90
91 // remove certificate
92 key11.removeCertificate(id1Key1Cert1.getName());
93 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
94 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 1);
95
Davide Pesavento78ca8ae2022-05-01 01:37:05 -040096 // set removed certificate as default, certificate is implicitly added
Yingdi Yucbe72b02015-11-25 17:35:37 -080097 BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert1));
98 const auto& defaultCert3 = key11.getDefaultCertificate();
Yingdi Yucbe72b02015-11-25 17:35:37 -080099 BOOST_CHECK_EQUAL(defaultCert3, id1Key1Cert1);
100 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 2);
101
102 // remove all certificates
103 key11.removeCertificate(id1Key1Cert1.getName());
104 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
105 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 1);
106 key11.removeCertificate(id1Key1Cert2.getName());
107 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert2.getName()), Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800108 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 0);
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400109 BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800110}
111
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400112class ReplaceFixture : public KeyChainFixture, public KeyImplFixture
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800113{
114};
115
Davide Pesavento8618c1e2022-05-05 15:20:02 -0400116BOOST_FIXTURE_TEST_CASE(ReplaceCertificate, ReplaceFixture)
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800117{
Davide Pesavento8618c1e2022-05-05 15:20:02 -0400118 key11.addCertificate(id1Key1Cert1);
119 BOOST_CHECK_EQUAL(key11.getCertificate(id1Key1Cert1.getName()), id1Key1Cert1);
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800120
121 auto otherCert = id1Key1Cert1;
122 SignatureInfo info;
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400123 info.setValidityPeriod(security::ValidityPeriod::makeRelative(-1_s, 10_s));
124 m_keyChain.sign(otherCert, security::SigningInfo().setSignatureInfo(info));
Davide Pesavento78ca8ae2022-05-01 01:37:05 -0400125 BOOST_TEST(otherCert.getName() == id1Key1Cert1.getName());
126 BOOST_TEST(otherCert.getContent() == id1Key1Cert1.getContent());
127 BOOST_TEST(otherCert != id1Key1Cert1);
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800128
Davide Pesavento8618c1e2022-05-05 15:20:02 -0400129 key11.addCertificate(otherCert); // overwrite cert
130 BOOST_TEST(key11.getCertificate(id1Key1Cert1.getName()) == otherCert);
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800131}
132
Yingdi Yucbe72b02015-11-25 17:35:37 -0800133BOOST_AUTO_TEST_CASE(Errors)
134{
Davide Pesavento07db0732022-05-06 15:20:26 -0400135 // illegal key name
Davide Pesavento765abc92021-12-27 00:44:04 -0500136 BOOST_CHECK_THROW(KeyImpl(Name("/wrong"), id1Key1, pibImpl), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800137
Yingdi Yucbe72b02015-11-25 17:35:37 -0800138 BOOST_CHECK_THROW(key11.addCertificate(id1Key2Cert1), std::invalid_argument);
139 BOOST_CHECK_THROW(key11.removeCertificate(id1Key2Cert1.getName()), std::invalid_argument);
140 BOOST_CHECK_THROW(key11.getCertificate(id1Key2Cert1.getName()), std::invalid_argument);
141 BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key2Cert1), std::invalid_argument);
142 BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key2Cert1.getName()), std::invalid_argument);
143}
144
Davide Pesavento07db0732022-05-06 15:20:26 -0400145BOOST_AUTO_TEST_CASE(UnknownKeyType)
146{
Davide Pesavento8e047e12024-02-12 16:50:23 -0500147 Name keyName = security::constructKeyName(id1, name::Component::fromUri("foo"));
Davide Pesavento07db0732022-05-06 15:20:26 -0400148 Buffer invalidKey{0x01, 0x02, 0x03, 0x04};
149 pibImpl->addKey(id1, keyName, invalidKey);
150
151 KeyImpl unknown(keyName, invalidKey, pibImpl);
152 BOOST_TEST(unknown.getIdentity() == id1);
153 BOOST_TEST(unknown.getName() == keyName);
154 BOOST_TEST(unknown.getKeyType() == KeyType::NONE);
155 BOOST_TEST(unknown.getPublicKey() == invalidKey, boost::test_tools::per_element());
156}
157
Yingdi Yucbe72b02015-11-25 17:35:37 -0800158BOOST_AUTO_TEST_SUITE_END() // TestKeyImpl
Yingdi Yucbe72b02015-11-25 17:35:37 -0800159BOOST_AUTO_TEST_SUITE_END() // Security
160
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400161} // namespace ndn::tests