blob: 9a0e85ff25efbc15684d1a8c37a9b67b3c888c99 [file] [log] [blame]
Yingdi Yu3bf91f52015-06-12 19:39:40 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev2fa59392016-07-29 17:24:23 -07003 * Copyright (c) 2013-2016 Regents of the University of California.
Yingdi Yu3bf91f52015-06-12 19:39:40 -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/pib-memory.hpp"
23#include "security/pib/pib-sqlite3.hpp"
24#include "security/pib/pib.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010025
26#include "boost-test.hpp"
Yingdi Yu3bf91f52015-06-12 19:39:40 -070027#include "pib-data-fixture.hpp"
28
Mickey Sweatt11314b72015-06-10 17:20:19 -070029#include <boost/filesystem.hpp>
Yingdi Yu3bf91f52015-06-12 19:39:40 -070030#include <boost/mpl/list.hpp>
Yingdi Yu3bf91f52015-06-12 19:39:40 -070031
32namespace ndn {
33namespace security {
34namespace tests {
35
Davide Pesaventoeee3e822016-11-26 19:19:34 +010036BOOST_AUTO_TEST_SUITE(Security)
Alexander Afanasyev97709c02016-08-25 19:58:30 -070037BOOST_AUTO_TEST_SUITE(TestPib)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010038BOOST_AUTO_TEST_SUITE(TestPibImpl)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070039
Mickey Sweatt11314b72015-06-10 17:20:19 -070040class PibMemoryWrapper
41{
42public:
43 PibMemory impl;
44};
45
46class PibSqlite3Wrapper
47{
48public:
49 PibSqlite3Wrapper()
50 : tmpPath(boost::filesystem::path(UNIT_TEST_CONFIG_PATH) / "DbTest")
51 , impl(tmpPath.c_str())
52 {
53 }
54
55 ~PibSqlite3Wrapper()
56 {
57 boost::filesystem::remove_all(tmpPath);
58 }
59
Davide Pesaventoeee3e822016-11-26 19:19:34 +010060public:
Mickey Sweatt11314b72015-06-10 17:20:19 -070061 boost::filesystem::path tmpPath;
62 PibSqlite3 impl;
63};
64
65typedef boost::mpl::list<PibMemoryWrapper,
66 PibSqlite3Wrapper> PibImpls;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070067
68BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, PibDataFixture)
69{
Mickey Sweatt11314b72015-06-10 17:20:19 -070070 T wrapper;
71 PibImpl& pibImpl = wrapper.impl;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070072
73 // no default setting, throw Error
74 BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error);
75
76 // check id1, which should not exist
77 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
78
79 // add id1, should be default
80 pibImpl.addIdentity(id1);
81 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
82 BOOST_CHECK_NO_THROW(pibImpl.getDefaultIdentity());
83 BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id1);
84
85 // add id2, should not be default
86 pibImpl.addIdentity(id2);
87 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id2), true);
88 BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id1);
89
90 // set id2 explicitly as default
91 pibImpl.setDefaultIdentity(id2);
92 BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id2);
93
94 // remove id2, should not have default identity
95 pibImpl.removeIdentity(id2);
96 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id2), false);
97 BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error);
98
99 // add id2 again, should be default
100 pibImpl.addIdentity(id2);
101 BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id2);
102
103 // get all identities, should contain id1 and id2
104 std::set<Name> idNames = pibImpl.getIdentities();
105 BOOST_CHECK_EQUAL(idNames.size(), 2);
106 BOOST_CHECK_EQUAL(idNames.count(id1), 1);
107 BOOST_CHECK_EQUAL(idNames.count(id2), 1);
108}
109
110BOOST_FIXTURE_TEST_CASE_TEMPLATE(KeyManagement, T, PibImpls, PibDataFixture)
111{
Mickey Sweatt11314b72015-06-10 17:20:19 -0700112 T wrapper;
113 PibImpl& pibImpl = wrapper.impl;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700114
115 // no default setting, throw Error
116 BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error);
117
118 // check id1Key1, should not exist, neither should id1.
119 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), false);
120 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
121
122 // add id1Key1, should be default, id1 should be added implicitly
123 pibImpl.addKey(id1, id1Key1Name.get(-1), id1Key1);
124 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), true);
125 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700126 const v1::PublicKey& keyBits = pibImpl.getKeyBits(id1, id1Key1Name.get(-1));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700127 BOOST_CHECK_EQUAL_COLLECTIONS(keyBits.get().buf(), keyBits.get().buf() + keyBits.get().size(),
128 id1Key1.get().buf(), id1Key1.get().buf() + id1Key1.get().size());
129 BOOST_CHECK_NO_THROW(pibImpl.getDefaultKeyOfIdentity(id1));
130 BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name.get(-1));
131
132 // add id1Key2, should not be default
133 pibImpl.addKey(id1, id1Key2Name.get(-1), id1Key2);
134 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key2Name.get(-1)), true);
135 BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name.get(-1));
136
137 // set id1Key2 explicitly as default
138 pibImpl.setDefaultKeyOfIdentity(id1, id1Key2Name.get(-1));
139 BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key2Name.get(-1));
140
141 // set a non-existing key as default, throw Error
142 BOOST_CHECK_THROW(pibImpl.setDefaultKeyOfIdentity(id1, name::Component("non-existing")),
143 Pib::Error);
144
145 // remove id1Key2, should not have default key
146 pibImpl.removeKey(id1, id1Key2Name.get(-1));
147 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key2Name.get(-1)), false);
148 BOOST_CHECK_THROW(pibImpl.getKeyBits(id1, id1Key2Name.get(-1)), Pib::Error);
149 BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error);
150
151 // add id1Key2 back, should be default
152 pibImpl.addKey(id1, id1Key2Name.get(-1), id1Key2);
153 BOOST_CHECK_NO_THROW(pibImpl.getKeyBits(id1, id1Key2Name.get(-1)));
154 BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key2Name.get(-1));
155
156 // get all the keys: id1Key1 and id1Key2
157 std::set<name::Component> keyNames = pibImpl.getKeysOfIdentity(id1);
158 BOOST_CHECK_EQUAL(keyNames.size(), 2);
159 BOOST_CHECK_EQUAL(keyNames.count(id1Key1Name.get(-1)), 1);
160 BOOST_CHECK_EQUAL(keyNames.count(id1Key2Name.get(-1)), 1);
161
162 // remove id1, should remove all the keys
163 pibImpl.removeIdentity(id1);
164 keyNames = pibImpl.getKeysOfIdentity(id1);
165 BOOST_CHECK_EQUAL(keyNames.size(), 0);
166}
167
168BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, PibDataFixture)
169{
Mickey Sweatt11314b72015-06-10 17:20:19 -0700170 T wrapper;
171 PibImpl& pibImpl = wrapper.impl;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700172
173 // no default setting, throw Error
174 BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), Pib::Error);
175
176 // check id1Key1Cert1, should not exist, neither should id1 and id1Key1
177 BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), false);
178 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
179 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), false);
180
181 // add id1Key1Cert1, should be default, id1 and id1Key1 should be added implicitly
182 pibImpl.addCertificate(id1Key1Cert1);
183 BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), true);
184 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
185 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700186 const v1::IdentityCertificate& cert = pibImpl.getCertificate(id1Key1Cert1.getName());
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700187 BOOST_CHECK_EQUAL_COLLECTIONS(cert.wireEncode().wire(),
188 cert.wireEncode().wire() + cert.wireEncode().size(),
189 id1Key1Cert1.wireEncode().wire(),
190 id1Key1Cert1.wireEncode().wire() + id1Key1Cert1.wireEncode().size());
191 BOOST_CHECK_NO_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)));
192 BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert1);
193
194 // add id1Key1Cert2, should not be default
195 pibImpl.addCertificate(id1Key1Cert2);
196 BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert2.getName()), true);
197 BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert1);
198
199 // set id1Key1Cert2 explicitly as default
200 pibImpl.setDefaultCertificateOfKey(id1, id1Key1Name.get(-1), id1Key1Cert2.getName());
201 BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert2);
202
203 // set a non-existing cert as default, throw Error
204 BOOST_CHECK_THROW(pibImpl.setDefaultCertificateOfKey(id1, id1Key1Name.get(-1), Name("/non-existing")),
205 Pib::Error);
206
207 // remove id1Key1Cert2, should not have default cert
208 pibImpl.removeCertificate(id1Key1Cert2.getName());
209 BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert2.getName()), false);
210 BOOST_CHECK_THROW(pibImpl.getCertificate(id1Key1Cert2.getName()), Pib::Error);
211 BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), Pib::Error);
212
213 // add id1Key1Cert2, should be default
214 pibImpl.addCertificate(id1Key1Cert2);
215 BOOST_CHECK_NO_THROW(pibImpl.getCertificate(id1Key1Cert1.getName()));
216 BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert2);
217
218 // get all certificates: id1Key1Cert1 and id1Key1Cert2
219 std::set<Name> certNames = pibImpl.getCertificatesOfKey(id1, id1Key1Name.get(-1));
220 BOOST_CHECK_EQUAL(certNames.size(), 2);
221 BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert1.getName()), 1);
222 BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert2.getName()), 1);
223
224 // remove id1Key1, should remove all the certs
225 pibImpl.removeKey(id1, id1Key1Name.get(-1));
226 certNames = pibImpl.getCertificatesOfKey(id1, id1Key1Name.get(-1));
227 BOOST_CHECK_EQUAL(certNames.size(), 0);
228}
229
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100230BOOST_AUTO_TEST_SUITE_END() // TestPibImpl
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700231BOOST_AUTO_TEST_SUITE_END() // TestPib
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100232BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700233
234} // namespace tests
235} // namespace security
236} // namespace ndn