blob: 10c7bb8e3ee03483756f9d34c670aea4a14f7c0d [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/identity-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"
28
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
67 identity1.addKey(id1Key1.buf(), id1Key1.size(), id1Key1Name);
68 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
82 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1.buf(), id1Key1.size(), id1Key1Name));
83 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
92 identity1.addKey(id1Key2.buf(), id1Key2.size(), id1Key2Name);
93 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
108 BOOST_REQUIRE_NO_THROW(identity1.setDefaultKey(id1Key1.buf(), id1Key1.size(), id1Key1Name));
109 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
124BOOST_AUTO_TEST_CASE(Errors)
125{
126 auto pibImpl = make_shared<pib::PibMemory>();
127
128 BOOST_CHECK_THROW(IdentityImpl(id1, pibImpl, false), Pib::Error);
129 IdentityImpl identity1(id1, pibImpl, true);
130
131 identity1.addKey(id1Key1.buf(), id1Key1.size(), id1Key1Name);
132 BOOST_CHECK_THROW(identity1.addKey(id1Key1.buf(), id1Key1.size(), id1Key1Name), Pib::Error);
133 BOOST_CHECK_THROW(identity1.addKey(id2Key1.buf(), id2Key1.size(), id2Key1Name), std::invalid_argument);
134 BOOST_CHECK_THROW(identity1.removeKey(id2Key1Name), std::invalid_argument);
135 BOOST_CHECK_THROW(identity1.getKey(id2Key1Name), std::invalid_argument);
136 BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1.buf(), id2Key1.size(), id2Key1Name), std::invalid_argument);
137 BOOST_CHECK_THROW(identity1.setDefaultKey(id2Key1Name), std::invalid_argument);
138}
139
140BOOST_AUTO_TEST_SUITE_END() // TestIdentityImpl
141BOOST_AUTO_TEST_SUITE_END() // Detail
142BOOST_AUTO_TEST_SUITE_END() // Pib
143BOOST_AUTO_TEST_SUITE_END() // Security
144
145} // namespace tests
146} // namespace detail
147} // namespace pib
148} // namespace security
149} // namespace ndn