blob: 52571135b05d2c67fa19de0b2ab1abb431dbc3cb [file] [log] [blame]
Yingdi Yucbe72b02015-11-25 17:35:37 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2017 Regents of the University of California.
4 *
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)
39BOOST_FIXTURE_TEST_SUITE(TestKeyImpl, ndn::security::tests::PibDataFixture)
40
41using security::Pib;
42
43BOOST_AUTO_TEST_CASE(Basic)
44{
45 auto pibImpl = make_shared<pib::PibMemory>();
46 KeyImpl key11(id1Key1Name, id1Key1.buf(), id1Key1.size(), pibImpl);
47
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>();
63 KeyImpl key11(id1Key1Name, id1Key1.buf(), id1Key1.size(), pibImpl);
64 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);
144 KeyImpl(id1Key1Name, id1Key1.buf(), id1Key1.size(), pibImpl);
145 KeyImpl key1(id1Key1Name, pibImpl);
146
147 KeyImpl(id1Key1Name, id1Key2.buf(), id1Key2.size(), pibImpl); // overwriting of the key should work
148 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(),
159 time::system_clock::now() + time::seconds(1)));
160 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);
176 KeyImpl key11(id1Key1Name, id1Key1.buf(), id1Key1.size(), pibImpl);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800177
178 BOOST_CHECK_THROW(KeyImpl(Name("/wrong"), pibImpl), std::invalid_argument);
179 BOOST_CHECK_THROW(KeyImpl(Name("/wrong"), id1Key1.buf(), id1Key1.size(), pibImpl), std::invalid_argument);
180 Buffer wrongKey;
181 BOOST_CHECK_THROW(KeyImpl(id1Key2Name, wrongKey.buf(), wrongKey.size(), pibImpl), std::invalid_argument);
182
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