blob: f55533f0a7dd7f0bbd7855732e398ea141a5d709 [file] [log] [blame]
Yingdi Yuadadb712015-04-15 17:23:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 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/in-memory-pib-impl.hpp"
23#include "security/pib.hpp"
24#include "pib-data-fixture.hpp"
25
26#include "boost-test.hpp"
27
28namespace ndn {
29namespace security {
30namespace tests {
31
32BOOST_AUTO_TEST_SUITE(SecurityInMemoryPibImpl)
33
34BOOST_AUTO_TEST_CASE(TpmLocatorManagement)
35{
36 InMemoryPibImpl pibImpl;
37
38 BOOST_CHECK_EQUAL(pibImpl.getTpmLocator(), "tpm-memory:");
39 BOOST_CHECK_THROW(pibImpl.setTpmLocator(""), PibImpl::Error);
40}
41
42BOOST_FIXTURE_TEST_CASE(IdentityManagement, PibDataFixture)
43{
44 InMemoryPibImpl pibImpl;
45
46 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
47 pibImpl.addIdentity(id1);
48 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
49 pibImpl.removeIdentity(id1);
50 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
51
52 BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error);
53 pibImpl.setDefaultIdentity(id1);
54 BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
55 BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id1);
56
57 pibImpl.addIdentity(id2);
58 std::set<Name> idNames = pibImpl.getIdentities();
59 BOOST_CHECK_EQUAL(idNames.size(), 2);
60 BOOST_CHECK_EQUAL(idNames.count(id1), 1);
61 BOOST_CHECK_EQUAL(idNames.count(id2), 1);
62}
63
64BOOST_FIXTURE_TEST_CASE(KeyManagement, PibDataFixture)
65{
66 InMemoryPibImpl pibImpl;
67
68 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), false);
69 pibImpl.addKey(id1, id1Key1Name.get(-1), id1Key1);
70 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), true);
71 pibImpl.removeKey(id1, id1Key1Name.get(-1));
72 BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), false);
73
74 BOOST_CHECK_THROW(pibImpl.getKeyBits(id1, id1Key1Name.get(-1)), Pib::Error);
75 pibImpl.addKey(id1, id1Key1Name.get(-1), id1Key1);
76 BOOST_CHECK_NO_THROW(pibImpl.getKeyBits(id1, id1Key1Name.get(-1)));
77 const PublicKey& keyBits = pibImpl.getKeyBits(id1, id1Key1Name.get(-1));
78 BOOST_CHECK_EQUAL_COLLECTIONS(keyBits.get().buf(), keyBits.get().buf() + keyBits.get().size(),
79 id1Key1.get().buf(), id1Key1.get().buf() + id1Key1.get().size());
80
81 pibImpl.addKey(id1, id1Key2Name.get(-1), id1Key2);
82 pibImpl.addKey(id2, id2Key2Name.get(-1), id2Key2);
83
84 std::set<name::Component> keyNames = pibImpl.getKeysOfIdentity(id1);
85 BOOST_CHECK_EQUAL(keyNames.size(), 2);
86 BOOST_CHECK_EQUAL(keyNames.count(id1Key1Name.get(-1)), 1);
87 BOOST_CHECK_EQUAL(keyNames.count(id1Key2Name.get(-1)), 1);
88
89 BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error);
90 pibImpl.setDefaultKeyOfIdentity(id1, id1Key1Name.get(-1));
91 BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name.get(-1));
92 BOOST_CHECK_THROW(pibImpl.setDefaultKeyOfIdentity(id1, name::Component("non-existing")),
93 Pib::Error);
94}
95
96BOOST_FIXTURE_TEST_CASE(CertificateManagement, PibDataFixture)
97{
98 InMemoryPibImpl pibImpl;
99
100 BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), false);
101 pibImpl.addCertificate(id1Key1Cert1);
102 BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), true);
103 pibImpl.removeCertificate(id1Key1Cert1.getName());
104 BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), false);
105
106 BOOST_CHECK_THROW(pibImpl.getCertificate(id1Key1Cert1.getName()), Pib::Error);
107 pibImpl.addCertificate(id1Key1Cert1);
108 BOOST_CHECK_NO_THROW(pibImpl.getCertificate(id1Key1Cert1.getName()));
109 const IdentityCertificate& cert = pibImpl.getCertificate(id1Key1Cert1.getName());
110 BOOST_CHECK_EQUAL_COLLECTIONS(cert.wireEncode().wire(),
111 cert.wireEncode().wire() + cert.wireEncode().size(),
112 id1Key1Cert1.wireEncode().wire(),
113 id1Key1Cert1.wireEncode().wire() + id1Key1Cert1.wireEncode().size());
114
115 pibImpl.addCertificate(id1Key1Cert2);
116 pibImpl.addCertificate(id1Key2Cert2);
117
118 std::set<Name> certNames = pibImpl.getCertificatesOfKey(id1, id1Key1Name.get(-1));
119 BOOST_CHECK_EQUAL(certNames.size(), 2);
120 BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert1.getName()), 1);
121 BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert2.getName()), 1);
122
123 BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), Pib::Error);
124 pibImpl.setDefaultCertificateOfKey(id1, id1Key1Name.get(-1), id1Key1Cert1.getName());
125 BOOST_CHECK_NO_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)));
126 const IdentityCertificate& defaultCert =
127 pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1));
128 BOOST_CHECK_EQUAL_COLLECTIONS(defaultCert.wireEncode().wire(),
129 defaultCert.wireEncode().wire() + defaultCert.wireEncode().size(),
130 id1Key1Cert1.wireEncode().wire(),
131 id1Key1Cert1.wireEncode().wire() + id1Key1Cert1.wireEncode().size());
132
133 BOOST_CHECK_THROW(pibImpl.setDefaultCertificateOfKey(id1, id1Key1Name.get(-1), id1Key2Cert1.getName()),
134 Pib::Error);
135}
136
137BOOST_AUTO_TEST_SUITE_END()
138
139} // namespace tests
140} // namespace security
141} // namespace ndn