blob: 1b4527fc8c62e7fa00fa1a1c0a837dd800cb1baf [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/identity-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"
27#include "tests/unit/security/pib/pib-data-fixture.hpp"
Yingdi Yucbe72b02015-11-25 17:35:37 -080028
29namespace ndn {
30namespace security {
31namespace pib {
32namespace detail {
33namespace tests {
34
35BOOST_AUTO_TEST_SUITE(Security)
36BOOST_AUTO_TEST_SUITE(Pib)
Yingdi Yucbe72b02015-11-25 17:35:37 -080037BOOST_FIXTURE_TEST_SUITE(TestIdentityImpl, ndn::security::tests::PibDataFixture)
38
39using security::Pib;
40
41BOOST_AUTO_TEST_CASE(Basic)
42{
43 auto pibImpl = make_shared<pib::PibMemory>();
44 IdentityImpl identity1(id1, pibImpl, true);
45
46 BOOST_CHECK_EQUAL(identity1.getName(), id1);
47}
48
49BOOST_AUTO_TEST_CASE(KeyOperation)
50{
51 auto pibImpl = make_shared<pib::PibMemory>();
52 IdentityImpl identity1(id1, pibImpl, true);
53 BOOST_CHECK_NO_THROW(IdentityImpl(id1, pibImpl, false));
54
55 // identity does not have any key
56 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 0);
57
58 // get non-existing key, throw Pib::Error
59 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
60 // get default key, throw Pib::Error
61 BOOST_CHECK_THROW(identity1.getDefaultKey(), Pib::Error);
62 // set non-existing key as default key, throw Pib::Error
63 BOOST_REQUIRE_THROW(identity1.setDefaultKey(id1Key1Name), Pib::Error);
64
65 // add key
Davide Pesavento765abc92021-12-27 00:44:04 -050066 identity1.addKey(id1Key1, id1Key1Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -080067 BOOST_CHECK_NO_THROW(identity1.getKey(id1Key1Name));
68
69 // new key becomes default key when there is no default key
70 BOOST_REQUIRE_NO_THROW(identity1.getDefaultKey());
71 const Key& defaultKey0 = identity1.getDefaultKey();
72 BOOST_CHECK_EQUAL(defaultKey0.getName(), id1Key1Name);
Davide Pesavento765abc92021-12-27 00:44:04 -050073 BOOST_CHECK_EQUAL_COLLECTIONS(defaultKey0.getPublicKey().begin(), defaultKey0.getPublicKey().end(),
74 id1Key1.begin(), id1Key1.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080075
76 // remove key
77 identity1.removeKey(id1Key1Name);
78 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
79 BOOST_CHECK_THROW(identity1.getDefaultKey(), Pib::Error);
80
81 // set default key directly
Davide Pesavento765abc92021-12-27 00:44:04 -050082 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1, id1Key1Name));
Yingdi Yucbe72b02015-11-25 17:35:37 -080083 BOOST_REQUIRE_NO_THROW(identity1.getDefaultKey());
84 BOOST_CHECK_NO_THROW(identity1.getKey(id1Key1Name));
85
86 // check default key
87 const Key& defaultKey1 = identity1.getDefaultKey();
88 BOOST_CHECK_EQUAL(defaultKey1.getName(), id1Key1Name);
Davide Pesavento765abc92021-12-27 00:44:04 -050089 BOOST_CHECK_EQUAL_COLLECTIONS(defaultKey1.getPublicKey().begin(), defaultKey1.getPublicKey().end(),
90 id1Key1.begin(), id1Key1.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -080091
92 // add another key
Davide Pesavento765abc92021-12-27 00:44:04 -050093 identity1.addKey(id1Key2, id1Key2Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -080094 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 2);
95
96 // set default key through name
97 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key2Name));
98 BOOST_REQUIRE_NO_THROW(identity1.getDefaultKey());
99 const Key& defaultKey2 = identity1.getDefaultKey();
100 BOOST_CHECK_EQUAL(defaultKey2.getName(), id1Key2Name);
Davide Pesavento765abc92021-12-27 00:44:04 -0500101 BOOST_CHECK_EQUAL_COLLECTIONS(defaultKey2.getPublicKey().begin(), defaultKey2.getPublicKey().end(),
102 id1Key2.begin(), id1Key2.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -0800103
104 // remove key
105 identity1.removeKey(id1Key1Name);
106 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
107 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 1);
108
109 // set default key directly again, change the default setting
Davide Pesavento765abc92021-12-27 00:44:04 -0500110 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1, id1Key1Name));
Yingdi Yucbe72b02015-11-25 17:35:37 -0800111 const Key& defaultKey3 = identity1.getDefaultKey();
112 BOOST_CHECK_EQUAL(defaultKey3.getName(), id1Key1Name);
Davide Pesavento765abc92021-12-27 00:44:04 -0500113 BOOST_CHECK_EQUAL_COLLECTIONS(defaultKey3.getPublicKey().begin(), defaultKey3.getPublicKey().end(),
114 id1Key1.begin(), id1Key1.end());
Yingdi Yucbe72b02015-11-25 17:35:37 -0800115 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 2);
116
117 // remove all keys
118 identity1.removeKey(id1Key1Name);
119 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
120 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 1);
121 identity1.removeKey(id1Key2Name);
122 BOOST_CHECK_THROW(identity1.getKey(id1Key2Name), Pib::Error);
123 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 0);
124 BOOST_CHECK_THROW(identity1.getDefaultKey(), Pib::Error);
125}
126
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800127BOOST_AUTO_TEST_CASE(Overwrite)
128{
129 auto pibImpl = make_shared<pib::PibMemory>();
130 IdentityImpl identity1(id1, pibImpl, true);
131
Davide Pesavento765abc92021-12-27 00:44:04 -0500132 identity1.addKey(id1Key1, id1Key1Name);
133 auto k1 = identity1.getKey(id1Key1Name);
134 BOOST_CHECK_EQUAL_COLLECTIONS(k1.getPublicKey().begin(), k1.getPublicKey().end(),
135 id1Key1.begin(), id1Key1.end());
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800136
Davide Pesavento765abc92021-12-27 00:44:04 -0500137 identity1.addKey(id1Key2, id1Key1Name); // overwriting key should work
138 auto k2 = identity1.getKey(id1Key1Name);
139 BOOST_CHECK_EQUAL_COLLECTIONS(k2.getPublicKey().begin(), k2.getPublicKey().end(),
140 id1Key2.begin(), id1Key2.end());
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800141}
142
Yingdi Yucbe72b02015-11-25 17:35:37 -0800143BOOST_AUTO_TEST_CASE(Errors)
144{
145 auto pibImpl = make_shared<pib::PibMemory>();
146
147 BOOST_CHECK_THROW(IdentityImpl(id1, pibImpl, false), Pib::Error);
148 IdentityImpl identity1(id1, pibImpl, true);
149
Davide Pesavento765abc92021-12-27 00:44:04 -0500150 identity1.addKey(id1Key1, id1Key1Name);
151 BOOST_CHECK_THROW(identity1.addKey(id2Key1, id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800152 BOOST_CHECK_THROW(identity1.removeKey(id2Key1Name), std::invalid_argument);
153 BOOST_CHECK_THROW(identity1.getKey(id2Key1Name), std::invalid_argument);
Davide Pesavento765abc92021-12-27 00:44:04 -0500154 BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1, id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800155 BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1Name), std::invalid_argument);
156}
157
158BOOST_AUTO_TEST_SUITE_END() // TestIdentityImpl
Yingdi Yucbe72b02015-11-25 17:35:37 -0800159BOOST_AUTO_TEST_SUITE_END() // Pib
160BOOST_AUTO_TEST_SUITE_END() // Security
161
162} // namespace tests
163} // namespace detail
164} // namespace pib
165} // namespace security
166} // namespace ndn