blob: 5b7961e5f6852ee0ae7bfaaec5a1410215cc242c [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu6ee2d362015-07-16 21:48:05 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yub8f8b342015-04-27 11:06:42 -07004 *
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
Alexander Afanasyev97709c02016-08-25 19:58:30 -070022#include "security/pib/key-container.hpp"
23#include "security/pib/pib.hpp"
24#include "security/pib/pib-memory.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070025
26#include "boost-test.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010027#include "pib-data-fixture.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070028
29namespace ndn {
30namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070031namespace pib {
Yingdi Yub8f8b342015-04-27 11:06:42 -070032namespace tests {
33
Yingdi Yu6ee2d362015-07-16 21:48:05 -070034using namespace ndn::security::tests;
35
Davide Pesaventoeee3e822016-11-26 19:19:34 +010036BOOST_AUTO_TEST_SUITE(Security)
Yingdi Yu6ee2d362015-07-16 21:48:05 -070037BOOST_AUTO_TEST_SUITE(Pib)
Yingdi Yucbe72b02015-11-25 17:35:37 -080038BOOST_FIXTURE_TEST_SUITE(TestKeyContainer, PibDataFixture)
Yingdi Yub8f8b342015-04-27 11:06:42 -070039
Yingdi Yu6ee2d362015-07-16 21:48:05 -070040using pib::Pib;
41
Yingdi Yucbe72b02015-11-25 17:35:37 -080042BOOST_AUTO_TEST_CASE(Basic)
Yingdi Yub8f8b342015-04-27 11:06:42 -070043{
Yingdi Yu3bf91f52015-06-12 19:39:40 -070044 auto pibImpl = make_shared<PibMemory>();
Yingdi Yub8f8b342015-04-27 11:06:42 -070045
Yingdi Yucbe72b02015-11-25 17:35:37 -080046 // start with an empty container
47 KeyContainer container(id1, pibImpl);
48 BOOST_CHECK_EQUAL(container.size(), 0);
49 BOOST_CHECK_EQUAL(container.getLoadedKeys().size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070050
Yingdi Yucbe72b02015-11-25 17:35:37 -080051 // add the first key
52 Key key11 = container.add(id1Key1.buf(), id1Key1.size(), id1Key1Name);
53 BOOST_CHECK_EQUAL(key11.getName(), id1Key1Name);
54 BOOST_CHECK(key11.getPublicKey() == id1Key1);
55 BOOST_CHECK_EQUAL(container.size(), 1);
56 BOOST_CHECK_EQUAL(container.getLoadedKeys().size(), 1);
57 BOOST_CHECK(container.find(id1Key1Name) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070058
Yingdi Yucbe72b02015-11-25 17:35:37 -080059 // add the same key again
60 Key key12 = container.add(id1Key1.buf(), id1Key1.size(), id1Key1Name);
61 BOOST_CHECK_EQUAL(key12.getName(), id1Key1Name);
62 BOOST_CHECK(key12.getPublicKey() == id1Key1);
63 BOOST_CHECK_EQUAL(container.size(), 1);
64 BOOST_CHECK_EQUAL(container.getLoadedKeys().size(), 1);
65 BOOST_CHECK(container.find(id1Key1Name) != container.end());
66
67 // add the second key
68 Key key21 = container.add(id1Key2.buf(), id1Key2.size(), id1Key2Name);
69 BOOST_CHECK_EQUAL(key21.getName(), id1Key2Name);
70 BOOST_CHECK(key21.getPublicKey() == id1Key2);
Yingdi Yub8f8b342015-04-27 11:06:42 -070071 BOOST_CHECK_EQUAL(container.size(), 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080072 BOOST_CHECK_EQUAL(container.getLoadedKeys().size(), 2);
Yingdi Yu6ee2d362015-07-16 21:48:05 -070073 BOOST_CHECK(container.find(id1Key1Name) != container.end());
74 BOOST_CHECK(container.find(id1Key2Name) != container.end());
Yingdi Yub8f8b342015-04-27 11:06:42 -070075
Yingdi Yucbe72b02015-11-25 17:35:37 -080076 // get keys
77 BOOST_REQUIRE_NO_THROW(container.get(id1Key1Name));
78 BOOST_REQUIRE_NO_THROW(container.get(id1Key2Name));
79 Name id1Key3Name = v2::constructKeyName(id1, name::Component("non-existing-id"));
80 BOOST_CHECK_THROW(container.get(id1Key3Name), Pib::Error);
81
82 // check key
83 Key key1 = container.get(id1Key1Name);
84 Key key2 = container.get(id1Key2Name);
85 BOOST_CHECK_EQUAL(key1.getName(), id1Key1Name);
86 BOOST_CHECK(key1.getPublicKey() == id1Key1);
87 BOOST_CHECK_EQUAL(key2.getName(), id1Key2Name);
88 BOOST_CHECK(key2.getPublicKey() == id1Key2);
89
90 // create another container from the same PibImpl
91 // cache should be empty
92 KeyContainer container2(id1, pibImpl);
93 BOOST_CHECK_EQUAL(container2.size(), 2);
94 BOOST_CHECK_EQUAL(container2.getLoadedKeys().size(), 0);
95
96 // get key, cache should be filled
97 BOOST_REQUIRE_NO_THROW(container2.get(id1Key1Name));
98 BOOST_CHECK_EQUAL(container2.size(), 2);
99 BOOST_CHECK_EQUAL(container2.getLoadedKeys().size(), 1);
100
101 BOOST_REQUIRE_NO_THROW(container2.get(id1Key2Name));
102 BOOST_CHECK_EQUAL(container2.size(), 2);
103 BOOST_CHECK_EQUAL(container2.getLoadedKeys().size(), 2);
104
105 // remove a key
106 container2.remove(id1Key1Name);
107 BOOST_CHECK_EQUAL(container2.size(), 1);
108 BOOST_CHECK_EQUAL(container2.getLoadedKeys().size(), 1);
109 BOOST_CHECK(container2.find(id1Key1Name) == container2.end());
110 BOOST_CHECK(container2.find(id1Key2Name) != container2.end());
111
112 // remove another key
113 container2.remove(id1Key2Name);
114 BOOST_CHECK_EQUAL(container2.size(), 0);
115 BOOST_CHECK_EQUAL(container2.getLoadedKeys().size(), 0);
116 BOOST_CHECK(container2.find(id1Key2Name) == container2.end());
117}
118
119BOOST_AUTO_TEST_CASE(Errors)
120{
121 auto pibImpl = make_shared<PibMemory>();
122
123 KeyContainer container(id1, pibImpl);
124
125 BOOST_CHECK_THROW(container.add(id2Key1.buf(), id2Key1.size(), id2Key1Name), std::invalid_argument);
126 BOOST_CHECK_THROW(container.remove(id2Key1Name), std::invalid_argument);
127 BOOST_CHECK_THROW(container.get(id2Key1Name), std::invalid_argument);
128}
129
130BOOST_AUTO_TEST_CASE(Iterator)
131{
132 auto pibImpl = make_shared<PibMemory>();
133 KeyContainer container(id1, pibImpl);
134
135 container.add(id1Key1.buf(), id1Key1.size(), id1Key1Name);
136 container.add(id1Key2.buf(), id1Key2.size(), id1Key2Name);
137
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700138 std::set<Name> keyNames;
139 keyNames.insert(id1Key1Name);
140 keyNames.insert(id1Key2Name);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700141
142 KeyContainer::const_iterator it = container.begin();
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700143 std::set<Name>::const_iterator testIt = keyNames.begin();
144 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700145 it++;
146 testIt++;
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700147 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700148 ++it;
149 testIt++;
150 BOOST_CHECK(it == container.end());
151
152 size_t count = 0;
153 testIt = keyNames.begin();
154 for (const auto& key : container) {
155 BOOST_CHECK_EQUAL(key.getIdentity(), id1);
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700156 BOOST_CHECK_EQUAL(key.getName(), *testIt);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700157 testIt++;
158 count++;
159 }
160 BOOST_CHECK_EQUAL(count, 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800161
162 BOOST_CHECK(KeyContainer::const_iterator() == KeyContainer::const_iterator());
163 BOOST_CHECK(KeyContainer::const_iterator() == container.end());
164 BOOST_CHECK(container.end() == KeyContainer::const_iterator());
Yingdi Yub8f8b342015-04-27 11:06:42 -0700165}
166
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100167BOOST_AUTO_TEST_SUITE_END() // TestKeyContainer
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700168BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100169BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700170
171} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700172} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700173} // namespace security
174} // namespace ndn