blob: 3890ddcc18cdcc1df6e05032c60d22eaf7d52492 [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/*
Jeremy Clark670a52f2020-08-29 21:48:26 -04003 * Copyright (c) 2013-2020 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 Pesavento5d0b0102017-10-07 13:43:16 -040066 identity1.addKey(id1Key1.data(), id1Key1.size(), 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);
73 BOOST_CHECK(defaultKey0.getPublicKey() == id1Key1);
74
75 // remove key
76 identity1.removeKey(id1Key1Name);
77 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
78 BOOST_CHECK_THROW(identity1.getDefaultKey(), Pib::Error);
79
80 // set default key directly
Davide Pesavento5d0b0102017-10-07 13:43:16 -040081 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1.data(), id1Key1.size(), id1Key1Name));
Yingdi Yucbe72b02015-11-25 17:35:37 -080082 BOOST_REQUIRE_NO_THROW(identity1.getDefaultKey());
83 BOOST_CHECK_NO_THROW(identity1.getKey(id1Key1Name));
84
85 // check default key
86 const Key& defaultKey1 = identity1.getDefaultKey();
87 BOOST_CHECK_EQUAL(defaultKey1.getName(), id1Key1Name);
88 BOOST_CHECK(defaultKey1.getPublicKey() == id1Key1);
89
90 // add another key
Davide Pesavento5d0b0102017-10-07 13:43:16 -040091 identity1.addKey(id1Key2.data(), id1Key2.size(), id1Key2Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -080092 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 2);
93
94 // set default key through name
95 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key2Name));
96 BOOST_REQUIRE_NO_THROW(identity1.getDefaultKey());
97 const Key& defaultKey2 = identity1.getDefaultKey();
98 BOOST_CHECK_EQUAL(defaultKey2.getName(), id1Key2Name);
99 BOOST_CHECK(defaultKey2.getPublicKey() == id1Key2);
100
101 // remove key
102 identity1.removeKey(id1Key1Name);
103 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
104 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 1);
105
106 // set default key directly again, change the default setting
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400107 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1.data(), id1Key1.size(), id1Key1Name));
Yingdi Yucbe72b02015-11-25 17:35:37 -0800108 const Key& defaultKey3 = identity1.getDefaultKey();
109 BOOST_CHECK_EQUAL(defaultKey3.getName(), id1Key1Name);
110 BOOST_CHECK(defaultKey3.getPublicKey() == id1Key1);
111 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 2);
112
113 // remove all keys
114 identity1.removeKey(id1Key1Name);
115 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
116 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 1);
117 identity1.removeKey(id1Key2Name);
118 BOOST_CHECK_THROW(identity1.getKey(id1Key2Name), Pib::Error);
119 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 0);
120 BOOST_CHECK_THROW(identity1.getDefaultKey(), Pib::Error);
121}
122
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800123BOOST_AUTO_TEST_CASE(Overwrite)
124{
125 auto pibImpl = make_shared<pib::PibMemory>();
126 IdentityImpl identity1(id1, pibImpl, true);
127
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400128 identity1.addKey(id1Key1.data(), id1Key1.size(), id1Key1Name);
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800129 BOOST_CHECK(identity1.getKey(id1Key1Name).getPublicKey() == id1Key1);
130
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400131 identity1.addKey(id1Key2.data(), id1Key2.size(), id1Key1Name); // overwriting key should work
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800132 BOOST_CHECK(identity1.getKey(id1Key1Name).getPublicKey() == id1Key2);
133}
134
Yingdi Yucbe72b02015-11-25 17:35:37 -0800135BOOST_AUTO_TEST_CASE(Errors)
136{
137 auto pibImpl = make_shared<pib::PibMemory>();
138
139 BOOST_CHECK_THROW(IdentityImpl(id1, pibImpl, false), Pib::Error);
140 IdentityImpl identity1(id1, pibImpl, true);
141
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400142 identity1.addKey(id1Key1.data(), id1Key1.size(), id1Key1Name);
143 BOOST_CHECK_THROW(identity1.addKey(id2Key1.data(), id2Key1.size(), id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800144 BOOST_CHECK_THROW(identity1.removeKey(id2Key1Name), std::invalid_argument);
145 BOOST_CHECK_THROW(identity1.getKey(id2Key1Name), std::invalid_argument);
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400146 BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1.data(), id2Key1.size(), id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800147 BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1Name), std::invalid_argument);
148}
149
150BOOST_AUTO_TEST_SUITE_END() // TestIdentityImpl
Yingdi Yucbe72b02015-11-25 17:35:37 -0800151BOOST_AUTO_TEST_SUITE_END() // Pib
152BOOST_AUTO_TEST_SUITE_END() // Security
153
154} // namespace tests
155} // namespace detail
156} // namespace pib
157} // namespace security
158} // namespace ndn