blob: 517ea6053817e8323090f2693969a42aef0d2f8f [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 Pesavento0f830802018-01-16 23:58:58 -05003 * Copyright (c) 2013-2018 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
22#include "security/pib/detail/key-impl.hpp"
23#include "security/pib/pib.hpp"
24#include "security/pib/pib-memory.hpp"
25#include "../pib-data-fixture.hpp"
26
27#include "boost-test.hpp"
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -080028#include "identity-management-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)
38BOOST_AUTO_TEST_SUITE(Detail)
Davide Pesavento5d0b0102017-10-07 13:43:16 -040039BOOST_FIXTURE_TEST_SUITE(TestKeyImpl, security::tests::PibDataFixture)
Yingdi Yucbe72b02015-11-25 17:35:37 -080040
41using security::Pib;
42
43BOOST_AUTO_TEST_CASE(Basic)
44{
45 auto pibImpl = make_shared<pib::PibMemory>();
Davide Pesavento5d0b0102017-10-07 13:43:16 -040046 KeyImpl key11(id1Key1Name, id1Key1.data(), id1Key1.size(), pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -080047
48 BOOST_CHECK_EQUAL(key11.getName(), id1Key1Name);
49 BOOST_CHECK_EQUAL(key11.getIdentity(), id1);
50 BOOST_CHECK_EQUAL(key11.getKeyType(), KeyType::EC);
51 BOOST_CHECK(key11.getPublicKey() == id1Key1);
52
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);
57 BOOST_CHECK(key11Bak.getPublicKey() == id1Key1);
58}
59
60BOOST_AUTO_TEST_CASE(CertificateOperation)
61{
62 auto pibImpl = make_shared<pib::PibMemory>();
Davide Pesavento5d0b0102017-10-07 13:43:16 -040063 KeyImpl key11(id1Key1Name, id1Key1.data(), id1Key1.size(), pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -080064 BOOST_CHECK_NO_THROW(KeyImpl(id1Key1Name, pibImpl));
65
66 // key does not have any certificate
67 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 0);
68
69 // get non-existing certificate, throw Pib::Error
70 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
71 // get default certificate, throw Pib::Error
72 BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error);
73 // set non-existing certificate as default certificate, throw Pib::Error
74 BOOST_REQUIRE_THROW(key11.setDefaultCertificate(id1Key1Cert1.getName()), Pib::Error);
75
76 // add certificate
77 key11.addCertificate(id1Key1Cert1);
78 BOOST_CHECK_NO_THROW(key11.getCertificate(id1Key1Cert1.getName()));
79
80 // new certificate becomes default certificate when there was no default certificate
81 BOOST_REQUIRE_NO_THROW(key11.getDefaultCertificate());
82 const auto& defaultCert0 = key11.getDefaultCertificate();
83 BOOST_CHECK_EQUAL(defaultCert0.getName(), id1Key1Cert1.getName());
84 BOOST_CHECK_EQUAL(defaultCert0, id1Key1Cert1);
85
86 // remove certificate
87 key11.removeCertificate(id1Key1Cert1.getName());
88 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
89 BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error);
90
91 // set default certificate directly
92 BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert1));
93 BOOST_REQUIRE_NO_THROW(key11.getDefaultCertificate());
94 BOOST_CHECK_NO_THROW(key11.getCertificate(id1Key1Cert1.getName()));
95
96 // check default cert
97 const auto& defaultCert1 = key11.getDefaultCertificate();
98 BOOST_CHECK_EQUAL(defaultCert1.getName(), id1Key1Cert1.getName());
99 BOOST_CHECK_EQUAL(defaultCert1, id1Key1Cert1);
100
101 // add another certificate
102 key11.addCertificate(id1Key1Cert2);
103 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 2);
104
105 // set default certificate through name
106 BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert2.getName()));
107 BOOST_REQUIRE_NO_THROW(key11.getDefaultCertificate());
108 const auto& defaultCert2 = key11.getDefaultCertificate();
109 BOOST_CHECK_EQUAL(defaultCert2.getName(), id1Key1Cert2.getName());
110 BOOST_CHECK_EQUAL(defaultCert2, id1Key1Cert2);
111
112 // remove certificate
113 key11.removeCertificate(id1Key1Cert1.getName());
114 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
115 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 1);
116
117 // set default certificate directly again, change the default setting
118 BOOST_REQUIRE_NO_THROW(key11.setDefaultCertificate(id1Key1Cert1));
119 const auto& defaultCert3 = key11.getDefaultCertificate();
120 BOOST_CHECK_EQUAL(defaultCert3.getName(), id1Key1Cert1.getName());
121 BOOST_CHECK_EQUAL(defaultCert3, id1Key1Cert1);
122 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 2);
123
124 // remove all certificates
125 key11.removeCertificate(id1Key1Cert1.getName());
126 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert1.getName()), Pib::Error);
127 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 1);
128 key11.removeCertificate(id1Key1Cert2.getName());
129 BOOST_CHECK_THROW(key11.getCertificate(id1Key1Cert2.getName()), Pib::Error);
130 BOOST_CHECK_THROW(key11.getDefaultCertificate(), Pib::Error);
131 BOOST_CHECK_EQUAL(key11.getCertificates().size(), 0);
132}
133
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800134class OverwriteFixture : public ndn::security::tests::PibDataFixture,
135 public ndn::tests::IdentityManagementFixture
136{
137};
138
139BOOST_FIXTURE_TEST_CASE(Overwrite, OverwriteFixture)
140{
141 auto pibImpl = make_shared<pib::PibMemory>();
142
143 BOOST_CHECK_THROW(KeyImpl(id1Key1Name, pibImpl), Pib::Error);
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400144 KeyImpl(id1Key1Name, id1Key1.data(), id1Key1.size(), pibImpl);
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800145 KeyImpl key1(id1Key1Name, pibImpl);
146
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400147 KeyImpl(id1Key1Name, id1Key2.data(), id1Key2.size(), pibImpl); // overwriting of the key should work
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800148 KeyImpl key2(id1Key1Name, pibImpl);
149
150 BOOST_CHECK(key1.getPublicKey() != key2.getPublicKey()); // key1 cached the original public key
151 BOOST_CHECK(key2.getPublicKey() == id1Key2);
152
153 key1.addCertificate(id1Key1Cert1);
154 BOOST_CHECK_EQUAL(key1.getCertificate(id1Key1Cert1.getName()), id1Key1Cert1);
155
156 auto otherCert = id1Key1Cert1;
157 SignatureInfo info;
158 info.setValidityPeriod(ValidityPeriod(time::system_clock::now(),
Davide Pesavento0f830802018-01-16 23:58:58 -0500159 time::system_clock::now() + 1_s));
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800160 m_keyChain.sign(otherCert, SigningInfo().setSignatureInfo(info));
161
162 BOOST_CHECK_EQUAL(otherCert.getName(), id1Key1Cert1.getName());
163 BOOST_CHECK(otherCert.getContent() == id1Key1Cert1.getContent());
164 BOOST_CHECK_NE(otherCert, id1Key1Cert1);
165
166 key1.addCertificate(otherCert);
167
168 BOOST_CHECK_EQUAL(key1.getCertificate(id1Key1Cert1.getName()), otherCert);
169}
170
Yingdi Yucbe72b02015-11-25 17:35:37 -0800171BOOST_AUTO_TEST_CASE(Errors)
172{
173 auto pibImpl = make_shared<pib::PibMemory>();
174
175 BOOST_CHECK_THROW(KeyImpl(id1Key1Name, pibImpl), Pib::Error);
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400176 KeyImpl key11(id1Key1Name, id1Key1.data(), id1Key1.size(), pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800177
178 BOOST_CHECK_THROW(KeyImpl(Name("/wrong"), pibImpl), std::invalid_argument);
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400179 BOOST_CHECK_THROW(KeyImpl(Name("/wrong"), id1Key1.data(), id1Key1.size(), pibImpl), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800180 Buffer wrongKey;
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400181 BOOST_CHECK_THROW(KeyImpl(id1Key2Name, wrongKey.data(), wrongKey.size(), pibImpl), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800182
183 key11.addCertificate(id1Key1Cert1);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800184 BOOST_CHECK_THROW(key11.addCertificate(id1Key2Cert1), std::invalid_argument);
185 BOOST_CHECK_THROW(key11.removeCertificate(id1Key2Cert1.getName()), std::invalid_argument);
186 BOOST_CHECK_THROW(key11.getCertificate(id1Key2Cert1.getName()), std::invalid_argument);
187 BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key2Cert1), std::invalid_argument);
188 BOOST_CHECK_THROW(key11.setDefaultCertificate(id1Key2Cert1.getName()), std::invalid_argument);
189}
190
191BOOST_AUTO_TEST_SUITE_END() // TestKeyImpl
192BOOST_AUTO_TEST_SUITE_END() // Detail
193BOOST_AUTO_TEST_SUITE_END() // Pib
194BOOST_AUTO_TEST_SUITE_END() // Security
195
196} // namespace tests
197} // namespace detail
198} // namespace pib
199} // namespace security
200} // namespace ndn