blob: fbce5286f2fc57ff6be9e8f26f0b81756ed7639c [file] [log] [blame]
Yingdi Yub8f8b342015-04-27 11:06:42 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento74daf742018-11-23 18:14:13 -05002/*
3 * Copyright (c) 2013-2018 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/security/pib/identity-container.hpp"
23#include "ndn-cxx/security/pib/pib.hpp"
24#include "ndn-cxx/security/pib/pib-memory.hpp"
Yingdi Yub8f8b342015-04-27 11:06:42 -070025
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "tests/boost-test.hpp"
27#include "tests/unit/security/pib/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(TestIdentityContainer, 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 IdentityContainer container(pibImpl);
48 BOOST_CHECK_EQUAL(container.size(), 0);
49 BOOST_CHECK_EQUAL(container.getLoadedIdentities().size(), 0);
Yingdi Yub8f8b342015-04-27 11:06:42 -070050
Yingdi Yucbe72b02015-11-25 17:35:37 -080051 // add the first identity
52 Identity identity11 = container.add(id1);
53 BOOST_CHECK_EQUAL(identity11.getName(), id1);
54 BOOST_CHECK_EQUAL(container.size(), 1);
55 BOOST_CHECK_EQUAL(container.getLoadedIdentities().size(), 1);
56 BOOST_CHECK(container.find(id1) != container.end());
57
58 // add the same identity again
59 Identity identity12 = container.add(id1);
60 BOOST_CHECK_EQUAL(identity12.getName(), id1);
61 BOOST_CHECK_EQUAL(container.size(), 1);
62 BOOST_CHECK_EQUAL(container.getLoadedIdentities().size(), 1);
63 BOOST_CHECK(container.find(id1) != container.end());
64
65 // add the second identity
66 Identity identity21 = container.add(id2);
67 BOOST_CHECK_EQUAL(identity21.getName(), id2);
Yingdi Yub8f8b342015-04-27 11:06:42 -070068 BOOST_CHECK_EQUAL(container.size(), 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -080069 BOOST_CHECK_EQUAL(container.getLoadedIdentities().size(), 2);
Yingdi Yub8f8b342015-04-27 11:06:42 -070070 BOOST_CHECK(container.find(id1) != container.end());
71 BOOST_CHECK(container.find(id2) != container.end());
72
Yingdi Yucbe72b02015-11-25 17:35:37 -080073 // get identities
74 BOOST_REQUIRE_NO_THROW(container.get(id1));
75 BOOST_REQUIRE_NO_THROW(container.get(id2));
76 BOOST_CHECK_THROW(container.get(Name("/non-existing")), Pib::Error);
77
78 // check identity
79 Identity identity1 = container.get(id1);
80 Identity identity2 = container.get(id2);
81 BOOST_CHECK_EQUAL(identity1.getName(), id1);
82 BOOST_CHECK_EQUAL(identity2.getName(), id2);
83
84 // create another container from the same PibImpl
85 // cache should be empty
86 IdentityContainer container2(pibImpl);
87 BOOST_CHECK_EQUAL(container2.size(), 2);
88 BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 0);
89
90 // get key, cache should be filled
91 BOOST_REQUIRE_NO_THROW(container2.get(id1));
92 BOOST_CHECK_EQUAL(container2.size(), 2);
93 BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 1);
94
95 BOOST_REQUIRE_NO_THROW(container2.get(id2));
96 BOOST_CHECK_EQUAL(container2.size(), 2);
97 BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 2);
98
99 // remove a key
100 container2.remove(id1);
101 BOOST_CHECK_EQUAL(container2.size(), 1);
102 BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 1);
103 BOOST_CHECK(container2.find(id1) == container2.end());
104 BOOST_CHECK(container2.find(id2) != container2.end());
105
106 // remove another key
107 container2.remove(id2);
108 BOOST_CHECK_EQUAL(container2.size(), 0);
109 BOOST_CHECK_EQUAL(container2.getLoadedIdentities().size(), 0);
110 BOOST_CHECK(container2.find(id2) == container2.end());
111
112}
113
114BOOST_AUTO_TEST_CASE(Iterator)
115{
116 auto pibImpl = make_shared<PibMemory>();
117 IdentityContainer container(pibImpl);
118 container.add(id1);
119 container.add(id2);
120
Yingdi Yub8f8b342015-04-27 11:06:42 -0700121 std::set<Name> idNames;
122 idNames.insert(id1);
123 idNames.insert(id2);
124
125 IdentityContainer::const_iterator it = container.begin();
126 std::set<Name>::const_iterator testIt = idNames.begin();
127 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
128 it++;
129 testIt++;
130 BOOST_CHECK_EQUAL((*it).getName(), *testIt);
131 ++it;
132 testIt++;
133 BOOST_CHECK(it == container.end());
134
135 size_t count = 0;
136 testIt = idNames.begin();
137 for (const auto& identity : container) {
138 BOOST_CHECK_EQUAL(identity.getName(), *testIt);
139 testIt++;
140 count++;
141 }
142 BOOST_CHECK_EQUAL(count, 2);
Yingdi Yucbe72b02015-11-25 17:35:37 -0800143
144 BOOST_CHECK(IdentityContainer::const_iterator() == IdentityContainer::const_iterator());
145 BOOST_CHECK(IdentityContainer::const_iterator() == container.end());
146 BOOST_CHECK(container.end() == IdentityContainer::const_iterator());
Yingdi Yub8f8b342015-04-27 11:06:42 -0700147}
148
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100149BOOST_AUTO_TEST_SUITE_END() // TestIdentityContainer
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700150BOOST_AUTO_TEST_SUITE_END() // Pib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100151BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yub8f8b342015-04-27 11:06:42 -0700152
153} // namespace tests
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700154} // namespace pib
Yingdi Yub8f8b342015-04-27 11:06:42 -0700155} // namespace security
156} // namespace ndn