blob: d86a95d56c3b7ab414715b21dc3e474a0ae6bb8d [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
22#include "security/pib-memory.hpp"
Mickey Sweatt11314b72015-06-10 17:20:19 -070023#include "security/pib-sqlite3.hpp"
Yingdi Yu3bf91f52015-06-12 19:39:40 -070024#include "security/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)
37BOOST_AUTO_TEST_SUITE(TestPibImpl)
Yingdi Yu3bf91f52015-06-12 19:39:40 -070038
Mickey Sweatt11314b72015-06-10 17:20:19 -070039class PibMemoryWrapper
40{
41public:
42 PibMemory impl;
43};
44
45class PibSqlite3Wrapper
46{
47public:
48 PibSqlite3Wrapper()
49 : tmpPath(boost::filesystem::path(UNIT_TEST_CONFIG_PATH) / "DbTest")
50 , impl(tmpPath.c_str())
51 {
52 }
53
54 ~PibSqlite3Wrapper()
55 {
56 boost::filesystem::remove_all(tmpPath);
57 }
58
Davide Pesaventoeee3e822016-11-26 19:19:34 +010059public:
Mickey Sweatt11314b72015-06-10 17:20:19 -070060 boost::filesystem::path tmpPath;
61 PibSqlite3 impl;
62};
63
64typedef boost::mpl::list<PibMemoryWrapper,
65 PibSqlite3Wrapper> PibImpls;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070066
67BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, PibDataFixture)
68{
Mickey Sweatt11314b72015-06-10 17:20:19 -070069 T wrapper;
70 PibImpl& pibImpl = wrapper.impl;
Yingdi Yu3bf91f52015-06-12 19:39:40 -070071
72 // no default setting, throw Error
73 BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error);
74
75 // check id1, which should not exist
76 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
77
78 // add id1, should be default
79 pibImpl.addIdentity(id1);
80 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
81 BOOST_CHECK_NO_THROW(pibImpl.getDefaultIdentity());
82 BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id1);
83
84 // add id2, should not be default
85 pibImpl.addIdentity(id2);
86 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id2), true);
87 BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id1);
88
89 // set id2 explicitly as default
90 pibImpl.setDefaultIdentity(id2);
91 BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id2);
92
93 // remove id2, should not have default identity
94 pibImpl.removeIdentity(id2);
95 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id2), false);
96 BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error);
97
98 // add id2 again, should be default
99 pibImpl.addIdentity(id2);
100 BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id2);
101
102 // get all identities, should contain id1 and id2
103 std::set<Name> idNames = pibImpl.getIdentities();
104 BOOST_CHECK_EQUAL(idNames.size(), 2);
105 BOOST_CHECK_EQUAL(idNames.count(id1), 1);
106 BOOST_CHECK_EQUAL(idNames.count(id2), 1);
107}
108
109BOOST_FIXTURE_TEST_CASE_TEMPLATE(KeyManagement, T, PibImpls, PibDataFixture)
110{
Mickey Sweatt11314b72015-06-10 17:20:19 -0700111 T wrapper;
112 PibImpl& pibImpl = wrapper.impl;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700113
114 // no default setting, throw Error
115 BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error);
116
117 // check id1Key1, should not exist, neither should id1.
118 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), false);
119 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
120
121 // add id1Key1, should be default, id1 should be added implicitly
122 pibImpl.addKey(id1, id1Key1Name.get(-1), id1Key1);
123 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), true);
124 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700125 const v1::PublicKey& keyBits = pibImpl.getKeyBits(id1, id1Key1Name.get(-1));
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700126 BOOST_CHECK_EQUAL_COLLECTIONS(keyBits.get().buf(), keyBits.get().buf() + keyBits.get().size(),
127 id1Key1.get().buf(), id1Key1.get().buf() + id1Key1.get().size());
128 BOOST_CHECK_NO_THROW(pibImpl.getDefaultKeyOfIdentity(id1));
129 BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name.get(-1));
130
131 // add id1Key2, should not be default
132 pibImpl.addKey(id1, id1Key2Name.get(-1), id1Key2);
133 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key2Name.get(-1)), true);
134 BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name.get(-1));
135
136 // set id1Key2 explicitly as default
137 pibImpl.setDefaultKeyOfIdentity(id1, id1Key2Name.get(-1));
138 BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key2Name.get(-1));
139
140 // set a non-existing key as default, throw Error
141 BOOST_CHECK_THROW(pibImpl.setDefaultKeyOfIdentity(id1, name::Component("non-existing")),
142 Pib::Error);
143
144 // remove id1Key2, should not have default key
145 pibImpl.removeKey(id1, id1Key2Name.get(-1));
146 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key2Name.get(-1)), false);
147 BOOST_CHECK_THROW(pibImpl.getKeyBits(id1, id1Key2Name.get(-1)), Pib::Error);
148 BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error);
149
150 // add id1Key2 back, should be default
151 pibImpl.addKey(id1, id1Key2Name.get(-1), id1Key2);
152 BOOST_CHECK_NO_THROW(pibImpl.getKeyBits(id1, id1Key2Name.get(-1)));
153 BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key2Name.get(-1));
154
155 // get all the keys: id1Key1 and id1Key2
156 std::set<name::Component> keyNames = pibImpl.getKeysOfIdentity(id1);
157 BOOST_CHECK_EQUAL(keyNames.size(), 2);
158 BOOST_CHECK_EQUAL(keyNames.count(id1Key1Name.get(-1)), 1);
159 BOOST_CHECK_EQUAL(keyNames.count(id1Key2Name.get(-1)), 1);
160
161 // remove id1, should remove all the keys
162 pibImpl.removeIdentity(id1);
163 keyNames = pibImpl.getKeysOfIdentity(id1);
164 BOOST_CHECK_EQUAL(keyNames.size(), 0);
165}
166
167BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, PibDataFixture)
168{
Mickey Sweatt11314b72015-06-10 17:20:19 -0700169 T wrapper;
170 PibImpl& pibImpl = wrapper.impl;
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700171
172 // no default setting, throw Error
173 BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), Pib::Error);
174
175 // check id1Key1Cert1, should not exist, neither should id1 and id1Key1
176 BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), false);
177 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
178 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), false);
179
180 // add id1Key1Cert1, should be default, id1 and id1Key1 should be added implicitly
181 pibImpl.addCertificate(id1Key1Cert1);
182 BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), true);
183 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
184 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), true);
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700185 const v1::IdentityCertificate& cert = pibImpl.getCertificate(id1Key1Cert1.getName());
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700186 BOOST_CHECK_EQUAL_COLLECTIONS(cert.wireEncode().wire(),
187 cert.wireEncode().wire() + cert.wireEncode().size(),
188 id1Key1Cert1.wireEncode().wire(),
189 id1Key1Cert1.wireEncode().wire() + id1Key1Cert1.wireEncode().size());
190 BOOST_CHECK_NO_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)));
191 BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert1);
192
193 // add id1Key1Cert2, should not be default
194 pibImpl.addCertificate(id1Key1Cert2);
195 BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert2.getName()), true);
196 BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert1);
197
198 // set id1Key1Cert2 explicitly as default
199 pibImpl.setDefaultCertificateOfKey(id1, id1Key1Name.get(-1), id1Key1Cert2.getName());
200 BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert2);
201
202 // set a non-existing cert as default, throw Error
203 BOOST_CHECK_THROW(pibImpl.setDefaultCertificateOfKey(id1, id1Key1Name.get(-1), Name("/non-existing")),
204 Pib::Error);
205
206 // remove id1Key1Cert2, should not have default cert
207 pibImpl.removeCertificate(id1Key1Cert2.getName());
208 BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert2.getName()), false);
209 BOOST_CHECK_THROW(pibImpl.getCertificate(id1Key1Cert2.getName()), Pib::Error);
210 BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), Pib::Error);
211
212 // add id1Key1Cert2, should be default
213 pibImpl.addCertificate(id1Key1Cert2);
214 BOOST_CHECK_NO_THROW(pibImpl.getCertificate(id1Key1Cert1.getName()));
215 BOOST_CHECK_EQUAL(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), id1Key1Cert2);
216
217 // get all certificates: id1Key1Cert1 and id1Key1Cert2
218 std::set<Name> certNames = pibImpl.getCertificatesOfKey(id1, id1Key1Name.get(-1));
219 BOOST_CHECK_EQUAL(certNames.size(), 2);
220 BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert1.getName()), 1);
221 BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert2.getName()), 1);
222
223 // remove id1Key1, should remove all the certs
224 pibImpl.removeKey(id1, id1Key1Name.get(-1));
225 certNames = pibImpl.getCertificatesOfKey(id1, id1Key1Name.get(-1));
226 BOOST_CHECK_EQUAL(certNames.size(), 0);
227}
228
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100229BOOST_AUTO_TEST_SUITE_END() // TestPibImpl
230BOOST_AUTO_TEST_SUITE_END() // Security
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700231
232} // namespace tests
233} // namespace security
234} // namespace ndn