blob: 40bf424c5d2a1e9ca10c412393bd50378522a5a5 [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 Pesavento74daf742018-11-23 18:14:13 -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
Junxiao Shi24c5a002018-12-12 04:47:15 +000022#include "ndn-cxx/security/pib/impl/identity-impl.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050023#include "ndn-cxx/security/pib/pib.hpp"
24#include "ndn-cxx/security/pib/pib-memory.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)
37BOOST_AUTO_TEST_SUITE(Detail)
38BOOST_FIXTURE_TEST_SUITE(TestIdentityImpl, ndn::security::tests::PibDataFixture)
39
40using security::Pib;
41
42BOOST_AUTO_TEST_CASE(Basic)
43{
44 auto pibImpl = make_shared<pib::PibMemory>();
45 IdentityImpl identity1(id1, pibImpl, true);
46
47 BOOST_CHECK_EQUAL(identity1.getName(), id1);
48}
49
50BOOST_AUTO_TEST_CASE(KeyOperation)
51{
52 auto pibImpl = make_shared<pib::PibMemory>();
53 IdentityImpl identity1(id1, pibImpl, true);
54 BOOST_CHECK_NO_THROW(IdentityImpl(id1, pibImpl, false));
55
56 // identity does not have any key
57 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 0);
58
59 // get non-existing key, throw Pib::Error
60 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
61 // get default key, throw Pib::Error
62 BOOST_CHECK_THROW(identity1.getDefaultKey(), Pib::Error);
63 // set non-existing key as default key, throw Pib::Error
64 BOOST_REQUIRE_THROW(identity1.setDefaultKey(id1Key1Name), Pib::Error);
65
66 // add key
Davide Pesavento5d0b0102017-10-07 13:43:16 -040067 identity1.addKey(id1Key1.data(), id1Key1.size(), id1Key1Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -080068 BOOST_CHECK_NO_THROW(identity1.getKey(id1Key1Name));
69
70 // new key becomes default key when there is no default key
71 BOOST_REQUIRE_NO_THROW(identity1.getDefaultKey());
72 const Key& defaultKey0 = identity1.getDefaultKey();
73 BOOST_CHECK_EQUAL(defaultKey0.getName(), id1Key1Name);
74 BOOST_CHECK(defaultKey0.getPublicKey() == id1Key1);
75
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 Pesavento5d0b0102017-10-07 13:43:16 -040082 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1.data(), id1Key1.size(), 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);
89 BOOST_CHECK(defaultKey1.getPublicKey() == id1Key1);
90
91 // add another key
Davide Pesavento5d0b0102017-10-07 13:43:16 -040092 identity1.addKey(id1Key2.data(), id1Key2.size(), id1Key2Name);
Yingdi Yucbe72b02015-11-25 17:35:37 -080093 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 2);
94
95 // set default key through name
96 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key2Name));
97 BOOST_REQUIRE_NO_THROW(identity1.getDefaultKey());
98 const Key& defaultKey2 = identity1.getDefaultKey();
99 BOOST_CHECK_EQUAL(defaultKey2.getName(), id1Key2Name);
100 BOOST_CHECK(defaultKey2.getPublicKey() == id1Key2);
101
102 // remove key
103 identity1.removeKey(id1Key1Name);
104 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
105 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 1);
106
107 // set default key directly again, change the default setting
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400108 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1.data(), id1Key1.size(), id1Key1Name));
Yingdi Yucbe72b02015-11-25 17:35:37 -0800109 const Key& defaultKey3 = identity1.getDefaultKey();
110 BOOST_CHECK_EQUAL(defaultKey3.getName(), id1Key1Name);
111 BOOST_CHECK(defaultKey3.getPublicKey() == id1Key1);
112 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 2);
113
114 // remove all keys
115 identity1.removeKey(id1Key1Name);
116 BOOST_CHECK_THROW(identity1.getKey(id1Key1Name), Pib::Error);
117 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 1);
118 identity1.removeKey(id1Key2Name);
119 BOOST_CHECK_THROW(identity1.getKey(id1Key2Name), Pib::Error);
120 BOOST_CHECK_EQUAL(identity1.getKeys().size(), 0);
121 BOOST_CHECK_THROW(identity1.getDefaultKey(), Pib::Error);
122}
123
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800124BOOST_AUTO_TEST_CASE(Overwrite)
125{
126 auto pibImpl = make_shared<pib::PibMemory>();
127 IdentityImpl identity1(id1, pibImpl, true);
128
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400129 identity1.addKey(id1Key1.data(), id1Key1.size(), id1Key1Name);
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800130 BOOST_CHECK(identity1.getKey(id1Key1Name).getPublicKey() == id1Key1);
131
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400132 identity1.addKey(id1Key2.data(), id1Key2.size(), id1Key1Name); // overwriting key should work
Alexander Afanasyeva10b2ff2017-01-30 12:44:15 -0800133 BOOST_CHECK(identity1.getKey(id1Key1Name).getPublicKey() == id1Key2);
134}
135
Yingdi Yucbe72b02015-11-25 17:35:37 -0800136BOOST_AUTO_TEST_CASE(Errors)
137{
138 auto pibImpl = make_shared<pib::PibMemory>();
139
140 BOOST_CHECK_THROW(IdentityImpl(id1, pibImpl, false), Pib::Error);
141 IdentityImpl identity1(id1, pibImpl, true);
142
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400143 identity1.addKey(id1Key1.data(), id1Key1.size(), id1Key1Name);
144 BOOST_CHECK_THROW(identity1.addKey(id2Key1.data(), id2Key1.size(), id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800145 BOOST_CHECK_THROW(identity1.removeKey(id2Key1Name), std::invalid_argument);
146 BOOST_CHECK_THROW(identity1.getKey(id2Key1Name), std::invalid_argument);
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400147 BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1.data(), id2Key1.size(), id2Key1Name), std::invalid_argument);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800148 BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1Name), std::invalid_argument);
149}
150
151BOOST_AUTO_TEST_SUITE_END() // TestIdentityImpl
152BOOST_AUTO_TEST_SUITE_END() // Detail
153BOOST_AUTO_TEST_SUITE_END() // Pib
154BOOST_AUTO_TEST_SUITE_END() // Security
155
156} // namespace tests
157} // namespace detail
158} // namespace pib
159} // namespace security
160} // namespace ndn