blob: c59efb5f5eb4193fa6ec40c2e246f2f81513668d [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 Pesavento765abc92021-12-27 00:44:04 -05003 * Copyright (c) 2013-2021 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"
Davide Pesavento4fb35d82019-10-31 19:33:10 -040023#include "ndn-cxx/security/pib/impl/pib-memory.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/security/pib/pib.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080025
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "tests/boost-test.hpp"
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050027#include "tests/key-chain-fixture.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050028#include "tests/unit/security/pib/pib-data-fixture.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080029
30namespace ndn {
31namespace security {
32namespace pib {
33namespace detail {
34namespace tests {
35
36BOOST_AUTO_TEST_SUITE(Security)
37BOOST_AUTO_TEST_SUITE(Pib)
Davide Pesavento5d0b0102017-10-07 13:43:16 -040038BOOST_FIXTURE_TEST_SUITE(TestKeyImpl, security::tests::PibDataFixture)
Yingdi Yucbe72b02015-11-25 17:35:37 -080039
40using security::Pib;
41
42BOOST_AUTO_TEST_CASE(Basic)
43{
44 auto pibImpl = make_shared<pib::PibMemory>();
Davide Pesavento765abc92021-12-27 00:44:04 -050045 KeyImpl key11(id1Key1Name, id1Key1, pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -080046
47 BOOST_CHECK_EQUAL(key11.getName(), id1Key1Name);
48 BOOST_CHECK_EQUAL(key11.getIdentity(), id1);
49 BOOST_CHECK_EQUAL(key11.getKeyType(), KeyType::EC);
Davide Pesavento765abc92021-12-27 00:44:04 -050050 BOOST_CHECK_EQUAL_COLLECTIONS(key11.getPublicKey().begin(), key11.getPublicKey().end(),
51 id1Key1.begin(), id1Key1.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080052
53 KeyImpl key11Bak(id1Key1Name, pibImpl);
54 BOOST_CHECK_EQUAL(key11Bak.getName(), id1Key1Name);
55 BOOST_CHECK_EQUAL(key11Bak.getIdentity(), id1);
56 BOOST_CHECK_EQUAL(key11Bak.getKeyType(), KeyType::EC);
Davide Pesavento765abc92021-12-27 00:44:04 -050057 BOOST_CHECK_EQUAL_COLLECTIONS(key11Bak.getPublicKey().begin(), key11Bak.getPublicKey().end(),
58 id1Key1.begin(), id1Key1.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080059}
60
61BOOST_AUTO_TEST_CASE(CertificateOperation)
62{
63 auto pibImpl = make_shared<pib::PibMemory>();
Davide Pesavento765abc92021-12-27 00:44:04 -050064 KeyImpl key11(id1Key1Name, id1Key1, pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -080065 BOOST_CHECK_NO_THROW(KeyImpl(id1Key1Name, pibImpl));
66
67 // key does not have any certificate
68 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 0);
69
70 // get non-existing certificate, throw Pib::Error
71 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
72 // get default certificate, throw Pib::Error
73 BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error);
74 // set non-existing certificate as default certificate, throw Pib::Error
75 BOOST_REQUIRE_THROW(key11.setDefaultCertificate(id1Key1Cert1.getName()), Pib::Error);
76
77 // add certificate
78 key11.addCertificate(id1Key1Cert1);
79 BOOST_CHECK_NO_THROW(key11.getCertificate(id1Key1Cert1.getName()));
80
81 // new certificate becomes default certificate when there was no default certificate
82 BOOST_REQUIRE_NO_THROW(key11.getDefaultCertificate());
83 const auto& defaultCert0 = key11.getDefaultCertificate();
84 BOOST_CHECK_EQUAL(defaultCert0.getName(), id1Key1Cert1.getName());
85 BOOST_CHECK_EQUAL(defaultCert0, id1Key1Cert1);
86
87 // remove certificate
88 key11.removeCertificate(id1Key1Cert1.getName());
89 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
90 BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error);
91
92 // set default certificate directly
93 BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert1));
94 BOOST_REQUIRE_NO_THROW(key11.getDefaultCertificate());
95 BOOST_CHECK_NO_THROW(key11.getCertificate(id1Key1Cert1.getName()));
96
97 // check default cert
98 const auto& defaultCert1 = key11.getDefaultCertificate();
99 BOOST_CHECK_EQUAL(defaultCert1.getName(), id1Key1Cert1.getName());
100 BOOST_CHECK_EQUAL(defaultCert1, id1Key1Cert1);
101
102 // add another certificate
103 key11.addCertificate(id1Key1Cert2);
104 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 2);
105
106 // set default certificate through name
107 BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert2.getName()));
108 BOOST_REQUIRE_NO_THROW(key11.getDefaultCertificate());
109 const auto& defaultCert2 = key11.getDefaultCertificate();
110 BOOST_CHECK_EQUAL(defaultCert2.getName(), id1Key1Cert2.getName());
111 BOOST_CHECK_EQUAL(defaultCert2, id1Key1Cert2);
112
113 // remove certificate
114 key11.removeCertificate(id1Key1Cert1.getName());
115 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
116 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 1);
117
118 // set default certificate directly again, change the default setting
119 BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert1));
120 const auto& defaultCert3 = key11.getDefaultCertificate();
121 BOOST_CHECK_EQUAL(defaultCert3.getName(), id1Key1Cert1.getName());
122 BOOST_CHECK_EQUAL(defaultCert3, id1Key1Cert1);
123 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 2);
124
125 // remove all certificates
126 key11.removeCertificate(id1Key1Cert1.getName());
127 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
128 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 1);
129 key11.removeCertificate(id1Key1Cert2.getName());
130 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert2.getName()), Pib::Error);
131 BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error);
132 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 0);
133}
134
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800135class OverwriteFixture : public ndn::security::tests::PibDataFixture,
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -0500136 public ndn::tests::KeyChainFixture
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800137{
138};
139
140BOOST_FIXTURE_TEST_CASE(Overwrite, OverwriteFixture)
141{
142 auto pibImpl = make_shared<pib::PibMemory>();
143
144 BOOST_CHECK_THROW(KeyImpl(id1Key1Name, pibImpl), Pib::Error);
Davide Pesavento765abc92021-12-27 00:44:04 -0500145 KeyImpl(id1Key1Name, id1Key1, pibImpl);
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800146 KeyImpl key1(id1Key1Name, pibImpl);
147
Davide Pesavento765abc92021-12-27 00:44:04 -0500148 KeyImpl(id1Key1Name, id1Key2, pibImpl); // overwriting of the key should work
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800149 KeyImpl key2(id1Key1Name, pibImpl);
150
Davide Pesavento765abc92021-12-27 00:44:04 -0500151 Buffer key1buf(key1.getPublicKey().begin(), key1.getPublicKey().end());
152 Buffer key2buf(key2.getPublicKey().begin(), key2.getPublicKey().end());
153 BOOST_CHECK(key1buf != key2buf); // key1 cached the original public key
154 BOOST_CHECK(key2buf == id1Key2);
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800155
156 key1.addCertificate(id1Key1Cert1);
157 BOOST_CHECK_EQUAL(key1.getCertificate(id1Key1Cert1.getName()), id1Key1Cert1);
158
159 auto otherCert = id1Key1Cert1;
160 SignatureInfo info;
161 info.setValidityPeriod(ValidityPeriod(time::system_clock::now(),
Davide Pesavento0f830802018-01-16 23:58:58 -0500162 time::system_clock::now() + 1_s));
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800163 m_keyChain.sign(otherCert, SigningInfo().setSignatureInfo(info));
164
165 BOOST_CHECK_EQUAL(otherCert.getName(), id1Key1Cert1.getName());
166 BOOST_CHECK(otherCert.getContent() == id1Key1Cert1.getContent());
167 BOOST_CHECK_NE(otherCert, id1Key1Cert1);
168
169 key1.addCertificate(otherCert);
170
171 BOOST_CHECK_EQUAL(key1.getCertificate(id1Key1Cert1.getName()), otherCert);
172}
173
Yingdi Yucbe72b02015-11-25 17:35:37 -0800174BOOST_AUTO_TEST_CASE(Errors)
175{
176 auto pibImpl = make_shared<pib::PibMemory>();
177
178 BOOST_CHECK_THROW(KeyImpl(id1Key1Name, pibImpl), Pib::Error);
Davide Pesavento765abc92021-12-27 00:44:04 -0500179 KeyImpl key11(id1Key1Name, id1Key1, pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800180
181 BOOST_CHECK_THROW(KeyImpl(Name("/wrong"), pibImpl), std::invalid_argument);
Davide Pesavento765abc92021-12-27 00:44:04 -0500182 BOOST_CHECK_THROW(KeyImpl(Name("/wrong"), id1Key1, pibImpl), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800183 Buffer wrongKey;
Davide Pesavento765abc92021-12-27 00:44:04 -0500184 BOOST_CHECK_THROW(KeyImpl(id1Key2Name, wrongKey, pibImpl), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800185
186 key11.addCertificate(id1Key1Cert1);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800187 BOOST_CHECK_THROW(key11.addCertificate(id1Key2Cert1), std::invalid_argument);
188 BOOST_CHECK_THROW(key11.removeCertificate(id1Key2Cert1.getName()), std::invalid_argument);
189 BOOST_CHECK_THROW(key11.getCertificate(id1Key2Cert1.getName()), std::invalid_argument);
190 BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key2Cert1), std::invalid_argument);
191 BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key2Cert1.getName()), std::invalid_argument);
192}
193
194BOOST_AUTO_TEST_SUITE_END() // TestKeyImpl
Yingdi Yucbe72b02015-11-25 17:35:37 -0800195BOOST_AUTO_TEST_SUITE_END() // Pib
196BOOST_AUTO_TEST_SUITE_END() // Security
197
198} // namespace tests
199} // namespace detail
200} // namespace pib
201} // namespace security
202} // namespace ndn